-
Notifications
You must be signed in to change notification settings - Fork 17.7k
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: document that directories returned by TB.TempDir should not be accessed after cleanup #43547
Comments
Also, I think the obvious answer is "your test is bad", but I am not sure it is. We have a bunch of components that all take stop channels/context cancellation. We even check goroutines for leaks after tests (using something like https://github.com/uber-go/goleak), so we don't just have spurious goroutines flying around. The problem is cancelling the context doesn't just immediately kill the goroutines, so they do a few last things until they finally realize the context has been cancelled (in this case, writing a file). It seems in these cases they only way you can actually write a correct test is something that ensures that a write is not triggered after the context has been cancelled, which I believe would require a lock around the context cancel and the file writing, which is extremely heavy and bizarre to support a testing edge case. The other alternative is to use something like goleak and wait until there are no goroutines sticking around, and make sure that is done before TempDir() so it occurs first - and also remember to do it on each subtest. So at first glance its obvious that writing a file after the test is complete is wrong, but I also don't think its clear how to not be wrong. |
Your test is bad, I think, or else the API under test is. 😅
No, it's fine to trigger writes after the context has been canceled. The important thing is to ensure that all writes have completed before the function that triggered the writes returns, regardless of whether the context is cancelled.
I don't know what |
That's fair, I think I would agree my test is bad after thinking about it more.. So likely this is just "a comment above TempDir would be helpful" |
* Fix mutex deadlock between search between writeTraceToHeadBlock, other improvments for mutex contention and Results channel panics Signed-off-by: Martin Disibio <mdisibio@gmail.com> * Fix flaky unlink error by avoiding t.TempDir golang/go#43547 * Prevent file already closed errors when searching wal * Comment * Fix data race from flushing in wal search * changelog
This is to try to prevent test failing due to trying to access the tempdir while it is being tore down. (go issue: golang/go#43547) ``` === RUN TestFileStoreMsgBlkFailOnKernelFaultLostDataReporting/AES-GCM-S2 filestore_test.go:5195: ------------> 128 testing.go:1225: TempDir RemoveAll cleanup: unlinkat ./TestFileStoreMsgBlkFailOnKernelFaultLostDataReportingAES-GCM-S23605508670/001/msgs: directory not empty --- FAIL: TestFileStoreMsgBlkFailOnKernelFaultLostDataReporting (0.02s) ``` Signed-off-by: Waldemar Quevedo <wally@nats.io>
This is to try to prevent test failing due to trying to access the tempdir while it is being torn down. (go issue: golang/go#43547) ``` === RUN TestFileStoreMsgBlkFailOnKernelFaultLostDataReporting/AES-GCM-S2 testing.go:1225: TempDir RemoveAll cleanup: unlinkat ./TestFileStoreMsgBlkFailOnKernelFaultLostDataReportingAES-GCM-S23605508670/001/msgs: directory not empty --- FAIL: TestFileStoreMsgBlkFailOnKernelFaultLostDataReporting (0.02s) ``` Also increases timeout slightly of `TestFileStoreNewWriteIndexInfo` which runs close to 1ms deadline sometimes: ``` === FAIL: server TestFileStoreNewWriteIndexInfo/None-None (4.85s) | filestore_test.go:5489: Unexpected elapsed time: 1.054065ms | --- FAIL: TestFileStoreNewWriteIndexInfo/None-None (4.85s) ```
What version of Go are you using (
go version
)?Does this issue reproduce with the latest release?
Yes
What operating system and processor architecture are you using (
go env
)?go env
OutputWhat did you do?
What did you expect to see?
Test passes. Alternatively, this caveat is sufficiently documented in t.TempDir and/or RemoveAll.
What did you see instead?
Test fails with:
testing.go:915: TempDir RemoveAll cleanup: unlinkat /tmp/TestTest064788875/001: directory not empty
This happens about 50% of the time in this case. In the real world scenario I am actually trying to test, it happens about 0.001% of the time, which makes it quite frustrating.
A manual version of t.TempDir which ignores this error passes, for what its worth.
I think I understand why it works this way, and unfortunately I don't see any great options here, so perhaps just some documentation around this is the best path forward.
The text was updated successfully, but these errors were encountered: