|
| 1 | +package io.temporal.worker; |
| 2 | + |
| 3 | +import static org.junit.Assert.assertNull; |
| 4 | + |
| 5 | +import io.temporal.activity.ActivityInterface; |
| 6 | +import io.temporal.activity.ActivityMethod; |
| 7 | +import io.temporal.testing.internal.SDKTestOptions; |
| 8 | +import io.temporal.testing.internal.SDKTestWorkflowRule; |
| 9 | +import io.temporal.workflow.Workflow; |
| 10 | +import io.temporal.workflow.shared.TestWorkflows; |
| 11 | +import org.junit.Rule; |
| 12 | +import org.junit.Test; |
| 13 | +import org.slf4j.MDC; |
| 14 | + |
| 15 | +public class MdcClearedBetweenTasksTest { |
| 16 | + @Rule |
| 17 | + public SDKTestWorkflowRule testWorkflowRule = |
| 18 | + SDKTestWorkflowRule.newBuilder() |
| 19 | + .setWorkflowTypes(TestWorkflowImpl.class) |
| 20 | + .setActivityImplementations(new SetMdcActivityImpl(), new GetMdcActivityImpl()) |
| 21 | + .build(); |
| 22 | + |
| 23 | + @Test |
| 24 | + public void testMdcClearedBetweenActivities() { |
| 25 | + TestWorkflows.TestWorkflowReturnString workflow = |
| 26 | + testWorkflowRule.newWorkflowStub(TestWorkflows.TestWorkflowReturnString.class); |
| 27 | + String result = workflow.execute(); |
| 28 | + assertNull(result); |
| 29 | + } |
| 30 | + |
| 31 | + public static class TestWorkflowImpl implements TestWorkflows.TestWorkflowReturnString { |
| 32 | + private final SetMdcActivity setMdcActivity = |
| 33 | + Workflow.newActivityStub( |
| 34 | + SetMdcActivity.class, SDKTestOptions.newActivityOptions20sScheduleToClose()); |
| 35 | + private final GetMdcActivity getMdcActivity = |
| 36 | + Workflow.newActivityStub( |
| 37 | + GetMdcActivity.class, SDKTestOptions.newActivityOptions20sScheduleToClose()); |
| 38 | + |
| 39 | + @Override |
| 40 | + public String execute() { |
| 41 | + setMdcActivity.execute(); |
| 42 | + return getMdcActivity.execute(); |
| 43 | + } |
| 44 | + } |
| 45 | + |
| 46 | + @ActivityInterface |
| 47 | + public interface SetMdcActivity { |
| 48 | + @ActivityMethod(name = "setMdc") |
| 49 | + void execute(); |
| 50 | + } |
| 51 | + |
| 52 | + public static class SetMdcActivityImpl implements SetMdcActivity { |
| 53 | + @Override |
| 54 | + public void execute() { |
| 55 | + MDC.put("mdcTest", "value"); |
| 56 | + } |
| 57 | + } |
| 58 | + |
| 59 | + @ActivityInterface |
| 60 | + public interface GetMdcActivity { |
| 61 | + @ActivityMethod(name = "getMdc") |
| 62 | + String execute(); |
| 63 | + } |
| 64 | + |
| 65 | + public static class GetMdcActivityImpl implements GetMdcActivity { |
| 66 | + @Override |
| 67 | + public String execute() { |
| 68 | + return MDC.get("mdcTest"); |
| 69 | + } |
| 70 | + } |
| 71 | +} |
0 commit comments