github.com/wangyougui/gf/v2@v2.6.5/os/gcron/gcron_z_unit_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/wangyougui/gf. 6 7 package gcron_test 8 9 import ( 10 "context" 11 "fmt" 12 "testing" 13 "time" 14 15 "github.com/wangyougui/gf/v2/container/garray" 16 "github.com/wangyougui/gf/v2/frame/g" 17 "github.com/wangyougui/gf/v2/os/gcron" 18 "github.com/wangyougui/gf/v2/test/gtest" 19 ) 20 21 var ( 22 ctx = context.TODO() 23 ) 24 25 func TestCron_Add_Close(t *testing.T) { 26 gtest.C(t, func(t *gtest.T) { 27 cron := gcron.New() 28 array := garray.New(true) 29 _, err1 := cron.Add(ctx, "* * * * * *", func(ctx context.Context) { 30 g.Log().Print(ctx, "cron1") 31 array.Append(1) 32 }) 33 _, err2 := cron.Add(ctx, "* * * * * *", func(ctx context.Context) { 34 g.Log().Print(ctx, "cron2") 35 array.Append(1) 36 }, "test") 37 t.Assert(err1, nil) 38 t.Assert(err2, nil) 39 t.Assert(cron.Size(), 2) 40 time.Sleep(1300 * time.Millisecond) 41 t.Assert(array.Len(), 2) 42 time.Sleep(1300 * time.Millisecond) 43 t.Assert(array.Len(), 4) 44 cron.Close() 45 time.Sleep(1300 * time.Millisecond) 46 fixedLength := array.Len() 47 time.Sleep(1300 * time.Millisecond) 48 t.Assert(array.Len(), fixedLength) 49 }) 50 } 51 52 func TestCron_Basic(t *testing.T) { 53 gtest.C(t, func(t *gtest.T) { 54 cron := gcron.New() 55 cron.Add(ctx, "* * * * * *", func(ctx context.Context) {}, "add") 56 // fmt.Println("start", time.Now()) 57 cron.DelayAdd(ctx, time.Second, "* * * * * *", func(ctx context.Context) {}, "delay_add") 58 t.Assert(cron.Size(), 1) 59 time.Sleep(1200 * time.Millisecond) 60 t.Assert(cron.Size(), 2) 61 62 cron.Remove("delay_add") 63 t.Assert(cron.Size(), 1) 64 65 entry1 := cron.Search("add") 66 entry2 := cron.Search("test-none") 67 t.AssertNE(entry1, nil) 68 t.Assert(entry2, nil) 69 }) 70 71 // test @ error 72 gtest.C(t, func(t *gtest.T) { 73 cron := gcron.New() 74 defer cron.Close() 75 _, err := cron.Add(ctx, "@aaa", func(ctx context.Context) {}, "add") 76 t.AssertNE(err, nil) 77 }) 78 79 // test @every error 80 gtest.C(t, func(t *gtest.T) { 81 cron := gcron.New() 82 defer cron.Close() 83 _, err := cron.Add(ctx, "@every xxx", func(ctx context.Context) {}, "add") 84 t.AssertNE(err, nil) 85 }) 86 } 87 88 func TestCron_Remove(t *testing.T) { 89 gtest.C(t, func(t *gtest.T) { 90 cron := gcron.New() 91 array := garray.New(true) 92 cron.Add(ctx, "* * * * * *", func(ctx context.Context) { 93 array.Append(1) 94 }, "add") 95 t.Assert(array.Len(), 0) 96 time.Sleep(1200 * time.Millisecond) 97 t.Assert(array.Len(), 1) 98 99 cron.Remove("add") 100 t.Assert(array.Len(), 1) 101 time.Sleep(1200 * time.Millisecond) 102 t.Assert(array.Len(), 1) 103 }) 104 } 105 106 func TestCron_Add_FixedPattern(t *testing.T) { 107 for i := 0; i < 5; i++ { 108 doTestCronAddFixedPattern(t) 109 } 110 } 111 112 func doTestCronAddFixedPattern(t *testing.T) { 113 gtest.C(t, func(t *gtest.T) { 114 var ( 115 now = time.Now() 116 cron = gcron.New() 117 array = garray.New(true) 118 expect = now.Add(time.Second * 2) 119 ) 120 defer cron.Close() 121 122 var pattern = fmt.Sprintf( 123 `%d %d %d %d %d %s`, 124 expect.Second(), expect.Minute(), expect.Hour(), expect.Day(), expect.Month(), expect.Weekday().String(), 125 ) 126 cron.SetLogger(g.Log()) 127 g.Log().Debugf(ctx, `pattern: %s`, pattern) 128 _, err := cron.Add(ctx, pattern, func(ctx context.Context) { 129 array.Append(1) 130 }) 131 t.AssertNil(err) 132 time.Sleep(3000 * time.Millisecond) 133 g.Log().Debug(ctx, `current time`) 134 t.Assert(array.Len(), 1) 135 }) 136 } 137 138 func TestCron_AddSingleton(t *testing.T) { 139 // un used, can be removed 140 gtest.C(t, func(t *gtest.T) { 141 cron := gcron.New() 142 cron.Add(ctx, "* * * * * *", func(ctx context.Context) {}, "add") 143 cron.DelayAdd(ctx, time.Second, "* * * * * *", func(ctx context.Context) {}, "delay_add") 144 t.Assert(cron.Size(), 1) 145 time.Sleep(1200 * time.Millisecond) 146 t.Assert(cron.Size(), 2) 147 148 cron.Remove("delay_add") 149 t.Assert(cron.Size(), 1) 150 151 entry1 := cron.Search("add") 152 entry2 := cron.Search("test-none") 153 t.AssertNE(entry1, nil) 154 t.Assert(entry2, nil) 155 }) 156 // keep this 157 gtest.C(t, func(t *gtest.T) { 158 cron := gcron.New() 159 array := garray.New(true) 160 cron.AddSingleton(ctx, "* * * * * *", func(ctx context.Context) { 161 array.Append(1) 162 time.Sleep(50 * time.Second) 163 }) 164 t.Assert(cron.Size(), 1) 165 time.Sleep(3500 * time.Millisecond) 166 t.Assert(array.Len(), 1) 167 }) 168 169 } 170 171 func TestCron_AddOnce1(t *testing.T) { 172 gtest.C(t, func(t *gtest.T) { 173 cron := gcron.New() 174 array := garray.New(true) 175 cron.AddOnce(ctx, "* * * * * *", func(ctx context.Context) { 176 array.Append(1) 177 }) 178 cron.AddOnce(ctx, "* * * * * *", func(ctx context.Context) { 179 array.Append(1) 180 }) 181 t.Assert(cron.Size(), 2) 182 time.Sleep(2500 * time.Millisecond) 183 t.Assert(array.Len(), 2) 184 t.Assert(cron.Size(), 0) 185 }) 186 } 187 188 func TestCron_AddOnce2(t *testing.T) { 189 gtest.C(t, func(t *gtest.T) { 190 cron := gcron.New() 191 array := garray.New(true) 192 cron.AddOnce(ctx, "@every 2s", func(ctx context.Context) { 193 array.Append(1) 194 }) 195 t.Assert(cron.Size(), 1) 196 time.Sleep(3000 * time.Millisecond) 197 t.Assert(array.Len(), 1) 198 t.Assert(cron.Size(), 0) 199 }) 200 } 201 202 func TestCron_AddTimes(t *testing.T) { 203 gtest.C(t, func(t *gtest.T) { 204 cron := gcron.New() 205 array := garray.New(true) 206 _, _ = cron.AddTimes(ctx, "* * * * * *", 2, func(ctx context.Context) { 207 array.Append(1) 208 }) 209 time.Sleep(3500 * time.Millisecond) 210 t.Assert(array.Len(), 2) 211 t.Assert(cron.Size(), 0) 212 }) 213 } 214 215 func TestCron_DelayAdd(t *testing.T) { 216 gtest.C(t, func(t *gtest.T) { 217 cron := gcron.New() 218 array := garray.New(true) 219 cron.DelayAdd(ctx, 500*time.Millisecond, "* * * * * *", func(ctx context.Context) { 220 array.Append(1) 221 }) 222 t.Assert(cron.Size(), 0) 223 time.Sleep(800 * time.Millisecond) 224 t.Assert(array.Len(), 0) 225 t.Assert(cron.Size(), 1) 226 time.Sleep(1000 * time.Millisecond) 227 t.Assert(array.Len(), 1) 228 t.Assert(cron.Size(), 1) 229 }) 230 } 231 232 func TestCron_DelayAddSingleton(t *testing.T) { 233 gtest.C(t, func(t *gtest.T) { 234 cron := gcron.New() 235 array := garray.New(true) 236 cron.DelayAddSingleton(ctx, 500*time.Millisecond, "* * * * * *", func(ctx context.Context) { 237 array.Append(1) 238 time.Sleep(10 * time.Second) 239 }) 240 t.Assert(cron.Size(), 0) 241 time.Sleep(2200 * time.Millisecond) 242 t.Assert(array.Len(), 1) 243 t.Assert(cron.Size(), 1) 244 }) 245 } 246 247 func TestCron_DelayAddOnce(t *testing.T) { 248 gtest.C(t, func(t *gtest.T) { 249 cron := gcron.New() 250 array := garray.New(true) 251 cron.DelayAddOnce(ctx, 500*time.Millisecond, "* * * * * *", func(ctx context.Context) { 252 array.Append(1) 253 }) 254 t.Assert(cron.Size(), 0) 255 time.Sleep(800 * time.Millisecond) 256 t.Assert(array.Len(), 0) 257 t.Assert(cron.Size(), 1) 258 time.Sleep(2200 * time.Millisecond) 259 t.Assert(array.Len(), 1) 260 t.Assert(cron.Size(), 0) 261 }) 262 } 263 264 func TestCron_DelayAddTimes(t *testing.T) { 265 gtest.C(t, func(t *gtest.T) { 266 cron := gcron.New() 267 array := garray.New(true) 268 cron.DelayAddTimes(ctx, 500*time.Millisecond, "* * * * * *", 2, func(ctx context.Context) { 269 array.Append(1) 270 }) 271 t.Assert(cron.Size(), 0) 272 time.Sleep(800 * time.Millisecond) 273 t.Assert(array.Len(), 0) 274 t.Assert(cron.Size(), 1) 275 time.Sleep(3000 * time.Millisecond) 276 t.Assert(array.Len(), 2) 277 t.Assert(cron.Size(), 0) 278 }) 279 }