|
| 1 | +/* |
| 2 | + * Copyright (C) 2022 Temporal Technologies, Inc. All Rights Reserved. |
| 3 | + * |
| 4 | + * Copyright (C) 2012-2016 Amazon.com, Inc. or its affiliates. All Rights Reserved. |
| 5 | + * |
| 6 | + * Modifications copyright (C) 2017 Uber Technologies, Inc. |
| 7 | + * |
| 8 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 9 | + * you may not use this material except in compliance with the License. |
| 10 | + * You may obtain a copy of the License at |
| 11 | + * |
| 12 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 13 | + * |
| 14 | + * Unless required by applicable law or agreed to in writing, software |
| 15 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 16 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 17 | + * See the License for the specific language governing permissions and |
| 18 | + * limitations under the License. |
| 19 | + */ |
| 20 | + |
| 21 | +package io.temporal.activity; |
| 22 | + |
| 23 | +import io.temporal.testing.internal.SDKTestOptions; |
| 24 | +import io.temporal.testing.internal.SDKTestWorkflowRule; |
| 25 | +import io.temporal.workflow.Workflow; |
| 26 | +import io.temporal.workflow.shared.TestActivities; |
| 27 | +import io.temporal.workflow.shared.TestWorkflows; |
| 28 | +import org.junit.Rule; |
| 29 | +import org.junit.Test; |
| 30 | + |
| 31 | +public class ActivityHeartbeatSentOnFailureTest { |
| 32 | + |
| 33 | + @Rule |
| 34 | + public SDKTestWorkflowRule testWorkflowRule = |
| 35 | + SDKTestWorkflowRule.newBuilder() |
| 36 | + .setWorkflowTypes(TestWorkflowImpl.class) |
| 37 | + .setActivityImplementations(new HeartBeatingActivityImpl()) |
| 38 | + .build(); |
| 39 | + |
| 40 | + /** Tests that the last Activity#heartbeat value is sent if the activity fails. */ |
| 41 | + @Test |
| 42 | + public void activityHeartbeatSentOnFailure() { |
| 43 | + TestWorkflows.NoArgsWorkflow workflow = |
| 44 | + testWorkflowRule.newWorkflowStub(TestWorkflows.NoArgsWorkflow.class); |
| 45 | + workflow.execute(); |
| 46 | + } |
| 47 | + |
| 48 | + public static class TestWorkflowImpl implements TestWorkflows.NoArgsWorkflow { |
| 49 | + |
| 50 | + private final TestActivities.NoArgsActivity activities = |
| 51 | + Workflow.newActivityStub( |
| 52 | + TestActivities.NoArgsActivity.class, |
| 53 | + SDKTestOptions.newActivityOptions20sScheduleToClose()); |
| 54 | + |
| 55 | + @Override |
| 56 | + public void execute() { |
| 57 | + activities.execute(); |
| 58 | + } |
| 59 | + } |
| 60 | + |
| 61 | + public static class HeartBeatingActivityImpl implements TestActivities.NoArgsActivity { |
| 62 | + @Override |
| 63 | + public void execute() { |
| 64 | + // If the heartbeat details are "3", then we know that the last heartbeat was sent. |
| 65 | + if (Activity.getExecutionContext().getHeartbeatDetails(String.class).orElse("").equals("3")) { |
| 66 | + return; |
| 67 | + } |
| 68 | + // Send 3 heartbeats and then fail, expecting the last heartbeat to be sent |
| 69 | + // even though the activity fails and the last two attempts would normally be throttled. |
| 70 | + Activity.getExecutionContext().heartbeat("1"); |
| 71 | + Activity.getExecutionContext().heartbeat("2"); |
| 72 | + Activity.getExecutionContext().heartbeat("3"); |
| 73 | + throw new RuntimeException("simulated failure"); |
| 74 | + } |
| 75 | + } |
| 76 | +} |
0 commit comments