@Test public void shouldReturnTheSameConsumer() { Retry.EventPublisher eventPublisher = retry.getEventPublisher(); Retry.EventPublisher eventPublisher2 = retry.getEventPublisher(); assertThat(eventPublisher).isEqualTo(eventPublisher2); }
@Test public void shouldConsumeOnRetryEvent() { given(helloWorldService.returnHelloWorld()) .willThrow(new WebServiceException("BAM!")); retry.getEventPublisher() .onRetry(event -> logger.info(event.getEventType().toString())); Try.ofSupplier(Retry.decorateSupplier(retry, helloWorldService::returnHelloWorld)); then(helloWorldService).should(times(3)).returnHelloWorld(); then(logger).should(times(2)).info("RETRY"); }
@Test public void shouldConsumeOnErrorEvent() { given(helloWorldService.returnHelloWorld()) .willThrow(new WebServiceException("BAM!")); retry.getEventPublisher() .onError(event -> logger.info(event.getEventType().toString())); Try.ofSupplier(Retry.decorateSupplier(retry, helloWorldService::returnHelloWorld)); then(logger).should(times(1)).info("ERROR"); then(helloWorldService).should(times(3)).returnHelloWorld(); }
@Test public void shouldConsumeOnSuccessEvent() { // Given the HelloWorldService returns Hello world given(helloWorldService.returnHelloWorld()) .willThrow(new WebServiceException("BAM!")) .willReturn("Hello world"); retry.getEventPublisher() .onSuccess(event -> logger.info(event.getEventType().toString())); retry.executeSupplier(helloWorldService::returnHelloWorld); then(helloWorldService).should(times(2)).returnHelloWorld(); then(logger).should(times(1)).info("SUCCESS"); }
@Test public void shouldReturnAfterThreeAttempts() { // Given the HelloWorldService throws an exception BDDMockito.willThrow(new WebServiceException("BAM!")).given(helloWorldService).sayHelloWorld(); // Create a Retry with default configuration Retry retry = Retry.ofDefaults("id"); TestSubscriber<RetryEvent.Type> testSubscriber = toFlowable(retry.getEventPublisher()) .map(RetryEvent::getEventType) .test(); // Decorate the invocation of the HelloWorldService CheckedRunnable retryableRunnable = Retry.decorateCheckedRunnable(retry, helloWorldService::sayHelloWorld); // When Try<Void> result = Try.run(retryableRunnable); // Then the helloWorldService should be invoked 3 times BDDMockito.then(helloWorldService).should(Mockito.times(3)).sayHelloWorld(); // and the result should be a failure Assertions.assertThat(result.isFailure()).isTrue(); // and the returned exception should be of type RuntimeException Assertions.assertThat(result.failed().get()).isInstanceOf(WebServiceException.class); Assertions.assertThat(sleptTime).isEqualTo(RetryConfig.DEFAULT_WAIT_DURATION*2); testSubscriber.assertValueCount(3) .assertValues(RetryEvent.Type.RETRY, RetryEvent.Type.RETRY, RetryEvent.Type.ERROR); }
@Test public void shouldReturnAfterTwoAttempts() { // Given the HelloWorldService throws an exception BDDMockito.willThrow(new WebServiceException("BAM!")).willDoNothing().given(helloWorldService).sayHelloWorld(); // Create a Retry with default configuration Retry retry = Retry.ofDefaults("id"); TestSubscriber<RetryEvent.Type> testSubscriber = toFlowable(retry.getEventPublisher()) .map(RetryEvent::getEventType) .test(); // Decorate the invocation of the HelloWorldService CheckedRunnable retryableRunnable = Retry.decorateCheckedRunnable(retry, helloWorldService::sayHelloWorld); // When Try<Void> result = Try.run(retryableRunnable); // Then the helloWorldService should be invoked 2 times BDDMockito.then(helloWorldService).should(Mockito.times(2)).sayHelloWorld(); // and the result should be a sucess Assertions.assertThat(result.isSuccess()).isTrue(); Assertions.assertThat(sleptTime).isEqualTo(RetryConfig.DEFAULT_WAIT_DURATION); testSubscriber.assertValueCount(2).assertValues(RetryEvent.Type.RETRY, RetryEvent.Type.SUCCESS); }
retry.getEventPublisher().onEvent(rConsumerRegistry.createEventConsumer(name, endpointsConfig.getRetries().getEventConsumerBufferSize()));
@Test public void shouldIgnoreError() { // Given the HelloWorldService throws an exception BDDMockito.willThrow(new WebServiceException("BAM!")).willDoNothing().given(helloWorldService).sayHelloWorld(); // Create a Retry with default configuration RetryConfig config = RetryConfig.custom() .retryOnException(t -> t instanceof IOException) .maxAttempts(3).build(); Retry retry = Retry.of("id", config); TestSubscriber<RetryEvent.Type> testSubscriber = toFlowable(retry.getEventPublisher()) .map(RetryEvent::getEventType) .test(); // Decorate the invocation of the HelloWorldService CheckedRunnable retryableRunnable = Retry.decorateCheckedRunnable(retry, helloWorldService::sayHelloWorld); // When Try<Void> result = Try.run(retryableRunnable); // Then the helloWorldService should be invoked 2 times BDDMockito.then(helloWorldService).should(Mockito.times(1)).sayHelloWorld(); // and the result should be a sucess Assertions.assertThat(result.isFailure()).isTrue(); Assertions.assertThat(sleptTime).isEqualTo(0); testSubscriber.assertValueCount(1).assertValues(RetryEvent.Type.IGNORED_ERROR); } }
); chain1.get("stream/events", ctx -> { Seq<Flux<RetryEvent>> eventStreams = retryRegistry.getAllRetries().map(retry -> ReactorAdapter.toFlux(retry.getEventPublisher())); Function<RetryEvent, String> data = r -> Jackson.getObjectWriter(chain1.getRegistry()).writeValueAsString(RetryEventDTO.createRetryEventDTO(r)); ServerSentEvents events = ServerSentEvents.serverSentEvents(Flux.merge(eventStreams), e -> e.id(RetryEvent::getName).event(c -> c.getEventType().name()).data(data)); new IllegalArgumentException(String.format("rate limiter with name %s not found", rateLimiterName))); Function<RetryEvent, String> data = r -> Jackson.getObjectWriter(chain1.getRegistry()).writeValueAsString(RetryEventDTO.createRetryEventDTO(r)); ServerSentEvents events = ServerSentEvents.serverSentEvents(ReactorAdapter.toFlux(retry.getEventPublisher()), e -> e.id(RetryEvent::getName).event(c -> c.getEventType().name()).data(data)); ctx.render(events); }); .getOrElseThrow(() -> new IllegalArgumentException(String.format("rate limiter with name %s not found", retryName))); Flux<RetryEvent> eventStream = ReactorAdapter.toFlux(retry.getEventPublisher()) .filter(event -> event.getEventType() == RetryEvent.Type.valueOf(eventType.toUpperCase())); Function<RetryEvent, String> data = r -> Jackson.getObjectWriter(chain1.getRegistry()).writeValueAsString(RetryEventDTO.createRetryEventDTO(r));
@Test public void shouldConsumeIgnoredErrorEvent() { given(helloWorldService.returnHelloWorld()) .willThrow(new WebServiceException("BAM!")); RetryConfig retryConfig = RetryConfig.custom() .retryOnException(throwable -> Match(throwable).of( Case($(instanceOf(WebServiceException.class)), false), Case($(), true))) .build(); retry = Retry.of("testName", retryConfig); retry.getEventPublisher() .onIgnoredError(event -> logger.info(event.getEventType().toString())); Try.ofSupplier(Retry.decorateSupplier(retry, helloWorldService::returnHelloWorld)); then(logger).should(times(1)).info("IGNORED_ERROR"); then(helloWorldService).should(times(1)).returnHelloWorld(); }
retry.getEventPublisher().onEvent(rConsumerRegistry.createEventConsumer(name, endpointsConfig.getRetries().getEventConsumerBufferSize()));
); chain1.get("stream/events", ctx -> { Seq<Flux<RetryEvent>> eventStreams = retryRegistry.getAllRetries().map(retry -> ReactorAdapter.toFlux(retry.getEventPublisher())); Function<RetryEvent, String> data = r -> Jackson.getObjectWriter(chain1.getRegistry()).writeValueAsString(RetryEventDTO.createRetryEventDTO(r)); ServerSentEvents events = ServerSentEvents.serverSentEvents(Flux.merge(eventStreams), e -> e.id(RetryEvent::getName).event(c -> c.getEventType().name()).data(data)); new IllegalArgumentException(String.format("rate limiter with name %s not found", rateLimiterName))); Function<RetryEvent, String> data = r -> Jackson.getObjectWriter(chain1.getRegistry()).writeValueAsString(RetryEventDTO.createRetryEventDTO(r)); ServerSentEvents events = ServerSentEvents.serverSentEvents(ReactorAdapter.toFlux(retry.getEventPublisher()), e -> e.id(RetryEvent::getName).event(c -> c.getEventType().name()).data(data)); ctx.render(events); }); .getOrElseThrow(() -> new IllegalArgumentException(String.format("rate limiter with name %s not found", retryName))); Flux<RetryEvent> eventStream = ReactorAdapter.toFlux(retry.getEventPublisher()) .filter(event -> event.getEventType() == RetryEvent.Type.valueOf(eventType.toUpperCase())); Function<RetryEvent, String> data = r -> Jackson.getObjectWriter(chain1.getRegistry()).writeValueAsString(RetryEventDTO.createRetryEventDTO(r));