summaryrefslogtreecommitdiff
path: root/tests/plugins/temporal/signal_test.go
blob: 5182628719074a07d1ac3123ff8c7585acacbc47 (plain)
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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
package tests

import (
	"context"
	"testing"
	"time"

	"github.com/pborman/uuid"
	"github.com/stretchr/testify/assert"
	"go.temporal.io/api/enums/v1"
	"go.temporal.io/api/history/v1"
	"go.temporal.io/sdk/client"
)

func Test_SignalsWithoutSignals(t *testing.T) {
	s := NewTestServer()
	defer s.MustClose()

	w, err := s.Client().ExecuteWorkflow(
		context.Background(),
		client.StartWorkflowOptions{
			TaskQueue: "default",
		},
		"SimpleSignalledWorkflow",
		"Hello World",
	)
	assert.NoError(t, err)

	var result int
	assert.NoError(t, w.Get(context.Background(), &result))
	assert.Equal(t, 0, result)
}

func Test_SendSignalDuringTimer(t *testing.T) {
	s := NewTestServer()
	defer s.MustClose()

	w, err := s.Client().SignalWithStartWorkflow(
		context.Background(),
		"signalled-"+uuid.New(),
		"add",
		10,
		client.StartWorkflowOptions{
			TaskQueue: "default",
		},
		"SimpleSignalledWorkflow",
	)
	assert.NoError(t, err)

	err = s.Client().SignalWorkflow(context.Background(), w.GetID(), w.GetRunID(), "add", -1)
	assert.NoError(t, err)

	var result int
	assert.NoError(t, w.Get(context.Background(), &result))
	assert.Equal(t, 9, result)

	s.AssertContainsEvent(t, w, func(event *history.HistoryEvent) bool {
		if event.EventType == enums.EVENT_TYPE_WORKFLOW_EXECUTION_SIGNALED {
			attr := event.Attributes.(*history.HistoryEvent_WorkflowExecutionSignaledEventAttributes)
			return attr.WorkflowExecutionSignaledEventAttributes.SignalName == "add"
		}

		return false
	})
}

func Test_SendSignalBeforeCompletingWorkflow(t *testing.T) {
	s := NewTestServer()
	defer s.MustClose()

	w, err := s.Client().ExecuteWorkflow(
		context.Background(),
		client.StartWorkflowOptions{
			TaskQueue: "default",
		},
		"SimpleSignalledWorkflowWithSleep",
	)
	assert.NoError(t, err)

	// should be around sleep(1) call
	time.Sleep(time.Second + time.Millisecond*200)

	err = s.Client().SignalWorkflow(context.Background(), w.GetID(), w.GetRunID(), "add", -1)
	assert.NoError(t, err)

	var result int
	assert.NoError(t, w.Get(context.Background(), &result))
	assert.Equal(t, -1, result)

	s.AssertContainsEvent(t, w, func(event *history.HistoryEvent) bool {
		if event.EventType == enums.EVENT_TYPE_WORKFLOW_EXECUTION_SIGNALED {
			attr := event.Attributes.(*history.HistoryEvent_WorkflowExecutionSignaledEventAttributes)
			return attr.WorkflowExecutionSignaledEventAttributes.SignalName == "add"
		}

		return false
	})
}

func Test_RuntimeSignal(t *testing.T) {
	s := NewTestServer()
	defer s.MustClose()

	w, err := s.Client().SignalWithStartWorkflow(
		context.Background(),
		"signalled-"+uuid.New(),
		"add",
		-1,
		client.StartWorkflowOptions{
			TaskQueue: "default",
		},
		"RuntimeSignalWorkflow",
	)
	assert.NoError(t, err)

	var result int
	assert.NoError(t, w.Get(context.Background(), &result))
	assert.Equal(t, -1, result)

	s.AssertContainsEvent(t, w, func(event *history.HistoryEvent) bool {
		if event.EventType == enums.EVENT_TYPE_WORKFLOW_EXECUTION_SIGNALED {
			attr := event.Attributes.(*history.HistoryEvent_WorkflowExecutionSignaledEventAttributes)
			return attr.WorkflowExecutionSignaledEventAttributes.SignalName == "add"
		}

		return false
	})
}

func Test_SignalSteps(t *testing.T) {
	s := NewTestServer()
	defer s.MustClose()

	w, err := s.Client().ExecuteWorkflow(
		context.Background(),
		client.StartWorkflowOptions{
			TaskQueue: "default",
		},
		"WorkflowWithSignalledSteps",
	)
	assert.NoError(t, err)

	err = s.Client().SignalWorkflow(context.Background(), w.GetID(), w.GetRunID(), "begin", true)
	assert.NoError(t, err)

	err = s.Client().SignalWorkflow(context.Background(), w.GetID(), w.GetRunID(), "next1", true)
	assert.NoError(t, err)

	v, err := s.Client().QueryWorkflow(context.Background(), w.GetID(), w.GetRunID(), "value", nil)
	assert.NoError(t, err)

	var r int
	assert.NoError(t, v.Get(&r))
	assert.Equal(t, 2, r)

	err = s.Client().SignalWorkflow(context.Background(), w.GetID(), w.GetRunID(), "next2", true)
	assert.NoError(t, err)

	v, err = s.Client().QueryWorkflow(context.Background(), w.GetID(), w.GetRunID(), "value", nil)
	assert.NoError(t, err)

	assert.NoError(t, v.Get(&r))
	assert.Equal(t, 3, r)

	var result int
	assert.NoError(t, w.Get(context.Background(), &result))

	// 3 ticks
	assert.Equal(t, 3, result)
}