Search in sources :

Example 1 with HystrixConfiguration

use of com.netflix.hystrix.config.HystrixConfiguration in project Hystrix by Netflix.

the class HystrixConfigurationStreamTest method testStreamHasData.

@Test
public void testStreamHasData() throws Exception {
    final AtomicBoolean commandShowsUp = new AtomicBoolean(false);
    final AtomicBoolean threadPoolShowsUp = new AtomicBoolean(false);
    final CountDownLatch latch = new CountDownLatch(1);
    final int NUM = 10;
    for (int i = 0; i < 2; i++) {
        HystrixCommand<Integer> cmd = Command.from(groupKey, commandKey, HystrixEventType.SUCCESS, 50);
        cmd.observe();
    }
    stream.observe().take(NUM).subscribe(new Subscriber<HystrixConfiguration>() {

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

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

        @Override
        public void onNext(HystrixConfiguration configuration) {
            System.out.println(System.currentTimeMillis() + " : " + Thread.currentThread().getName() + " : Received data with : " + configuration.getCommandConfig().size() + " commands");
            if (configuration.getCommandConfig().containsKey(commandKey)) {
                commandShowsUp.set(true);
            }
            if (!configuration.getThreadPoolConfig().isEmpty()) {
                threadPoolShowsUp.set(true);
            }
        }
    });
    assertTrue(latch.await(10000, TimeUnit.MILLISECONDS));
    assertTrue(commandShowsUp.get());
    assertTrue(threadPoolShowsUp.get());
}
Also used : AtomicInteger(java.util.concurrent.atomic.AtomicInteger) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) CountDownLatch(java.util.concurrent.CountDownLatch) HystrixConfiguration(com.netflix.hystrix.config.HystrixConfiguration) CommandStreamTest(com.netflix.hystrix.metric.CommandStreamTest) Test(org.junit.Test)

Example 2 with HystrixConfiguration

use of com.netflix.hystrix.config.HystrixConfiguration in project Hystrix by Netflix.

the class HystrixConfigurationStreamTest method testTwoSubscribersOneUnsubscribes.

@Test
public void testTwoSubscribersOneUnsubscribes() throws Exception {
    final CountDownLatch latch1 = new CountDownLatch(1);
    final CountDownLatch latch2 = new CountDownLatch(1);
    final AtomicInteger payloads1 = new AtomicInteger(0);
    final AtomicInteger payloads2 = new AtomicInteger(0);
    Subscription s1 = stream.observe().take(100).doOnUnsubscribe(new Action0() {

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

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

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

        @Override
        public void onNext(HystrixConfiguration configuration) {
            System.out.println(System.currentTimeMillis() + " : " + Thread.currentThread().getName() + " : Dashboard 1 OnNext : " + configuration);
            payloads1.incrementAndGet();
        }
    });
    Subscription s2 = stream.observe().take(100).doOnUnsubscribe(new Action0() {

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

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

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

        @Override
        public void onNext(HystrixConfiguration configuration) {
            System.out.println(System.currentTimeMillis() + " : " + Thread.currentThread().getName() + " : Dashboard 2 OnNext : " + configuration);
            payloads2.incrementAndGet();
        }
    });
    //execute 1 command, then unsubscribe from first stream. then execute the rest
    for (int i = 0; i < 50; i++) {
        HystrixCommand<Integer> cmd = Command.from(groupKey, commandKey, HystrixEventType.SUCCESS, 50);
        cmd.execute();
        if (i == 1) {
            s1.unsubscribe();
        }
    }
    assertTrue(latch1.await(10000, TimeUnit.MILLISECONDS));
    assertTrue(latch2.await(10000, TimeUnit.MILLISECONDS));
    System.out.println("s1 got : " + payloads1.get() + ", s2 got : " + payloads2.get());
    assertTrue("s1 got data", payloads1.get() > 0);
    assertTrue("s2 got data", payloads2.get() > 0);
    assertTrue("s1 got less data than s2", payloads2.get() > payloads1.get());
}
Also used : AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Action0(rx.functions.Action0) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) CountDownLatch(java.util.concurrent.CountDownLatch) Subscription(rx.Subscription) HystrixConfiguration(com.netflix.hystrix.config.HystrixConfiguration) CommandStreamTest(com.netflix.hystrix.metric.CommandStreamTest) Test(org.junit.Test)

Example 3 with HystrixConfiguration

use of com.netflix.hystrix.config.HystrixConfiguration in project Hystrix by Netflix.

the class HystrixConfigurationStreamTest method testTwoSubscribersBothUnsubscribe.

