|
| 1 | +// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. |
| 2 | +// SPDX-License-Identifier: Apache-2.0 |
| 3 | +package com.amazonaws.lambda.durable.execution; |
| 4 | + |
| 5 | +import static org.junit.jupiter.api.Assertions.assertEquals; |
| 6 | +import static org.junit.jupiter.api.Assertions.assertFalse; |
| 7 | +import static org.junit.jupiter.api.Assertions.assertTrue; |
| 8 | +import static org.mockito.ArgumentMatchers.any; |
| 9 | +import static org.mockito.Mockito.mock; |
| 10 | +import static org.mockito.Mockito.never; |
| 11 | +import static org.mockito.Mockito.timeout; |
| 12 | +import static org.mockito.Mockito.verify; |
| 13 | +import static org.mockito.Mockito.when; |
| 14 | + |
| 15 | +import java.time.Clock; |
| 16 | +import java.time.Duration; |
| 17 | +import java.time.Instant; |
| 18 | +import java.time.ZoneOffset; |
| 19 | +import java.util.ArrayList; |
| 20 | +import java.util.List; |
| 21 | +import java.util.concurrent.CompletableFuture; |
| 22 | +import java.util.concurrent.ExecutionException; |
| 23 | +import java.util.concurrent.TimeUnit; |
| 24 | +import java.util.function.Function; |
| 25 | +import org.junit.jupiter.api.BeforeEach; |
| 26 | +import org.junit.jupiter.api.Test; |
| 27 | + |
| 28 | +class ApiRequestBatcherTest { |
| 29 | + private static final Duration MAX_DELAY_MILLIS = Duration.ofMillis(100); |
| 30 | + private static final int MAX_BATCH_SIZE = 3; |
| 31 | + private static final int MAX_BATCH_BINARY_SIZE_IN_BYTES = 100; |
| 32 | + |
| 33 | + private static class Input {} |
| 34 | + |
| 35 | + private Input input; |
| 36 | + private Clock fixedClock; |
| 37 | + private ApiRequestBatcher<Input> cut; |
| 38 | + private Function<List<Input>, CompletableFuture<Void>> doBatchAction; |
| 39 | + private CompletableFuture<Void> batchResultFuture; |
| 40 | + |
| 41 | + @BeforeEach |
| 42 | + void setUp() { |
| 43 | + input = mock(Input.class); |
| 44 | + doBatchAction = mock(); |
| 45 | + batchResultFuture = new CompletableFuture<>(); |
| 46 | + fixedClock = Clock.fixed(Instant.now(), ZoneOffset.UTC); |
| 47 | + cut = new ApiRequestBatcher<>( |
| 48 | + MAX_DELAY_MILLIS, MAX_BATCH_SIZE, MAX_BATCH_BINARY_SIZE_IN_BYTES, item -> 0, doBatchAction); |
| 49 | + |
| 50 | + when(doBatchAction.apply(any())).thenReturn(batchResultFuture); |
| 51 | + } |
| 52 | + |
| 53 | + @Test |
| 54 | + void whenSingleActionPerformed_anUncompletedFutureIsReturned() { |
| 55 | + CompletableFuture<Void> resultFuture = cut.doAction(input); |
| 56 | + |
| 57 | + verify(doBatchAction, never()).apply(any()); |
| 58 | + assertFalse(resultFuture.isDone()); |
| 59 | + } |
| 60 | + |
| 61 | + @Test |
| 62 | + void whenMultipleActionsPerformedBelowMaxBatchSize_anUncompletedFutureIsReturnedEachTime() { |
| 63 | + List<CompletableFuture<Void>> resultFutures = new ArrayList<>(); |
| 64 | + for (int i = 0; i < MAX_BATCH_SIZE - 1; i++) { |
| 65 | + resultFutures.add(cut.doAction(input)); |
| 66 | + } |
| 67 | + |
| 68 | + verify(doBatchAction, never()).apply(any()); |
| 69 | + assertTrue(resultFutures.stream().noneMatch(CompletableFuture::isDone)); |
| 70 | + } |
| 71 | + |
| 72 | + @Test |
| 73 | + void whenMultipleActionsPerformedMatchingMaxBatchSize_batchInvokeIsPerformed() { |
| 74 | + List<CompletableFuture<Void>> resultFutures = new ArrayList<>(); |
| 75 | + for (int i = 0; i < MAX_BATCH_SIZE; i++) { |
| 76 | + resultFutures.add(cut.doAction(input)); |
| 77 | + } |
| 78 | + |
| 79 | + verify(doBatchAction).apply(any()); |
| 80 | + assertTrue(resultFutures.stream().noneMatch(CompletableFuture::isDone)); |
| 81 | + } |
| 82 | + |
| 83 | + @Test |
| 84 | + void whenBatchInvokeThrows_allFuturesCompleteWithThatException() throws InterruptedException { |
| 85 | + CompletableFuture<Void> resultFuture1 = cut.doAction(input); |
| 86 | + CompletableFuture<Void> resultFuture2 = cut.doAction(input); |
| 87 | + CompletableFuture<Void> resultFuture3 = cut.doAction(input); |
| 88 | + |
| 89 | + assertFalse(resultFuture1.isDone()); |
| 90 | + assertFalse(resultFuture2.isDone()); |
| 91 | + assertFalse(resultFuture3.isDone()); |
| 92 | + |
| 93 | + Throwable batchCause = mock(Throwable.class); |
| 94 | + batchResultFuture.completeExceptionally(batchCause); |
| 95 | + |
| 96 | + assertTrue(resultFuture1.isCompletedExceptionally()); |
| 97 | + assertTrue(resultFuture2.isCompletedExceptionally()); |
| 98 | + assertTrue(resultFuture3.isCompletedExceptionally()); |
| 99 | + |
| 100 | + assertEquals(batchCause, getFutureCause(resultFuture1)); |
| 101 | + assertEquals(batchCause, getFutureCause(resultFuture2)); |
| 102 | + assertEquals(batchCause, getFutureCause(resultFuture3)); |
| 103 | + } |
| 104 | + |
| 105 | + @Test |
| 106 | + void whenBatchInvokeReturnsOutcome_allFuturesCompleteSuccessfully() { |
| 107 | + Input input1 = mock(Input.class); |
| 108 | + Input input2 = mock(Input.class); |
| 109 | + Input input3 = mock(Input.class); |
| 110 | + |
| 111 | + CompletableFuture<Void> resultFuture1 = cut.doAction(input1); |
| 112 | + CompletableFuture<Void> resultFuture2 = cut.doAction(input2); |
| 113 | + CompletableFuture<Void> resultFuture3 = cut.doAction(input3); |
| 114 | + |
| 115 | + assertFalse(resultFuture1.isDone()); |
| 116 | + assertFalse(resultFuture2.isDone()); |
| 117 | + assertFalse(resultFuture3.isDone()); |
| 118 | + |
| 119 | + batchResultFuture.complete(null); |
| 120 | + |
| 121 | + assertTrue(resultFuture1.isDone()); |
| 122 | + assertTrue(resultFuture2.isDone()); |
| 123 | + assertTrue(resultFuture3.isDone()); |
| 124 | + |
| 125 | + assertFalse(resultFuture1.isCompletedExceptionally()); |
| 126 | + assertFalse(resultFuture2.isCompletedExceptionally()); |
| 127 | + assertFalse(resultFuture3.isCompletedExceptionally()); |
| 128 | + } |
| 129 | + |
| 130 | + @Test |
| 131 | + void testDoAction_whenCannotAddItemDueToBinarySizeConstraint_thenFlushCurrentBatchAndCreateNewOne() { |
| 132 | + var cut = new ApiRequestBatcher<>( |
| 133 | + MAX_DELAY_MILLIS, |
| 134 | + MAX_BATCH_SIZE, |
| 135 | + MAX_BATCH_BINARY_SIZE_IN_BYTES, |
| 136 | + item -> MAX_BATCH_BINARY_SIZE_IN_BYTES, |
| 137 | + doBatchAction); |
| 138 | + List<CompletableFuture<Void>> resultFutures = new ArrayList<>(); |
| 139 | + |
| 140 | + resultFutures.add(cut.doAction(input)); |
| 141 | + resultFutures.add(cut.doAction(input)); |
| 142 | + |
| 143 | + verify(doBatchAction).apply(any()); |
| 144 | + |
| 145 | + assertTrue(resultFutures.stream().noneMatch(CompletableFuture::isDone)); |
| 146 | + } |
| 147 | + |
| 148 | + @Test |
| 149 | + void whenTimerFires_batchIsProcessed() { |
| 150 | + var timerCut = new ApiRequestBatcher<>( |
| 151 | + Duration.ofMillis(1), MAX_BATCH_SIZE, MAX_BATCH_BINARY_SIZE_IN_BYTES, item -> 0, doBatchAction); |
| 152 | + |
| 153 | + CompletableFuture<Void> resultFuture = timerCut.doAction(input); |
| 154 | + |
| 155 | + // Wait for the timeout to trigger |
| 156 | + CompletableFuture.delayedExecutor(10, TimeUnit.MILLISECONDS).execute(() -> {}); |
| 157 | + |
| 158 | + verify(doBatchAction, timeout(50)).apply(any()); |
| 159 | + assertFalse(resultFuture.isDone()); |
| 160 | + } |
| 161 | + |
| 162 | + @Test |
| 163 | + void whenBatchInvokeThrowsCompletionException_allFuturesCompleteWithUnwrappedCause() throws InterruptedException { |
| 164 | + CompletableFuture<Void> resultFuture1 = cut.doAction(input); |
| 165 | + CompletableFuture<Void> resultFuture2 = cut.doAction(input); |
| 166 | + CompletableFuture<Void> resultFuture3 = cut.doAction(input); |
| 167 | + |
| 168 | + RuntimeException rootCause = new RuntimeException("Root cause"); |
| 169 | + batchResultFuture.completeExceptionally(rootCause); |
| 170 | + |
| 171 | + assertTrue(resultFuture1.isCompletedExceptionally()); |
| 172 | + assertTrue(resultFuture2.isCompletedExceptionally()); |
| 173 | + assertTrue(resultFuture3.isCompletedExceptionally()); |
| 174 | + |
| 175 | + // Should get unwrapped root cause, not the CompletionException wrapper |
| 176 | + assertEquals(rootCause, getFutureCause(resultFuture1)); |
| 177 | + assertEquals(rootCause, getFutureCause(resultFuture2)); |
| 178 | + assertEquals(rootCause, getFutureCause(resultFuture3)); |
| 179 | + } |
| 180 | + |
| 181 | + private Throwable getFutureCause(CompletableFuture<?> failedFuture) throws InterruptedException { |
| 182 | + try { |
| 183 | + failedFuture.get(); |
| 184 | + return null; |
| 185 | + } catch (ExecutionException cause) { |
| 186 | + return cause.getCause(); |
| 187 | + } |
| 188 | + } |
| 189 | +} |
0 commit comments