github.com/zhongdalu/gf@v1.0.0/g/os/gtimer/gtimer_z_unit_0_test.go (about) 1 // Copyright 2018 gf Author(https://github.com/zhongdalu/gf). 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/zhongdalu/gf. 6 7 // Package functions 8 9 package gtimer_test 10 11 import ( 12 "github.com/zhongdalu/gf/g/container/garray" 13 "github.com/zhongdalu/gf/g/os/gtimer" 14 "github.com/zhongdalu/gf/g/test/gtest" 15 "testing" 16 "time" 17 ) 18 19 func TestSetTimeout(t *testing.T) { 20 gtest.Case(t, func() { 21 array := garray.New() 22 gtimer.SetTimeout(200*time.Millisecond, func() { 23 array.Append(1) 24 }) 25 time.Sleep(1000 * time.Millisecond) 26 gtest.Assert(array.Len(), 1) 27 }) 28 } 29 30 func TestSetInterval(t *testing.T) { 31 gtest.Case(t, func() { 32 array := garray.New() 33 gtimer.SetInterval(200*time.Millisecond, func() { 34 array.Append(1) 35 }) 36 time.Sleep(1100 * time.Millisecond) 37 gtest.Assert(array.Len(), 5) 38 }) 39 } 40 41 func TestAddEntry(t *testing.T) { 42 gtest.Case(t, func() { 43 array := garray.New() 44 gtimer.AddEntry(200*time.Millisecond, func() { 45 array.Append(1) 46 }, false, 2, gtimer.STATUS_READY) 47 time.Sleep(1100 * time.Millisecond) 48 gtest.Assert(array.Len(), 2) 49 }) 50 } 51 52 func TestAddSingleton(t *testing.T) { 53 gtest.Case(t, func() { 54 array := garray.New() 55 gtimer.AddSingleton(200*time.Millisecond, func() { 56 array.Append(1) 57 time.Sleep(10000 * time.Millisecond) 58 }) 59 time.Sleep(1100 * time.Millisecond) 60 gtest.Assert(array.Len(), 1) 61 }) 62 } 63 64 func TestAddTimes(t *testing.T) { 65 gtest.Case(t, func() { 66 array := garray.New() 67 gtimer.AddTimes(200*time.Millisecond, 2, func() { 68 array.Append(1) 69 }) 70 time.Sleep(1000 * time.Millisecond) 71 gtest.Assert(array.Len(), 2) 72 }) 73 } 74 75 func TestDelayAdd(t *testing.T) { 76 gtest.Case(t, func() { 77 array := garray.New() 78 gtimer.DelayAdd(200*time.Millisecond, 200*time.Millisecond, func() { 79 array.Append(1) 80 }) 81 time.Sleep(300 * time.Millisecond) 82 gtest.Assert(array.Len(), 0) 83 time.Sleep(200 * time.Millisecond) 84 gtest.Assert(array.Len(), 1) 85 }) 86 } 87 88 func TestDelayAddEntry(t *testing.T) { 89 gtest.Case(t, func() { 90 array := garray.New() 91 gtimer.DelayAddEntry(200*time.Millisecond, 200*time.Millisecond, func() { 92 array.Append(1) 93 }, false, 2, gtimer.STATUS_READY) 94 time.Sleep(300 * time.Millisecond) 95 gtest.Assert(array.Len(), 0) 96 time.Sleep(1000 * time.Millisecond) 97 gtest.Assert(array.Len(), 2) 98 }) 99 } 100 101 func TestDelayAddSingleton(t *testing.T) { 102 gtest.Case(t, func() { 103 array := garray.New() 104 gtimer.DelayAddSingleton(200*time.Millisecond, 200*time.Millisecond, func() { 105 array.Append(1) 106 time.Sleep(10000 * time.Millisecond) 107 }) 108 time.Sleep(300 * time.Millisecond) 109 gtest.Assert(array.Len(), 0) 110 time.Sleep(1000 * time.Millisecond) 111 gtest.Assert(array.Len(), 1) 112 }) 113 } 114 115 func TestDelayAddOnce(t *testing.T) { 116 gtest.Case(t, func() { 117 array := garray.New() 118 gtimer.DelayAddOnce(200*time.Millisecond, 200*time.Millisecond, func() { 119 array.Append(1) 120 }) 121 time.Sleep(300 * time.Millisecond) 122 gtest.Assert(array.Len(), 0) 123 time.Sleep(1000 * time.Millisecond) 124 gtest.Assert(array.Len(), 1) 125 }) 126 } 127 128 func TestDelayAddTimes(t *testing.T) { 129 gtest.Case(t, func() { 130 array := garray.New() 131 gtimer.DelayAddTimes(200*time.Millisecond, 200*time.Millisecond, 2, func() { 132 array.Append(1) 133 }) 134 time.Sleep(300 * time.Millisecond) 135 gtest.Assert(array.Len(), 0) 136 time.Sleep(1000 * time.Millisecond) 137 gtest.Assert(array.Len(), 2) 138 }) 139 }