From 983afb2b45fb0b60b6977e88b190b544e8e7fb94 Mon Sep 17 00:00:00 2001 From: binwiederhier Date: Wed, 1 Mar 2023 11:36:48 -0500 Subject: [PATCH] Fix some iffy tests with waitFor function --- go.mod | 2 +- go.sum | 2 ++ server/server_account_test.go | 41 ++++++++++++++++++++++++----------- server/server_test.go | 32 +++++++++++++++++++++++---- 4 files changed, 59 insertions(+), 18 deletions(-) diff --git a/go.mod b/go.mod index 020d74d8..546c3ced 100644 --- a/go.mod +++ b/go.mod @@ -19,7 +19,7 @@ require ( golang.org/x/sync v0.1.0 golang.org/x/term v0.5.0 golang.org/x/time v0.3.0 - google.golang.org/api v0.110.0 + google.golang.org/api v0.111.0 gopkg.in/yaml.v2 v2.4.0 ) diff --git a/go.sum b/go.sum index d69467a0..ad40c8b3 100644 --- a/go.sum +++ b/go.sum @@ -167,6 +167,8 @@ golang.org/x/xerrors v0.0.0-20220907171357-04be3eba64a2 h1:H2TDz8ibqkAF6YGhCdN3j golang.org/x/xerrors v0.0.0-20220907171357-04be3eba64a2/go.mod h1:K8+ghG5WaK9qNqU5K3HdILfMLy1f3aNYFI/wnl100a8= google.golang.org/api v0.110.0 h1:l+rh0KYUooe9JGbGVx71tbFo4SMbMTXK3I3ia2QSEeU= google.golang.org/api v0.110.0/go.mod h1:7FC4Vvx1Mooxh8C5HWjzZHcavuS2f6pmJpZx60ca7iI= +google.golang.org/api v0.111.0 h1:bwKi+z2BsdwYFRKrqwutM+axAlYLz83gt5pDSXCJT+0= +google.golang.org/api v0.111.0/go.mod h1:qtFHvU9mhgTJegR31csQ+rwxyUTHOKFqCKWp1J0fdw0= google.golang.org/appengine v1.1.0/go.mod h1:EbEs0AVv82hx2wNQdGPgUI5lhzA/G0D9YwlJXL52JkM= google.golang.org/appengine v1.4.0/go.mod h1:xpcJRLb0r/rnEns0DIKYYv+WjYCduHsrkT7/EB5XEv4= google.golang.org/appengine v1.6.7 h1:FZR1q0exgwxzPzp/aF+VccGrSfxfPpkBqjIIEq3ru6c= diff --git a/server/server_account_test.go b/server/server_account_test.go index 3e14d51f..7c644689 100644 --- a/server/server_account_test.go +++ b/server/server_account_test.go @@ -657,6 +657,17 @@ func TestAccount_Reservation_Delete_Messages_And_Attachments(t *testing.T) { m2 := toMessage(t, rr.Body.String()) require.FileExists(t, filepath.Join(s.config.AttachmentCacheDir, m2.ID)) + // Pre-verify message count and file + ms, err := s.messageCache.Messages("mytopic1", sinceAllMessages, false) + require.Nil(t, err) + require.Equal(t, 1, len(ms)) + require.FileExists(t, filepath.Join(s.config.AttachmentCacheDir, m1.ID)) + + ms, err = s.messageCache.Messages("mytopic2", sinceAllMessages, false) + require.Nil(t, err) + require.Equal(t, 1, len(ms)) + require.FileExists(t, filepath.Join(s.config.AttachmentCacheDir, m2.ID)) + // Delete reservation rr = request(t, s, "DELETE", "/v1/account/reservation/mytopic1", ``, map[string]string{ "X-Delete-Messages": "true", @@ -672,9 +683,13 @@ func TestAccount_Reservation_Delete_Messages_And_Attachments(t *testing.T) { // Verify that messages and attachments were deleted // This does not explicitly call the manager! - time.Sleep(time.Second) + waitFor(t, func() bool { + ms, err := s.messageCache.Messages("mytopic1", sinceAllMessages, false) + require.Nil(t, err) + return len(ms) == 0 && !util.FileExists(filepath.Join(s.config.AttachmentCacheDir, m1.ID)) + }) - ms, err := s.messageCache.Messages("mytopic1", sinceAllMessages, false) + ms, err = s.messageCache.Messages("mytopic1", sinceAllMessages, false) require.Nil(t, err) require.Equal(t, 0, len(ms)) require.NoFileExists(t, filepath.Join(s.config.AttachmentCacheDir, m1.ID)) @@ -712,13 +727,12 @@ func TestAccount_Persist_UserStats_After_Tier_Change(t *testing.T) { }) require.Equal(t, 200, rr.Code) - // Wait for stats queue writer - time.Sleep(600 * time.Millisecond) - - // Verify that message stats were persisted - u, err := s.userManager.User("phil") - require.Nil(t, err) - require.Equal(t, int64(1), u.Stats.Messages) + // Wait for stats queue writer, verify that message stats were persisted + waitFor(t, func() bool { + u, err := s.userManager.User("phil") + require.Nil(t, err) + return int64(1) == u.Stats.Messages + }) // Change tier, make a request (to reset limiters) require.Nil(t, s.userManager.ChangeTier("phil", "pro")) @@ -736,10 +750,11 @@ func TestAccount_Persist_UserStats_After_Tier_Change(t *testing.T) { require.Equal(t, 200, rr.Code) // Verify that message stats were persisted - time.Sleep(600 * time.Millisecond) - u, err = s.userManager.User("phil") - require.Nil(t, err) - require.Equal(t, int64(2), u.Stats.Messages) // v.EnqueueUserStats had run! + waitFor(t, func() bool { + u, err := s.userManager.User("phil") + require.Nil(t, err) + return int64(2) == u.Stats.Messages // v.EnqueueUserStats had run! + }) // Stats keep counting rr = request(t, s, "GET", "/v1/account", "", map[string]string{ diff --git a/server/server_test.go b/server/server_test.go index ccda967a..69237696 100644 --- a/server/server_test.go +++ b/server/server_test.go @@ -914,7 +914,15 @@ func TestServer_StatsResetter(t *testing.T) { require.Equal(t, int64(2), account.Stats.Messages) // Wait for stats resetter to run - time.Sleep(2200 * time.Millisecond) + waitFor(t, func() bool { + response = request(t, s, "GET", "/v1/account", "", map[string]string{ + "Authorization": util.BasicAuth("phil", "phil"), + }) + require.Equal(t, 200, response.Code) + account, err = util.UnmarshalJSON[apiAccountResponse](io.NopCloser(response.Body)) + require.Nil(t, err) + return account.Stats.Messages == 0 + }) // User stats show 0 messages now! response = request(t, s, "GET", "/v1/account", "", map[string]string{ @@ -1661,9 +1669,10 @@ func TestServer_PublishAttachmentAndExpire(t *testing.T) { require.Equal(t, content, response.Body.String()) // Prune and makes sure it's gone - time.Sleep(time.Second) // Sigh ... - s.execManager() - require.NoFileExists(t, file) + waitFor(t, func() bool { + s.execManager() // May run many times + return !util.FileExists(file) + }) response = request(t, s, "GET", path, "", nil) require.Equal(t, 404, response.Code) } @@ -2311,3 +2320,18 @@ func readAll(t *testing.T, rc io.ReadCloser) string { } return string(b) } + +func waitFor(t *testing.T, f func() bool) { + waitForWithMaxWait(t, 5*time.Second, f) +} + +func waitForWithMaxWait(t *testing.T, maxWait time.Duration, f func() bool) { + start := time.Now() + for time.Since(start) < maxWait { + if f() { + return + } + time.Sleep(100 * time.Millisecond) + } + t.Fatalf("Function f did not succeed after %v", maxWait) +}