diff --git a/sdk/core/azure-core/src/main/java/com/azure/core/util/polling/DefaultSyncPoller.java b/sdk/core/azure-core/src/main/java/com/azure/core/util/polling/DefaultSyncPoller.java index 9f81df628e56c..b33dda069e883 100644 --- a/sdk/core/azure-core/src/main/java/com/azure/core/util/polling/DefaultSyncPoller.java +++ b/sdk/core/azure-core/src/main/java/com/azure/core/util/polling/DefaultSyncPoller.java @@ -72,6 +72,9 @@ final class DefaultSyncPoller implements SyncPoller { // this.pollingContext.setOnetimeActivationResponse(this.activationResponse); this.pollingContext.setLatestResponse(this.activationResponse); + if (this.activationResponse.getStatus().isComplete()) { + this.terminalPollContext = this.pollingContext; + } } @Override diff --git a/sdk/core/azure-core/src/test/java/com/azure/core/util/polling/PollerTests.java b/sdk/core/azure-core/src/test/java/com/azure/core/util/polling/PollerTests.java index dc3f8c45aa24e..73479df4183f9 100644 --- a/sdk/core/azure-core/src/test/java/com/azure/core/util/polling/PollerTests.java +++ b/sdk/core/azure-core/src/test/java/com/azure/core/util/polling/PollerTests.java @@ -32,6 +32,7 @@ import static com.azure.core.util.polling.PollerFlux.error; import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertThrows; +import static org.junit.jupiter.api.Assertions.fail; import static org.mockito.ArgumentMatchers.any; import static org.mockito.Mockito.when; @@ -213,6 +214,36 @@ public void noPollingForSynchronouslyCompletedActivationTest() { assertEquals(1, activationCallCount[0]); } + @Test + public void noPollingForSynchronouslyCompletedActivationInSyncPollerTest() { + int[] activationCallCount = new int[1]; + activationCallCount[0] = 0; + when(activationOperationWithResponse.apply(any())).thenReturn(Mono.defer(() -> { + activationCallCount[0]++; + return Mono.just(new PollResponse(LongRunningOperationStatus.SUCCESSFULLY_COMPLETED, + new Response("ActivationDone"))); + })); + + SyncPoller syncPoller = create( + Duration.ofMillis(10), + activationOperationWithResponse, + pollOperation, + cancelOperation, + fetchResultOperation) + .getSyncPoller(); + + when(pollOperation.apply(any())).thenReturn( + Mono.error(new RuntimeException("Polling shouldn't happen for synchronously completed activation."))); + + try { + PollResponse response = syncPoller.waitForCompletion(Duration.ofSeconds(1)); + assertEquals(LongRunningOperationStatus.SUCCESSFULLY_COMPLETED, response.getStatus()); + assertEquals(1, activationCallCount[0]); + } catch (Exception e) { + fail("SyncPoller did not complete on activation", e); + } + } + @Test public void ensurePollingForInProgressActivationResponseTest() { final Duration retryAfter = Duration.ofMillis(10);