github.com/gogf/gf@v1.16.9/os/gcron/gcron_unit_1_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_Add_Close(t *testing.T) { 20 gtest.C(t, func(t *gtest.T) { 21 cron := gcron.New() 22 array := garray.New(true) 23 _, err1 := cron.Add("* * * * * *", func() { 24 g.Log().Println("cron1") 25 array.Append(1) 26 }) 27 _, err2 := cron.Add("* * * * * *", func() { 28 g.Log().Println("cron2") 29 array.Append(1) 30 }, "test") 31 t.Assert(err1, nil) 32 t.Assert(err2, nil) 33 t.Assert(cron.Size(), 2) 34 time.Sleep(1300 * time.Millisecond) 35 t.Assert(array.Len(), 2) 36 time.Sleep(1300 * time.Millisecond) 37 t.Assert(array.Len(), 4) 38 cron.Close() 39 time.Sleep(1300 * time.Millisecond) 40 fixedLength := array.Len() 41 time.Sleep(1300 * time.Millisecond) 42 t.Assert(array.Len(), fixedLength) 43 }) 44 } 45 46 func TestCron_Basic(t *testing.T) { 47 gtest.C(t, func(t *gtest.T) { 48 cron := gcron.New() 49 cron.Add("* * * * * *", func() {}, "add") 50 //fmt.Println("start", time.Now()) 51 cron.DelayAdd(time.Second, "* * * * * *", func() {}, "delay_add") 52 t.Assert(cron.Size(), 1) 53 time.Sleep(1200 * time.Millisecond) 54 t.Assert(cron.Size(), 2) 55 56 cron.Remove("delay_add") 57 t.Assert(cron.Size(), 1) 58 59 entry1 := cron.Search("add") 60 entry2 := cron.Search("test-none") 61 t.AssertNE(entry1, nil) 62 t.Assert(entry2, nil) 63 }) 64 } 65 66 func TestCron_Remove(t *testing.T) { 67 gtest.C(t, func(t *gtest.T) { 68 cron := gcron.New() 69 array := garray.New(true) 70 cron.Add("* * * * * *", func() { 71 array.Append(1) 72 }, "add") 73 t.Assert(array.Len(), 0) 74 time.Sleep(1200 * time.Millisecond) 75 t.Assert(array.Len(), 1) 76 77 cron.Remove("add") 78 t.Assert(array.Len(), 1) 79 time.Sleep(1200 * time.Millisecond) 80 t.Assert(array.Len(), 1) 81 }) 82 } 83 84 func TestCron_AddSingleton(t *testing.T) { 85 // un used, can be removed 86 gtest.C(t, func(t *gtest.T) { 87 cron := gcron.New() 88 cron.Add("* * * * * *", func() {}, "add") 89 cron.DelayAdd(time.Second, "* * * * * *", func() {}, "delay_add") 90 t.Assert(cron.Size(), 1) 91 time.Sleep(1200 * time.Millisecond) 92 t.Assert(cron.Size(), 2) 93 94 cron.Remove("delay_add") 95 t.Assert(cron.Size(), 1) 96 97 entry1 := cron.Search("add") 98 entry2 := cron.Search("test-none") 99 t.AssertNE(entry1, nil) 100 t.Assert(entry2, nil) 101 }) 102 // keep this 103 gtest.C(t, func(t *gtest.T) { 104 cron := gcron.New() 105 array := garray.New(true) 106 cron.AddSingleton("* * * * * *", func() { 107 array.Append(1) 108 time.Sleep(50 * time.Second) 109 }) 110 t.Assert(cron.Size(), 1) 111 time.Sleep(3500 * time.Millisecond) 112 t.Assert(array.Len(), 1) 113 }) 114 115 } 116 117 func TestCron_AddOnce1(t *testing.T) { 118 gtest.C(t, func(t *gtest.T) { 119 cron := gcron.New() 120 array := garray.New(true) 121 cron.AddOnce("* * * * * *", func() { 122 array.Append(1) 123 }) 124 cron.AddOnce("* * * * * *", func() { 125 array.Append(1) 126 }) 127 t.Assert(cron.Size(), 2) 128 time.Sleep(2500 * time.Millisecond) 129 t.Assert(array.Len(), 2) 130 t.Assert(cron.Size(), 0) 131 }) 132 } 133 134 func TestCron_AddOnce2(t *testing.T) { 135 gtest.C(t, func(t *gtest.T) { 136 cron := gcron.New() 137 array := garray.New(true) 138 cron.AddOnce("@every 2s", func() { 139 array.Append(1) 140 }) 141 t.Assert(cron.Size(), 1) 142 time.Sleep(3000 * time.Millisecond) 143 t.Assert(array.Len(), 1) 144 t.Assert(cron.Size(), 0) 145 }) 146 } 147 148 func TestCron_AddTimes(t *testing.T) { 149 gtest.C(t, func(t *gtest.T) { 150 cron := gcron.New() 151 array := garray.New(true) 152 cron.AddTimes("* * * * * *", 2, func() { 153 array.Append(1) 154 }) 155 time.Sleep(3500 * time.Millisecond) 156 t.Assert(array.Len(), 2) 157 t.Assert(cron.Size(), 0) 158 }) 159 } 160 161 func TestCron_DelayAdd(t *testing.T) { 162 gtest.C(t, func(t *gtest.T) { 163 cron := gcron.New() 164 array := garray.New(true) 165 cron.DelayAdd(500*time.Millisecond, "* * * * * *", func() { 166 array.Append(1) 167 }) 168 t.Assert(cron.Size(), 0) 169 time.Sleep(800 * time.Millisecond) 170 t.Assert(array.Len(), 0) 171 t.Assert(cron.Size(), 1) 172 time.Sleep(1000 * time.Millisecond) 173 t.Assert(array.Len(), 1) 174 t.Assert(cron.Size(), 1) 175 }) 176 } 177 178 func TestCron_DelayAddSingleton(t *testing.T) { 179 gtest.C(t, func(t *gtest.T) { 180 cron := gcron.New() 181 array := garray.New(true) 182 cron.DelayAddSingleton(500*time.Millisecond, "* * * * * *", func() { 183 array.Append(1) 184 time.Sleep(10 * time.Second) 185 }) 186 t.Assert(cron.Size(), 0) 187 time.Sleep(2200 * time.Millisecond) 188 t.Assert(array.Len(), 1) 189 t.Assert(cron.Size(), 1) 190 }) 191 } 192 193 func TestCron_DelayAddOnce(t *testing.T) { 194 gtest.C(t, func(t *gtest.T) { 195 cron := gcron.New() 196 array := garray.New(true) 197 cron.DelayAddOnce(500*time.Millisecond, "* * * * * *", func() { 198 array.Append(1) 199 }) 200 t.Assert(cron.Size(), 0) 201 time.Sleep(800 * time.Millisecond) 202 t.Assert(array.Len(), 0) 203 t.Assert(cron.Size(), 1) 204 time.Sleep(2200 * time.Millisecond) 205 t.Assert(array.Len(), 1) 206 t.Assert(cron.Size(), 0) 207 }) 208 } 209 210 func TestCron_DelayAddTimes(t *testing.T) { 211 gtest.C(t, func(t *gtest.T) { 212 cron := gcron.New() 213 array := garray.New(true) 214 cron.DelayAddTimes(500*time.Millisecond, "* * * * * *", 2, func() { 215 array.Append(1) 216 }) 217 t.Assert(cron.Size(), 0) 218 time.Sleep(800 * time.Millisecond) 219 t.Assert(array.Len(), 0) 220 t.Assert(cron.Size(), 1) 221 time.Sleep(3000 * time.Millisecond) 222 t.Assert(array.Len(), 2) 223 t.Assert(cron.Size(), 0) 224 }) 225 }