Skip to content

Commit 46084fb

Browse files
Effi-SAkramBitar
authored andcommitted
Added global ticker and timeout
Signed-off-by: Effi-S <effi.szt@gmail.com>
1 parent e63e6ea commit 46084fb

1 file changed

Lines changed: 10 additions & 2 deletions

File tree

token/services/storage/db/dbtest/notifier.go

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -60,13 +60,21 @@ func (c *dbEventsCollector[T]) AssertSize(size int) error {
6060
c.close <- true
6161
}()
6262

63+
// The overall timeout must be computed once: allocating time.After inside
64+
// the loop would reset it on every poll tick, so it could never elapse and
65+
// AssertSize would spin forever when the expected size is never reached.
66+
timeout := time.NewTimer(time.Second)
67+
defer timeout.Stop()
68+
ticker := time.NewTicker(20 * time.Millisecond)
69+
defer ticker.Stop()
70+
6371
for {
6472
select {
6573
case <-c.close:
6674
return errors.Errorf("db events collector closed")
67-
case <-time.After(time.Second):
75+
case <-timeout.C:
6876
return errors.Errorf("db events collector timeout")
69-
case <-time.After(20 * time.Millisecond):
77+
case <-ticker.C:
7078
c.mu.RLock()
7179
resultSize := len(c.result)
7280
c.mu.RUnlock()

0 commit comments

Comments
 (0)