public static ChannelHandlerContextMocks mockChannelHandlerContextWithTraceInfo() { return mockChannelHandlerContextWithTraceInfo("123"); }
@Before public void setup() { ctx = TestUtil.mockChannelHandlerContext().mockContext; distributedTracingConfigMock = mock(DistributedTracingConfig.class); responseSender = new ResponseSender(null, null, distributedTracingConfigMock); requestInfo = mock(RequestInfo.class); httpHeaders = new DefaultHttpHeaders(); responseInfo = new FullResponseInfo(); responseInfo.setDesiredContentWriterEncoding(Charset.forName("UTF-8")); responseInfo.setDesiredContentWriterMimeType("application/json"); when(requestInfo.getHeaders()).thenReturn(httpHeaders); }
public static ChannelHandlerContextMocks mockChannelHandlerContextWithTraceInfo(String userId) { if (Tracer.getInstance().getCurrentSpan() == null) { Tracer.getInstance().startRequestWithRootSpan("mockChannelHandlerContext", userId); } ChannelHandlerContextMocks channelHandlerMocks = mockChannelHandlerContext(); when(channelHandlerMocks.mockHttpProcessingState.getLoggerMdcContextMap()).thenReturn(MDC.getCopyOfContextMap()); when(channelHandlerMocks.mockHttpProcessingState.getDistributedTraceStack()).thenReturn(Tracer.getInstance().getCurrentSpanStackCopy()); return channelHandlerMocks; }
@Test public void executeAsyncCall_shouldReturnCompletableFuture() throws Exception { // given String expectedResult = UUID.randomUUID().toString(); ctxMock = TestUtil.mockChannelHandlerContextWithTraceInfo().mockContext; Span parentSpan = Tracer.getInstance().getCurrentSpan(); AtomicReference<Span> runningSpan = new AtomicReference<>(); // when CompletableFuture<String> completableFuture = AsyncNettyHelper.supplyAsync( () -> { runningSpan.set(Tracer.getInstance().getCurrentSpan()); return expectedResult; }, executor, ctxMock); // then assertThat(completableFuture.isCompletedExceptionally()).isFalse(); assertThat(completableFuture.get()).isEqualTo(expectedResult); // verify new span is not created, but existing span does successfully hop threads assertThat(runningSpan.get()).isEqualTo(parentSpan); }
@DataProvider(value = { "GZIP", "X_GZIP", "DEFLATE", "X_DEFLATE", "CONTENT_ENCODING_THAT_DOES_NOT_REPRESENT_COMPRESSED_PAYLOAD", "ENDPOINT_DOES_NOT_WANT_DECOMPRESS", "NULL_ENDPOINT" }) @Test public void newContentDecoder_works_as_expected(NewContentDecoderScenario scenario) throws Exception { // given SmartHttpContentDecompressor decompressor = new SmartHttpContentDecompressor(); TestUtil.ChannelHandlerContextMocks mocks = TestUtil.mockChannelHandlerContext(); Whitebox.setInternalState(decompressor, "ctx", mocks.mockContext); ChannelMetadata channelMetadata = new ChannelMetadata(false); ChannelConfig channelConfigMock = mock(ChannelConfig.class); doReturn(scenario.endpoint).when(mocks.mockHttpProcessingState).getEndpointForExecution(); doReturn(channelMetadata).when(mocks.mockChannel).metadata(); doReturn(channelConfigMock).when(mocks.mockChannel).config(); // when EmbeddedChannel result = decompressor.newContentDecoder(scenario.contentEncoding); // then if (scenario.expectValidDecompressor) { assertThat(result).isNotNull(); } else { assertThat(result).isNull(); } }
@Test public void executeAsyncCall_shouldReturnCompletableFutureUsingCircuitBreaker() throws Exception { // given String expectedResult = UUID.randomUUID().toString(); ctxMock = TestUtil.mockChannelHandlerContextWithTraceInfo().mockContext; CircuitBreaker<String> circuitBreaker = spy(new CircuitBreakerImpl<>()); Span parentSpan = Tracer.getInstance().getCurrentSpan(); AtomicReference<Span> runningSpan = new AtomicReference<>(); // when CompletableFuture<String> circuitBreakerCompletableFuture = AsyncNettyHelper.supplyAsync( () -> { runningSpan.set(Tracer.getInstance().getCurrentSpan()); return expectedResult; }, circuitBreaker, executor, ctxMock); // then verify(circuitBreaker).executeAsyncCall(anyObject()); assertThat(circuitBreakerCompletableFuture.isCompletedExceptionally()).isFalse(); assertThat(circuitBreakerCompletableFuture.get()).isEqualTo(expectedResult); // verify new span is not created, but existing span does successfully hop threads assertThat(runningSpan.get()).isEqualTo(parentSpan); }
@Test public void executeAsyncCall_shouldReturnCompletableFutureUsingSpanName() throws Exception { // given String expectedResult = UUID.randomUUID().toString(); String expectedSpanName = "nonCircuitBreakerWithSpan"; ctxMock = TestUtil.mockChannelHandlerContextWithTraceInfo().mockContext; Span parentSpan = Tracer.getInstance().getCurrentSpan(); AtomicReference<Span> runningSpan = new AtomicReference<>(); // when CompletableFuture<String> completableFuture = AsyncNettyHelper.supplyAsync( expectedSpanName, () -> { runningSpan.set(Tracer.getInstance().getCurrentSpan()); return expectedResult; }, executor, ctxMock); // then assertThat(completableFuture.isCompletedExceptionally()).isFalse(); assertThat(completableFuture.get()).isEqualTo(expectedResult); // verify span is as expected assertThat(runningSpan.get().getParentSpanId()).isEqualTo(parentSpan.getSpanId()); assertThat(runningSpan.get().getSpanName()).isEqualTo(expectedSpanName); assertThat(runningSpan.get().getTraceId()).isEqualTo(parentSpan.getTraceId()); }
@Test public void executeAsyncCall_shouldReturnCompletableFutureUsingCircuitBreakerWithSpanName() throws Exception { // given String expectedResult = UUID.randomUUID().toString(); String expectedSpanName = "circuitBreakerWithSpan"; ctxMock = TestUtil.mockChannelHandlerContextWithTraceInfo().mockContext; Span parentSpan = Tracer.getInstance().getCurrentSpan(); CircuitBreaker<String> circuitBreaker = spy(new CircuitBreakerImpl<>()); AtomicReference<Span> runningSpan = new AtomicReference<>(); // when CompletableFuture<String> circuitBreakerFuture = AsyncNettyHelper.supplyAsync( expectedSpanName, () -> { runningSpan.set(Tracer.getInstance().getCurrentSpan()); return expectedResult; }, circuitBreaker, executor, ctxMock); // then assertThat(circuitBreakerFuture.isCompletedExceptionally()).isFalse(); assertThat(circuitBreakerFuture.get()).isEqualTo(expectedResult); verify(circuitBreaker).executeAsyncCall(anyObject()); // verify span is as expected assertThat(runningSpan.get().getParentSpanId()).isEqualTo(parentSpan.getSpanId()); assertThat(runningSpan.get().getSpanName()).isEqualTo(expectedSpanName); assertThat(runningSpan.get().getTraceId()).isEqualTo(parentSpan.getTraceId()); } }