summaryrefslogtreecommitdiff
path: root/testrig/util.go
diff options
context:
space:
mode:
authorLibravatar tobi <31960611+tsmethurst@users.noreply.github.com>2022-08-31 17:31:21 +0200
committerLibravatar GitHub <noreply@github.com>2022-08-31 17:31:21 +0200
commit0245c606d77c8b99833ccc2c0923a298fb482236 (patch)
tree16311e89656894f09cfaeb8b0f21b5ac9e4de502 /testrig/util.go
parent[feature] add rate limit middleware (#741) (diff)
downloadgotosocial-0245c606d77c8b99833ccc2c0923a298fb482236.tar.xz
[chore] Test fixes (#788)
* use 'test' value for testrig storage backend * update test dependency * add WaitFor func in testrig * use WaitFor function instead of time.Sleep * tidy up tests * make SentMessages a sync.map * go fmt
Diffstat (limited to 'testrig/util.go')
-rw-r--r--testrig/util.go25
1 files changed, 25 insertions, 0 deletions
diff --git a/testrig/util.go b/testrig/util.go
index 3c0c5b52f..eb00ab362 100644
--- a/testrig/util.go
+++ b/testrig/util.go
@@ -87,3 +87,28 @@ func TimeMustParse(timeString string) time.Time {
}
return t
}
+
+// WaitFor calls condition every 200ms, returning true
+// when condition() returns true, or false after 5s.
+//
+// It's useful for when you're waiting for something to
+// happen, but you don't know exactly how long it will take,
+// and you want to fail if the thing doesn't happen within 5s.
+func WaitFor(condition func() bool) bool {
+ tick := time.NewTicker(200 * time.Millisecond)
+ defer tick.Stop()
+
+ timeout := time.NewTimer(5 * time.Second)
+ defer timeout.Stop()
+
+ for {
+ select {
+ case <-tick.C:
+ if condition() {
+ return true
+ }
+ case <-timeout.C:
+ return false
+ }
+ }
+}