Skip to content

Commit bcdf2a2

Browse files
authored
Merge pull request #125 from cschleiden/optimize-tests
Use exponential backoff when waiting for workflow results
2 parents 2d5d87e + 54c6b48 commit bcdf2a2

File tree

3 files changed

+20
-16
lines changed

3 files changed

+20
-16
lines changed

.github/workflows/go.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ jobs:
3838
- name: Tests
3939
run: |
4040
go install github.com/jstemmer/go-junit-report/v2@latest
41-
go test -race -count 2 -v ./... 2>&1 | go-junit-report -set-exit-code -iocopy -out "${{ github.workspace }}/report.xml"
41+
go test -race -count 1 -v ./... 2>&1 | go-junit-report -set-exit-code -iocopy -out "${{ github.workspace }}/report.xml"
4242
4343
4444
- name: Test Summary

backend/test/e2e.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -476,12 +476,14 @@ func EndToEndBackendTest(t *testing.T, setup func() TestBackend, teardown func(b
476476
w := worker.New(b, workerOptions)
477477

478478
t.Cleanup(func() {
479-
cancel()
479+
// Wait for in-progress executions to finish
480480
if err := w.WaitForCompletion(); err != nil {
481481
log.Println("Worker did not stop in time")
482482
t.FailNow()
483483
}
484484

485+
cancel()
486+
485487
if teardown != nil {
486488
teardown(b)
487489
}

client/client.go

Lines changed: 16 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import (
77
"time"
88

99
"github.com/benbjohnson/clock"
10+
"github.com/cenkalti/backoff/v4"
1011
"github.com/cschleiden/go-workflows/backend"
1112
a "github.com/cschleiden/go-workflows/internal/args"
1213
"github.com/cschleiden/go-workflows/internal/converter"
@@ -125,13 +126,21 @@ func (c *client) WaitForWorkflowInstance(ctx context.Context, instance *workflow
125126
timeout = time.Second * 20
126127
}
127128

128-
ticker := c.clock.Ticker(time.Second)
129-
defer ticker.Stop()
129+
b := backoff.ExponentialBackOff{
130+
InitialInterval: time.Millisecond * 1,
131+
MaxInterval: time.Second * 1,
132+
Multiplier: 1.5,
133+
RandomizationFactor: 0.5,
134+
MaxElapsedTime: timeout,
135+
Stop: backoff.Stop,
136+
Clock: c.clock,
137+
}
138+
b.Reset()
130139

131-
ctx, cancel := c.clock.WithTimeout(ctx, timeout)
132-
defer cancel()
140+
ticker := backoff.NewTicker(&b)
141+
defer ticker.Stop()
133142

134-
for {
143+
for range ticker.C {
135144
s, err := c.backend.GetWorkflowInstanceState(ctx, instance)
136145
if err != nil {
137146
return fmt.Errorf("getting workflow state: %w", err)
@@ -140,16 +149,9 @@ func (c *client) WaitForWorkflowInstance(ctx context.Context, instance *workflow
140149
if s == backend.WorkflowStateFinished {
141150
return nil
142151
}
143-
144-
ticker.Reset(time.Second)
145-
select {
146-
case <-ticker.C:
147-
continue
148-
149-
case <-ctx.Done():
150-
return errors.New("workflow did not finish in specified timeout")
151-
}
152152
}
153+
154+
return errors.New("workflow did not finish in specified timeout")
153155
}
154156

155157
// GetWorkflowResult gets the workflow result for the given workflow result. It first waits for the workflow to finish or until

0 commit comments

Comments
 (0)