Search in sources :

Example 61 with HystrixCommandKey

use of com.netflix.hystrix.HystrixCommandKey in project Hystrix by Netflix.

the class HealthCountsStreamTest method testTwoSubscribersOneUnsubscribes.

@Test
public void testTwoSubscribersOneUnsubscribes() throws Exception {
    HystrixCommandKey key = HystrixCommandKey.Factory.asKey("CMD-Health-O");
    stream = HealthCountsStream.getInstance(key, 10, 100);
    final CountDownLatch latch1 = new CountDownLatch(1);
    final CountDownLatch latch2 = new CountDownLatch(1);
    final AtomicInteger healthCounts1 = new AtomicInteger(0);
    final AtomicInteger healthCounts2 = new AtomicInteger(0);
    Subscription s1 = stream.observe().take(10).observeOn(Schedulers.computation()).doOnUnsubscribe(new Action0() {

        @Override
        public void call() {
            latch1.countDown();
        }
    }).subscribe(new Subscriber<HystrixCommandMetrics.HealthCounts>() {

        @Override
        public void onCompleted() {
            System.out.println(System.currentTimeMillis() + " : " + Thread.currentThread().getName() + " : Health 1 OnCompleted");
            latch1.countDown();
        }

        @Override
        public void onError(Throwable e) {
            System.out.println(System.currentTimeMillis() + " : " + Thread.currentThread().getName() + " : Health 1 OnError : " + e);
            latch1.countDown();
        }

        @Override
        public void onNext(HystrixCommandMetrics.HealthCounts healthCounts) {
            System.out.println(System.currentTimeMillis() + " : " + Thread.currentThread().getName() + " : Health 1 OnNext : " + healthCounts);
            healthCounts1.incrementAndGet();
        }
    });
    Subscription s2 = stream.observe().take(10).observeOn(Schedulers.computation()).doOnUnsubscribe(new Action0() {

        @Override
        public void call() {
            latch2.countDown();
        }
    }).subscribe(new Subscriber<HystrixCommandMetrics.HealthCounts>() {

        @Override
        public void onCompleted() {
            System.out.println(System.currentTimeMillis() + " : " + Thread.currentThread().getName() + " : Health 2 OnCompleted");
            latch2.countDown();
        }

        @Override
        public void onError(Throwable e) {
            System.out.println(System.currentTimeMillis() + " : " + Thread.currentThread().getName() + " : Health 2 OnError : " + e);
            latch2.countDown();
        }

        @Override
        public void onNext(HystrixCommandMetrics.HealthCounts healthCounts) {
            System.out.println(System.currentTimeMillis() + " : " + Thread.currentThread().getName() + " : Health 2 OnNext : " + healthCounts + " : " + healthCounts2.get());
            healthCounts2.incrementAndGet();
        }
    });
    //execute 5 commands, then unsubscribe from first stream. then execute the rest
    for (int i = 0; i < 10; i++) {
        HystrixCommand<Integer> cmd = CommandStreamTest.Command.from(groupKey, key, HystrixEventType.SUCCESS, 20);
        cmd.execute();
        if (i == 5) {
            s1.unsubscribe();
        }
    }
    //only 1/2 subscriptions has been cancelled
    assertTrue(stream.isSourceCurrentlySubscribed());
    assertTrue(latch1.await(10000, TimeUnit.MILLISECONDS));
    assertTrue(latch2.await(10000, TimeUnit.MILLISECONDS));
    System.out.println("s1 got : " + healthCounts1.get() + ", s2 got : " + healthCounts2.get());
    assertTrue("s1 got data", healthCounts1.get() > 0);
    assertTrue("s2 got data", healthCounts2.get() > 0);
    assertTrue("s1 got less data than s2", healthCounts2.get() > healthCounts1.get());
}
Also used : Action0(rx.functions.Action0) HystrixCommandKey(com.netflix.hystrix.HystrixCommandKey) CountDownLatch(java.util.concurrent.CountDownLatch) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Subscription(rx.Subscription) HystrixCommandMetrics(com.netflix.hystrix.HystrixCommandMetrics) CommandStreamTest(com.netflix.hystrix.metric.CommandStreamTest) Test(org.junit.Test)

Example 62 with HystrixCommandKey

use of com.netflix.hystrix.HystrixCommandKey in project Hystrix by Netflix.

