From e49a29197b6719d347aaa1566108b6e99177e3a6 Mon Sep 17 00:00:00 2001 From: Bryan Boreham Date: Mon, 15 Jun 2015 13:48:24 +0100 Subject: [PATCH] Add a Flush() routine to synchronize callers --- common/sched_queue.go | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/common/sched_queue.go b/common/sched_queue.go index e5ed0667..7e7c9f44 100644 --- a/common/sched_queue.go +++ b/common/sched_queue.go @@ -142,3 +142,15 @@ func (cq *SchedQueue) durationUntilNext(now time.Time) time.Duration { } return time.Duration(math.MaxInt64) } + +// ensure that the background goroutine has executed any pending callbacks +func (cq *SchedQueue) Flush() { + ch := make(chan struct{}) + flushFunc := func() time.Time { + atomic.AddUint64(&cq.counter, ^uint64(0)) // decrement so nobody sees this + close(ch) + return time.Time{} + } + cq.schedChan <- &schedCall{c: flushFunc, t: time.Time{}} + <-ch +}