forked from temporalio/samples-go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
workflow_test.go
81 lines (68 loc) · 2.26 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
package expense
import (
"io"
"net/http"
"net/http/httptest"
"testing"
"time"
"github.com/stretchr/testify/mock"
"github.com/stretchr/testify/suite"
"go.temporal.io/sdk/testsuite"
)
type UnitTestSuite struct {
suite.Suite
testsuite.WorkflowTestSuite
}
func TestUnitTestSuite(t *testing.T) {
suite.Run(t, new(UnitTestSuite))
}
func (s *UnitTestSuite) Test_WorkflowWithMockActivities() {
env := s.NewTestWorkflowEnvironment()
env.RegisterActivity(CreateExpenseActivity)
env.RegisterActivity(WaitForDecisionActivity)
env.RegisterActivity(PaymentActivity)
env.OnActivity(CreateExpenseActivity, mock.Anything, mock.Anything).Return(nil).Once()
env.OnActivity(WaitForDecisionActivity, mock.Anything, mock.Anything).Return("APPROVED", nil).Once()
env.OnActivity(PaymentActivity, mock.Anything, mock.Anything).Return(nil).Once()
env.ExecuteWorkflow(SampleExpenseWorkflow, "test-expense-id")
s.True(env.IsWorkflowCompleted())
s.NoError(env.GetWorkflowError())
var workflowResult string
err := env.GetWorkflowResult(&workflowResult)
s.NoError(err)
s.Equal("COMPLETED", workflowResult)
env.AssertExpectations(s.T())
}
func (s *UnitTestSuite) Test_WorkflowWithMockServer() {
env := s.NewTestWorkflowEnvironment()
env.RegisterActivity(CreateExpenseActivity)
env.RegisterActivity(WaitForDecisionActivity)
env.RegisterActivity(PaymentActivity)
// setup mock expense server
handler := func(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "application/text")
switch r.URL.Path {
case "/create":
case "/registerCallback":
taskToken := []byte(r.PostFormValue("task_token"))
// simulate the expense is approved one hour later.
env.RegisterDelayedCallback(func() {
_ = env.CompleteActivity(taskToken, "APPROVED", nil)
}, time.Hour)
case "/action":
}
_, _ = io.WriteString(w, "SUCCEED")
}
server := httptest.NewServer(http.HandlerFunc(handler))
defer server.Close()
// pointing server to test mock
expenseServerHostPort = server.URL
env.ExecuteWorkflow(SampleExpenseWorkflow, "test-expense-id")
s.True(env.IsWorkflowCompleted())
s.NoError(env.GetWorkflowError())
var workflowResult string
err := env.GetWorkflowResult(&workflowResult)
s.NoError(err)
s.Equal("COMPLETED", workflowResult)
env.AssertExpectations(s.T())
}