the class CumulativeCommandEventCounterStreamTest method testCancelled.

@Test
public void testCancelled() {
    HystrixCommandKey key = HystrixCommandKey.Factory.asKey("CMD-CumulativeCounter-M");
    stream = CumulativeCommandEventCounterStream.getInstance(key, 10, 500);
    stream.startCachingStreamValuesIfUnstarted();
    final CountDownLatch latch = new CountDownLatch(1);
    stream.observe().take(5).subscribe(getSubscriber(latch));
    Command toCancel = Command.from(groupKey, key, HystrixEventType.SUCCESS, 500);
    System.out.println(System.currentTimeMillis() + " : " + Thread.currentThread().getName() + " : about to observe and subscribe");
    Subscription s = toCancel.observe().doOnUnsubscribe(new Action0() {

        @Override
        public void call() {
            System.out.println(System.currentTimeMillis() + " : " + Thread.currentThread().getName() + " : UnSubscribe from command.observe()");
        }
    }).subscribe(new Subscriber<Integer>() {

        @Override
        public void onCompleted() {
            System.out.println("Command OnCompleted");
        }

        @Override
        public void onError(Throwable e) {
            System.out.println("Command OnError : " + e);
        }

        @Override
        public void onNext(Integer i) {
            System.out.println("Command OnNext : " + i);
        }
    });
    System.out.println(System.currentTimeMillis() + " : " + Thread.currentThread().getName() + " : about to unsubscribe");
    s.unsubscribe();
    try {
        assertTrue(latch.await(10000, TimeUnit.MILLISECONDS));
    } catch (InterruptedException ex) {
        fail("Interrupted ex");
    }
    assertEquals(HystrixEventType.values().length, stream.getLatest().length);
    long[] expected = new long[HystrixEventType.values().length];
    expected[HystrixEventType.CANCELLED.ordinal()] = 1;
    assertArrayEquals(expected, stream.getLatest());
}
Also used : Action0(rx.functions.Action0) HystrixCommandKey(com.netflix.hystrix.HystrixCommandKey) CountDownLatch(java.util.concurrent.CountDownLatch) Subscription(rx.Subscription) CommandStreamTest(com.netflix.hystrix.metric.CommandStreamTest) Test(org.junit.Test)

Example 63 with HystrixCommandKey

use of com.netflix.hystrix.HystrixCommandKey in project Hystrix by Netflix.

the class CumulativeCommandEventCounterStreamTest method testMultipleEventsOverTimeGetStoredAndNeverAgeOut.

@Test
public void testMultipleEventsOverTimeGetStoredAndNeverAgeOut() {
    HystrixCommandKey key = HystrixCommandKey.Factory.asKey("CMD-CumulativeCounter-N");
    stream = CumulativeCommandEventCounterStream.getInstance(key, 10, 100);
    stream.startCachingStreamValuesIfUnstarted();
    //by doing a take(30), we ensure that no rolling out of window takes place
    final CountDownLatch latch = new CountDownLatch(1);
    stream.observe().take(30).subscribe(getSubscriber(latch));
    Command cmd1 = Command.from(groupKey, key, HystrixEventType.SUCCESS, 20);
    Command cmd2 = Command.from(groupKey, key, HystrixEventType.FAILURE, 10);
    cmd1.observe();
    cmd2.observe();
    try {
        assertTrue(latch.await(10000, TimeUnit.MILLISECONDS));
    } catch (InterruptedException ex) {
        fail("Interrupted ex");
    }
    assertEquals(HystrixEventType.values().length, stream.getLatest().length);
    long[] expected = new long[HystrixEventType.values().length];
    expected[HystrixEventType.SUCCESS.ordinal()] = 1;
    expected[HystrixEventType.FAILURE.ordinal()] = 1;
    expected[HystrixEventType.FALLBACK_SUCCESS.ordinal()] = 1;
    assertArrayEquals(expected, stream.getLatest());
}
Also used : HystrixCommandKey(com.netflix.hystrix.HystrixCommandKey) CountDownLatch(java.util.concurrent.CountDownLatch) CommandStreamTest(com.netflix.hystrix.metric.CommandStreamTest) Test(org.junit.Test)

Example 64 with HystrixCommandKey

use of com.netflix.hystrix.HystrixCommandKey in project Hystrix by Netflix.

