Search in sources :

Example 1 with JustATestException

use of com.hotels.styx.support.JustATestException in project styx by ExpediaGroup.

the class HttpPipelineHandlerTest method discardsOnErrorForEarlierRequestsInWaitingForResponseState.

@Test
public void discardsOnErrorForEarlierRequestsInWaitingForResponseState() throws Exception {
    setUpFor2Requests();
    handler.channelRead0(ctx, request);
    assertThat(handler.state(), is(WAITING_FOR_RESPONSE));
    responseObservable.onNext(response);
    assertThat(handler.state(), is(SENDING_RESPONSE));
    writerFuture.complete(null);
    assertThat(handler.state(), is(ACCEPTING_REQUESTS));
    handler.channelRead0(ctx, request2);
    assertThat(handler.state(), is(WAITING_FOR_RESPONSE));
    responseObservable.onError(new JustATestException());
    assertThat(handler.state(), is(WAITING_FOR_RESPONSE));
}
Also used : JustATestException(com.hotels.styx.support.JustATestException) Test(org.junit.jupiter.api.Test)

Example 2 with JustATestException

use of com.hotels.styx.support.JustATestException in project styx by ExpediaGroup.

the class HttpPipelineHandlerTest method mapsStyxClientExceptionToInternalServerErrorInWaitingForResponseState.

@Test
public void mapsStyxClientExceptionToInternalServerErrorInWaitingForResponseState() throws Exception {
    // In Waiting For Response state,
    // The response observable emits a StyxClientException.
    // Then, respond with INTERNAL_SERVER_ERROR and close the channel.
    setupHandlerTo(WAITING_FOR_RESPONSE);
    responseObservable.onError(new StyxClientException("Client error occurred", new JustATestException()));
    assertThat(responseUnsubscribed.get(), is(true));
    ArgumentCaptor<LiveHttpResponse> captor = ArgumentCaptor.forClass(LiveHttpResponse.class);
    verify(responseWriter).write(captor.capture());
    HttpResponse response = Mono.from(captor.getValue().aggregate(100)).block();
    assertThat(response.status(), is(INTERNAL_SERVER_ERROR));
    assertThat(response.header(CONNECTION), is(Optional.of("close")));
    assertThat(response.header(CONTENT_LENGTH), is(Optional.of("29")));
    assertThat(response.bodyAs(UTF_8), is("Site temporarily unavailable."));
    writerFuture.complete(null);
    verify(statsCollector).onComplete(request.id(), 500);
    verify(errorListener).proxyErrorOccurred(any(LiveHttpRequest.class), any(InetSocketAddress.class), eq(INTERNAL_SERVER_ERROR), any(RuntimeException.class));
    // NOTE: channel closure is not verified. This is because cannot mock channel future.
    verify(ctx).close();
    assertThat(handler.state(), is(TERMINATED));
}
Also used : JustATestException(com.hotels.styx.support.JustATestException) StyxClientException(com.hotels.styx.client.StyxClientException) LiveHttpRequest(com.hotels.styx.api.LiveHttpRequest) InetSocketAddress(java.net.InetSocketAddress) HttpResponse(com.hotels.styx.api.HttpResponse) LiveHttpResponse(com.hotels.styx.api.LiveHttpResponse) DefaultHttpResponse(io.netty.handler.codec.http.DefaultHttpResponse) LiveHttpResponse(com.hotels.styx.api.LiveHttpResponse) Test(org.junit.jupiter.api.Test)

Example 3 with JustATestException

use of com.hotels.styx.support.JustATestException in project styx by ExpediaGroup.

the class QueueDrainingEventProcessorTest method handlesEventProcessorExceptions.

@Test
public void handlesEventProcessorExceptions() throws Exception {
    for (int i = 0; i < 1000; i++) {
        CyclicBarrier barrier1 = new CyclicBarrier(2);
        CyclicBarrier barrier2 = new CyclicBarrier(2);
        CyclicBarrier barrier3 = new CyclicBarrier(2);
        QueueDrainingEventProcessor eventProcessor = new QueueDrainingEventProcessor((event) -> ((Consumer<Void>) event).accept(null), false);
        startThread(() -> {
            Consumer<Void> lockEvent = consumerEvent((x) -> {
                await(barrier1);
                try {
                    throw new JustATestException();
                } finally {
                    await(barrier2);
                }
            });
            eventProcessor.submit(lockEvent);
        });
        barrier1.await();
        Consumer<Void> event2 = mock(Consumer.class);
        eventProcessor.submit(consumerEvent(x -> {
            event2.accept(null);
            await(barrier3);
        }));
        await(barrier2);
        await(barrier3);
        verify(event2).accept(eq(null));
    }
}
Also used : Test(org.junit.jupiter.api.Test) Consumer(java.util.function.Consumer) Mockito(org.mockito.Mockito) CyclicBarrier(java.util.concurrent.CyclicBarrier) InOrder(org.mockito.InOrder) JustATestException(com.hotels.styx.support.JustATestException) Matchers.eq(org.mockito.Matchers.eq) BrokenBarrierException(java.util.concurrent.BrokenBarrierException) Thread.currentThread(java.lang.Thread.currentThread) Mockito.mock(org.mockito.Mockito.mock) Mockito.verify(org.mockito.Mockito.verify) JustATestException(com.hotels.styx.support.JustATestException) CyclicBarrier(java.util.concurrent.CyclicBarrier) Test(org.junit.jupiter.api.Test)

