-
Notifications
You must be signed in to change notification settings - Fork 202
/
workflow_test.go
91 lines (78 loc) · 2.34 KB
/
workflow_test.go
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
82
83
84
85
86
87
88
89
90
91
package updatabletimer
import (
"errors"
"github.com/stretchr/testify/suite"
"go.temporal.io/sdk/testsuite"
"testing"
"time"
)
type UnitTestSuite struct {
suite.Suite
testsuite.WorkflowTestSuite
}
func TestUnitTestSuite(t *testing.T) {
suite.Run(t, new(UnitTestSuite))
}
func (s *UnitTestSuite) Test_Sleep() {
env := s.NewTestWorkflowEnvironment()
start := env.Now()
wakeUpTime := env.Now().Add(30 * time.Minute)
env.RegisterDelayedCallback(func() {
value, err := env.QueryWorkflow(QueryType)
s.NoError(err)
var queryResult time.Time
err = value.Get(&queryResult)
s.NoError(err)
s.True(wakeUpTime.Equal(queryResult))
}, time.Minute*10)
env.ExecuteWorkflow(Workflow, wakeUpTime)
s.True(env.IsWorkflowCompleted())
s.NoError(env.GetWorkflowError())
elapsed := env.Now().Sub(start)
s.Equal(elapsed, 30*time.Minute)
}
func (s *UnitTestSuite) Test_UpdateWakeUpTime() {
env := s.NewTestWorkflowEnvironment()
start := env.Now()
// Update wake-up time
updatedWakeUpTime1 := env.Now().Add(15 * time.Minute)
env.RegisterDelayedCallback(func() {
env.SignalWorkflow(SignalType, updatedWakeUpTime1)
}, time.Minute*10)
// Check that it was updated
env.RegisterDelayedCallback(func() {
value, err := env.QueryWorkflow(QueryType)
s.NoError(err)
var queryResult time.Time
err = value.Get(&queryResult)
s.NoError(err)
s.True(updatedWakeUpTime1.Equal(queryResult))
}, time.Minute*11)
// Update wake-up time again
updatedWakeUpTime2 := env.Now().Add(40 * time.Minute)
env.RegisterDelayedCallback(func() {
env.SignalWorkflow(SignalType, updatedWakeUpTime2)
}, time.Minute*12)
wakeUpTime := env.Now().Add(30 * time.Minute)
env.ExecuteWorkflow(Workflow, wakeUpTime)
s.True(env.IsWorkflowCompleted())
s.NoError(env.GetWorkflowError())
elapsed := env.Now().Sub(start)
// Check that the wake-up time was updated
s.Equal(elapsed, 40*time.Minute)
}
func (s *UnitTestSuite) Test_CancelSleep() {
env := s.NewTestWorkflowEnvironment()
start := env.Now()
wakeUpTime := env.Now().Add(30 * time.Minute)
env.RegisterDelayedCallback(func() {
env.CancelWorkflow()
}, time.Minute*10)
env.ExecuteWorkflow(Workflow, wakeUpTime)
s.True(env.IsWorkflowCompleted())
workflowErr := env.GetWorkflowError()
s.Error(workflowErr)
s.EqualError(errors.Unwrap(workflowErr), "canceled")
elapsed := env.Now().Sub(start)
s.Equal(elapsed, 10*time.Minute)
}