-
Notifications
You must be signed in to change notification settings - Fork 94
Expand file tree
/
Copy pathtester_pending_futures_test.go
More file actions
81 lines (65 loc) · 2.74 KB
/
Copy pathtester_pending_futures_test.go
File metadata and controls
81 lines (65 loc) · 2.74 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
package tester
import (
"context"
"testing"
"time"
wf "github.com/cschleiden/go-workflows/workflow"
"github.com/stretchr/testify/require"
)
// Test that timer futures are properly detected as pending when workflow completes without waiting
func TestPendingTimerFutures(t *testing.T) {
wft := NewWorkflowTester[any](workflowWithPendingTimer)
// This should panic due to pending timer future
require.Panics(t, func() {
wft.Execute(context.Background())
}, "Expected panic about pending timer futures")
}
func workflowWithPendingTimer(ctx wf.Context) error {
// Schedule a timer but don't wait for it
wf.ScheduleTimer(ctx, 10*time.Second)
return nil // BUG: Returns without waiting for timer
}
// This test demonstrates the CORRECT behavior: activities automatically block workflow completion
func TestActivitiesAutomaticallyBlockWorkflowCompletion(t *testing.T) {
wft := NewWorkflowTester[any](workflowWithScheduledActivity)
wft.Registry().RegisterActivity(testActivity)
// Activities automatically block workflow completion - this is the correct behavior
// The workflow will wait for the activity to complete before finishing
wft.Execute(context.Background())
require.True(t, wft.WorkflowFinished())
result, err := wft.WorkflowResult()
require.NoError(t, err)
require.Nil(t, result)
}
func workflowWithScheduledActivity(ctx wf.Context) error {
// Schedule activity but don't explicitly wait for it
// The workflow framework automatically waits for activities to complete
wf.ExecuteActivity[string](ctx, wf.DefaultActivityOptions, testActivity)
return nil // This returns after the activity completes (automatic blocking)
}
func workflowWithPendingActivity(ctx wf.Context) (string, error) {
// Schedule activity but don't explicitly wait for it
wf.ExecuteActivity[string](ctx, wf.DefaultActivityOptions, testActivity)
// Even though we don't call future.Get(), the workflow framework
// automatically waits for the activity to complete
return "should-not-be-returned", nil
}
func testActivity(ctx context.Context) (string, error) {
return "activity-result", nil
}
// Test that workflow properly waits for activities when explicitly using Get()
func TestWorkflowExplicitlyWaitsForActivity(t *testing.T) {
wft := NewWorkflowTester[string](workflowExplicitlyWaiting)
wft.Registry().RegisterActivity(testActivity)
wft.Execute(context.Background())
require.True(t, wft.WorkflowFinished())
result, err := wft.WorkflowResult()
require.NoError(t, err)
require.Equal(t, "activity-result", result)
}
func workflowExplicitlyWaiting(ctx wf.Context) (string, error) {
// Schedule activity and explicitly wait for it
future := wf.ExecuteActivity[string](ctx, wf.DefaultActivityOptions, testActivity)
result, err := future.Get(ctx)
return result, err
}