|
| 1 | +package io.temporal.internal.activity; |
| 2 | + |
| 3 | +import static org.junit.Assert.*; |
| 4 | +import static org.mockito.ArgumentMatchers.any; |
| 5 | +import static org.mockito.Mockito.*; |
| 6 | + |
| 7 | +import com.uber.m3.tally.NoopScope; |
| 8 | +import io.grpc.Status; |
| 9 | +import io.grpc.StatusRuntimeException; |
| 10 | +import io.temporal.activity.ActivityInfo; |
| 11 | +import io.temporal.api.enums.v1.TimeoutType; |
| 12 | +import io.temporal.api.workflowservice.v1.RecordActivityTaskHeartbeatResponse; |
| 13 | +import io.temporal.api.workflowservice.v1.WorkflowServiceGrpc; |
| 14 | +import io.temporal.client.ActivityCanceledException; |
| 15 | +import io.temporal.client.ActivityCompletionException; |
| 16 | +import io.temporal.common.converter.GlobalDataConverter; |
| 17 | +import io.temporal.failure.TimeoutFailure; |
| 18 | +import io.temporal.serviceclient.WorkflowServiceStubs; |
| 19 | +import io.temporal.testUtils.Eventually; |
| 20 | +import java.time.Duration; |
| 21 | +import java.util.Optional; |
| 22 | +import java.util.concurrent.Executors; |
| 23 | +import java.util.concurrent.ScheduledExecutorService; |
| 24 | +import java.util.concurrent.atomic.AtomicInteger; |
| 25 | +import org.junit.After; |
| 26 | +import org.junit.Before; |
| 27 | +import org.junit.Test; |
| 28 | + |
| 29 | +public class HeartbeatContextImplTest { |
| 30 | + |
| 31 | + private static final long TEST_BUFFER_MILLIS = 200; |
| 32 | + |
| 33 | + private ScheduledExecutorService heartbeatExecutor; |
| 34 | + private WorkflowServiceStubs service; |
| 35 | + private WorkflowServiceGrpc.WorkflowServiceBlockingStub blockingStub; |
| 36 | + |
| 37 | + @Before |
| 38 | + public void setUp() { |
| 39 | + heartbeatExecutor = Executors.newScheduledThreadPool(1); |
| 40 | + service = mock(WorkflowServiceStubs.class); |
| 41 | + blockingStub = mock(WorkflowServiceGrpc.WorkflowServiceBlockingStub.class); |
| 42 | + when(service.blockingStub()).thenReturn(blockingStub); |
| 43 | + when(blockingStub.withOption(any(), any())).thenReturn(blockingStub); |
| 44 | + } |
| 45 | + |
| 46 | + @After |
| 47 | + public void tearDown() { |
| 48 | + heartbeatExecutor.shutdownNow(); |
| 49 | + } |
| 50 | + |
| 51 | + @Test |
| 52 | + public void heartbeatTimeoutLocallyCancelsActivity() { |
| 53 | + Duration heartbeatTimeout = Duration.ofMillis(500); |
| 54 | + |
| 55 | + // All heartbeat RPCs fail with UNAVAILABLE |
| 56 | + when(blockingStub.recordActivityTaskHeartbeat(any())) |
| 57 | + .thenThrow(new StatusRuntimeException(Status.UNAVAILABLE)); |
| 58 | + |
| 59 | + ActivityInfo info = activityInfoWithHeartbeatTimeout(heartbeatTimeout); |
| 60 | + HeartbeatContextImpl ctx = createHeartbeatContext(info); |
| 61 | + |
| 62 | + long startNanos = System.nanoTime(); |
| 63 | + ctx.heartbeat("details-1"); |
| 64 | + |
| 65 | + ActivityCompletionException caught = |
| 66 | + Eventually.assertEventually( |
| 67 | + Duration.ofSeconds(10), |
| 68 | + () -> { |
| 69 | + try { |
| 70 | + ctx.heartbeat("poll"); |
| 71 | + fail("Expected ActivityCanceledException"); |
| 72 | + return null; |
| 73 | + } catch (ActivityCompletionException e) { |
| 74 | + return e; |
| 75 | + } |
| 76 | + }); |
| 77 | + |
| 78 | + long elapsedMs = Duration.ofNanos(System.nanoTime() - startNanos).toMillis(); |
| 79 | + |
| 80 | + assertSame(ActivityCanceledException.class, caught.getClass()); |
| 81 | + assertNotNull("Expected a TimeoutFailure cause", caught.getCause()); |
| 82 | + assertSame(TimeoutFailure.class, caught.getCause().getClass()); |
| 83 | + assertEquals( |
| 84 | + TimeoutType.TIMEOUT_TYPE_HEARTBEAT, ((TimeoutFailure) caught.getCause()).getTimeoutType()); |
| 85 | + long expectedMinMs = heartbeatTimeout.toMillis() + TEST_BUFFER_MILLIS; |
| 86 | + assertTrue( |
| 87 | + "Timeout should not fire before heartbeat timeout + buffer (" |
| 88 | + + elapsedMs |
| 89 | + + "ms elapsed, expected >= " |
| 90 | + + expectedMinMs |
| 91 | + + "ms)", |
| 92 | + elapsedMs >= expectedMinMs); |
| 93 | + |
| 94 | + ctx.cancelOutstandingHeartbeat(); |
| 95 | + } |
| 96 | + |
| 97 | + @Test |
| 98 | + public void heartbeatTimeoutResetsOnSuccessfulSend() { |
| 99 | + Duration heartbeatTimeout = Duration.ofMillis(500); |
| 100 | + AtomicInteger callCount = new AtomicInteger(); |
| 101 | + |
| 102 | + // First call succeeds, then all subsequent calls fail |
| 103 | + when(blockingStub.recordActivityTaskHeartbeat(any())) |
| 104 | + .thenAnswer( |
| 105 | + invocation -> { |
| 106 | + if (callCount.getAndIncrement() == 0) { |
| 107 | + return RecordActivityTaskHeartbeatResponse.getDefaultInstance(); |
| 108 | + } |
| 109 | + throw new StatusRuntimeException(Status.UNAVAILABLE); |
| 110 | + }); |
| 111 | + |
| 112 | + ActivityInfo info = activityInfoWithHeartbeatTimeout(heartbeatTimeout); |
| 113 | + HeartbeatContextImpl ctx = createHeartbeatContext(info); |
| 114 | + |
| 115 | + // The first heartbeat() call sends the RPC synchronously (no scheduled heartbeat yet). |
| 116 | + // Record the time before calling — the timer reset happens during this call. |
| 117 | + long resetNanos = System.nanoTime(); |
| 118 | + ctx.heartbeat("details-1"); |
| 119 | + assertEquals("First RPC should have been the successful one", 1, callCount.get()); |
| 120 | + |
| 121 | + // Poll until the timeout fires again (from the reset point) |
| 122 | + Eventually.assertEventually( |
| 123 | + Duration.ofSeconds(10), |
| 124 | + () -> { |
| 125 | + try { |
| 126 | + ctx.heartbeat("poll"); |
| 127 | + fail("Expected ActivityCanceledException"); |
| 128 | + } catch (ActivityCanceledException e) { |
| 129 | + // expected |
| 130 | + } |
| 131 | + }); |
| 132 | + |
| 133 | + long elapsedSinceResetMs = Duration.ofNanos(System.nanoTime() - resetNanos).toMillis(); |
| 134 | + long expectedMinMs = heartbeatTimeout.toMillis() + TEST_BUFFER_MILLIS; |
| 135 | + assertTrue( |
| 136 | + "Timeout should not fire before heartbeat timeout + buffer from reset point (" |
| 137 | + + elapsedSinceResetMs |
| 138 | + + "ms elapsed since reset, expected >= " |
| 139 | + + expectedMinMs |
| 140 | + + "ms)", |
| 141 | + elapsedSinceResetMs >= expectedMinMs); |
| 142 | + |
| 143 | + ctx.cancelOutstandingHeartbeat(); |
| 144 | + } |
| 145 | + |
| 146 | + @Test |
| 147 | + public void heartbeatTimeoutPersistsAcrossMultipleCalls() { |
| 148 | + Duration heartbeatTimeout = Duration.ofMillis(500); |
| 149 | + |
| 150 | + // All heartbeat RPCs fail with UNAVAILABLE |
| 151 | + when(blockingStub.recordActivityTaskHeartbeat(any())) |
| 152 | + .thenThrow(new StatusRuntimeException(Status.UNAVAILABLE)); |
| 153 | + |
| 154 | + ActivityInfo info = activityInfoWithHeartbeatTimeout(heartbeatTimeout); |
| 155 | + HeartbeatContextImpl ctx = createHeartbeatContext(info); |
| 156 | + |
| 157 | + ctx.heartbeat("details-1"); |
| 158 | + |
| 159 | + // Wait for timeout to fire |
| 160 | + Eventually.assertEventually( |
| 161 | + Duration.ofSeconds(10), |
| 162 | + () -> { |
| 163 | + try { |
| 164 | + ctx.heartbeat("poll"); |
| 165 | + fail("Expected ActivityCanceledException"); |
| 166 | + } catch (ActivityCanceledException e) { |
| 167 | + // expected |
| 168 | + } |
| 169 | + }); |
| 170 | + |
| 171 | + // Subsequent calls should continue to throw |
| 172 | + for (int i = 0; i < 5; i++) { |
| 173 | + try { |
| 174 | + ctx.heartbeat("details-" + i); |
| 175 | + fail("Expected ActivityCanceledException on call " + i); |
| 176 | + } catch (ActivityCompletionException e) { |
| 177 | + assertSame(ActivityCanceledException.class, e.getClass()); |
| 178 | + assertNotNull(e.getCause()); |
| 179 | + assertSame(TimeoutFailure.class, e.getCause().getClass()); |
| 180 | + } |
| 181 | + } |
| 182 | + |
| 183 | + ctx.cancelOutstandingHeartbeat(); |
| 184 | + } |
| 185 | + |
| 186 | + private HeartbeatContextImpl createHeartbeatContext(ActivityInfo info) { |
| 187 | + return new HeartbeatContextImpl( |
| 188 | + service, |
| 189 | + "test-namespace", |
| 190 | + info, |
| 191 | + GlobalDataConverter.get(), |
| 192 | + heartbeatExecutor, |
| 193 | + new NoopScope(), |
| 194 | + "test-identity", |
| 195 | + Duration.ofSeconds(60), |
| 196 | + Duration.ofSeconds(30), |
| 197 | + TEST_BUFFER_MILLIS); |
| 198 | + } |
| 199 | + |
| 200 | + private static ActivityInfo activityInfoWithHeartbeatTimeout(Duration heartbeatTimeout) { |
| 201 | + ActivityInfo info = mock(ActivityInfo.class); |
| 202 | + when(info.getHeartbeatTimeout()).thenReturn(heartbeatTimeout); |
| 203 | + when(info.getTaskToken()).thenReturn(new byte[] {1, 2, 3}); |
| 204 | + when(info.getWorkflowId()).thenReturn("test-workflow-id"); |
| 205 | + when(info.getWorkflowType()).thenReturn("test-workflow-type"); |
| 206 | + when(info.getActivityType()).thenReturn("test-activity-type"); |
| 207 | + when(info.getActivityTaskQueue()).thenReturn("test-task-queue"); |
| 208 | + when(info.getActivityId()).thenReturn("test-activity-id"); |
| 209 | + when(info.isLocal()).thenReturn(false); |
| 210 | + when(info.getHeartbeatDetails()).thenReturn(Optional.empty()); |
| 211 | + return info; |
| 212 | + } |
| 213 | +} |
0 commit comments