New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
testing: start test log for caching before TestMain #24122
Comments
What is |
@ianlancetaylor whoops, yes! Meant EDIT: Issue updated. |
Thanks. For complete clarity, can you provide a small self-contained example? |
Sure, give me a few minutes to throw together some sample code and I'll update you when it's ready. |
My fault: as it turns out I can no longer reproduce this issue today. It occurred yesterday with the same exact version of go, set of tests, etc. I'll reopen this issue if needed once I figure out what set of steps caused the original failure (clearly the steps I gave are not accurate) Thanks |
@ianlancetaylor Figured it out. Basically, the issue is with SQL migrations. My To be fair, I don't think it should be Go's responsibility to keep track of extraneous files in the repository and know which ones are critical to the testing process, but I also think it could make sense to always run |
@bakatz can you please provide an example of code with the issue? |
@ysmolsky sure, apologies on the delay on this; been really busy at work. I'll provide an example repo when I next have a free moment. Should be within the next day or two |
Ping on that info, @bakatz |
@andybons Whoops, I completely forgot about providing a repo for this issue. I've just reproduced the issue with a self-contained piece of code and I'm pushing up the code to github right now. I'll provide a link to the repo once the code is available. Thanks for the reminder |
@andybons here is the self-contained repository which you can use to reproduce the bug. See EDIT: I've also updated the original issue's "steps to reproduce" since I now know exactly how to reproduce the issue. |
Hey @andybons - I'd like to discuss how this issue should be fixed and then just try to fix it myself since I have some free time at the moment. I have a couple questions:
Thanks! |
I do not think we should always run The current guideline we use is that if the test opens any files under GOPATH, then if those files change then the test is no longer cached. This is documented at https://golang.org/cmd/go: "Tests that open files within the package's source root (usually $GOPATH) or that consult environment variables only match future runs in which the files and environment variables are unchanged." It sounds like your test is opening a .sql file that is not under GOPATH, and that that file is changing. If that is correct, then we do expect the test caching to fail for your case. The workaround for this sort of test is to run it with -test.count=1, which disables caching. We could also consider changing the current cache plan if we can come up with a simple and efficient mechanism. |
@ianlancetaylor Gotcha. That is not correct RE: "opening a .sql file that is not under $GOPATH" -- all of my files are under the directory |
@bakatz What exactly is changing that causes the cached test result to be invalid? |
@ianlancetaylor Adding a new .sql file or updating an existing .sql file within some subdirectory under $GOPATH (i.e. $GOPATH/src/someproject/migrations/somenewmigration.sql). To explain the "real world" situation a bit more: say I have a failing test that is written for an API that isn't working correctly because the database is missing a column. In order to fix this test, I go and add the missing column via a new .sql migration script and re-run the test. The test comes back as cached, so the new .sql migration script that has been added to the filesystem doesn't run, and the test continues to "fail" (even though it's just returning the cached failed result). I then have to re-run the test with |
@bakatz Thanks. That sounds like a bug. Can you provide the output of |
Oh, sorry, you provided a repro above. I'll take a look. |
I see the problem. The checks for whether changed files affect the hash only affect tests themselves. Files opened in var migrationContents []byte
var setMigrationContentsOnce sync.Once
func setMigrationContents(t *testing.T) {
migrationContents, err := ioutil.ReadFile("./some_migration.sql")
if err != nil {
t.Fatal(err)
}
}
func TestSomething(t *testing.,T) {
setMigrationContentsOnce.Do(setMigrationContents)
} |
I'll change this issue to consider whether we should check file I/O for caching in |
@ianlancetaylor thanks for looking into this. The sync.Once workaround is pretty neat; didn't know sync.Once existed till now! Regarding the decision though: is there an argument for not having a file I/O check RE: TestMain? It seems like the behavior here should be the same as it is for any other regular test, but maybe I'm missing something? |
As I see it, the argument against is basically that, at least given the current implementation, it would mean that code before |
Hi ! |
What version of Go are you using (
go version
)?go version go1.10 windows/amd64
Does this issue reproduce with the latest release?
Yes
What operating system and processor architecture are you using (
go env
)?What did you do?
test_something.go
file whose tests require amain_test.go
file for setup/teardowntest_something.go
main_test
is executedWhat did you expect to see?
main_test
is executed again and a cache miss occurs.What did you see instead?
main_test
is not executed again and a cache hit occurs. See https://github.com/bakatz/golangissue21422 for more info.The text was updated successfully, but these errors were encountered: