github.com/gogf/gf/v2@v2.7.4/os/gtimer/gtimer_z_unit_timer_test.go (about) 1 // Copyright GoFrame Author(https://goframe.org). All Rights Reserved. 2 // 3 // This Source Code Form is subject to the terms of the MIT License. 4 // If a copy of the MIT was not distributed with this file, 5 // You can obtain one at https://github.com/gogf/gf. 6 7 // Timer Operations 8 9 package gtimer_test 10 11 import ( 12 "context" 13 "testing" 14 "time" 15 16 "github.com/gogf/gf/v2/container/garray" 17 "github.com/gogf/gf/v2/os/gtimer" 18 "github.com/gogf/gf/v2/test/gtest" 19 ) 20 21 func TestTimer_Add_Close(t *testing.T) { 22 gtest.C(t, func(t *gtest.T) { 23 timer := gtimer.New() 24 array := garray.New(true) 25 //fmt.Println("start", time.Now()) 26 timer.Add(ctx, 200*time.Millisecond, func(ctx context.Context) { 27 //fmt.Println("job1", time.Now()) 28 array.Append(1) 29 }) 30 timer.Add(ctx, 200*time.Millisecond, func(ctx context.Context) { 31 //fmt.Println("job2", time.Now()) 32 array.Append(1) 33 }) 34 timer.Add(ctx, 400*time.Millisecond, func(ctx context.Context) { 35 //fmt.Println("job3", time.Now()) 36 array.Append(1) 37 }) 38 time.Sleep(250 * time.Millisecond) 39 t.Assert(array.Len(), 2) 40 time.Sleep(250 * time.Millisecond) 41 t.Assert(array.Len(), 5) 42 timer.Close() 43 time.Sleep(250 * time.Millisecond) 44 fixedLength := array.Len() 45 time.Sleep(250 * time.Millisecond) 46 t.Assert(array.Len(), fixedLength) 47 }) 48 } 49 50 func TestTimer_Start_Stop_Close(t *testing.T) { 51 gtest.C(t, func(t *gtest.T) { 52 timer := gtimer.New() 53 array := garray.New(true) 54 timer.Add(ctx, 1000*time.Millisecond, func(ctx context.Context) { 55 array.Append(1) 56 }) 57 t.Assert(array.Len(), 0) 58 time.Sleep(1200 * time.Millisecond) 59 t.Assert(array.Len(), 1) 60 timer.Stop() 61 time.Sleep(1200 * time.Millisecond) 62 t.Assert(array.Len(), 1) 63 timer.Start() 64 time.Sleep(1200 * time.Millisecond) 65 t.Assert(array.Len(), 2) 66 timer.Close() 67 time.Sleep(1200 * time.Millisecond) 68 t.Assert(array.Len(), 2) 69 }) 70 } 71 72 func TestJob_Reset(t *testing.T) { 73 gtest.C(t, func(t *gtest.T) { 74 timer := gtimer.New() 75 array := garray.New(true) 76 job := timer.AddSingleton(ctx, 500*time.Millisecond, func(ctx context.Context) { 77 array.Append(1) 78 }) 79 time.Sleep(300 * time.Millisecond) 80 job.Reset() 81 time.Sleep(300 * time.Millisecond) 82 job.Reset() 83 time.Sleep(300 * time.Millisecond) 84 job.Reset() 85 time.Sleep(600 * time.Millisecond) 86 t.Assert(array.Len(), 1) 87 }) 88 } 89 90 func TestTimer_AddSingleton(t *testing.T) { 91 gtest.C(t, func(t *gtest.T) { 92 timer := gtimer.New() 93 array := garray.New(true) 94 timer.AddSingleton(ctx, 200*time.Millisecond, func(ctx context.Context) { 95 array.Append(1) 96 time.Sleep(10 * time.Second) 97 }) 98 time.Sleep(250 * time.Millisecond) 99 t.Assert(array.Len(), 1) 100 101 time.Sleep(500 * time.Millisecond) 102 t.Assert(array.Len(), 1) 103 }) 104 } 105 106 func TestTimer_AddSingletonWithQuick(t *testing.T) { 107 gtest.C(t, func(t *gtest.T) { 108 timer := gtimer.New(gtimer.TimerOptions{ 109 Interval: 100 * time.Millisecond, 110 Quick: true, 111 }) 112 array := garray.New(true) 113 timer.AddSingleton(ctx, 5*time.Second, func(ctx context.Context) { 114 array.Append(1) 115 time.Sleep(10 * time.Second) 116 }) 117 time.Sleep(250 * time.Millisecond) 118 t.Assert(array.Len(), 1) 119 120 time.Sleep(500 * time.Millisecond) 121 t.Assert(array.Len(), 1) 122 }) 123 } 124 125 func TestTimer_AddSingletonWithoutQuick(t *testing.T) { 126 gtest.C(t, func(t *gtest.T) { 127 timer := gtimer.New(gtimer.TimerOptions{ 128 Interval: 100 * time.Millisecond, 129 Quick: false, 130 }) 131 array := garray.New(true) 132 timer.AddSingleton(ctx, 5*time.Second, func(ctx context.Context) { 133 array.Append(1) 134 time.Sleep(10 * time.Second) 135 }) 136 time.Sleep(250 * time.Millisecond) 137 t.Assert(array.Len(), 0) 138 139 time.Sleep(500 * time.Millisecond) 140 t.Assert(array.Len(), 0) 141 }) 142 } 143 144 func TestTimer_AddOnce(t *testing.T) { 145 gtest.C(t, func(t *gtest.T) { 146 timer := gtimer.New() 147 array := garray.New(true) 148 timer.AddOnce(ctx, 200*time.Millisecond, func(ctx context.Context) { 149 array.Append(1) 150 }) 151 timer.AddOnce(ctx, 200*time.Millisecond, func(ctx context.Context) { 152 array.Append(1) 153 }) 154 time.Sleep(250 * time.Millisecond) 155 t.Assert(array.Len(), 2) 156 time.Sleep(250 * time.Millisecond) 157 t.Assert(array.Len(), 2) 158 timer.Close() 159 time.Sleep(250 * time.Millisecond) 160 fixedLength := array.Len() 161 time.Sleep(250 * time.Millisecond) 162 t.Assert(array.Len(), fixedLength) 163 }) 164 } 165 166 func TestTimer_AddTimes(t *testing.T) { 167 gtest.C(t, func(t *gtest.T) { 168 timer := gtimer.New() 169 array := garray.New(true) 170 timer.AddTimes(ctx, 200*time.Millisecond, 2, func(ctx context.Context) { 171 array.Append(1) 172 }) 173 time.Sleep(1000 * time.Millisecond) 174 t.Assert(array.Len(), 2) 175 }) 176 } 177 178 func TestTimer_DelayAdd(t *testing.T) { 179 gtest.C(t, func(t *gtest.T) { 180 timer := gtimer.New() 181 array := garray.New(true) 182 timer.DelayAdd(ctx, 200*time.Millisecond, 200*time.Millisecond, func(ctx context.Context) { 183 array.Append(1) 184 }) 185 time.Sleep(250 * time.Millisecond) 186 t.Assert(array.Len(), 0) 187 time.Sleep(250 * time.Millisecond) 188 t.Assert(array.Len(), 1) 189 }) 190 } 191 192 func TestTimer_DelayAddJob(t *testing.T) { 193 gtest.C(t, func(t *gtest.T) { 194 timer := gtimer.New() 195 array := garray.New(true) 196 timer.DelayAddEntry(ctx, 200*time.Millisecond, 200*time.Millisecond, func(ctx context.Context) { 197 array.Append(1) 198 }, false, 100, gtimer.StatusReady) 199 time.Sleep(250 * time.Millisecond) 200 t.Assert(array.Len(), 0) 201 time.Sleep(250 * time.Millisecond) 202 t.Assert(array.Len(), 1) 203 }) 204 } 205 206 func TestTimer_DelayAddSingleton(t *testing.T) { 207 gtest.C(t, func(t *gtest.T) { 208 timer := gtimer.New() 209 array := garray.New(true) 210 timer.DelayAddSingleton(ctx, 200*time.Millisecond, 200*time.Millisecond, func(ctx context.Context) { 211 array.Append(1) 212 time.Sleep(10 * time.Second) 213 }) 214 time.Sleep(250 * time.Millisecond) 215 t.Assert(array.Len(), 0) 216 217 time.Sleep(1000 * time.Millisecond) 218 t.Assert(array.Len(), 1) 219 }) 220 } 221 222 func TestTimer_DelayAddOnce(t *testing.T) { 223 gtest.C(t, func(t *gtest.T) { 224 timer := gtimer.New() 225 array := garray.New(true) 226 timer.DelayAddOnce(ctx, 200*time.Millisecond, 200*time.Millisecond, func(ctx context.Context) { 227 array.Append(1) 228 }) 229 time.Sleep(250 * time.Millisecond) 230 t.Assert(array.Len(), 0) 231 232 time.Sleep(250 * time.Millisecond) 233 t.Assert(array.Len(), 1) 234 235 time.Sleep(500 * time.Millisecond) 236 t.Assert(array.Len(), 1) 237 }) 238 } 239 240 func TestTimer_DelayAddTimes(t *testing.T) { 241 gtest.C(t, func(t *gtest.T) { 242 timer := gtimer.New() 243 array := garray.New(true) 244 timer.DelayAddTimes(ctx, 200*time.Millisecond, 500*time.Millisecond, 2, func(ctx context.Context) { 245 array.Append(1) 246 }) 247 time.Sleep(200 * time.Millisecond) 248 t.Assert(array.Len(), 0) 249 250 time.Sleep(600 * time.Millisecond) 251 t.Assert(array.Len(), 1) 252 253 time.Sleep(600 * time.Millisecond) 254 t.Assert(array.Len(), 2) 255 256 time.Sleep(1000 * time.Millisecond) 257 t.Assert(array.Len(), 2) 258 }) 259 } 260 261 func TestTimer_AddLessThanInterval(t *testing.T) { 262 gtest.C(t, func(t *gtest.T) { 263 timer := gtimer.New(gtimer.TimerOptions{ 264 Interval: 100 * time.Millisecond, 265 }) 266 array := garray.New(true) 267 timer.Add(ctx, 20*time.Millisecond, func(ctx context.Context) { 268 array.Append(1) 269 }) 270 time.Sleep(50 * time.Millisecond) 271 t.Assert(array.Len(), 0) 272 273 time.Sleep(110 * time.Millisecond) 274 t.Assert(array.Len(), 1) 275 276 time.Sleep(110 * time.Millisecond) 277 t.Assert(array.Len(), 2) 278 }) 279 } 280 281 func TestTimer_AddLeveledJob1(t *testing.T) { 282 gtest.C(t, func(t *gtest.T) { 283 timer := gtimer.New() 284 array := garray.New(true) 285 timer.DelayAdd(ctx, 1000*time.Millisecond, 1000*time.Millisecond, func(ctx context.Context) { 286 array.Append(1) 287 }) 288 time.Sleep(1500 * time.Millisecond) 289 t.Assert(array.Len(), 0) 290 time.Sleep(1300 * time.Millisecond) 291 t.Assert(array.Len(), 1) 292 }) 293 } 294 295 func TestTimer_Exit(t *testing.T) { 296 gtest.C(t, func(t *gtest.T) { 297 timer := gtimer.New() 298 array := garray.New(true) 299 timer.Add(ctx, 200*time.Millisecond, func(ctx context.Context) { 300 array.Append(1) 301 gtimer.Exit() 302 }) 303 time.Sleep(1000 * time.Millisecond) 304 t.Assert(array.Len(), 1) 305 }) 306 }