Search in sources :

Example 16 with ServiceUnavailableException

use of javax.ws.rs.ServiceUnavailableException in project oci-java-sdk by oracle.

the class CircuitBreakerConfigurationTest method validateCircuitBreakerConfigurationWithDefaultValues.

@Test
public void validateCircuitBreakerConfigurationWithDefaultValues() {
    CircuitBreakerConfiguration config = CircuitBreakerConfiguration.builder().build();
    JaxRsCircuitBreakerImpl circuitBreaker = new JaxRsCircuitBreakerImpl(config);
    CircuitBreakerConfig internalConfig = circuitBreaker.getInternalCircuitBreakerConfig();
    assertEquals(internalConfig.getFailureRateThreshold(), CircuitBreakerConfiguration.DEFAULT_FAILURE_RATE_THRESHOLD, 0.1);
    assertEquals(internalConfig.getSlowCallRateThreshold(), CircuitBreakerConfiguration.DEFAULT_SLOW_CALL_RATE_THRESHOLD, 0.1);
    assertEquals(internalConfig.getSlowCallDurationThreshold(), Duration.ofMinutes(CircuitBreakerConfiguration.DEFAULT_SLOW_CALL_DURATION_THRESHOLD));
    assertEquals(internalConfig.getPermittedNumberOfCallsInHalfOpenState(), CircuitBreakerConfiguration.DEFAULT_PERMITTED_CALLS_IN_HALF_OPEN_STATE);
    assertEquals(internalConfig.getSlidingWindowType(), CircuitBreakerConfig.SlidingWindowType.TIME_BASED);
    assertEquals(internalConfig.getSlidingWindowSize(), CircuitBreakerConfiguration.DEFAULT_SLIDING_WINDOW_SIZE);
    assertEquals(internalConfig.getMinimumNumberOfCalls(), CircuitBreakerConfiguration.DEFAULT_MINIMUM_NUMBER_OF_CALLS);
    assertEquals(internalConfig.getWaitDurationInOpenState(), Duration.ofSeconds(CircuitBreakerConfiguration.DEFAULT_WAIT_DURATION_IN_OPEN_STATE));
    assertTrue(internalConfig.isAutomaticTransitionFromOpenToHalfOpenEnabled());
    Response response503 = mock(Response.class);
    Mockito.when(response503.getStatus()).thenReturn(503);
    HttpStatusErrorException ex = new HttpStatusErrorException(response503);
    Predicate<Throwable> recordExceptionPredicate = internalConfig.getRecordExceptionPredicate();
    assertTrue(recordExceptionPredicate.test(ex));
    assertTrue(recordExceptionPredicate.test(new ServiceUnavailableException()));
    assertTrue(recordExceptionPredicate.test(new InternalServerErrorException()));
    assertTrue(recordExceptionPredicate.test(new ProcessingException("test")));
    Set<Integer> recordHttpStatuses = circuitBreaker.getRecordHttpStatuses();
    assertTrue(recordHttpStatuses.contains(CircuitBreakerConfiguration.INTERNAL_SERVER_ERROR));
    assertTrue(recordHttpStatuses.contains(CircuitBreakerConfiguration.SERVICE_UNAVAILABLE));
    assertTrue(recordHttpStatuses.contains(CircuitBreakerConfiguration.TOO_MANY_REQUESTS));
    assertTrue(recordHttpStatuses.contains(CircuitBreakerConfiguration.BAD_GATEWAY));
    assertTrue(recordHttpStatuses.contains(CircuitBreakerConfiguration.GATEWAY_TIMEOUT));
    assertEquals(recordHttpStatuses.size(), 5);
}
Also used : Response(javax.ws.rs.core.Response) InternalServerErrorException(javax.ws.rs.InternalServerErrorException) ServiceUnavailableException(javax.ws.rs.ServiceUnavailableException) CircuitBreakerConfiguration(com.oracle.bmc.circuitbreaker.CircuitBreakerConfiguration) CircuitBreakerConfig(io.github.resilience4j.circuitbreaker.CircuitBreakerConfig) ProcessingException(javax.ws.rs.ProcessingException) Test(org.junit.Test)

