@@ -22,7 +22,7 @@ import XCTest
22
22
class WorkerTests : XCTestCase {
23
23
func testWorkerOutput( ) {
24
24
let host = WorkflowHost (
25
- workflow: TaskTestWorkerWorkflow ( key: " " )
25
+ workflow: TaskTestWorkerWorkflow ( key: " " , initialState : 0 )
26
26
)
27
27
28
28
let expectation = XCTestExpectation ( )
@@ -38,8 +38,120 @@ class WorkerTests: XCTestCase {
38
38
disposable? . dispose ( )
39
39
}
40
40
41
+ func testWorkflowUpdate( ) {
42
+ // Create the workflow which causes the TaskTestWorker to run.
43
+ let host = WorkflowHost (
44
+ workflow: TaskTestWorkerWorkflow ( key: " " , initialState: 0 )
45
+ )
46
+
47
+ var expectation = XCTestExpectation ( )
48
+ // Set to observe renderings
49
+ // This expectation should be called after the TaskTestWorker runs and
50
+ // updates the state.
51
+ var disposable = host. rendering. signal. observeValues { rendering in
52
+ expectation. fulfill ( )
53
+ }
54
+
55
+ // Test to make sure the initial state of the workflow is correct.
56
+ XCTAssertEqual ( 0 , host. rendering. value)
57
+
58
+ // Wait for the worker to run.
59
+ wait ( for: [ expectation] , timeout: 1.0 )
60
+ // Test to make sure the rendering after the worker runs is correct.
61
+ XCTAssertEqual ( 1 , host. rendering. value)
62
+
63
+ disposable? . dispose ( )
64
+
65
+ expectation = XCTestExpectation ( )
66
+ // Set to observe renderings
67
+ // This expectation should be called after the workflow is updated.
68
+ // After the host is updated with a new workflow instance the
69
+ // initial state should be 1.
70
+ disposable = host. rendering. signal. observeValues { rendering in
71
+ expectation. fulfill ( )
72
+ }
73
+
74
+ // Updated the workflow to a new initial state.
75
+ host. update ( workflow: TaskTestWorkerWorkflow ( key: " " , initialState: 7 ) )
76
+
77
+ // Wait for the workflow to render after being updated.
78
+ wait ( for: [ expectation] , timeout: 1.0 )
79
+ // Test to make sure the rendering matches the initial state.
80
+ XCTAssertEqual ( 7 , host. rendering. value)
81
+
82
+ expectation = XCTestExpectation ( )
83
+ // Set to observe renderings
84
+ // This expectation should be called when the worker runs.
85
+ // The worker isEquivalent is false because we have changed the initialState.
86
+ disposable = host. rendering. signal. observeValues { rendering in
87
+ expectation. fulfill ( )
88
+ }
89
+
90
+ // Wait for the worker to trigger a rendering.
91
+ wait ( for: [ expectation] , timeout: 1.0 )
92
+ // Check to make sure the rendering is correct.
93
+ XCTAssertEqual ( 8 , host. rendering. value)
94
+ }
95
+
96
+ func testWorkflowKeyChange( ) {
97
+ // Create the workflow which causes the TaskTestWorker to run.
98
+ let host = WorkflowHost (
99
+ workflow: TaskTestWorkerWorkflow ( key: " " , initialState: 0 )
100
+ )
101
+
102
+ var expectation = XCTestExpectation ( )
103
+ // Set to observe renderings
104
+ // This expectation should be called after the TaskTestWorker runs and
105
+ // updates the state.
106
+ var disposable = host. rendering. signal. observeValues { rendering in
107
+ expectation. fulfill ( )
108
+ }
109
+
110
+ // Test to make sure the initial state of the workflow is correct.
111
+ XCTAssertEqual ( 0 , host. rendering. value)
112
+
113
+ // Wait for the worker to run.
114
+ wait ( for: [ expectation] , timeout: 1.0 )
115
+ // Test to make sure the rendering after the worker runs is correct.
116
+ XCTAssertEqual ( 1 , host. rendering. value)
117
+
118
+ disposable? . dispose ( )
119
+
120
+ expectation = XCTestExpectation ( )
121
+ // Set to observe renderings
122
+ // This expectation should be called after the workflow is updated.
123
+ // After the host is updated with a new workflow instance the
124
+ // initial state should be 1.
125
+ disposable = host. rendering. signal. observeValues { rendering in
126
+ expectation. fulfill ( )
127
+ }
128
+
129
+ // Update the workflow to a new key which should force the worker to run.
130
+ host. update ( workflow: TaskTestWorkerWorkflow ( key: " key " , initialState: 0 ) )
131
+
132
+ // Wait for the workflow to render after being updated.
133
+ wait ( for: [ expectation] , timeout: 1.0 )
134
+ // Test to make sure the rendering matches the existing state
135
+ // since the inititalState didn't change.
136
+ XCTAssertEqual ( 1 , host. rendering. value)
137
+
138
+ expectation = XCTestExpectation ( )
139
+ // Set to observe renderings
140
+ // This expectation should be called when the worker runs.
141
+ // The worker should run because the key was changed for the workflow.
142
+ disposable = host. rendering. signal. observeValues { rendering in
143
+ expectation. fulfill ( )
144
+ }
145
+
146
+ // Wait for the worker to trigger a rendering.
147
+ wait ( for: [ expectation] , timeout: 1.0 )
148
+ // Check to make sure the rendering is correct.
149
+ // The worker adds one to the initialState so this should be 1.
150
+ XCTAssertEqual ( 1 , host. rendering. value)
151
+ }
152
+
41
153
func testExpectedWorker( ) {
42
- TaskTestWorkerWorkflow ( key: " 123 " )
154
+ TaskTestWorkerWorkflow ( key: " 123 " , initialState : 0 )
43
155
. renderTester ( )
44
156
. expectWorkflow (
45
157
type: WorkerWorkflow< TaskTestWorker> . self ,
@@ -127,11 +239,11 @@ private struct TaskTestWorkerWorkflow: Workflow {
127
239
typealias Rendering = Int
128
240
129
241
let key : String
130
-
131
- func makeInitialState( ) -> Int { 0 }
242
+ let initialState : Int
243
+ func makeInitialState( ) -> Int { initialState }
132
244
133
245
func render( state: Int , context: RenderContext < TaskTestWorkerWorkflow > ) -> Int {
134
- TaskTestWorker ( )
246
+ TaskTestWorker ( initialState : initialState )
135
247
. mapOutput { output in
136
248
AnyWorkflowAction { state in
137
249
state = output
@@ -141,18 +253,27 @@ private struct TaskTestWorkerWorkflow: Workflow {
141
253
. running ( in: context, key: key)
142
254
return state
143
255
}
256
+
257
+ func workflowDidChange( from previousWorkflow: TaskTestWorkerWorkflow , state: inout Int ) {
258
+ if previousWorkflow. initialState != initialState {
259
+ state = initialState
260
+ }
261
+ }
144
262
}
145
263
146
264
private struct TaskTestWorker : Worker {
147
265
typealias Output = Int
148
-
266
+ let initialState : Int
267
+
149
268
func run( ) async -> Int {
150
269
do {
151
270
try await Task . sleep ( nanoseconds: 10000000 )
152
271
} catch { }
153
272
154
- return 1
273
+ return initialState + 1
155
274
}
156
275
157
- func isEquivalent( to otherWorker: TaskTestWorker ) -> Bool { true }
276
+ func isEquivalent( to otherWorker: TaskTestWorker ) -> Bool {
277
+ return otherWorker. initialState == initialState
278
+ }
158
279
}
0 commit comments