@Test
public void testTwoSubscribersBothUnsubscribe() throws Exception {
    final CountDownLatch latch1 = new CountDownLatch(1);
    final CountDownLatch latch2 = new CountDownLatch(1);
    final AtomicInteger payloads1 = new AtomicInteger(0);
    final AtomicInteger payloads2 = new AtomicInteger(0);
    Subscription s1 = stream.observe().take(100).doOnUnsubscribe(new Action0() {

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

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

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

        @Override
        public void onNext(HystrixConfiguration configuration) {
            System.out.println(System.currentTimeMillis() + " : " + Thread.currentThread().getName() + " : Dashboard 1 OnNext : " + configuration);
            payloads1.incrementAndGet();
        }
    });
    Subscription s2 = stream.observe().take(100).doOnUnsubscribe(new Action0() {

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

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

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

        @Override
        public void onNext(HystrixConfiguration configuration) {
            System.out.println(System.currentTimeMillis() + " : " + Thread.currentThread().getName() + " : Dashboard 2 OnNext : " + configuration);
            payloads2.incrementAndGet();
        }
    });
    //execute 2 commands, then unsubscribe from both streams, then execute the rest
    for (int i = 0; i < 10; i++) {
        HystrixCommand<Integer> cmd = Command.from(groupKey, commandKey, HystrixEventType.SUCCESS, 50);
        cmd.execute();
        if (i == 2) {
            s1.unsubscribe();
            s2.unsubscribe();
        }
    }
    //both subscriptions have been cancelled - source should be too
    assertFalse(stream.isSourceCurrentlySubscribed());
    assertTrue(latch1.await(10000, TimeUnit.MILLISECONDS));
    assertTrue(latch2.await(10000, TimeUnit.MILLISECONDS));
    System.out.println("s1 got : " + payloads1.get() + ", s2 got : " + payloads2.get());
    assertTrue("s1 got data", payloads1.get() > 0);
    assertTrue("s2 got data", payloads2.get() > 0);
}
Also used : AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Action0(rx.functions.Action0) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) CountDownLatch(java.util.concurrent.CountDownLatch) Subscription(rx.Subscription) HystrixConfiguration(com.netflix.hystrix.config.HystrixConfiguration) CommandStreamTest(com.netflix.hystrix.metric.CommandStreamTest) Test(org.junit.Test)

Example 4 with HystrixConfiguration

use of com.netflix.hystrix.config.HystrixConfiguration in project Hystrix by Netflix.

the class HystrixConfigurationStreamTest method testTwoSubscribersOneSlowOneFast.

@Test
public void testTwoSubscribersOneSlowOneFast() throws Exception {
    final CountDownLatch latch = new CountDownLatch(1);
    final AtomicBoolean foundError = new AtomicBoolean(false);
    Observable<HystrixConfiguration> fast = stream.observe().observeOn(Schedulers.newThread());
    Observable<HystrixConfiguration> slow = stream.observe().observeOn(Schedulers.newThread()).map(new Func1<HystrixConfiguration, HystrixConfiguration>() {

        @Override
        public HystrixConfiguration call(HystrixConfiguration config) {
            try {
                Thread.sleep(100);
                return config;
            } catch (InterruptedException ex) {
                return config;
            }
        }
    });
    Observable<Boolean> checkZippedEqual = Observable.zip(fast, slow, new Func2<HystrixConfiguration, HystrixConfiguration, Boolean>() {

        @Override
        public Boolean call(HystrixConfiguration payload, HystrixConfiguration payload2) {
            return payload == payload2;
        }
    });
    Subscription s1 = checkZippedEqual.take(10000).subscribe(new Subscriber<Boolean>() {

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

        @Override
        public void onError(Throwable e) {
            System.out.println(System.currentTimeMillis() + " : " + Thread.currentThread().getName() + " : OnError : " + e);
            e.printStackTrace();
            foundError.set(true);
            latch.countDown();
        }

        @Override
        public void onNext(Boolean b) {
        //System.out.println(System.currentTimeMillis() + " : " + Thread.currentThread().getName() + " : OnNext : " + b);
        }
    });
    for (int i = 0; i < 50; i++) {
        HystrixCommand<Integer> cmd = Command.from(groupKey, commandKey, HystrixEventType.SUCCESS, 50);
        cmd.execute();
    }
    latch.await(10000, TimeUnit.MILLISECONDS);
    assertFalse(foundError.get());
}
Also used : CountDownLatch(java.util.concurrent.CountDownLatch) HystrixConfiguration(com.netflix.hystrix.config.HystrixConfiguration) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) Subscription(rx.Subscription) CommandStreamTest(com.netflix.hystrix.metric.CommandStreamTest) Test(org.junit.Test)

Aggregations

HystrixConfiguration (com.netflix.hystrix.config.HystrixConfiguration)4 CommandStreamTest (com.netflix.hystrix.metric.CommandStreamTest)4 CountDownLatch (java.util.concurrent.CountDownLatch)4 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)4 Test (org.junit.Test)4 Subscription (rx.Subscription)3 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)2 Action0 (rx.functions.Action0)2