Skip to content

Commit

Permalink
Support terminal status on initial op in sync poller (#23564)
Browse files Browse the repository at this point in the history
* Support terminal status on initial op in sync poller

* Add test for default sync poller fix

* Unused imports
  • Loading branch information
jianghaolu authored Aug 17, 2021
1 parent 7bc4506 commit 72c87fe
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,9 @@ final class DefaultSyncPoller<T, U> implements SyncPoller<T, U> {
//
this.pollingContext.setOnetimeActivationResponse(this.activationResponse);
this.pollingContext.setLatestResponse(this.activationResponse);
if (this.activationResponse.getStatus().isComplete()) {
this.terminalPollContext = this.pollingContext;
}
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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<Response>(LongRunningOperationStatus.SUCCESSFULLY_COMPLETED,
new Response("ActivationDone")));
}));

SyncPoller<Response, CertificateOutput> 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> 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);
Expand Down

0 comments on commit 72c87fe

Please sign in to comment.