|
| 1 | +/* |
| 2 | + * Copyright (c) 2020 Temporal Technologies, Inc. All Rights Reserved |
| 3 | + * |
| 4 | + * Copyright 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"). You may not |
| 9 | + * use this file except in compliance with the License. A copy of the License is |
| 10 | + * located at |
| 11 | + * |
| 12 | + * http://aws.amazon.com/apache2.0 |
| 13 | + * |
| 14 | + * or in the "license" file accompanying this file. This file is distributed on |
| 15 | + * an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either |
| 16 | + * express or implied. See the License for the specific language governing |
| 17 | + * permissions and limitations under the License. |
| 18 | + */ |
| 19 | + |
| 20 | +package io.temporal.samples.sleepfordays; |
| 21 | + |
| 22 | +import static org.junit.jupiter.api.Assertions.assertEquals; |
| 23 | +import static org.mockito.Mockito.*; |
| 24 | + |
| 25 | +import io.temporal.client.WorkflowClient; |
| 26 | +import io.temporal.testing.TestWorkflowEnvironment; |
| 27 | +import io.temporal.testing.TestWorkflowExtension; |
| 28 | +import io.temporal.worker.Worker; |
| 29 | +import java.time.Duration; |
| 30 | +import org.junit.jupiter.api.Test; |
| 31 | +import org.junit.jupiter.api.Timeout; |
| 32 | +import org.junit.jupiter.api.extension.RegisterExtension; |
| 33 | + |
| 34 | +public class SleepForDaysJUnit5Test { |
| 35 | + |
| 36 | + @RegisterExtension |
| 37 | + public TestWorkflowExtension testWorkflowRule = |
| 38 | + TestWorkflowExtension.newBuilder() |
| 39 | + .registerWorkflowImplementationTypes(SleepForDaysImpl.class) |
| 40 | + .setDoNotStart(true) |
| 41 | + .build(); |
| 42 | + |
| 43 | + @Test |
| 44 | + @Timeout(8) |
| 45 | + public void testSleepForDays( |
| 46 | + TestWorkflowEnvironment testEnv, Worker worker, SleepForDaysWorkflow workflow) { |
| 47 | + // Mock activity |
| 48 | + SendEmailActivity activities = mock(SendEmailActivity.class); |
| 49 | + worker.registerActivitiesImplementations(activities); |
| 50 | + // Start environment |
| 51 | + testEnv.start(); |
| 52 | + |
| 53 | + // Start workflow |
| 54 | + WorkflowClient.start(workflow::sleepForDays); |
| 55 | + |
| 56 | + long startTime = testEnv.currentTimeMillis(); |
| 57 | + // Time-skip 5 minutes. |
| 58 | + testEnv.sleep(Duration.ofMinutes(5)); |
| 59 | + // Check that the activity has been called, we're now waiting for the sleep to finish. |
| 60 | + verify(activities, times(1)).sendEmail(anyString()); |
| 61 | + // Time-skip 3 days. |
| 62 | + testEnv.sleep(Duration.ofDays(90)); |
| 63 | + // Expect 3 more activity calls. |
| 64 | + verify(activities, times(4)).sendEmail(anyString()); |
| 65 | + // Send the signal to complete the workflow. |
| 66 | + workflow.complete(); |
| 67 | + // Expect no more activity calls to have been made - workflow is complete. |
| 68 | + verify(activities, times(4)).sendEmail(anyString()); |
| 69 | + // Expect more than 90 days to have passed. |
| 70 | + long endTime = testEnv.currentTimeMillis(); |
| 71 | + assertEquals(true, endTime - startTime > Duration.ofDays(90).toMillis()); |
| 72 | + } |
| 73 | +} |
0 commit comments