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