@Test public void listenerProvided() { Object originalListener = new Object(); proxy = new JettyEventListenerProxy(originalListener, spanStore); Object[] args = proxy.getProxyConstructorArguments(); assertThat(args, is(new Object[] { originalListener, Boolean.TRUE })); } }
@Test public void happyPath() throws IOException { int status = 200; proxy.onResponseStatus("version", status, "reason"); verifyZeroInteractions(spanStore); assertThat(proxy.getStatus(), is(status)); } }
/** * Connection failed. * * @param ex * Throwable */ @ProxyMethod(parameterTypes = "java.lang.Throwable") public void onConnectionFailed(Throwable ex) { handleThrowable(ex); if (null != originalListener) { WHttpEventListenerWrapper.ON_CONNECTION_FAILED.call(originalListener, ex); } }
@BeforeMethod public void init() { // can not use @InjectMocks as span store gets injected to listener place proxy = new JettyEventListenerProxy(null, spanStore); }
@Test public void spanStoreListenerNull() { Proxy proxyListener = new Proxy(); when(httpExchange.getEventListener()).thenReturn(null); when(runtimeLinker.createProxy(eq(JettyEventListenerProxy.class), Mockito.<JettyEventListenerProxy> any(), Mockito.<ClassLoader> any())).thenReturn(proxyListener); AsyncClientRequestAdapter<TextMap> adapter = sensor.getAsyncClientRequestAdapter(object, new Object[] { httpExchange }, rsc); adapter.getSpanStoreAdapter().setSpanStore(spanStore); verify(httpExchange).setEventListener(proxyListener); ArgumentCaptor<JettyEventListenerProxy> proxyCaptor = ArgumentCaptor.forClass(JettyEventListenerProxy.class); verify(runtimeLinker).createProxy(eq(JettyEventListenerProxy.class), proxyCaptor.capture(), eq(httpExchange.getClass().getClassLoader())); assertThat(proxyCaptor.getValue().getOriginalListener(), is(nullValue())); assertThat(proxyCaptor.getValue().getSpanStore(), is(spanStore)); verifyNoMoreInteractions(runtimeLinker); verifyZeroInteractions(object, rsc); }
@Test public void happyPath() throws IOException { int status = 200; proxy.onResponseStatus("version", status, "reason"); // can not be called before status reporting proxy.onResponseComplete(); ArgumentCaptor<TagsProvidingAdapter> captor = ArgumentCaptor.forClass(TagsProvidingAdapter.class); verify(spanStore).finishSpan(captor.capture()); TagsProvidingAdapter adapter = captor.getValue(); assertThat(adapter, is(instanceOf(HttpResponseAdapter.class))); assertThat(adapter.getTags(), hasEntry(Tags.HTTP_STATUS.getKey(), String.valueOf(status))); } }
@Test public void listenerNull() { Object[] args = proxy.getProxyConstructorArguments(); assertThat(args, is(new Object[] { null, Boolean.FALSE })); }
@Test public void happyPath() throws IOException { proxy.onRequestCommitted(); verify(spanStore).startSpan(); } }
@Test public void happyPath() throws IOException { IOException ex = new IOException(); proxy.onConnectionFailed(ex); ArgumentCaptor<TagsProvidingAdapter> captor = ArgumentCaptor.forClass(TagsProvidingAdapter.class); verify(spanStore).finishSpan(captor.capture()); TagsProvidingAdapter adapter = captor.getValue(); assertThat(adapter, is(instanceOf(ThrowableAwareResponseAdapter.class))); assertThat(adapter.getTags(), hasEntry(Tags.ERROR.getKey(), String.valueOf(true))); assertThat(adapter.getTags(), hasEntry(ExtraTags.THROWABLE_TYPE, ex.getClass().getSimpleName())); } }
@Test public void happyPath() throws IOException { IOException ex = new IOException(); proxy.onException(ex); ArgumentCaptor<TagsProvidingAdapter> captor = ArgumentCaptor.forClass(TagsProvidingAdapter.class); verify(spanStore).finishSpan(captor.capture()); TagsProvidingAdapter adapter = captor.getValue(); assertThat(adapter, is(instanceOf(ThrowableAwareResponseAdapter.class))); assertThat(adapter.getTags(), hasEntry(Tags.ERROR.getKey(), String.valueOf(true))); assertThat(adapter.getTags(), hasEntry(ExtraTags.THROWABLE_TYPE, ex.getClass().getSimpleName())); } }
/** * {@inheritDoc} */ @Override public void setSpanStore(SpanStore spanStore) { // get original request listener Object originalListener = cache.invokeMethod(jettyHttpExchange.getClass(), "getEventListener", new Class<?>[] {}, jettyHttpExchange, new Object[] {}, null); // create proxy for this listener JettyEventListenerProxy listenerProxy = new JettyEventListenerProxy(originalListener, spanStore); Object proxyObject = runtimeLinker.createProxy(JettyEventListenerProxy.class, listenerProxy, jettyHttpExchange.getClass().getClassLoader()); // find the interface event listener interface, it's in the super-class of the proxy Class<?> eventListenerClass = proxyObject.getClass().getSuperclass().getInterfaces()[0]; // replace with our listener cache.invokeMethod(jettyHttpExchange.getClass(), "setEventListener", new Class<?>[] { eventListenerClass }, jettyHttpExchange, new Object[] { proxyObject }, null); }
@Test public void spanStore() { Object listener = new Object(); Proxy proxyListener = new Proxy(); when(httpExchange.getEventListener()).thenReturn(listener); when(runtimeLinker.createProxy(eq(JettyEventListenerProxy.class), Mockito.<JettyEventListenerProxy> any(), Mockito.<ClassLoader> any())).thenReturn(proxyListener); AsyncClientRequestAdapter<TextMap> adapter = sensor.getAsyncClientRequestAdapter(object, new Object[] { httpExchange }, rsc); adapter.getSpanStoreAdapter().setSpanStore(spanStore); verify(httpExchange).setEventListener(proxyListener); ArgumentCaptor<JettyEventListenerProxy> proxyCaptor = ArgumentCaptor.forClass(JettyEventListenerProxy.class); verify(runtimeLinker).createProxy(eq(JettyEventListenerProxy.class), proxyCaptor.capture(), eq(httpExchange.getClass().getClassLoader())); assertThat(proxyCaptor.getValue().getOriginalListener(), is(listener)); assertThat(proxyCaptor.getValue().getSpanStore(), is(spanStore)); verifyNoMoreInteractions(runtimeLinker); verifyZeroInteractions(object, rsc); }
/** * Exception occurred. * * @param ex * Throwable */ @ProxyMethod(parameterTypes = "java.lang.Throwable") public void onException(Throwable ex) { handleThrowable(ex); if (null != originalListener) { WHttpEventListenerWrapper.ON_EXCEPTION.call(originalListener, ex); } }