Example 4 with JustATestException

use of com.hotels.styx.support.JustATestException in project styx by ExpediaGroup.

the class FileBackedBackendServicesRegistryTest method fileChanged_logsInfoWhenFailsToReadFile.

@Test
public void fileChanged_logsInfoWhenFailsToReadFile() throws Exception {
    FileBackedRegistry<BackendService> delegate = mock(FileBackedRegistry.class);
    when(delegate.fileName()).thenReturn("/monitored/origins.yml");
    when(delegate.reload()).thenReturn(completedFuture(reloaded("md5-hash: 9034890345289043, Successfully reloaded"))).thenReturn(completedFuture(failed("md5-hash: 9034890345289043, Failed to reload", new JustATestException())));
    registry = new FileBackedBackendServicesRegistry(delegate, FileMonitor.DISABLED);
    registry.startService().get();
    registry.fileChanged();
    assertThat(log.lastMessage(), is(loggingEvent(ERROR, "Backend services reload failed. reason='File Monitor', md5-hash: 9034890345289043, Failed to reload, file='/monitored/origins.yml'", JustATestException.class, JustATestException.DEFAULT_MESSAGE)));
}
Also used : BackendService(com.hotels.styx.api.extension.service.BackendService) JustATestException(com.hotels.styx.support.JustATestException) Test(org.junit.jupiter.api.Test)

Example 5 with JustATestException

use of com.hotels.styx.support.JustATestException in project styx by ExpediaGroup.

the class FileBackedBackendServicesRegistryTest method serviceStarts_failsToStartWhenReloadFails.

@Test
public void serviceStarts_failsToStartWhenReloadFails() {
    FileBackedRegistry<BackendService> delegate = mock(FileBackedRegistry.class);
    when(delegate.fileName()).thenReturn("/monitored/origins.yml");
    when(delegate.reload()).thenReturn(completedFuture(failed("md5-hash: 9034890345289043, Failed to load file", new JustATestException())));
    registry = new FileBackedBackendServicesRegistry(delegate, FileMonitor.DISABLED);
    Exception e = assertThrows(ExecutionException.class, () -> registry.startService().get());
    assertEquals("java.lang.RuntimeException: com.hotels.styx.support.JustATestException: This is not a real exception. We are just testing exception handling", e.getMessage());
}
Also used : BackendService(com.hotels.styx.api.extension.service.BackendService) JustATestException(com.hotels.styx.support.JustATestException) TimeoutException(java.util.concurrent.TimeoutException) JustATestException(com.hotels.styx.support.JustATestException) IOException(java.io.IOException) CompletionException(java.util.concurrent.CompletionException) ServiceFailureException(com.hotels.styx.api.extension.service.spi.ServiceFailureException) ExecutionException(java.util.concurrent.ExecutionException) Test(org.junit.jupiter.api.Test)

Aggregations

JustATestException (com.hotels.styx.support.JustATestException)9 Test (org.junit.jupiter.api.Test)9 BackendService (com.hotels.styx.api.extension.service.BackendService)4 IOException (java.io.IOException)2 Matchers.eq (org.mockito.Matchers.eq)2 Mockito.mock (org.mockito.Mockito.mock)2 Mockito.verify (org.mockito.Mockito.verify)2 HttpResponse (com.hotels.styx.api.HttpResponse)1 LiveHttpRequest (com.hotels.styx.api.LiveHttpRequest)1 LiveHttpResponse (com.hotels.styx.api.LiveHttpResponse)1 Resource (com.hotels.styx.api.Resource)1 Registry (com.hotels.styx.api.extension.service.spi.Registry)1 ReloadResult (com.hotels.styx.api.extension.service.spi.Registry.ReloadResult)1 ReloadResult.reloaded (com.hotels.styx.api.extension.service.spi.Registry.ReloadResult.reloaded)1 ReloadResult.unchanged (com.hotels.styx.api.extension.service.spi.Registry.ReloadResult.unchanged)1 ServiceFailureException (com.hotels.styx.api.extension.service.spi.ServiceFailureException)1 StyxClientException (com.hotels.styx.client.StyxClientException)1 StyxFutures.await (com.hotels.styx.common.StyxFutures.await)1 DefaultHttpResponse (io.netty.handler.codec.http.DefaultHttpResponse)1 ByteArrayInputStream (java.io.ByteArrayInputStream)1