github.com/songzhibin97/gkit@v1.2.13/internal/clock/time_test.go (about) 1 package clock 2 3 import ( 4 "fmt" 5 "testing" 6 "time" 7 8 "github.com/stretchr/testify/assert" 9 ) 10 11 // Time zone should be considered for time related operations 12 func TestFormatTimeMillis(t *testing.T) { 13 type args struct { 14 ts uint64 15 } 16 _, offset := time.Now().Zone() 17 offsetMs := uint64(offset * 1000) 18 19 tests := []struct { 20 name string 21 args args 22 want string 23 }{ 24 { 25 "test1", 26 args{1582421778887 - offsetMs}, 27 "2020-02-23 01:36:18", //"2020-02-23 09:36:18", 28 }, { 29 "test2", 30 args{1577808000000 - offsetMs}, 31 "2019-12-31 16:00:00", //"2020-01-01 00:00:00", 32 }, { 33 "test3", 34 args{1582423015000 - offsetMs}, 35 "2020-02-23 01:56:55", //"2020-02-23 09:56:55", 36 }, { 37 "test4", 38 args{1564382218000 - offsetMs}, 39 "2019-07-29 06:36:58", //"2019-07-29 14:36:58", 40 }, { 41 "test5", 42 args{1582427442295 - offsetMs}, 43 "2020-02-23 03:10:42", //"2020-02-23 11:10:42", 44 }, 45 } 46 for _, tt := range tests { 47 t.Run(tt.name, func(t *testing.T) { 48 if got := FormatTimeMillis(tt.args.ts); got != tt.want { 49 t.Errorf("%s FormatTimeMillis() = %v, want %v", tt.name, got, tt.want) 50 } 51 }) 52 } 53 } 54 55 func TestFormatDate(t *testing.T) { 56 type args struct { 57 tsMillis uint64 58 } 59 _, offset := time.Now().Zone() 60 offsetMs := uint64(offset * 1000) 61 62 tests := []struct { 63 name string 64 args args 65 want string 66 }{ 67 { 68 "test1", 69 args{1564382218000 - offsetMs}, 70 "2019-07-29", 71 }, { 72 "test2", 73 args{1577808000000 - offsetMs}, 74 "2019-12-31", //"2020-01-01", 75 }, 76 } 77 for _, tt := range tests { 78 t.Run(tt.name, func(t *testing.T) { 79 if got := FormatDate(tt.args.tsMillis); got != tt.want { 80 t.Errorf("FormatDate() = %v, want %v", got, tt.want) 81 } 82 }) 83 } 84 } 85 86 func TestGetTimeMillis(t *testing.T) { 87 got := GetTimeMillis() 88 fmt.Printf("GetTimeMillis() %d", got) 89 fmt.Println(FormatTimeMillis(got)) 90 } 91 92 func TestGetTimeNano(t *testing.T) { 93 got := GetTimeNano() 94 fmt.Println(got) 95 } 96 97 func TestMockClock(t *testing.T) { 98 clock := NewMockClock() 99 100 now := clock.Now() 101 assert.Equal(t, clock.GetTimeNano(), uint64(now.UnixNano())) 102 assert.Equal(t, clock.GetTimeMillis(), uint64(now.UnixNano()/1e6)) 103 104 last := now 105 d := 23*time.Hour + 59*time.Minute + 59*time.Second + 999*time.Millisecond + 999*time.Microsecond 106 clock.Sleep(d) 107 assert.Equal(t, last.Add(d), clock.Now()) 108 } 109 110 func TestMockTicker(t *testing.T) { 111 SetClock(NewMockClock()) 112 defer SetClock(NewRealClock()) 113 114 d := 23*time.Hour + 59*time.Minute + 59*time.Second + 999*time.Millisecond + 999*time.Microsecond 115 116 ticker := NewMockTicker(d) 117 defer ticker.Stop() 118 119 ticked := 0 120 121 ticker.check() 122 select { 123 case <-ticker.C(): 124 ticked++ 125 default: 126 } 127 assert.Equal(t, ticked, 0) 128 129 Sleep(d - 1) 130 ticker.check() 131 select { 132 case <-ticker.C(): 133 ticked++ 134 default: 135 } 136 assert.Equal(t, ticked, 0) 137 138 Sleep(1) 139 ticker.check() 140 select { 141 case <-ticker.C(): 142 ticked++ 143 default: 144 } 145 assert.Equal(t, ticked, 1) 146 147 Sleep(d + 1) 148 ticker.check() 149 select { 150 case <-ticker.C(): 151 ticked++ 152 default: 153 } 154 assert.Equal(t, ticked, 2) 155 156 Sleep(d - 1) 157 ticker.check() 158 select { 159 case <-ticker.C(): 160 ticked++ 161 default: 162 } 163 assert.Equal(t, ticked, 3) 164 } 165 166 func BenchmarkGetTimeInMs(b *testing.B) { 167 StartTicker() 168 GetTimeMillisDirect := func() uint64 { 169 tickerNow := GetTimestamp() 170 if tickerNow > uint64(0) { 171 return tickerNow 172 } 173 return uint64(time.Now().UnixNano()) / UnixTimeUnitOffset 174 } 175 176 b.ReportAllocs() 177 b.ResetTimer() 178 179 b.Run("GetTimeMillis", func(b *testing.B) { 180 for i := 0; i < b.N; i++ { 181 GetTimeMillis() 182 } 183 }) 184 b.Run("GetTimeMillisDirect", func(b *testing.B) { 185 for i := 0; i < b.N; i++ { 186 GetTimeMillisDirect() 187 } 188 }) 189 } 190 191 func BenchmarkGetTimeInMsWithTicker(b *testing.B) { 192 b.ReportAllocs() 193 b.ResetTimer() 194 for i := 0; i < b.N; i++ { 195 GetTimestamp() 196 } 197 } 198 199 func BenchmarkNow(b *testing.B) { 200 b.Run("util.Now", func(b *testing.B) { 201 b.ReportAllocs() 202 b.ResetTimer() 203 for i := 0; i < b.N; i++ { 204 Now() 205 } 206 }) 207 b.Run("time.Now", func(b *testing.B) { 208 b.ReportAllocs() 209 b.ResetTimer() 210 for i := 0; i < b.N; i++ { 211 time.Now() 212 } 213 }) 214 }