SerialHystrixRequestEvents extends SerialHystrixMetric { public static String toJsonString(HystrixRequestEvents requestEvents) { StringWriter jsonString = new StringWriter(); try { JsonGenerator json = jsonFactory.createGenerator(jsonString); serializeRequestEvents(requestEvents, json); } catch (Exception e) { throw new RuntimeException(e); } return jsonString.getBuffer().toString(); }  @Deprecated static byte[] toBytes(HystrixRequestEvents requestEvents); static String toJsonString(HystrixRequestEvents requestEvents);  }
@Test public void testEmpty() throws IOException { HystrixRequestEvents request = new HystrixRequestEvents(new ArrayList<HystrixInvokableInfo<?>>()); String actual = SerialHystrixRequestEvents.toJsonString(request); assertEquals("[]", actual); }
@Test public void testSingleSuccess() throws IOException { List<HystrixInvokableInfo<?>> executions = new ArrayList<HystrixInvokableInfo<?>>(); executions.add(new SimpleExecution(fooKey, 100, HystrixEventType.SUCCESS)); HystrixRequestEvents request = new HystrixRequestEvents(executions); String actual = SerialHystrixRequestEvents.toJsonString(request); assertEquals("[{\"name\":\"Foo\",\"events\":[\"SUCCESS\"],\"latencies\":[100]}]", actual); }
@Test public void testSingleFailureFallbackMissing() throws IOException { List<HystrixInvokableInfo<?>> executions = new ArrayList<HystrixInvokableInfo<?>>(); executions.add(new SimpleExecution(fooKey, 101, HystrixEventType.FAILURE, HystrixEventType.FALLBACK_MISSING)); HystrixRequestEvents request = new HystrixRequestEvents(executions); String actual = SerialHystrixRequestEvents.toJsonString(request); assertEquals("[{\"name\":\"Foo\",\"events\":[\"FAILURE\",\"FALLBACK_MISSING\"],\"latencies\":[101]}]", actual); }
@Test public void testSingleFailureFallbackSuccess() throws IOException { List<HystrixInvokableInfo<?>> executions = new ArrayList<HystrixInvokableInfo<?>>(); executions.add(new SimpleExecution(fooKey, 102, HystrixEventType.FAILURE, HystrixEventType.FALLBACK_SUCCESS)); HystrixRequestEvents request = new HystrixRequestEvents(executions); String actual = SerialHystrixRequestEvents.toJsonString(request); assertEquals("[{\"name\":\"Foo\",\"events\":[\"FAILURE\",\"FALLBACK_SUCCESS\"],\"latencies\":[102]}]", actual); }
@Test public void testSingleFailureFallbackRejected() throws IOException { List<HystrixInvokableInfo<?>> executions = new ArrayList<HystrixInvokableInfo<?>>(); executions.add(new SimpleExecution(fooKey, 103, HystrixEventType.FAILURE, HystrixEventType.FALLBACK_REJECTION)); HystrixRequestEvents request = new HystrixRequestEvents(executions); String actual = SerialHystrixRequestEvents.toJsonString(request); assertEquals("[{\"name\":\"Foo\",\"events\":[\"FAILURE\",\"FALLBACK_REJECTION\"],\"latencies\":[103]}]", actual); }
@Test public void testSingleFailureFallbackFailure() throws IOException { List<HystrixInvokableInfo<?>> executions = new ArrayList<HystrixInvokableInfo<?>>(); executions.add(new SimpleExecution(fooKey, 104, HystrixEventType.FAILURE, HystrixEventType.FALLBACK_FAILURE)); HystrixRequestEvents request = new HystrixRequestEvents(executions); String actual = SerialHystrixRequestEvents.toJsonString(request); assertEquals("[{\"name\":\"Foo\",\"events\":[\"FAILURE\",\"FALLBACK_FAILURE\"],\"latencies\":[104]}]", actual); }
@Test public void testSingleTimeoutFallbackSuccess() throws IOException { List<HystrixInvokableInfo<?>> executions = new ArrayList<HystrixInvokableInfo<?>>(); executions.add(new SimpleExecution(fooKey, 105, HystrixEventType.TIMEOUT, HystrixEventType.FALLBACK_SUCCESS)); HystrixRequestEvents request = new HystrixRequestEvents(executions); String actual = SerialHystrixRequestEvents.toJsonString(request); assertEquals("[{\"name\":\"Foo\",\"events\":[\"TIMEOUT\",\"FALLBACK_SUCCESS\"],\"latencies\":[105]}]", actual); }
@Test public void testSingleSemaphoreRejectedFallbackSuccess() throws IOException { List<HystrixInvokableInfo<?>> executions = new ArrayList<HystrixInvokableInfo<?>>(); executions.add(new SimpleExecution(fooKey, 1, HystrixEventType.SEMAPHORE_REJECTED, HystrixEventType.FALLBACK_SUCCESS)); HystrixRequestEvents request = new HystrixRequestEvents(executions); String actual = SerialHystrixRequestEvents.toJsonString(request); assertEquals("[{\"name\":\"Foo\",\"events\":[\"SEMAPHORE_REJECTED\",\"FALLBACK_SUCCESS\"],\"latencies\":[1]}]", actual); }
@Test public void testSingleThreadPoolRejectedFallbackSuccess() throws IOException { List<HystrixInvokableInfo<?>> executions = new ArrayList<HystrixInvokableInfo<?>>(); executions.add(new SimpleExecution(fooKey, 1, HystrixEventType.THREAD_POOL_REJECTED, HystrixEventType.FALLBACK_SUCCESS)); HystrixRequestEvents request = new HystrixRequestEvents(executions); String actual = SerialHystrixRequestEvents.toJsonString(request); assertEquals("[{\"name\":\"Foo\",\"events\":[\"THREAD_POOL_REJECTED\",\"FALLBACK_SUCCESS\"],\"latencies\":[1]}]", actual); }
@Test public void testSingleShortCircuitedFallbackSuccess() throws IOException { List<HystrixInvokableInfo<?>> executions = new ArrayList<HystrixInvokableInfo<?>>(); executions.add(new SimpleExecution(fooKey, 1, HystrixEventType.SHORT_CIRCUITED, HystrixEventType.FALLBACK_SUCCESS)); HystrixRequestEvents request = new HystrixRequestEvents(executions); String actual = SerialHystrixRequestEvents.toJsonString(request); assertEquals("[{\"name\":\"Foo\",\"events\":[\"SHORT_CIRCUITED\",\"FALLBACK_SUCCESS\"],\"latencies\":[1]}]", actual); }
@Test public void testSingleBadRequest() throws IOException { List<HystrixInvokableInfo<?>> executions = new ArrayList<HystrixInvokableInfo<?>>(); executions.add(new SimpleExecution(fooKey, 50, HystrixEventType.BAD_REQUEST)); HystrixRequestEvents request = new HystrixRequestEvents(executions); String actual = SerialHystrixRequestEvents.toJsonString(request); assertEquals("[{\"name\":\"Foo\",\"events\":[\"BAD_REQUEST\"],\"latencies\":[50]}]", actual); }
@Test public void testTwoSuccessesSameKey() throws IOException { List<HystrixInvokableInfo<?>> executions = new ArrayList<HystrixInvokableInfo<?>>(); HystrixInvokableInfo<Integer> foo1 = new SimpleExecution(fooKey, 23, HystrixEventType.SUCCESS); HystrixInvokableInfo<Integer> foo2 = new SimpleExecution(fooKey, 34, HystrixEventType.SUCCESS); executions.add(foo1); executions.add(foo2); HystrixRequestEvents request = new HystrixRequestEvents(executions); String actual = SerialHystrixRequestEvents.toJsonString(request); assertEquals("[{\"name\":\"Foo\",\"events\":[\"SUCCESS\"],\"latencies\":[23,34]}]", actual); }
@Test public void testTwoSuccessesDifferentKey() throws IOException { List<HystrixInvokableInfo<?>> executions = new ArrayList<HystrixInvokableInfo<?>>(); HystrixInvokableInfo<Integer> foo1 = new SimpleExecution(fooKey, 23, HystrixEventType.SUCCESS); HystrixInvokableInfo<Integer> bar1 = new SimpleExecution(barKey, 34, HystrixEventType.SUCCESS); executions.add(foo1); executions.add(bar1); HystrixRequestEvents request = new HystrixRequestEvents(executions); String actual = SerialHystrixRequestEvents.toJsonString(request); assertTrue(actual.equals("[{\"name\":\"Foo\",\"events\":[\"SUCCESS\"],\"latencies\":[23]},{\"name\":\"Bar\",\"events\":[\"SUCCESS\"],\"latencies\":[34]}]") || actual.equals("[{\"name\":\"Bar\",\"events\":[\"SUCCESS\"],\"latencies\":[34]},{\"name\":\"Foo\",\"events\":[\"SUCCESS\"],\"latencies\":[23]}]")); }
@Test public void testTwoFailuresSameKey() throws IOException { List<HystrixInvokableInfo<?>> executions = new ArrayList<HystrixInvokableInfo<?>>(); HystrixInvokableInfo<Integer> foo1 = new SimpleExecution(fooKey, 56, HystrixEventType.FAILURE, HystrixEventType.FALLBACK_SUCCESS); HystrixInvokableInfo<Integer> foo2 = new SimpleExecution(fooKey, 67, HystrixEventType.FAILURE, HystrixEventType.FALLBACK_SUCCESS); executions.add(foo1); executions.add(foo2); HystrixRequestEvents request = new HystrixRequestEvents(executions); String actual = SerialHystrixRequestEvents.toJsonString(request); assertEquals("[{\"name\":\"Foo\",\"events\":[\"FAILURE\",\"FALLBACK_SUCCESS\"],\"latencies\":[56,67]}]", actual); }
@Test public void testTwoSuccessesOneFailureSameKey() throws IOException { List<HystrixInvokableInfo<?>> executions = new ArrayList<HystrixInvokableInfo<?>>(); HystrixInvokableInfo<Integer> foo1 = new SimpleExecution(fooKey, 10, HystrixEventType.SUCCESS); HystrixInvokableInfo<Integer> foo2 = new SimpleExecution(fooKey, 67, HystrixEventType.FAILURE, HystrixEventType.FALLBACK_SUCCESS); HystrixInvokableInfo<Integer> foo3 = new SimpleExecution(fooKey, 11, HystrixEventType.SUCCESS); executions.add(foo1); executions.add(foo2); executions.add(foo3); HystrixRequestEvents request = new HystrixRequestEvents(executions); String actual = SerialHystrixRequestEvents.toJsonString(request); assertTrue(actual.equals("[{\"name\":\"Foo\",\"events\":[\"SUCCESS\"],\"latencies\":[10,11]},{\"name\":\"Foo\",\"events\":[\"FAILURE\",\"FALLBACK_SUCCESS\"],\"latencies\":[67]}]") || actual.equals("[{\"name\":\"Foo\",\"events\":[\"FAILURE\",\"FALLBACK_SUCCESS\"],\"latencies\":[67]},{\"name\":\"Foo\",\"events\":[\"SUCCESS\"],\"latencies\":[10,11]}]")); }
@Test public void testSingleResponseFromCache() throws IOException { List<HystrixInvokableInfo<?>> executions = new ArrayList<HystrixInvokableInfo<?>>(); HystrixInvokableInfo<Integer> foo1 = new SimpleExecution(fooKey, 23, "cacheKeyA", HystrixEventType.SUCCESS); HystrixInvokableInfo<Integer> cachedFoo1 = new SimpleExecution(fooKey, "cacheKeyA"); executions.add(foo1); executions.add(cachedFoo1); HystrixRequestEvents request = new HystrixRequestEvents(executions); String actual = SerialHystrixRequestEvents.toJsonString(request); assertEquals("[{\"name\":\"Foo\",\"events\":[\"SUCCESS\"],\"latencies\":[23],\"cached\":1}]", actual); }
@Test public void testMultipleResponsesFromCache() throws IOException { List<HystrixInvokableInfo<?>> executions = new ArrayList<HystrixInvokableInfo<?>>(); HystrixInvokableInfo<Integer> foo1 = new SimpleExecution(fooKey, 23, "cacheKeyA", HystrixEventType.SUCCESS); HystrixInvokableInfo<Integer> cachedFoo1 = new SimpleExecution(fooKey, "cacheKeyA"); HystrixInvokableInfo<Integer> anotherCachedFoo1 = new SimpleExecution(fooKey, "cacheKeyA"); executions.add(foo1); executions.add(cachedFoo1); executions.add(anotherCachedFoo1); HystrixRequestEvents request = new HystrixRequestEvents(executions); String actual = SerialHystrixRequestEvents.toJsonString(request); assertEquals("[{\"name\":\"Foo\",\"events\":[\"SUCCESS\"],\"latencies\":[23],\"cached\":2}]", actual); }
@Test public void testMultipleCacheKeys() throws IOException { List<HystrixInvokableInfo<?>> executions = new ArrayList<HystrixInvokableInfo<?>>(); HystrixInvokableInfo<Integer> foo1 = new SimpleExecution(fooKey, 23, "cacheKeyA", HystrixEventType.SUCCESS); HystrixInvokableInfo<Integer> cachedFoo1 = new SimpleExecution(fooKey, "cacheKeyA"); HystrixInvokableInfo<Integer> foo2 = new SimpleExecution(fooKey, 67, "cacheKeyB", HystrixEventType.SUCCESS); HystrixInvokableInfo<Integer> cachedFoo2 = new SimpleExecution(fooKey, "cacheKeyB"); executions.add(foo1); executions.add(cachedFoo1); executions.add(foo2); executions.add(cachedFoo2); HystrixRequestEvents request = new HystrixRequestEvents(executions); String actual = SerialHystrixRequestEvents.toJsonString(request); assertTrue(actual.equals("[{\"name\":\"Foo\",\"events\":[\"SUCCESS\"],\"latencies\":[67],\"cached\":1},{\"name\":\"Foo\",\"events\":[\"SUCCESS\"],\"latencies\":[23],\"cached\":1}]") || actual.equals("[{\"name\":\"Foo\",\"events\":[\"SUCCESS\"],\"latencies\":[23],\"cached\":1},{\"name\":\"Foo\",\"events\":[\"SUCCESS\"],\"latencies\":[67],\"cached\":1}]")); }
@Test public void testSingleSuccessMultipleEmits() throws IOException { List<HystrixInvokableInfo<?>> executions = new ArrayList<HystrixInvokableInfo<?>>(); executions.add(new SimpleExecution(fooKey, 100, HystrixEventType.EMIT, HystrixEventType.EMIT, HystrixEventType.EMIT, HystrixEventType.SUCCESS)); HystrixRequestEvents request = new HystrixRequestEvents(executions); String actual = SerialHystrixRequestEvents.toJsonString(request); assertEquals("[{\"name\":\"Foo\",\"events\":[{\"name\":\"EMIT\",\"count\":3},\"SUCCESS\"],\"latencies\":[100]}]", actual); }
@Test public void testSingleSuccessMultipleEmitsAndFallbackEmits() throws IOException { List<HystrixInvokableInfo<?>> executions = new ArrayList<HystrixInvokableInfo<?>>(); executions.add(new SimpleExecution(fooKey, 100, HystrixEventType.EMIT, HystrixEventType.EMIT, HystrixEventType.EMIT, HystrixEventType.FAILURE, HystrixEventType.FALLBACK_EMIT, HystrixEventType.FALLBACK_EMIT, HystrixEventType.FALLBACK_SUCCESS)); HystrixRequestEvents request = new HystrixRequestEvents(executions); String actual = SerialHystrixRequestEvents.toJsonString(request); assertEquals("[{\"name\":\"Foo\",\"events\":[{\"name\":\"EMIT\",\"count\":3},\"FAILURE\",{\"name\":\"FALLBACK_EMIT\",\"count\":2},\"FALLBACK_SUCCESS\"],\"latencies\":[100]}]", actual); }
@Test public void testCollapsedBatchOfOne() throws IOException { List<HystrixInvokableInfo<?>> executions = new ArrayList<HystrixInvokableInfo<?>>(); executions.add(new SimpleExecution(fooKey, 53, collapserKey, 1, HystrixEventType.SUCCESS)); HystrixRequestEvents request = new HystrixRequestEvents(executions); String actual = SerialHystrixRequestEvents.toJsonString(request); assertEquals("[{\"name\":\"Foo\",\"events\":[\"SUCCESS\"],\"latencies\":[53],\"collapsed\":{\"name\":\"FooCollapser\",\"count\":1}}]", actual); }
@Test public void testCollapsedBatchOfSix() throws IOException { List<HystrixInvokableInfo<?>> executions = new ArrayList<HystrixInvokableInfo<?>>(); executions.add(new SimpleExecution(fooKey, 53, collapserKey, 6, HystrixEventType.SUCCESS)); HystrixRequestEvents request = new HystrixRequestEvents(executions); String actual = SerialHystrixRequestEvents.toJsonString(request); assertEquals("[{\"name\":\"Foo\",\"events\":[\"SUCCESS\"],\"latencies\":[53],\"collapsed\":{\"name\":\"FooCollapser\",\"count\":6}}]", actual); }