From 0b09920c0aa4fef2d8fc7234e6272845466422c0 Mon Sep 17 00:00:00 2001 From: Mark Rushakoff Date: Tue, 11 Apr 2023 12:33:39 -0400 Subject: [PATCH] test(store): ensure busy manager goroutine is finished (#15788) ## Description Without this change, there were intermittent failures due to a non-empty t.TempDir() directory at the end of a test: ```console $ go test -run=LoadChunk$ ./snapshots/ -count=220 -failfast --- FAIL: TestManager_LoadChunk (0.01s) testing.go:1225: TempDir RemoveAll cleanup: unlinkat /var/folders/.../T/TestManager_LoadChunk1472891739/001: directory not empty FAIL FAIL cosmossdk.io/store/snapshots 0.177s ``` With this change, I can no longer reproduce the flaky failure: ```console $ go test -run=LoadChunk$ ./snapshots/ -count=1000 -failfast ok cosmossdk.io/store/snapshots 15.722s ``` --- ### Author Checklist *All items are required. Please add a note to the item if the item is not applicable and please add links to any relevant follow up issues.* I have... - [x] included the correct [type prefix](https://github.com/commitizen/conventional-commit-types/blob/v3.0.0/index.json) in the PR title - [x] targeted the correct branch (see [PR Targeting](https://github.com/cosmos/cosmos-sdk/blob/main/CONTRIBUTING.md#pr-targeting)) - [x] provided a link to the relevant issue or specification - [x] reviewed "Files changed" and left comments if necessary - [ ] confirmed all CI checks have passed ### Reviewers Checklist *All items are required. Please add a note if the item is not applicable and please add your handle next to the items reviewed if you only reviewed selected items.* I have... - [ ] confirmed the correct [type prefix](https://github.com/commitizen/conventional-commit-types/blob/v3.0.0/index.json) in the PR title - [ ] confirmed all author checklist items have been addressed - [ ] confirmed that this PR does not change production code --- store/snapshots/helpers_test.go | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/store/snapshots/helpers_test.go b/store/snapshots/helpers_test.go index 3472af6068..0cb307642a 100644 --- a/store/snapshots/helpers_test.go +++ b/store/snapshots/helpers_test.go @@ -181,13 +181,24 @@ func setupBusyManager(t *testing.T) *snapshots.Manager { mgr := snapshots.NewManager(store, opts, hung, nil, log.NewNopLogger()) require.Equal(t, opts.Interval, hung.snapshotInterval) + // Channel to ensure the test doesn't finish until the goroutine is done. + // Without this, there are intermittent test failures about + // the t.TempDir() cleanup failing due to the directory not being empty. + done := make(chan struct{}) + go func() { + defer close(done) _, err := mgr.Create(1) require.NoError(t, err) _, didPruneHeight := hung.prunedHeights[1] require.True(t, didPruneHeight) }() time.Sleep(10 * time.Millisecond) + + t.Cleanup(func() { + <-done + }) + t.Cleanup(hung.Close) return mgr