Example 17 with ServiceUnavailableException

use of javax.ws.rs.ServiceUnavailableException in project cxf by apache.

the class DateHeaderProviderTest method testToFromSimpleString.

@Test
public void testToFromSimpleString() {
    Date retry = new Date();
    ServiceUnavailableException ex = new ServiceUnavailableException(retry);
    Date retry2 = ex.getRetryTime(new Date());
    assertEquals(HttpUtils.toHttpDate(retry), HttpUtils.toHttpDate(retry2));
}
Also used : ServiceUnavailableException(javax.ws.rs.ServiceUnavailableException) Date(java.util.Date) Test(org.junit.Test)

Example 18 with ServiceUnavailableException

use of javax.ws.rs.ServiceUnavailableException in project jersey by jersey.

the class ItemStoreResource method itemEvents.

/**
     * Connect or re-connect to SSE event stream.
     *
     * @param lastEventId Value of custom SSE HTTP <tt>{@value SseFeature#LAST_EVENT_ID_HEADER}</tt> header.
     *                    Defaults to {@code -1} if not set.
     * @return new SSE event output stream representing the (re-)established SSE client connection.
     * @throws InternalServerErrorException in case replaying missed events to the reconnected output stream fails.
     * @throws ServiceUnavailableException  in case the reconnect delay is set to a positive value.
     */
@GET
@Path("events")
@Produces(SseFeature.SERVER_SENT_EVENTS)
public EventOutput itemEvents(@HeaderParam(SseFeature.LAST_EVENT_ID_HEADER) @DefaultValue("-1") int lastEventId) {
    final EventOutput eventOutput = new EventOutput();
    if (lastEventId >= 0) {
        LOGGER.info("Received last event id :" + lastEventId);
        // decide the reconnect handling strategy based on current reconnect delay value.
        final long delay = reconnectDelay;
        if (delay > 0) {
            LOGGER.info("Non-zero reconnect delay [" + delay + "] - responding with HTTP 503.");
            throw new ServiceUnavailableException(delay);
        } else {
            LOGGER.info("Zero reconnect delay - reconnecting.");
            replayMissedEvents(lastEventId, eventOutput);
        }
    }
    if (!broadcaster.add(eventOutput)) {
        LOGGER.severe("!!! Unable to add new event output to the broadcaster !!!");
        // let's try to force a 5s delayed client reconnect attempt
        throw new ServiceUnavailableException(5L);
    }
    return eventOutput;
}
Also used : EventOutput(org.glassfish.jersey.media.sse.EventOutput) ServiceUnavailableException(javax.ws.rs.ServiceUnavailableException) Path(javax.ws.rs.Path) Produces(javax.ws.rs.Produces) GET(javax.ws.rs.GET)

Example 19 with ServiceUnavailableException

use of javax.ws.rs.ServiceUnavailableException in project jersey by jersey.

the class ItemStoreResource method itemEvents.

/**
     * Connect or re-connect to SSE event stream.
     *
     * @param lastEventId Value of custom SSE HTTP <tt>{@value SseFeature#LAST_EVENT_ID_HEADER}</tt> header.
     *                    Defaults to {@code -1} if not set.
     * @return new SSE event output stream representing the (re-)established SSE client connection.
     * @throws InternalServerErrorException in case replaying missed events to the reconnected output stream fails.
     * @throws ServiceUnavailableException  in case the reconnect delay is set to a positive value.
     */
