Skip to content

Commit 81ed15a

Browse files
JoshVanLcicoyledapr-bot
authored
Integration: Expand workflow timer tests (dapr#8758)
* Integration: Expand workflow timer tests Expand workflow timer tests to cover scenarios of pausing and resuming timer workflows to ensure timers expirations are based on the clock time of the timer creation. Signed-off-by: joshvanl <me@joshvanl.dev> * Lint Signed-off-by: joshvanl <me@joshvanl.dev> --------- Signed-off-by: joshvanl <me@joshvanl.dev> Co-authored-by: Cassie Coyle <cassie@diagrid.io> Co-authored-by: Dapr Bot <56698301+dapr-bot@users.noreply.github.com>
1 parent 2f9f708 commit 81ed15a

File tree

4 files changed

+165
-21
lines changed

4 files changed

+165
-21
lines changed

tests/integration/suite/daprd/workflow/timer.go renamed to tests/integration/suite/daprd/workflow/timer/base.go

Lines changed: 18 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,11 @@ See the License for the specific language governing permissions and
1111
limitations under the License.
1212
*/
1313

14-
package workflow
14+
package timer
1515

1616
import (
1717
"context"
18+
"sync/atomic"
1819
"testing"
1920
"time"
2021

@@ -26,45 +27,41 @@ import (
2627
"github.com/dapr/dapr/tests/integration/suite"
2728
"github.com/dapr/durabletask-go/api"
2829
"github.com/dapr/durabletask-go/task"
30+
"github.com/dapr/kit/ptr"
2931
)
3032

3133
func init() {
32-
suite.Register(new(timer))
34+
suite.Register(new(base))
3335
}
3436

35-
type timer struct {
37+
type base struct {
3638
workflow *workflow.Workflow
3739
}
3840

39-
func (i *timer) Setup(t *testing.T) []framework.Option {
40-
i.workflow = workflow.New(t)
41+
func (b *base) Setup(t *testing.T) []framework.Option {
42+
b.workflow = workflow.New(t)
4143

4244
return []framework.Option{
43-
framework.WithProcesses(i.workflow),
45+
framework.WithProcesses(b.workflow),
4446
}
4547
}
4648

47-
func (i *timer) Run(t *testing.T, ctx context.Context) {
48-
i.workflow.WaitUntilRunning(t, ctx)
49+
func (b *base) Run(t *testing.T, ctx context.Context) {
50+
b.workflow.WaitUntilRunning(t, ctx)
4951

50-
i.workflow.Registry().AddOrchestratorN("timer", func(ctx *task.OrchestrationContext) (any, error) {
51-
if err := ctx.CreateTimer(time.Second * 4).Await(nil); err != nil {
52-
return nil, err
52+
var now atomic.Pointer[time.Time]
53+
b.workflow.Registry().AddOrchestratorN("timer", func(ctx *task.OrchestrationContext) (any, error) {
54+
if !ctx.IsReplaying {
55+
now.Store(ptr.Of(time.Now()))
5356
}
54-
require.NoError(t, ctx.CallActivity("abc", task.WithActivityInput("abc")).Await(nil))
55-
return nil, nil
57+
return nil, ctx.CreateTimer(time.Second * 4).Await(nil)
5658
})
57-
i.workflow.Registry().AddActivityN("abc", func(ctx task.ActivityContext) (any, error) {
58-
var f string
59-
require.NoError(t, ctx.GetInput(&f))
60-
return nil, nil
61-
})
62-
client := i.workflow.BackendClient(t, ctx)
6359

64-
now := time.Now()
60+
client := b.workflow.BackendClient(t, ctx)
61+
6562
id, err := client.ScheduleNewOrchestration(ctx, "timer", api.WithInstanceID("timer"))
6663
require.NoError(t, err)
6764
_, err = client.WaitForOrchestrationCompletion(ctx, id)
6865
require.NoError(t, err)
69-
assert.GreaterOrEqual(t, time.Since(now).Seconds(), 4.0)
66+
assert.InDelta(t, 4.0, time.Since(*now.Load()).Seconds(), 1.0)
7067
}
Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
/*
2+
Copyright 2025 The Dapr Authors
3+
Licensed under the Apache License, Version 2.0 (the "License");
4+
you may not use this file except in compliance with the License.
5+
You may obtain a copy of the License at
6+
http://wwb.apache.org/licenses/LICENSE-2.0
7+
Unless required by applicable law or agreed to in writing, software
8+
distributed under the License is distributed on an "AS IS" BASIS,
9+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
10+
See the License for the specific language governing permissions and
11+
limitations under the License.
12+
*/
13+
14+
package timer
15+
16+
import (
17+
"context"
18+
"sync/atomic"
19+
"testing"
20+
"time"
21+
22+
"github.com/stretchr/testify/assert"
23+
"github.com/stretchr/testify/require"
24+
25+
"github.com/dapr/dapr/tests/integration/framework"
26+
"github.com/dapr/dapr/tests/integration/framework/process/workflow"
27+
"github.com/dapr/dapr/tests/integration/suite"
28+
"github.com/dapr/durabletask-go/task"
29+
"github.com/dapr/kit/ptr"
30+
)
31+
32+
func init() {
33+
suite.Register(new(resumeearly))
34+
}
35+
36+
type resumeearly struct {
37+
workflow *workflow.Workflow
38+
}
39+
40+
func (r *resumeearly) Setup(t *testing.T) []framework.Option {
41+
r.workflow = workflow.New(t)
42+
43+
return []framework.Option{
44+
framework.WithProcesses(r.workflow),
45+
}
46+
}
47+
48+
func (r *resumeearly) Run(t *testing.T, ctx context.Context) {
49+
r.workflow.WaitUntilRunning(t, ctx)
50+
51+
var now atomic.Pointer[time.Time]
52+
r.workflow.Registry().AddOrchestratorN("timer", func(ctx *task.OrchestrationContext) (any, error) {
53+
if !ctx.IsReplaying {
54+
now.Store(ptr.Of(time.Now()))
55+
}
56+
return nil, ctx.CreateTimer(time.Second * 8).Await(nil)
57+
})
58+
59+
client := r.workflow.BackendClient(t, ctx)
60+
61+
id, err := client.ScheduleNewOrchestration(ctx, "timer")
62+
require.NoError(t, err)
63+
64+
time.Sleep(time.Second * 1)
65+
require.NoError(t, client.SuspendOrchestration(ctx, id, "foo"))
66+
67+
time.Sleep(time.Second * 3)
68+
require.NoError(t, client.ResumeOrchestration(ctx, id, "bar"))
69+
70+
_, err = client.WaitForOrchestrationCompletion(ctx, id)
71+
require.NoError(t, err)
72+
assert.InDelta(t, 8.0, time.Since(*now.Load()).Seconds(), 1.0)
73+
}
Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
/*
2+
Copyright 2025 The Dapr Authors
3+
Licensed under the Apache License, Version 2.0 (the "License");
4+
you may not use this file except in compliance with the License.
5+
You may obtain a copy of the License at
6+
http://wwb.apache.org/licenses/LICENSE-2.0
7+
Unless required by applicable law or agreed to in writing, software
8+
distributed under the License is distributed on an "AS IS" BASIS,
9+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
10+
See the License for the specific language governing permissions and
11+
limitations under the License.
12+
*/
13+
14+
package timer
15+
16+
import (
17+
"context"
18+
"sync/atomic"
19+
"testing"
20+
"time"
21+
22+
"github.com/stretchr/testify/assert"
23+
"github.com/stretchr/testify/require"
24+
25+
"github.com/dapr/dapr/tests/integration/framework"
26+
"github.com/dapr/dapr/tests/integration/framework/process/workflow"
27+
"github.com/dapr/dapr/tests/integration/suite"
28+
"github.com/dapr/durabletask-go/task"
29+
"github.com/dapr/kit/ptr"
30+
)
31+
32+
func init() {
33+
suite.Register(new(resumelate))
34+
}
35+
36+
type resumelate struct {
37+
workflow *workflow.Workflow
38+
}
39+
40+
func (r *resumelate) Setup(t *testing.T) []framework.Option {
41+
r.workflow = workflow.New(t)
42+
43+
return []framework.Option{
44+
framework.WithProcesses(r.workflow),
45+
}
46+
}
47+
48+
func (r *resumelate) Run(t *testing.T, ctx context.Context) {
49+
r.workflow.WaitUntilRunning(t, ctx)
50+
51+
var now atomic.Pointer[time.Time]
52+
r.workflow.Registry().AddOrchestratorN("timer", func(ctx *task.OrchestrationContext) (any, error) {
53+
if !ctx.IsReplaying {
54+
now.Store(ptr.Of(time.Now()))
55+
}
56+
return nil, ctx.CreateTimer(time.Second * 8).Await(nil)
57+
})
58+
59+
client := r.workflow.BackendClient(t, ctx)
60+
61+
id, err := client.ScheduleNewOrchestration(ctx, "timer")
62+
require.NoError(t, err)
63+
64+
time.Sleep(time.Second * 1)
65+
require.NoError(t, client.SuspendOrchestration(ctx, id, "foo"))
66+
67+
time.Sleep(time.Second * 10)
68+
require.NoError(t, client.ResumeOrchestration(ctx, id, "bar"))
69+
70+
_, err = client.WaitForOrchestrationCompletion(ctx, id)
71+
require.NoError(t, err)
72+
assert.InDelta(t, 11.0, time.Since(*now.Load()).Seconds(), 1.0)
73+
}

tests/integration/suite/daprd/workflow/workflow.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,4 +20,5 @@ import (
2020
_ "github.com/dapr/dapr/tests/integration/suite/daprd/workflow/reconnect"
2121
_ "github.com/dapr/dapr/tests/integration/suite/daprd/workflow/rerun"
2222
_ "github.com/dapr/dapr/tests/integration/suite/daprd/workflow/scheduler"
23+
_ "github.com/dapr/dapr/tests/integration/suite/daprd/workflow/timer"
2324
)

0 commit comments

Comments
 (0)