github.com/gogf/gf/v2@v2.7.4/os/gcron/gcron_z_unit_entry_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 "context" 11 "testing" 12 "time" 13 14 "github.com/gogf/gf/v2/container/garray" 15 "github.com/gogf/gf/v2/os/gcron" 16 "github.com/gogf/gf/v2/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(ctx, 500*time.Millisecond, "* * * * * *", 2, func(ctx context.Context) { 26 array.Append(1) 27 }) 28 t.Assert(cron.Size(), 0) 29 time.Sleep(800 * time.Millisecond) 30 t.Assert(array.Len(), 0) 31 t.Assert(cron.Size(), 1) 32 time.Sleep(3000 * time.Millisecond) 33 t.Assert(array.Len(), 2) 34 t.Assert(cron.Size(), 0) 35 }) 36 37 gtest.C(t, func(t *gtest.T) { 38 var ( 39 cron = gcron.New() 40 array = garray.New(true) 41 ) 42 entry, err1 := cron.Add(ctx, "* * * * * *", func(ctx context.Context) { 43 array.Append(1) 44 }) 45 t.Assert(err1, nil) 46 t.Assert(array.Len(), 0) 47 t.Assert(cron.Size(), 1) 48 time.Sleep(1300 * time.Millisecond) 49 t.Assert(array.Len(), 1) 50 t.Assert(cron.Size(), 1) 51 entry.Stop() 52 time.Sleep(5000 * time.Millisecond) 53 t.Assert(array.Len(), 1) 54 t.Assert(cron.Size(), 1) 55 entry.Start() 56 time.Sleep(1000 * time.Millisecond) 57 t.Assert(array.Len(), 2) 58 t.Assert(cron.Size(), 1) 59 entry.Close() 60 time.Sleep(1200 * time.Millisecond) 61 t.Assert(cron.Size(), 0) 62 }) 63 }