Skip to content

Commit 9a17799

Browse files
committed
Adding new workflow delaystart with 30 sec delay
1 parent df6f7bd commit 9a17799

File tree

10 files changed

+600
-20
lines changed

10 files changed

+600
-20
lines changed

Makefile

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ export PATH := $(GOPATH)/bin:$(PATH)
77
default: test
88

99
PROGS = helloworld \
10+
delaystart \
1011
branch \
1112
childworkflow \
1213
crossdomain \
@@ -52,6 +53,7 @@ TEST_DIRS=./cmd/samples/cron \
5253
./cmd/samples/recipes/choice \
5354
./cmd/samples/recipes/greetings \
5455
./cmd/samples/recipes/helloworld \
56+
./cmd/samples/recipes/delaystart \
5557
./cmd/samples/recipes/cancelactivity \
5658
./cmd/samples/recipes/pickfirst \
5759
./cmd/samples/recipes/mutex \
@@ -75,6 +77,9 @@ cancelactivity:
7577
helloworld:
7678
go build -o bin/helloworld cmd/samples/recipes/helloworld/*.go
7779

80+
delaystart:
81+
go build -o bin/delaystart cmd/samples/recipes/delaystart/*.go
82+
7883
branch:
7984
go build -o bin/branch cmd/samples/recipes/branch/*.go
8085

@@ -168,6 +173,7 @@ sideeffect:
168173
go build -o bin/sideeffect cmd/samples/recipes/sideeffect/*.go
169174

170175
bins: helloworld \
176+
delaystart \
171177
branch \
172178
crossdomain \
173179
childworkflow \

cmd/samples/README.md

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,18 @@ See instructions for running the Cadence Server: https://github.com/uber/cadence
1010

1111
## Steps to run samples
1212
### Build Samples
13+
* Build all the workflows at once
1314
```
1415
make
1516
```
17+
* Build a workflow individually
18+
```
19+
make <WORKFLOW NAME>
20+
21+
Example:
22+
make delaystart
23+
make hellowrold
24+
```
1625

1726
### Run HelloWorld Sample
1827
* Start workers for helloworld workflow and activities
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
package main
2+
3+
import (
4+
"context"
5+
"github.com/stretchr/testify/require"
6+
"go.uber.org/cadence/activity"
7+
"go.uber.org/cadence/testsuite"
8+
"go.uber.org/cadence/worker"
9+
"go.uber.org/zap"
10+
"go.uber.org/zap/zapcore"
11+
"testing"
12+
)
13+
14+
func sampleActivity(ctx context.Context) error {
15+
logger := activity.GetLogger(ctx)
16+
logger.Info("test logging")
17+
return nil
18+
}
19+
20+
func Test_Activity_Noop_Logger(t *testing.T) {
21+
testSuite := &testsuite.WorkflowTestSuite{}
22+
env := testSuite.NewTestActivityEnvironment()
23+
env.RegisterActivity(sampleActivity)
24+
val, err := env.ExecuteActivity(sampleActivity)
25+
require.Nil(t, err)
26+
require.True(t, !val.HasValue())
27+
}
28+
29+
func Test_Activity_Print_Logger(t *testing.T) {
30+
testSuite := &testsuite.WorkflowTestSuite{}
31+
env := testSuite.NewTestActivityEnvironment()
32+
33+
logger, err := zap.NewProduction()
34+
require.Nil(t, err)
35+
36+
var outputLogs []string
37+
logger = logger.WithOptions(zap.Hooks(
38+
func(entry zapcore.Entry) error {
39+
outputLogs = append(outputLogs, entry.Message)
40+
return nil
41+
},
42+
))
43+
44+
env.SetWorkerOptions(worker.Options{
45+
Logger: logger,
46+
})
47+
env.RegisterActivity(sampleActivity)
48+
49+
val, err := env.ExecuteActivity(sampleActivity)
50+
51+
require.Nil(t, err)
52+
require.True(t, !val.HasValue())
53+
require.True(t, len(outputLogs)==1)
54+
require.True(t, outputLogs[0] == "test logging")
55+
}
Lines changed: 159 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,159 @@
1+
[
2+
{
3+
"eventId":1,
4+
"timestamp":1558126752505445000,
5+
"eventType":"WorkflowExecutionStarted",
6+
"version":-24,
7+
"taskId":33554432,
8+
"workflowExecutionStartedEventAttributes":{
9+
"workflowType":{
10+
"name":"github.com/uber-common/cadence-samples/cmd/samples/recipes/delaystart.delayStartWorkflow"
11+
},
12+
"taskList":{
13+
"name":"helloWorldGroup"
14+
},
15+
"input":"IkNhZGVuY2UiCg==",
16+
"executionStartToCloseTimeoutSeconds":3600,
17+
"taskStartToCloseTimeoutSeconds":10,
18+
"identity":"66434@longer-C02V60N3HTDG@",
19+
"attempt":0,
20+
"firstDecisionTaskBackoffSeconds":0
21+
}
22+
},
23+
{
24+
"eventId":2,
25+
"timestamp":1558126752505631000,
26+
"eventType":"DecisionTaskScheduled",
27+
"version":-24,
28+
"taskId":33554433,
29+
"decisionTaskScheduledEventAttributes":{
30+
"taskList":{
31+
"name":"helloWorldGroup"
32+
},
33+
"startToCloseTimeoutSeconds":10,
34+
"attempt":0
35+
}
36+
},
37+
{
38+
"eventId":3,
39+
"timestamp":1558126757360699000,
40+
"eventType":"DecisionTaskStarted",
41+
"version":-24,
42+
"taskId":33554438,
43+
"decisionTaskStartedEventAttributes":{
44+
"scheduledEventId":2,
45+
"identity":"66471@longer-C02V60N3HTDG@helloWorldGroup",
46+
"requestId":"665325ec-74eb-4337-bf82-fceeb60e2196"
47+
}
48+
},
49+
{
50+
"eventId":4,
51+
"timestamp":1558126757385307000,
52+
"eventType":"DecisionTaskCompleted",
53+
"version":-24,
54+
"taskId":33554441,
55+
"decisionTaskCompletedEventAttributes":{
56+
"scheduledEventId":2,
57+
"startedEventId":3,
58+
"identity":"66471@longer-C02V60N3HTDG@helloWorldGroup",
59+
"binaryChecksum":"b2e32759177ccbb3e67ad7694aec233c"
60+
}
61+
},
62+
{
63+
"eventId":5,
64+
"timestamp":1558126757385333000,
65+
"eventType":"ActivityTaskScheduled",
66+
"version":-24,
67+
"taskId":33554442,
68+
"activityTaskScheduledEventAttributes":{
69+
"activityId":"0",
70+
"activityType":{
71+
"name":"github.com/uber-common/cadence-samples/cmd/samples/recipes/helloworld.helloWorldActivity"
72+
},
73+
"taskList":{
74+
"name":"helloWorldGroup"
75+
},
76+
"input":"IkNhZGVuY2UiCg==",
77+
"scheduleToCloseTimeoutSeconds":3600,
78+
"scheduleToStartTimeoutSeconds":3600,
79+
"startToCloseTimeoutSeconds":3600,
80+
"heartbeatTimeoutSeconds":3600,
81+
"decisionTaskCompletedEventId":4
82+
}
83+
},
84+
{
85+
"eventId":6,
86+
"timestamp":1558126757393919000,
87+
"eventType":"ActivityTaskStarted",
88+
"version":-24,
89+
"taskId":33554446,
90+
"activityTaskStartedEventAttributes":{
91+
"scheduledEventId":5,
92+
"identity":"66471@longer-C02V60N3HTDG@helloWorldGroup",
93+
"requestId":"45c4006a-ae7c-4392-baa6-c090857f884b",
94+
"attempt":0
95+
}
96+
},
97+
{
98+
"eventId":7,
99+
"timestamp":1558126757403468000,
100+
"eventType":"ActivityTaskCompleted",
101+
"version":-24,
102+
"taskId":33554447,
103+
"activityTaskCompletedEventAttributes":{
104+
"result":"IkhlbGxvIENhZGVuY2UhIgo=",
105+
"scheduledEventId":5,
106+
"startedEventId":6,
107+
"identity":"66471@longer-C02V60N3HTDG@helloWorldGroup"
108+
}
109+
},
110+
{
111+
"eventId":8,
112+
"timestamp":1558126757403476000,
113+
"eventType":"DecisionTaskScheduled",
114+
"version":-24,
115+
"taskId":33554450,
116+
"decisionTaskScheduledEventAttributes":{
117+
"taskList":{
118+
"name":"longer-C02V60N3HTDG:33ab3ada-4636-4386-8575-81dd8dc02e9a"
119+
},
120+
"startToCloseTimeoutSeconds":10,
121+
"attempt":0
122+
}
123+
},
124+
{
125+
"eventId":9,
126+
"timestamp":1558126757410564000,
127+
"eventType":"DecisionTaskStarted",
128+
"version":-24,
129+
"taskId":33554454,
130+
"decisionTaskStartedEventAttributes":{
131+
"scheduledEventId":8,
132+
"identity":"66471@longer-C02V60N3HTDG@helloWorldGroup",
133+
"requestId":"cb1fdadf-f46b-4840-9b97-863f4b3b6b11"
134+
}
135+
},
136+
{
137+
"eventId":10,
138+
"timestamp":1558126757491491000,
139+
"eventType":"DecisionTaskCompleted",
140+
"version":-24,
141+
"taskId":33554457,
142+
"decisionTaskCompletedEventAttributes":{
143+
"scheduledEventId":8,
144+
"startedEventId":9,
145+
"identity":"66471@longer-C02V60N3HTDG@helloWorldGroup",
146+
"binaryChecksum":"b2e32759177ccbb3e67ad7694aec233c"
147+
}
148+
},
149+
{
150+
"eventId":11,
151+
"timestamp":1558126757491513000,
152+
"eventType":"WorkflowExecutionCompleted",
153+
"version":-24,
154+
"taskId":33554458,
155+
"workflowExecutionCompletedEventAttributes":{
156+
"decisionTaskCompletedEventId":10
157+
}
158+
}
159+
]
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
package main
2+
3+
import (
4+
"context"
5+
"time"
6+
7+
"go.uber.org/cadence/activity"
8+
"go.uber.org/cadence/workflow"
9+
"go.uber.org/zap"
10+
)
11+
12+
/**
13+
* This is the hello world workflow sample.
14+
*/
15+
16+
// ApplicationName is the task list for this sample
17+
const ApplicationName = "delaystartGroup"
18+
19+
const delayStartWorkflowName = "delayStartWorkflow"
20+
21+
// helloWorkflow workflow decider
22+
func delayStartWorkflow(ctx workflow.Context, delayStart time.Duration) error {
23+
ao := workflow.ActivityOptions{
24+
ScheduleToStartTimeout: time.Minute,
25+
StartToCloseTimeout: time.Minute,
26+
HeartbeatTimeout: time.Second * 20,
27+
}
28+
ctx = workflow.WithActivityOptions(ctx, ao)
29+
30+
logger := workflow.GetLogger(ctx)
31+
logger.Info("delaystart workflow started after waiting for " + delayStart.String())
32+
var helloworldResult string
33+
err := workflow.ExecuteActivity(ctx, delayStartActivity, delayStart).Get(ctx, &helloworldResult)
34+
if err != nil {
35+
logger.Error("Activity failed after waiting for "+delayStart.String(), zap.Error(err))
36+
return err
37+
}
38+
39+
// Adding a new activity to the workflow will result in a non-determinstic change for the workflow
40+
// Please check https://cadenceworkflow.io/docs/go-client/workflow-versioning/ for more information
41+
//
42+
// Un-commenting the following code and the TestReplayWorkflowHistoryFromFile in replay_test.go
43+
// will fail due to the non-determinstic change
44+
//
45+
// If you have a completed workflow execution without the following code and run the
46+
// TestWorkflowShadowing in shadow_test.go or start the worker in shadow mode (using -m shadower)
47+
// those two shadowing check will also fail due to the non-deterministic change
48+
//
49+
// err := workflow.ExecuteActivity(ctx, helloWorldActivity, name).Get(ctx, &helloworldResult)
50+
// if err != nil {
51+
// logger.Error("Activity failed.", zap.Error(err))
52+
// return err
53+
// }
54+
55+
logger.Info("Workflow completed.", zap.String("Result", helloworldResult))
56+
57+
return nil
58+
}
59+
60+
func delayStartActivity(ctx context.Context, delayStart time.Duration) (string, error) {
61+
logger := activity.GetLogger(ctx)
62+
logger.Info("delayStartActivity started after " + delayStart.String())
63+
return "Activity started after " + delayStart.String(), nil
64+
}
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
package main
2+
3+
import (
4+
"testing"
5+
"time"
6+
7+
"github.com/stretchr/testify/require"
8+
"go.uber.org/cadence/activity"
9+
"go.uber.org/cadence/encoded"
10+
"go.uber.org/cadence/testsuite"
11+
)
12+
13+
func Test_Workflow(t *testing.T) {
14+
testSuite := &testsuite.WorkflowTestSuite{}
15+
16+
env := testSuite.NewTestWorkflowEnvironment()
17+
env.RegisterWorkflow(delayStartWorkflow)
18+
env.RegisterActivity(delayStartActivity)
19+
20+
var activityMessage string
21+
env.SetOnActivityCompletedListener(func(activityInfo *activity.Info, result encoded.Value, err error) {
22+
result.Get(&activityMessage)
23+
})
24+
25+
delayStart := 30 * time.Second
26+
env.ExecuteWorkflow(delayStartWorkflow, delayStart)
27+
28+
require.True(t, env.IsWorkflowCompleted())
29+
require.NoError(t, env.GetWorkflowError())
30+
require.Equal(t, "Activity started after "+delayStart.String(), activityMessage)
31+
}

0 commit comments

Comments
 (0)