the class CumulativeCommandEventCounterStreamTest method testEmptyStreamProducesZeros.

@Test
public void testEmptyStreamProducesZeros() {
    HystrixCommandKey key = HystrixCommandKey.Factory.asKey("CMD-CumulativeCounter-A");
    stream = CumulativeCommandEventCounterStream.getInstance(key, 10, 500);
    stream.startCachingStreamValuesIfUnstarted();
    final CountDownLatch latch = new CountDownLatch(1);
    stream.observe().take(5).subscribe(getSubscriber(latch));
    try {
        assertTrue(latch.await(10000, TimeUnit.MILLISECONDS));
    } catch (InterruptedException ex) {
        fail("Interrupted ex");
    }
    assertEquals(HystrixEventType.values().length, stream.getLatest().length);
    assertFalse(hasData(stream.getLatest()));
}
Also used : HystrixCommandKey(com.netflix.hystrix.HystrixCommandKey) CountDownLatch(java.util.concurrent.CountDownLatch) CommandStreamTest(com.netflix.hystrix.metric.CommandStreamTest) Test(org.junit.Test)

Example 65 with HystrixCommandKey

use of com.netflix.hystrix.HystrixCommandKey in project Hystrix by Netflix.

the class CumulativeCommandEventCounterStreamTest method testSingleFailure.

@Test
public void testSingleFailure() {
    HystrixCommandKey key = HystrixCommandKey.Factory.asKey("CMD-CumulativeCounter-C");
    stream = CumulativeCommandEventCounterStream.getInstance(key, 10, 500);
    stream.startCachingStreamValuesIfUnstarted();
    final CountDownLatch latch = new CountDownLatch(1);
    stream.observe().take(5).subscribe(getSubscriber(latch));
    Command cmd = Command.from(groupKey, key, HystrixEventType.FAILURE, 20);
    cmd.observe();
    try {
        assertTrue(latch.await(10000, TimeUnit.MILLISECONDS));
    } catch (InterruptedException ex) {
        fail("Interrupted ex");
    }
    assertEquals(HystrixEventType.values().length, stream.getLatest().length);
    long[] expected = new long[HystrixEventType.values().length];
    expected[HystrixEventType.FAILURE.ordinal()] = 1;
    expected[HystrixEventType.FALLBACK_SUCCESS.ordinal()] = 1;
    assertArrayEquals(expected, stream.getLatest());
}
Also used : HystrixCommandKey(com.netflix.hystrix.HystrixCommandKey) CountDownLatch(java.util.concurrent.CountDownLatch) CommandStreamTest(com.netflix.hystrix.metric.CommandStreamTest) Test(org.junit.Test)

Aggregations

HystrixCommandKey (com.netflix.hystrix.HystrixCommandKey)113 Test (org.junit.Test)106 CommandStreamTest (com.netflix.hystrix.metric.CommandStreamTest)99 CountDownLatch (java.util.concurrent.CountDownLatch)99 HystrixThreadPoolKey (com.netflix.hystrix.HystrixThreadPoolKey)42 HystrixCommandGroupKey (com.netflix.hystrix.HystrixCommandGroupKey)38 ArrayList (java.util.ArrayList)25 CachedValuesHistogram (com.netflix.hystrix.metric.CachedValuesHistogram)9 HystrixCommandProperties (com.netflix.hystrix.HystrixCommandProperties)8 HystrixContextRunnable (com.netflix.hystrix.strategy.concurrency.HystrixContextRunnable)8 HystrixCommandMetrics (com.netflix.hystrix.HystrixCommandMetrics)7 HystrixCircuitBreaker (com.netflix.hystrix.HystrixCircuitBreaker)5 HystrixCommand (com.netflix.hystrix.HystrixCommand)4 Map (java.util.Map)4 JsonGenerator (com.fasterxml.jackson.core.JsonGenerator)3 HystrixPropertiesCommandDefault (com.netflix.hystrix.strategy.properties.HystrixPropertiesCommandDefault)3 StringWriter (java.io.StringWriter)3 HystrixCollapserKey (com.netflix.hystrix.HystrixCollapserKey)2 HystrixCollapserConfiguration (com.netflix.hystrix.config.HystrixCollapserConfiguration)2 HystrixCommandConfiguration (com.netflix.hystrix.config.HystrixCommandConfiguration)2