Fix some iffy tests with waitFor function

This commit is contained in:
binwiederhier 2023-03-01 11:36:48 -05:00
parent cd3429842b
commit 983afb2b45
4 changed files with 59 additions and 18 deletions

2
go.mod
View file

@ -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
)

2
go.sum
View file

@ -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=

View file

@ -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{

View file

@ -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)
}