github.com/zhongdalu/gf@v1.0.0/g/os/gtimer/gtimer_z_unit_1_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 // Timer Operations 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 New() *gtimer.Timer { 20 return gtimer.New(10, 10*time.Millisecond) 21 } 22 23 func TestTimer_Add_Close(t *testing.T) { 24 gtest.Case(t, func() { 25 timer := New() 26 array := garray.New() 27 //fmt.Println("start", time.Now()) 28 timer.Add(200*time.Millisecond, func() { 29 //fmt.Println("entry1", time.Now()) 30 array.Append(1) 31 }) 32 timer.Add(200*time.Millisecond, func() { 33 //fmt.Println("entry2", time.Now()) 34 array.Append(1) 35 }) 36 timer.Add(400*time.Millisecond, func() { 37 //fmt.Println("entry3", time.Now()) 38 array.Append(1) 39 }) 40 time.Sleep(250 * time.Millisecond) 41 gtest.Assert(array.Len(), 2) 42 time.Sleep(250 * time.Millisecond) 43 gtest.Assert(array.Len(), 5) 44 timer.Close() 45 time.Sleep(250 * time.Millisecond) 46 fixedLength := array.Len() 47 time.Sleep(250 * time.Millisecond) 48 gtest.Assert(array.Len(), fixedLength) 49 }) 50 } 51 52 func TestTimer_Start_Stop_Close(t *testing.T) { 53 gtest.Case(t, func() { 54 timer := New() 55 array := garray.New() 56 timer.Add(200*time.Millisecond, func() { 57 //glog.Println("add...") 58 array.Append(1) 59 }) 60 gtest.Assert(array.Len(), 0) 61 time.Sleep(300 * time.Millisecond) 62 gtest.Assert(array.Len(), 1) 63 timer.Stop() 64 time.Sleep(1000 * time.Millisecond) 65 gtest.Assert(array.Len(), 1) 66 timer.Start() 67 time.Sleep(200 * time.Millisecond) 68 gtest.Assert(array.Len(), 2) 69 timer.Close() 70 time.Sleep(1000 * time.Millisecond) 71 gtest.Assert(array.Len(), 2) 72 }) 73 } 74 75 func TestTimer_AddSingleton(t *testing.T) { 76 gtest.Case(t, func() { 77 timer := New() 78 array := garray.New() 79 timer.AddSingleton(200*time.Millisecond, func() { 80 array.Append(1) 81 time.Sleep(10 * time.Second) 82 }) 83 time.Sleep(250 * time.Millisecond) 84 gtest.Assert(array.Len(), 1) 85 86 time.Sleep(500 * time.Millisecond) 87 gtest.Assert(array.Len(), 1) 88 }) 89 } 90 91 func TestTimer_AddOnce(t *testing.T) { 92 gtest.Case(t, func() { 93 timer := New() 94 array := garray.New() 95 timer.AddOnce(200*time.Millisecond, func() { 96 array.Append(1) 97 }) 98 timer.AddOnce(200*time.Millisecond, func() { 99 array.Append(1) 100 }) 101 time.Sleep(250 * time.Millisecond) 102 gtest.Assert(array.Len(), 2) 103 time.Sleep(250 * time.Millisecond) 104 gtest.Assert(array.Len(), 2) 105 timer.Close() 106 time.Sleep(250 * time.Millisecond) 107 fixedLength := array.Len() 108 time.Sleep(250 * time.Millisecond) 109 gtest.Assert(array.Len(), fixedLength) 110 }) 111 } 112 113 func TestTimer_AddTimes(t *testing.T) { 114 gtest.Case(t, func() { 115 timer := New() 116 array := garray.New() 117 timer.AddTimes(200*time.Millisecond, 2, func() { 118 array.Append(1) 119 }) 120 time.Sleep(1000 * time.Millisecond) 121 gtest.Assert(array.Len(), 2) 122 }) 123 } 124 125 func TestTimer_DelayAdd(t *testing.T) { 126 gtest.Case(t, func() { 127 timer := New() 128 array := garray.New() 129 timer.DelayAdd(200*time.Millisecond, 200*time.Millisecond, func() { 130 array.Append(1) 131 }) 132 time.Sleep(250 * time.Millisecond) 133 gtest.Assert(array.Len(), 0) 134 time.Sleep(250 * time.Millisecond) 135 gtest.Assert(array.Len(), 1) 136 }) 137 } 138 139 func TestTimer_DelayAddEntry(t *testing.T) { 140 gtest.Case(t, func() { 141 timer := New() 142 array := garray.New() 143 timer.DelayAddEntry(200*time.Millisecond, 200*time.Millisecond, func() { 144 array.Append(1) 145 }, false, 100, gtimer.STATUS_READY) 146 time.Sleep(250 * time.Millisecond) 147 gtest.Assert(array.Len(), 0) 148 time.Sleep(250 * time.Millisecond) 149 gtest.Assert(array.Len(), 1) 150 }) 151 } 152 153 func TestTimer_DelayAddSingleton(t *testing.T) { 154 gtest.Case(t, func() { 155 timer := New() 156 array := garray.New() 157 timer.DelayAddSingleton(200*time.Millisecond, 200*time.Millisecond, func() { 158 array.Append(1) 159 time.Sleep(10 * time.Second) 160 }) 161 time.Sleep(250 * time.Millisecond) 162 gtest.Assert(array.Len(), 0) 163 164 time.Sleep(1000 * time.Millisecond) 165 gtest.Assert(array.Len(), 1) 166 }) 167 } 168 169 func TestTimer_DelayAddOnce(t *testing.T) { 170 gtest.Case(t, func() { 171 timer := New() 172 array := garray.New() 173 timer.DelayAddOnce(200*time.Millisecond, 200*time.Millisecond, func() { 174 array.Append(1) 175 }) 176 time.Sleep(250 * time.Millisecond) 177 gtest.Assert(array.Len(), 0) 178 179 time.Sleep(250 * time.Millisecond) 180 gtest.Assert(array.Len(), 1) 181 182 time.Sleep(500 * time.Millisecond) 183 gtest.Assert(array.Len(), 1) 184 }) 185 } 186 187 func TestTimer_DelayAddTimes(t *testing.T) { 188 gtest.Case(t, func() { 189 timer := New() 190 array := garray.New() 191 timer.DelayAddTimes(200*time.Millisecond, 500*time.Millisecond, 2, func() { 192 array.Append(1) 193 }) 194 time.Sleep(200 * time.Millisecond) 195 gtest.Assert(array.Len(), 0) 196 197 time.Sleep(600 * time.Millisecond) 198 gtest.Assert(array.Len(), 1) 199 200 time.Sleep(600 * time.Millisecond) 201 gtest.Assert(array.Len(), 2) 202 203 time.Sleep(1000 * time.Millisecond) 204 gtest.Assert(array.Len(), 2) 205 }) 206 } 207 208 func TestTimer_AddLessThanInterval(t *testing.T) { 209 gtest.Case(t, func() { 210 timer := gtimer.New(10, 100*time.Millisecond) 211 array := garray.New() 212 timer.Add(20*time.Millisecond, func() { 213 array.Append(1) 214 }) 215 time.Sleep(50 * time.Millisecond) 216 gtest.Assert(array.Len(), 0) 217 218 time.Sleep(110 * time.Millisecond) 219 gtest.Assert(array.Len(), 1) 220 221 time.Sleep(110 * time.Millisecond) 222 gtest.Assert(array.Len(), 2) 223 }) 224 } 225 226 func TestTimer_AddLeveledEntry1(t *testing.T) { 227 gtest.Case(t, func() { 228 timer := New() 229 array := garray.New() 230 //glog.Println("start") 231 timer.DelayAdd(1000*time.Millisecond, 1001*time.Millisecond, func() { 232 //glog.Println("add") 233 array.Append(1) 234 }) 235 time.Sleep(1500 * time.Millisecond) 236 gtest.Assert(array.Len(), 0) 237 time.Sleep(1300 * time.Millisecond) 238 //glog.Println("check") 239 gtest.Assert(array.Len(), 1) 240 }) 241 } 242 243 func TestTimer_Exit(t *testing.T) { 244 gtest.Case(t, func() { 245 timer := New() 246 array := garray.New() 247 timer.Add(200*time.Millisecond, func() { 248 array.Append(1) 249 gtimer.Exit() 250 }) 251 time.Sleep(1000 * time.Millisecond) 252 gtest.Assert(array.Len(), 1) 253 }) 254 }