21
21
package io .temporal .workflow ;
22
22
23
23
import static org .junit .Assert .assertEquals ;
24
+ import static org .junit .Assert .assertNotSame ;
24
25
import static org .junit .Assert .assertNull ;
26
+ import static org .junit .Assert .assertSame ;
25
27
26
28
import io .temporal .testing .internal .SDKTestWorkflowRule ;
27
29
import io .temporal .workflow .shared .TestWorkflows .TestWorkflow1 ;
30
+ import io .temporal .workflow .shared .TestWorkflows .TestWorkflowReturnString ;
28
31
import java .time .Duration ;
29
32
import java .util .concurrent .atomic .AtomicInteger ;
30
33
import org .junit .Assert ;
@@ -47,9 +50,11 @@ public void testWorkflowLocals() {
47
50
48
51
public static class TestWorkflowLocals implements TestWorkflow1 {
49
52
53
+ @ SuppressWarnings ("deprecation" )
50
54
private final WorkflowThreadLocal <Integer > threadLocal =
51
55
WorkflowThreadLocal .withInitial (() -> 2 );
52
56
57
+ @ SuppressWarnings ("deprecation" )
53
58
private final WorkflowLocal <Integer > workflowLocal = WorkflowLocal .withInitial (() -> 5 );
54
59
55
60
@ Override
@@ -84,12 +89,15 @@ public static class TestWorkflowLocalsSupplierReuse implements TestWorkflow1 {
84
89
private final AtomicInteger localCalls = new AtomicInteger (0 );
85
90
private final AtomicInteger threadLocalCalls = new AtomicInteger (0 );
86
91
92
+ @ SuppressWarnings ("deprecation" )
87
93
private final WorkflowThreadLocal <Integer > workflowThreadLocal =
88
94
WorkflowThreadLocal .withInitial (
89
95
() -> {
90
96
threadLocalCalls .addAndGet (1 );
91
97
return null ;
92
98
});
99
+
100
+ @ SuppressWarnings ("deprecation" )
93
101
private final WorkflowLocal <Integer > workflowLocal =
94
102
WorkflowLocal .withInitial (
95
103
() -> {
@@ -131,4 +139,93 @@ public void testWorkflowLocalsSupplierReuse() {
131
139
String result = workflowStub .execute (testWorkflowRule .getTaskQueue ());
132
140
Assert .assertEquals ("ok" , result );
133
141
}
142
+
143
+ @ SuppressWarnings ("deprecation" )
144
+ static final WorkflowThreadLocal <AtomicInteger > threadLocal =
145
+ WorkflowThreadLocal .withInitial (() -> new AtomicInteger (2 ));
146
+
147
+ @ SuppressWarnings ("deprecation" )
148
+ static final WorkflowLocal <AtomicInteger > workflowLocal =
149
+ WorkflowLocal .withInitial (() -> new AtomicInteger (5 ));
150
+
151
+ static final WorkflowThreadLocal <AtomicInteger > threadLocalCached =
152
+ WorkflowThreadLocal .withCachedInitial (() -> new AtomicInteger (2 ));
153
+
154
+ static final WorkflowLocal <AtomicInteger > workflowLocalCached =
155
+ WorkflowLocal .withCachedInitial (() -> new AtomicInteger (5 ));
156
+
157
+ public static class TestInit implements TestWorkflowReturnString {
158
+
159
+ @ Override
160
+ public String execute () {
161
+ assertEquals (2 , threadLocal .get ().getAndSet (3 ));
162
+ assertEquals (5 , workflowLocal .get ().getAndSet (6 ));
163
+ assertEquals (2 , threadLocalCached .get ().getAndSet (3 ));
164
+ assertEquals (5 , workflowLocalCached .get ().getAndSet (6 ));
165
+ String out = Workflow .newChildWorkflowStub (TestWorkflow1 .class ).execute ("ign" );
166
+ assertEquals ("ok" , out );
167
+ return "result="
168
+ + threadLocal .get ().get ()
169
+ + ", "
170
+ + workflowLocal .get ().get ()
171
+ + ", "
172
+ + threadLocalCached .get ().get ()
173
+ + ", "
174
+ + workflowLocalCached .get ().get ();
175
+ }
176
+ }
177
+
178
+ public static class TestChildInit implements TestWorkflow1 {
179
+
180
+ @ Override
181
+ public String execute (String arg1 ) {
182
+ assertEquals (2 , threadLocal .get ().getAndSet (8 ));
183
+ assertEquals (5 , workflowLocal .get ().getAndSet (0 ));
184
+ return "ok" ;
185
+ }
186
+ }
187
+
188
+ @ Rule
189
+ public SDKTestWorkflowRule testWorkflowRuleInitialValueNotShared =
190
+ SDKTestWorkflowRule .newBuilder ()
191
+ .setWorkflowTypes (TestInit .class , TestChildInit .class )
192
+ .build ();
193
+
194
+ @ Test
195
+ public void testWorkflowInitialNotShared () {
196
+ TestWorkflowReturnString workflowStub =
197
+ testWorkflowRuleInitialValueNotShared .newWorkflowStubTimeoutOptions (
198
+ TestWorkflowReturnString .class );
199
+ String result = workflowStub .execute ();
200
+ Assert .assertEquals ("result=2, 5, 3, 6" , result );
201
+ }
202
+
203
+ public static class TestCaching implements TestWorkflow1 {
204
+
205
+ @ Override
206
+ public String execute (String arg1 ) {
207
+ assertNotSame (threadLocal .get (), threadLocal .get ());
208
+ assertNotSame (workflowLocal .get (), workflowLocal .get ());
209
+ threadLocal .set (threadLocal .get ());
210
+ workflowLocal .set (workflowLocal .get ());
211
+ assertSame (threadLocal .get (), threadLocal .get ());
212
+ assertSame (workflowLocal .get (), workflowLocal .get ());
213
+
214
+ assertSame (threadLocalCached .get (), threadLocalCached .get ());
215
+ assertSame (workflowLocalCached .get (), workflowLocalCached .get ());
216
+ return "ok" ;
217
+ }
218
+ }
219
+
220
+ @ Rule
221
+ public SDKTestWorkflowRule testWorkflowRuleCaching =
222
+ SDKTestWorkflowRule .newBuilder ().setWorkflowTypes (TestCaching .class ).build ();
223
+
224
+ @ Test
225
+ public void testWorkflowLocalCaching () {
226
+ TestWorkflow1 workflowStub =
227
+ testWorkflowRuleCaching .newWorkflowStubTimeoutOptions (TestWorkflow1 .class );
228
+ String out = workflowStub .execute ("ign" );
229
+ assertEquals ("ok" , out );
230
+ }
134
231
}
0 commit comments