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());
}
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());
}
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());
}
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()));
}
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());
}
Aggregations