From 0245c606d77c8b99833ccc2c0923a298fb482236 Mon Sep 17 00:00:00 2001 From: tobi <31960611+tsmethurst@users.noreply.github.com> Date: Wed, 31 Aug 2022 17:31:21 +0200 Subject: [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 --- testrig/util.go | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) (limited to 'testrig/util.go') 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 + } + } +} -- cgit v1.2.3