@@ -3,7 +3,6 @@ package clockwork
33import (
44 "context"
55 "errors"
6- "reflect"
76 "testing"
87 "time"
98)
@@ -13,147 +12,141 @@ import (
1312// process to get killed, providing a stack trace.
1413const timeout = time .Minute
1514
16- func TestFakeClockAfter (t * testing.T ) {
15+ func TestAfter (t * testing.T ) {
1716 t .Parallel ()
1817 fc := & FakeClock {}
1918
20- neg := fc .After (- 1 )
21- select {
22- case <- neg :
23- default :
24- t .Errorf ("negative did not return!" )
19+ var timers []<- chan time.Time
20+ for i := 0 ; i < 3 ; i ++ {
21+ timers = append (timers , fc .After (time .Duration (i * 2 + 1 ))) // 1, 3, 5
2522 }
2623
27- zero := fc .After (0 )
28- select {
29- case <- zero :
30- default :
31- t .Errorf ("zero did not return!" )
32- }
33- one := fc .After (1 )
34- two := fc .After (2 )
35- six := fc .After (6 )
36- ten := fc .After (10 )
37- fc .Advance (1 )
38- select {
39- case <- one :
40- default :
41- t .Errorf ("one did not return!" )
42- }
43- select {
44- case <- two :
45- t .Errorf ("two returned prematurely!" )
46- case <- six :
47- t .Errorf ("six returned prematurely!" )
48- case <- ten :
49- t .Errorf ("ten returned prematurely!" )
50- default :
24+ // Nothing fired immediately.
25+ for i , ch := range timers {
26+ select {
27+ case <- ch :
28+ t .Errorf ("Timer at time=%v fired at time=0" , i * 2 + 1 )
29+ default :
30+ }
5131 }
32+
33+ // First timer fires at time=1.
5234 fc .Advance (1 )
5335 select {
54- case <- two :
36+ case <- timers [ 0 ] :
5537 default :
56- t .Errorf ("two did not return! " )
38+ t .Errorf ("Timer at time=1 did not fire at time=1 " )
5739 }
58- select {
59- case <- six :
60- t . Errorf ( "six returned prematurely!" )
61- case <- ten :
62- t . Errorf ( "ten returned prematurely!" )
63- default :
40+ for i , ch := range timers [ 1 :] {
41+ select {
42+ case <- ch :
43+ t . Errorf ( "Timer at time=%v fired at time=1" , i * 2 + 3 )
44+ default :
45+ }
6446 }
47+
48+ // Should not change anything.
6549 fc .Advance (1 )
66- select {
67- case <- six :
68- t . Errorf ( "six returned prematurely!" )
69- case <- ten :
70- t . Errorf ( "ten returned prematurely!" )
71- default :
50+ for i , ch := range timers [ 1 :] {
51+ select {
52+ case <- ch :
53+ t . Errorf ( "Timer at time=%v fired at time=2" , i * 2 + 3 )
54+ default :
55+ }
7256 }
73- fc .Advance (3 )
57+
58+ // Add 1 more timer at time 5. Should fire at the same time as our timer in chs[2]
59+ timers = append (timers , fc .After (time .Duration (3 ))) // Current time + 3 = 2 + 3 = 5
60+
61+ // Skip over timer at time 3, advancing directly to 4. Check it works as expected.
62+ fc .Advance (2 )
7463 select {
75- case <- six :
64+ case <- timers [ 1 ] :
7665 default :
77- t .Errorf ("six did not return! " )
66+ t .Errorf ("Timer at time=3 did not fire at time=4 " )
7867 }
79- select {
80- case <- ten :
81- t .Errorf ("ten returned prematurely!" )
82- default :
68+ for _ , i := range []int {2 , 3 } {
69+ select {
70+ case <- timers [i ]:
71+ t .Errorf ("Timer at time=5 fired at time=4" )
72+ default :
73+ }
8374 }
84- fc .Advance (100 )
85- select {
86- case <- ten :
87- default :
88- t .Errorf ("ten did not return!" )
75+
76+ fc .Advance (1 )
77+ for idx , tIdex := range []int {2 , 3 } {
78+ select {
79+ case <- timers [tIdex ]:
80+ default :
81+ t .Errorf ("Timer at time=5 #%v did not fire at time=5" , idx )
82+ }
8983 }
9084}
9185
92- func TestNewFakeClock (t * testing.T ) {
86+ func TestAfterZero (t * testing.T ) {
9387 t .Parallel ()
94- fc := NewFakeClock ()
95- now := fc .Now ()
96- if now .IsZero () {
97- t .Fatalf ("fakeClock.Now() fulfills IsZero" )
88+ cases := []struct {
89+ name string
90+
91+ d time.Duration
92+ }{
93+ {name : "zero" },
94+ {
95+ name : "negative" ,
96+ d : - time .Second ,
97+ },
9898 }
9999
100- now2 := fc .Now ()
101- if ! reflect .DeepEqual (now , now2 ) {
102- t .Fatalf ("fakeClock.Now() returned different value: want=%#v got=%#v" , now , now2 )
100+ ctx , cancel := context .WithTimeout (context .Background (), timeout )
101+ defer cancel ()
102+
103+ for _ , tc := range cases {
104+ t .Run (tc .name , func (t * testing.T ) {
105+ fc := & FakeClock {}
106+ select {
107+ case <- fc .After (tc .d ):
108+ case <- ctx .Done ():
109+ t .Errorf ("FakeClock.After() did not return." )
110+ }
111+ })
103112 }
104113}
105114
106- func TestNewFakeClockAt (t * testing.T ) {
115+ func TestNewFakeClockIsNotZero (t * testing.T ) {
107116 t .Parallel ()
108- t1 := time .Date (1999 , time .February , 3 , 4 , 5 , 6 , 7 , time .UTC )
109- fc := NewFakeClockAt (t1 )
110- now := fc .Now ()
111- if ! reflect .DeepEqual (now , t1 ) {
112- t .Fatalf ("fakeClock.Now() returned unexpected non-initialised value: want=%#v, got %#v" , t1 , now )
117+ fc := NewFakeClock ()
118+ if fc .Now ().IsZero () {
119+ t .Errorf ("NewFakeClock.Now().IsZero() returned true, want false" )
113120 }
114121}
115122
116- func TestFakeClockSince (t * testing.T ) {
123+ func TestNewFakeClockAt (t * testing.T ) {
117124 t .Parallel ()
118- fc := NewFakeClock ()
119- now := fc .Now ()
120- elapsedTime := time .Second
121- fc .Advance (elapsedTime )
122- if fc .Since (now ) != elapsedTime {
123- t .Fatalf ("fakeClock.Since() returned unexpected duration, got: %d, want: %d" , fc .Since (now ), elapsedTime )
125+ want := time .Date (1999 , time .February , 3 , 4 , 5 , 6 , 7 , time .UTC )
126+ if got := NewFakeClockAt (want ).Now (); ! got .Equal (want ) {
127+ t .Errorf ("fakeClock.Now() returned %v, want: %v" , got , want )
124128 }
125129}
126130
127- func TestFakeClockUntil (t * testing.T ) {
131+ func TestSince (t * testing.T ) {
128132 t .Parallel ()
129- testTime := time .Now ()
130- fc := NewFakeClockAt (testTime )
131-
132- testOffset := time .Minute
133- probeTime := testTime .Add (testOffset )
134-
135- elapsedTime := time .Second
136- fc .Advance (elapsedTime )
137-
138- expectedDuration := testOffset - elapsedTime
139- if fc .Until (probeTime ) != expectedDuration {
140- t .Fatalf ("fakeClock.Until() returned unexpected duration, got: %d, want: %d" , fc .Until (probeTime ), expectedDuration )
133+ start := time .Date (1999 , time .February , 3 , 4 , 5 , 6 , 7 , time .UTC )
134+ want := time .Second
135+ fc := NewFakeClockAt (start .Add (want ))
136+ if got := fc .Since (start ); got != want {
137+ t .Errorf ("fakeClock.Since() returned %v, want: %v" , got , want )
141138 }
142139}
143140
144- // This used to result in a deadlock.
145- // https://github.com/jonboulle/clockwork/issues/35
146- func TestTwoBlockersOneBlock (t * testing.T ) {
141+ func TestUntil (t * testing.T ) {
147142 t .Parallel ()
148- fc := & FakeClock {}
149-
150- ft1 := fc .NewTicker (time .Second )
151- ft2 := fc .NewTicker (time .Second )
152-
153- fc .BlockUntil (1 )
154- fc .BlockUntil (2 )
155- ft1 .Stop ()
156- ft2 .Stop ()
143+ start := time .Date (1999 , time .February , 3 , 4 , 5 , 6 , 7 , time .UTC )
144+ fc := NewFakeClockAt (start )
145+ want := time .Second
146+ end := start .Add (want )
147+ if got := fc .Until (end ); got != want {
148+ t .Errorf ("fakeClock.Until() returned %v, want: %v" , got , want )
149+ }
157150}
158151
159152func TestBlockUntilContext (t * testing.T ) {
0 commit comments