@@ -13,7 +13,7 @@ import (
1313
1414var rnd = random .New (random.CryptoSeed {})
1515
16- func TestSetFlowOfTime_invalidMultiplier (t * testing.T ) {
16+ func TestSetSpeed (t * testing.T ) {
1717 t .Run ("on zero" , func (t * testing.T ) {
1818 dtb := & doubles.TB {}
1919 defer dtb .Finish ()
@@ -30,6 +30,21 @@ func TestSetFlowOfTime_invalidMultiplier(t *testing.T) {
3030 })
3131 assert .True (t , dtb .IsFailed )
3232 })
33+ t .Run ("on positive value" , func (t * testing.T ) {
34+ timecop .SetSpeed (t , 10000000 )
35+ s := clock .TimeNow ()
36+ time .Sleep (time .Millisecond )
37+ e := clock .TimeNow ()
38+ assert .True (t , time .Hour < e .Sub (s ))
39+ })
40+ t .Run ("on frozen time SetSpeed don't start the time" , func (t * testing.T ) {
41+ now := time .Now ()
42+ timecop .Travel (t , now , timecop .Freeze ())
43+ timecop .SetSpeed (t , rnd .Float64 ())
44+ time .Sleep (time .Microsecond )
45+ got := clock .TimeNow ()
46+ assert .True (t , now .Equal (got ))
47+ })
3348}
3449
3550const buffer = 500 * time .Millisecond
@@ -85,27 +100,37 @@ func TestTravel_timeTime(t *testing.T) {
85100 assert .Equal (t , hour , got .Hour ())
86101 assert .Equal (t , minute , got .Minute ())
87102 assert .True (t , second - 1 <= got .Second () && got .Second () <= second + 1 )
88- assert .True (t , nano - 100 <= got .Nanosecond () && got .Nanosecond () <= nano + 3000 )
89- })
90- }
91-
92- func TestTravelTo (t * testing.T ) {
93- t .Run ("on no travel" , func (t * testing.T ) {
94- t1 := time .Now ()
95- t2 := clock .TimeNow ()
96- assert .True (t , t1 .Equal (t2 ) || t1 .Before (t2 ))
103+ assert .True (t , nano - int (buffer ) <= got .Nanosecond () && got .Nanosecond () <= nano + int (buffer ))
97104 })
98- t .Run ("on travelling " , func (t * testing.T ) {
105+ t .Run ("on travel with freeze " , func (t * testing.T ) {
99106 now := time .Now ()
100107 var (
101- year = now .Year ()
102- month = now .Month ()
103- day = now .Day () + rnd .IntB (1 , 3 )
108+ year = rnd .IntB (0 , now .Year ())
109+ month = time .Month (rnd .IntB (1 , 12 ))
110+ day = rnd .IntB (1 , 20 )
111+ hour = rnd .IntB (1 , 23 )
112+ minute = rnd .IntB (1 , 59 )
113+ second = rnd .IntB (1 , 59 )
114+ nano = rnd .IntB (1 , int (time .Microsecond - 1 ))
104115 )
105- timecop .TravelTo (t , year , month , day )
116+ date := time .Date (year , month , day , hour , minute , second , nano , time .Local )
117+ timecop .Travel (t , date , timecop .Freeze ())
118+ time .Sleep (time .Millisecond )
106119 got := clock .TimeNow ()
107- assert .Equal (t , year , got .Year ())
108- assert .Equal (t , month , got .Month ())
109- assert .Equal (t , day , got .Day ())
120+ assert .True (t , date .Equal (got ))
121+
122+ assert .EventuallyWithin (time .Second ).Assert (t , func (it assert.It ) {
123+ it .Must .False (date .Equal (clock .TimeNow ()))
124+ })
125+ })
126+ }
127+
128+ func TestTravel_cleanup (t * testing.T ) {
129+ date := time .Now ().AddDate (- 10 , 0 , 0 )
130+ t .Run ("" , func (t * testing.T ) {
131+ timecop .Travel (t , date , timecop .Freeze ())
132+ assert .Equal (t , date .Year (), clock .TimeNow ().Year ())
110133 })
134+ const msg = "was not expected that timecop travel leak out from the sub test"
135+ assert .NotEqual (t , date .Year (), clock .TimeNow ().Year (), msg )
111136}
0 commit comments