@@ -9,12 +9,16 @@ package gcron_test
99import (
1010 "context"
1111 "fmt"
12+ "os"
13+ "os/signal"
14+ "syscall"
1215 "testing"
1316 "time"
1417
1518 "github.com/gogf/gf/v2/container/garray"
1619 "github.com/gogf/gf/v2/frame/g"
1720 "github.com/gogf/gf/v2/os/gcron"
21+ "github.com/gogf/gf/v2/os/glog"
1822 "github.com/gogf/gf/v2/test/gtest"
1923)
2024
@@ -277,3 +281,41 @@ func TestCron_DelayAddTimes(t *testing.T) {
277281 t .Assert (cron .Size (), 0 )
278282 })
279283}
284+
285+ func TestCron_JobWaiter (t * testing.T ) {
286+ gtest .C (t , func (t * gtest.T ) {
287+ var err error
288+ s1 := garray .New (true )
289+ s2 := garray .New (true )
290+ _ , err = gcron .Add (ctx , "* * * * * *" , func (ctx context.Context ) {
291+ g .Log ().Debug (ctx , "Every second" )
292+ s1 .Append (struct {}{})
293+ }, "MyFirstCronJob" )
294+ t .Assert (err , nil )
295+ _ , err = gcron .Add (ctx , "*/2 * * * * *" , func (ctx context.Context ) {
296+ g .Log ().Debug (ctx , "Every 2s job start" )
297+ time .Sleep (3 * time .Second )
298+ s2 .Append (struct {}{})
299+ g .Log ().Debug (ctx , "Every 2s job after 3 second end" )
300+ }, "MySecondCronJob" )
301+ t .Assert (err , nil )
302+
303+ quit := make (chan os.Signal , 1 )
304+ signal .Notify (quit , syscall .SIGINT , syscall .SIGTERM )
305+
306+ go func () {
307+ time .Sleep (4 * time .Second ) // Ensure that the job is triggered twice
308+ glog .Print (ctx , "Sending SIGINT" )
309+ quit <- syscall .SIGINT // Send SIGINT
310+ }()
311+
312+ sig := <- quit
313+ glog .Printf (ctx , "Signal received: %s, stopping cron" , sig )
314+
315+ glog .Print (ctx , "Waiting for all cron jobs to complete..." )
316+ gcron .StopGracefully ()
317+ glog .Print (ctx , "All cron jobs completed" )
318+ t .Assert (s1 .Len (), 4 )
319+ t .Assert (s2 .Len (), 2 )
320+ })
321+ }
0 commit comments