github.com/gogf/gf@v1.16.9/os/gcron/gcron_unit_2_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 package gcron_test 8 9 import ( 10 "github.com/gogf/gf/frame/g" 11 "testing" 12 "time" 13 14 "github.com/gogf/gf/container/garray" 15 "github.com/gogf/gf/os/gcron" 16 "github.com/gogf/gf/test/gtest" 17 ) 18 19 func TestCron_Entry_Operations(t *testing.T) { 20 gtest.C(t, func(t *gtest.T) { 21 var ( 22 cron = gcron.New() 23 array = garray.New(true) 24 ) 25 cron.DelayAddTimes(500*time.Millisecond, "* * * * * *", 2, func() { 26 g.Log().Println("add times") 27 array.Append(1) 28 }) 29 t.Assert(cron.Size(), 0) 30 time.Sleep(800 * time.Millisecond) 31 t.Assert(array.Len(), 0) 32 t.Assert(cron.Size(), 1) 33 time.Sleep(3000 * time.Millisecond) 34 t.Assert(array.Len(), 2) 35 t.Assert(cron.Size(), 0) 36 }) 37 38 gtest.C(t, func(t *gtest.T) { 39 var ( 40 cron = gcron.New() 41 array = garray.New(true) 42 ) 43 entry, err1 := cron.Add("* * * * * *", func() { 44 g.Log().Println("add") 45 array.Append(1) 46 }) 47 t.Assert(err1, nil) 48 t.Assert(array.Len(), 0) 49 t.Assert(cron.Size(), 1) 50 time.Sleep(1300 * time.Millisecond) 51 t.Assert(array.Len(), 1) 52 t.Assert(cron.Size(), 1) 53 entry.Stop() 54 time.Sleep(5000 * time.Millisecond) 55 t.Assert(array.Len(), 1) 56 t.Assert(cron.Size(), 1) 57 entry.Start() 58 g.Log().Println("start") 59 time.Sleep(1000 * time.Millisecond) 60 t.Assert(array.Len(), 2) 61 t.Assert(cron.Size(), 1) 62 entry.Close() 63 time.Sleep(1200 * time.Millisecond) 64 t.Assert(cron.Size(), 0) 65 }) 66 }