|
2 | 2 |
|
3 | 3 | import static org.junit.Assert.*; |
4 | 4 |
|
| 5 | +import io.temporal.api.common.v1.Payload; |
5 | 6 | import io.temporal.common.MethodRetry; |
6 | 7 | import io.temporal.common.RetryOptions; |
| 8 | +import io.temporal.common.context.ContextPropagator; |
7 | 9 | import io.temporal.testing.TestActivityEnvironment; |
8 | 10 | import io.temporal.workflow.shared.TestActivities.TestActivity; |
9 | 11 | import io.temporal.workflow.shared.TestActivities.TestActivityImpl; |
10 | 12 | import java.lang.reflect.Method; |
11 | 13 | import java.time.Duration; |
| 14 | +import java.util.Collections; |
| 15 | +import java.util.List; |
12 | 16 | import java.util.Map; |
13 | 17 | import org.junit.*; |
14 | 18 | import org.junit.rules.Timeout; |
@@ -62,6 +66,69 @@ public void testActivityOptionsMerge() { |
62 | 66 | Assert.assertEquals(methodOps1, merged); |
63 | 67 | } |
64 | 68 |
|
| 69 | + @Test |
| 70 | + public void testActivityOptionsMergeWithImmutableContextPropagators() { |
| 71 | + // Local class to avoid code duplication |
| 72 | + class TestContextPropagator implements ContextPropagator { |
| 73 | + private final String name; |
| 74 | + |
| 75 | + TestContextPropagator(String name) { |
| 76 | + this.name = name; |
| 77 | + } |
| 78 | + |
| 79 | + @Override |
| 80 | + public String getName() { |
| 81 | + return name; |
| 82 | + } |
| 83 | + |
| 84 | + @Override |
| 85 | + public Map<String, Payload> serializeContext(Object context) { |
| 86 | + return Collections.emptyMap(); |
| 87 | + } |
| 88 | + |
| 89 | + @Override |
| 90 | + public Object deserializeContext(Map<String, Payload> context) { |
| 91 | + return null; |
| 92 | + } |
| 93 | + |
| 94 | + @Override |
| 95 | + public Object getCurrentContext() { |
| 96 | + return null; |
| 97 | + } |
| 98 | + |
| 99 | + @Override |
| 100 | + public void setCurrentContext(Object context) {} |
| 101 | + } |
| 102 | + |
| 103 | + ContextPropagator propagator1 = new TestContextPropagator("propagator1"); |
| 104 | + ContextPropagator propagator2 = new TestContextPropagator("propagator2"); |
| 105 | + |
| 106 | + // Create options with immutable singleton lists |
| 107 | + // This tests the fix for https://github.com/temporalio/sdk-java/issues/2482 |
| 108 | + ActivityOptions options1 = |
| 109 | + ActivityOptions.newBuilder() |
| 110 | + .setStartToCloseTimeout(Duration.ofSeconds(1)) |
| 111 | + .setContextPropagators(Collections.singletonList(propagator1)) |
| 112 | + .build(); |
| 113 | + |
| 114 | + ActivityOptions options2 = |
| 115 | + ActivityOptions.newBuilder() |
| 116 | + .setStartToCloseTimeout(Duration.ofSeconds(2)) |
| 117 | + .setContextPropagators(Collections.singletonList(propagator2)) |
| 118 | + .build(); |
| 119 | + |
| 120 | + // Merging should not throw UnsupportedOperationException |
| 121 | + ActivityOptions merged = |
| 122 | + ActivityOptions.newBuilder(options1).mergeActivityOptions(options2).build(); |
| 123 | + |
| 124 | + // Verify both context propagators are present in the merged result |
| 125 | + List<ContextPropagator> mergedPropagators = merged.getContextPropagators(); |
| 126 | + assertNotNull(mergedPropagators); |
| 127 | + assertEquals(2, mergedPropagators.size()); |
| 128 | + assertEquals("propagator1", mergedPropagators.get(0).getName()); |
| 129 | + assertEquals("propagator2", mergedPropagators.get(1).getName()); |
| 130 | + } |
| 131 | + |
65 | 132 | @Test |
66 | 133 | public void testActivityOptionsDefaultInstance() { |
67 | 134 | testEnv.registerActivitiesImplementations(new TestActivityImpl()); |
|
0 commit comments