|
| 1 | +package io.temporal.internal.activity; |
| 2 | + |
| 3 | +import static org.junit.Assert.*; |
| 4 | + |
| 5 | +import io.temporal.activity.ActivityExecutionContext; |
| 6 | +import java.lang.reflect.InvocationHandler; |
| 7 | +import java.lang.reflect.Proxy; |
| 8 | +import java.util.concurrent.atomic.AtomicReference; |
| 9 | +import org.junit.Assume; |
| 10 | +import org.junit.Test; |
| 11 | + |
| 12 | +public class CurrentActivityExecutionContextTest { |
| 13 | + |
| 14 | + private static ActivityExecutionContext proxyContext() { |
| 15 | + InvocationHandler handler = (proxy, method, args) -> null; |
| 16 | + return (ActivityExecutionContext) |
| 17 | + Proxy.newProxyInstance( |
| 18 | + ActivityExecutionContext.class.getClassLoader(), |
| 19 | + new Class[] {ActivityExecutionContext.class}, |
| 20 | + handler); |
| 21 | + } |
| 22 | + |
| 23 | + @Test |
| 24 | + public void platformThreadNestedSetUnsetBehavior() { |
| 25 | + ActivityExecutionContext ctx1 = proxyContext(); |
| 26 | + ActivityExecutionContext ctx2 = proxyContext(); |
| 27 | + |
| 28 | + assertFalse(CurrentActivityExecutionContext.isSet()); |
| 29 | + assertThrows(IllegalStateException.class, CurrentActivityExecutionContext::get); |
| 30 | + |
| 31 | + CurrentActivityExecutionContext.set(ctx1); |
| 32 | + assertTrue(CurrentActivityExecutionContext.isSet()); |
| 33 | + assertSame("should return ctx1", ctx1, CurrentActivityExecutionContext.get()); |
| 34 | + |
| 35 | + CurrentActivityExecutionContext.set(ctx2); |
| 36 | + assertTrue(CurrentActivityExecutionContext.isSet()); |
| 37 | + assertSame("should return ctx2 (top of stack)", ctx2, CurrentActivityExecutionContext.get()); |
| 38 | + |
| 39 | + CurrentActivityExecutionContext.unset(); |
| 40 | + assertTrue(CurrentActivityExecutionContext.isSet()); |
| 41 | + assertSame("after popping, should return ctx1", ctx1, CurrentActivityExecutionContext.get()); |
| 42 | + |
| 43 | + CurrentActivityExecutionContext.unset(); |
| 44 | + assertFalse(CurrentActivityExecutionContext.isSet()); |
| 45 | + assertThrows( |
| 46 | + "get() should throw after final unset", |
| 47 | + IllegalStateException.class, |
| 48 | + CurrentActivityExecutionContext::get); |
| 49 | + } |
| 50 | + |
| 51 | + @Test |
| 52 | + public void virtualThreadNestedSetUnsetBehavior_ifSupported() throws Exception { |
| 53 | + boolean supportsVirtual; |
| 54 | + try { |
| 55 | + Thread.class.getMethod("startVirtualThread", Runnable.class); |
| 56 | + supportsVirtual = true; |
| 57 | + } catch (NoSuchMethodException e) { |
| 58 | + supportsVirtual = false; |
| 59 | + } |
| 60 | + |
| 61 | + Assume.assumeTrue("Virtual threads not supported in this JVM; skipping", supportsVirtual); |
| 62 | + |
| 63 | + AtomicReference<Throwable> failure = new AtomicReference<>(null); |
| 64 | + AtomicReference<ActivityExecutionContext> seenAfterFirstSet = new AtomicReference<>(null); |
| 65 | + AtomicReference<ActivityExecutionContext> seenAfterSecondSet = new AtomicReference<>(null); |
| 66 | + AtomicReference<Boolean> seenIsSetAfterFinalUnset = new AtomicReference<>(null); |
| 67 | + |
| 68 | + Thread vt = |
| 69 | + Thread.startVirtualThread( |
| 70 | + () -> { |
| 71 | + try { |
| 72 | + ActivityExecutionContext vctx1 = proxyContext(); |
| 73 | + ActivityExecutionContext vctx2 = proxyContext(); |
| 74 | + |
| 75 | + assertFalse(CurrentActivityExecutionContext.isSet()); |
| 76 | + try { |
| 77 | + CurrentActivityExecutionContext.get(); |
| 78 | + fail("get() should have thrown when no context is set"); |
| 79 | + } catch (IllegalStateException expected) { |
| 80 | + } |
| 81 | + |
| 82 | + CurrentActivityExecutionContext.set(vctx1); |
| 83 | + seenAfterFirstSet.set(CurrentActivityExecutionContext.get()); |
| 84 | + |
| 85 | + CurrentActivityExecutionContext.set(vctx2); |
| 86 | + seenAfterSecondSet.set(CurrentActivityExecutionContext.get()); |
| 87 | + |
| 88 | + CurrentActivityExecutionContext.unset(); |
| 89 | + ActivityExecutionContext afterPop = CurrentActivityExecutionContext.get(); |
| 90 | + if (afterPop != vctx1) { |
| 91 | + throw new AssertionError("after pop expected vctx1 but got " + afterPop); |
| 92 | + } |
| 93 | + |
| 94 | + CurrentActivityExecutionContext.unset(); |
| 95 | + seenIsSetAfterFinalUnset.set(CurrentActivityExecutionContext.isSet()); |
| 96 | + try { |
| 97 | + CurrentActivityExecutionContext.get(); |
| 98 | + throw new AssertionError("get() should have thrown after final unset"); |
| 99 | + } catch (IllegalStateException expected) { |
| 100 | + } |
| 101 | + } catch (Throwable t) { |
| 102 | + failure.set(t); |
| 103 | + } |
| 104 | + }); |
| 105 | + |
| 106 | + vt.join(); |
| 107 | + |
| 108 | + if (failure.get() != null) { |
| 109 | + Throwable t = failure.get(); |
| 110 | + if (t instanceof AssertionError) { |
| 111 | + throw (AssertionError) t; |
| 112 | + } else { |
| 113 | + throw new RuntimeException(t); |
| 114 | + } |
| 115 | + } |
| 116 | + |
| 117 | + assertNotNull("virtual thread did not record first set", seenAfterFirstSet.get()); |
| 118 | + assertNotNull("virtual thread did not record second (nested) set", seenAfterSecondSet.get()); |
| 119 | + assertFalse("expected context to be unset at the end", seenIsSetAfterFinalUnset.get()); |
| 120 | + } |
| 121 | +} |
0 commit comments