@GET
@Path("events")
@Produces(SseFeature.SERVER_SENT_EVENTS)
public EventOutput itemEvents(@HeaderParam(SseFeature.LAST_EVENT_ID_HEADER) @DefaultValue("-1") int lastEventId) {
    final EventOutput eventOutput = new EventOutput();
    if (lastEventId >= 0) {
        LOGGER.info("Received last event id :" + lastEventId);
        // decide the reconnect handling strategy based on current reconnect delay value.
        final long delay = reconnectDelay;
        if (delay > 0) {
            LOGGER.info("Non-zero reconnect delay [" + delay + "] - responding with HTTP 503.");
            throw new ServiceUnavailableException(delay);
        } else {
            LOGGER.info("Zero reconnect delay - reconnecting.");
            replayMissedEvents(lastEventId, eventOutput);
        }
    }
    if (!broadcaster.add(eventOutput)) {
        LOGGER.severe("!!! Unable to add new event output to the broadcaster !!!");
        // let's try to force a 5s delayed client reconnect attempt
        throw new ServiceUnavailableException(5L);
    }
    return eventOutput;
}
Also used : EventOutput(org.glassfish.jersey.media.sse.EventOutput) ServiceUnavailableException(javax.ws.rs.ServiceUnavailableException) Path(javax.ws.rs.Path) Produces(javax.ws.rs.Produces) GET(javax.ws.rs.GET)

Example 20 with ServiceUnavailableException

use of javax.ws.rs.ServiceUnavailableException in project robozonky by RoboZonky.

the class SessionTest method investmentFailed.

@Test
void investmentFailed() {
    final Zonky z = AbstractZonkyLeveragingTest.harmlessZonky(10_000);
    final Authenticated auth = mockAuthentication(z);
    final RecommendedLoan r = AbstractZonkyLeveragingTest.mockLoanDescriptor().recommend(200).get();
    final Exception thrown = new ServiceUnavailableException();
    final Investor p = mock(Investor.class);
    doThrow(thrown).when(p).invest(eq(r), anyBoolean());
    final Portfolio portfolio = Portfolio.create(z, mockBalance(z));
    final Session t = new Session(portfolio, Collections.emptySet(), p, auth);
    assertThatThrownBy(() -> t.invest(r)).isSameAs(thrown);
}
Also used : Authenticated(com.github.robozonky.app.authentication.Authenticated) RecommendedLoan(com.github.robozonky.api.strategies.RecommendedLoan) Portfolio(com.github.robozonky.app.portfolio.Portfolio) ServiceUnavailableException(javax.ws.rs.ServiceUnavailableException) ServiceUnavailableException(javax.ws.rs.ServiceUnavailableException) Zonky(com.github.robozonky.common.remote.Zonky) AbstractZonkyLeveragingTest(com.github.robozonky.app.AbstractZonkyLeveragingTest) Test(org.junit.jupiter.api.Test)

Aggregations

ServiceUnavailableException (javax.ws.rs.ServiceUnavailableException)38 NotFoundException (javax.ws.rs.NotFoundException)16 GET (javax.ws.rs.GET)13 Path (javax.ws.rs.Path)13 Produces (javax.ws.rs.Produces)13 BadRequestException (javax.ws.rs.BadRequestException)12 Response (javax.ws.rs.core.Response)12 WebApplicationException (javax.ws.rs.WebApplicationException)11 ForbiddenException (javax.ws.rs.ForbiddenException)9 InternalServerErrorException (javax.ws.rs.InternalServerErrorException)8 DataFile (edu.harvard.iq.dataverse.DataFile)7 ByteArrayOutputStream (java.io.ByteArrayOutputStream)7 IOException (java.io.IOException)7 Test (org.junit.Test)6 ClientErrorException (javax.ws.rs.ClientErrorException)5 ServerErrorException (javax.ws.rs.ServerErrorException)5 NotAuthorizedException (javax.ws.rs.NotAuthorizedException)4 RedirectionException (javax.ws.rs.RedirectionException)4 CircuitBreakerConfiguration (com.oracle.bmc.circuitbreaker.CircuitBreakerConfiguration)3 FileMetadata (edu.harvard.iq.dataverse.FileMetadata)3