Search in sources :

Example 11 with CachedValuesHistogram

use of com.netflix.hystrix.metric.CachedValuesHistogram in project Hystrix by Netflix.

the class RollingCommandLatencyDistributionStreamTest method testThreadPoolRejectedCommandDoesNotGetLatencyTracked.

@Test
public void testThreadPoolRejectedCommandDoesNotGetLatencyTracked() {
    HystrixCommandKey key = HystrixCommandKey.Factory.asKey("CMD-Latency-E");
    stream = RollingCommandLatencyDistributionStream.getInstance(key, 10, 100);
    stream.startCachingStreamValuesIfUnstarted();
    //10 commands with latency should occupy the entire threadpool.  execute those, then wait for bucket to roll
    //next command should be a thread-pool rejection
    List<Command> commands = new ArrayList<Command>();
    for (int i = 0; i < 10; i++) {
        commands.add(Command.from(groupKey, key, HystrixEventType.SUCCESS, 200));
    }
    final CountDownLatch latch = new CountDownLatch(1);
    stream.observe().take(10).subscribe(new Subscriber<CachedValuesHistogram>() {

        @Override
        public void onCompleted() {
            latch.countDown();
        }

        @Override
        public void onError(Throwable e) {
            fail(e.getMessage());
        }

        @Override
        public void onNext(CachedValuesHistogram distribution) {
            System.out.println(System.currentTimeMillis() + " : " + Thread.currentThread().getName() + " Received distribution with count : " + distribution.getTotalCount() + " and mean : " + distribution.getMean());
        //                if (distribution.getTotalCount() > 0) {
        //                    assertBetween(200, 250, (int) distribution.getMean());
        //                }
        }
    });
    for (Command cmd : commands) {
        cmd.observe();
    }
    Command threadPoolRejected = Command.from(groupKey, key, HystrixEventType.SUCCESS);
    try {
        Thread.sleep(40);
        threadPoolRejected.observe();
    } catch (InterruptedException ie) {
        fail(ie.getMessage());
    }
    try {
        assertTrue(latch.await(10000, TimeUnit.MILLISECONDS));
    } catch (InterruptedException ex) {
        fail("Interrupted ex");
    }
    assertEquals(10, stream.getLatest().getTotalCount());
    assertBetween(200, 250, stream.getLatestMean());
    System.out.println("ReqLog : " + HystrixRequestLog.getCurrentRequest().getExecutedCommandsAsString());
    assertTrue(threadPoolRejected.isResponseThreadPoolRejected());
}
Also used : HystrixCommandKey(com.netflix.hystrix.HystrixCommandKey) CachedValuesHistogram(com.netflix.hystrix.metric.CachedValuesHistogram) ArrayList(java.util.ArrayList) CountDownLatch(java.util.concurrent.CountDownLatch) CommandStreamTest(com.netflix.hystrix.metric.CommandStreamTest) Test(org.junit.Test)

Example 12 with CachedValuesHistogram

use of com.netflix.hystrix.metric.CachedValuesHistogram in project Hystrix by Netflix.

the class RollingCommandLatencyDistributionStreamTest method testShortCircuitedCommandDoesNotGetLatencyTracked.

@Test
public void testShortCircuitedCommandDoesNotGetLatencyTracked() {
    HystrixCommandKey key = HystrixCommandKey.Factory.asKey("CMD-Latency-D");
    stream = RollingCommandLatencyDistributionStream.getInstance(key, 10, 100);
    stream.startCachingStreamValuesIfUnstarted();
    //3 failures is enough to trigger short-circuit.  execute those, then wait for bucket to roll
    //next command should be a short-circuit
    List<Command> commands = new ArrayList<Command>();
    for (int i = 0; i < 3; i++) {
        commands.add(Command.from(groupKey, key, HystrixEventType.FAILURE, 0));
    }
    final CountDownLatch latch = new CountDownLatch(1);
    stream.observe().take(10).subscribe(new Subscriber<CachedValuesHistogram>() {

        @Override
        public void onCompleted() {
            latch.countDown();
        }

        @Override
        public void onError(Throwable e) {
            fail(e.getMessage());
        }

        @Override
        public void onNext(CachedValuesHistogram distribution) {
            System.out.println(System.currentTimeMillis() + " : " + Thread.currentThread().getName() + " Received distribution with count : " + distribution.getTotalCount() + " and mean : " + distribution.getMean());
            assertBetween(0, 30, (int) distribution.getMean());
        }
    });
    for (Command cmd : commands) {
        cmd.observe();
    }
    Command shortCircuit = Command.from(groupKey, key, HystrixEventType.SUCCESS);
    try {
        Thread.sleep(200);
        shortCircuit.observe();
    } catch (InterruptedException ie) {
        fail(ie.getMessage());
    }
    try {
        assertTrue(latch.await(10000, TimeUnit.MILLISECONDS));
    } catch (InterruptedException ex) {
        fail("Interrupted ex");
    }
    assertEquals(3, stream.getLatest().getTotalCount());
    assertBetween(0, 30, stream.getLatestMean());
    System.out.println("ReqLog : " + HystrixRequestLog.getCurrentRequest().getExecutedCommandsAsString());
    assertTrue(shortCircuit.isResponseShortCircuited());
}
Also used : HystrixCommandKey(com.netflix.hystrix.HystrixCommandKey) CachedValuesHistogram(com.netflix.hystrix.metric.CachedValuesHistogram) ArrayList(java.util.ArrayList) CountDownLatch(java.util.concurrent.CountDownLatch) CommandStreamTest(com.netflix.hystrix.metric.CommandStreamTest) Test(org.junit.Test)

Aggregations

CachedValuesHistogram (com.netflix.hystrix.metric.CachedValuesHistogram)12 CommandStreamTest (com.netflix.hystrix.metric.CommandStreamTest)12 CountDownLatch (java.util.concurrent.CountDownLatch)12 Test (org.junit.Test)12 HystrixCommandKey (com.netflix.hystrix.HystrixCommandKey)9 HystrixCollapserKey (com.netflix.hystrix.HystrixCollapserKey)3 ArrayList (java.util.ArrayList)3 HystrixContextRunnable (com.netflix.hystrix.strategy.concurrency.HystrixContextRunnable)1