Skip to content

Commit 6182b4f

Browse files
committed
make the counter atomic in TestScheduledWorkflows to handle occasional slowness in stopping the workflow scheduler
1 parent d46ebd1 commit 6182b4f

File tree

1 file changed

+5
-5
lines changed

1 file changed

+5
-5
lines changed

dbos/workflows_test.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import (
77
"reflect"
88
"runtime"
99
"sync"
10+
"sync/atomic"
1011
"testing"
1112
"time"
1213

@@ -1269,7 +1270,7 @@ func TestWorkflowDeadLetterQueue(t *testing.T) {
12691270
}
12701271

12711272
var (
1272-
counter = 0
1273+
counter atomic.Int64
12731274
counter1Ch = make(chan time.Time, 100)
12741275
)
12751276

@@ -1278,8 +1279,7 @@ func TestScheduledWorkflows(t *testing.T) {
12781279

12791280
RegisterWorkflow(dbosCtx, func(ctx DBOSContext, scheduledTime time.Time) (string, error) {
12801281
startTime := time.Now()
1281-
counter++
1282-
if counter == 10 {
1282+
if counter.Add(1) == 10 {
12831283
return "", fmt.Errorf("counter reached 10, stopping workflow")
12841284
}
12851285
select {
@@ -1335,8 +1335,8 @@ func TestScheduledWorkflows(t *testing.T) {
13351335
// Stop the workflowScheduler and check if it stops executing
13361336
dbosCtx.(*dbosContext).getWorkflowScheduler().Stop()
13371337
time.Sleep(3 * time.Second) // Wait a bit to ensure no more executions
1338-
currentCounter := counter // If more scheduled executions happen, this can also trigger a data race. If the scheduler is correct, there should be no race.
1339-
require.Less(t, counter, currentCounter+2, "Scheduled workflow continued executing after stopping scheduler")
1338+
currentCounter := counter.Load()
1339+
require.Less(t, counter.Load(), currentCounter+2, "Scheduled workflow continued executing after stopping scheduler")
13401340
})
13411341
}
13421342

0 commit comments

Comments
 (0)