Skip to content

Commit

Permalink
fix: os/gcron: unit testing case of package gcron occasionally failed #…
Browse files Browse the repository at this point in the history
  • Loading branch information
wanghaolonggit committed Jan 21, 2025
1 parent baea1c7 commit 10b0be0
Showing 1 changed file with 8 additions and 0 deletions.
8 changes: 8 additions & 0 deletions os/gcron/gcron_z_unit_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
"fmt"
"os"
"os/signal"
"sync"
"syscall"
"testing"
"time"
Expand Down Expand Up @@ -287,12 +288,17 @@ func TestCron_JobWaiter(t *testing.T) {
var err error
s1 := garray.New(true)
s2 := garray.New(true)
var firstJobWaitGroup, secondJobWaitGroup sync.WaitGroup
firstJobWaitGroup.Add(4)
secondJobWaitGroup.Add(2)
_, err = gcron.Add(ctx, "* * * * * *", func(ctx context.Context) {
firstJobWaitGroup.Done()
g.Log().Debug(ctx, "Every second")
s1.Append(struct{}{})
}, "MyFirstCronJob")
t.Assert(err, nil)
_, err = gcron.Add(ctx, "*/2 * * * * *", func(ctx context.Context) {
secondJobWaitGroup.Done()
g.Log().Debug(ctx, "Every 2s job start")
time.Sleep(3 * time.Second)
s2.Append(struct{}{})
Expand All @@ -305,6 +311,8 @@ func TestCron_JobWaiter(t *testing.T) {

go func() {
time.Sleep(4 * time.Second) // Ensure that the job is triggered twice
firstJobWaitGroup.Wait()
secondJobWaitGroup.Wait()
glog.Print(ctx, "Sending SIGINT")
quit <- syscall.SIGINT // Send SIGINT
}()
Expand Down

0 comments on commit 10b0be0

Please sign in to comment.