clock: optionally wait until tickers are registered in TestClock

This commit is contained in:
Andras Banki-Horvath committed 2020-05-26 23:55:26 +02:00
1 parent 7f1a450a7f
commit e81061bda4
2 files changed
+57 -1

No files matched your search

+35
View File
@@ -1,8 +1,11 @@
package clock
import (
"fmt"
"testing"
"time"
"github.com/stretchr/testify/assert"
)
var (
@@ -42,6 +45,7 @@ func TestTickAfter(t *testing.T) {
select {
case <-ticker:
tick = true
case <-time.After(time.Millisecond):
}
@@ -61,3 +65,34 @@ func TestTickAfter(t *testing.T) {
tickOrTimeOut(ticker2, true)
tickOrTimeOut(ticker3, false)
}
// TestTickSignal tests that TickAfter signals registration allowing
// safe time advancement.
func TestTickSignal(t *testing.T) {
const interval = time.Second
ch := make(chan time.Duration)
c := NewTestClockWithTickSignal(testTime, ch)
err := make(chan error, 1)
go func() {
select {
// TickAfter will signal registration but will not
// tick, unless we read the signal and set the time.
case <-c.TickAfter(interval):
err <- nil
// Signal timeout if tick didn't happen.
case <-time.After(time.Second):
err <- fmt.Errorf("timeout")
}
}()
tick := <-ch
// Expect that the interval is correctly passed over the channel.
assert.Equal(t, interval, tick)
// Once the ticker is registered, set the time to make it fire.
c.SetTime(testTime.Add(time.Second))
assert.NoError(t, <-err)
}