Search in sources :

Example 21 with Subscription

use of rx.Subscription in project Hystrix by Netflix.

the class RollingConcurrencyStream method unsubscribe.

public void unsubscribe() {
    Subscription s = rollingMaxSubscription.get();
    if (s != null) {
        s.unsubscribe();
        rollingMaxSubscription.compareAndSet(s, null);
    }
}
Also used : Subscription(rx.Subscription)

Example 22 with Subscription

use of rx.Subscription in project Hystrix by Netflix.

the class RollingDistributionStream method unsubscribe.

public void unsubscribe() {
    Subscription s = rollingDistributionSubscription.get();
    if (s != null) {
        s.unsubscribe();
        rollingDistributionSubscription.compareAndSet(s, null);
    }
}
Also used : Subscription(rx.Subscription)

Example 23 with Subscription

use of rx.Subscription in project Hystrix by Netflix.

the class HystrixStreamingOutputProvider method writeTo.

@Override
public void writeTo(HystrixStream o, Class<?> t, Type gt, Annotation[] as, MediaType mediaType, MultivaluedMap<String, Object> httpHeaders, final OutputStream entity) throws IOException {
    Subscription sampleSubscription = null;
    final AtomicBoolean moreDataWillBeSent = new AtomicBoolean(true);
    try {
        sampleSubscription = o.getSampleStream().observeOn(Schedulers.io()).subscribe(new Subscriber<String>() {

            @Override
            public void onCompleted() {
                LOGGER.error("HystrixSampleSseServlet: ({}) received unexpected OnCompleted from sample stream", getClass().getSimpleName());
                moreDataWillBeSent.set(false);
            }

            @Override
            public void onError(Throwable e) {
                moreDataWillBeSent.set(false);
            }

            @Override
            public void onNext(String sampleDataAsString) {
                if (sampleDataAsString != null) {
                    try {
                        entity.write(("data: " + sampleDataAsString + "\n\n").getBytes());
                        entity.flush();
                    } catch (IOException ioe) {
                        moreDataWillBeSent.set(false);
                    }
                }
            }
        });
        while (moreDataWillBeSent.get()) {
            try {
                Thread.sleep(o.getPausePollerThreadDelayInMs());
            } catch (InterruptedException e) {
                moreDataWillBeSent.set(false);
            }
        }
    } finally {
        o.getConcurrentConnections().decrementAndGet();
        if (sampleSubscription != null && !sampleSubscription.isUnsubscribed()) {
            sampleSubscription.unsubscribe();
        }
    }
}
Also used : AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) Subscriber(rx.Subscriber) IOException(java.io.IOException) Subscription(rx.Subscription)

Example 24 with Subscription

use of rx.Subscription in project Hystrix by Netflix.

the class HystrixCollapserTest method testUnsubscribeFromSomeDuplicateArgsDoesNotRemoveFromBatch.

@Test
public void testUnsubscribeFromSomeDuplicateArgsDoesNotRemoveFromBatch() throws Exception {
    final int NUM = 10;
    List<Observable<Integer>> observables = new ArrayList<Observable<Integer>>();
    for (int i = 0; i < NUM; i++) {
        MyCollapser c = new MyCollapser("5", true);
        observables.add(c.toObservable());
    }
    List<TestSubscriber<Integer>> subscribers = new ArrayList<TestSubscriber<Integer>>();
    List<Subscription> subscriptions = new ArrayList<Subscription>();
    for (final Observable<Integer> o : observables) {
        final TestSubscriber<Integer> sub = new TestSubscriber<Integer>();
        subscribers.add(sub);
        Subscription s = o.subscribe(sub);
        subscriptions.add(s);
    }
    //unsubscribe from all but 1
    for (int i = 0; i < NUM - 1; i++) {
        Subscription s = subscriptions.get(i);
        s.unsubscribe();
    }
    Thread.sleep(100);
    //all subscribers with an active subscription should receive the same value
    for (TestSubscriber<Integer> sub : subscribers) {
        if (!sub.isUnsubscribed()) {
            sub.awaitTerminalEvent(1000, TimeUnit.MILLISECONDS);
            System.out.println("Subscriber received : " + sub.getOnNextEvents());
            sub.assertNoErrors();
            sub.assertValues(5);
        } else {
            System.out.println("Subscriber is unsubscribed");
        }
    }
}
Also used : AtomicInteger(java.util.concurrent.atomic.AtomicInteger) ArrayList(java.util.ArrayList) TestSubscriber(rx.observers.TestSubscriber) Subscription(rx.Subscription) Observable(rx.Observable) Test(org.junit.Test)

Example 25 with Subscription

use of rx.Subscription in project Hystrix by Netflix.

the class HystrixCollapserTest method testEarlyUnsubscribeExecutedViaToObservable.

@Test
public void testEarlyUnsubscribeExecutedViaToObservable() throws Exception {
    TestCollapserTimer timer = new TestCollapserTimer();
    HystrixCollapser<List<String>, String, String> collapser1 = new TestRequestCollapser(timer, 1);
    Observable<String> response1 = collapser1.toObservable();
    HystrixCollapser<List<String>, String, String> collapser2 = new TestRequestCollapser(timer, 2);
    Observable<String> response2 = collapser2.toObservable();
    final CountDownLatch latch1 = new CountDownLatch(1);
    final CountDownLatch latch2 = new CountDownLatch(1);
    final AtomicReference<String> value1 = new AtomicReference<String>(null);
    final AtomicReference<String> value2 = new AtomicReference<String>(null);
    Subscription s1 = response1.doOnUnsubscribe(new Action0() {

        @Override
        public void call() {
            System.out.println(System.currentTimeMillis() + " : s1 Unsubscribed!");
            latch1.countDown();
        }
    }).subscribe(new Subscriber<String>() {

        @Override
        public void onCompleted() {
            System.out.println(System.currentTimeMillis() + " : s1 OnCompleted");
            latch1.countDown();
        }

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

        @Override
        public void onNext(String s) {
            System.out.println(System.currentTimeMillis() + " : s1 OnNext : " + s);
            value1.set(s);
        }
    });
    Subscription s2 = response2.doOnUnsubscribe(new Action0() {

        @Override
        public void call() {
            System.out.println(System.currentTimeMillis() + " : s2 Unsubscribed!");
            latch2.countDown();
        }
    }).subscribe(new Subscriber<String>() {

        @Override
        public void onCompleted() {
            System.out.println(System.currentTimeMillis() + " : s2 OnCompleted");
            latch2.countDown();
        }

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

        @Override
        public void onNext(String s) {
            System.out.println(System.currentTimeMillis() + " : s2 OnNext : " + s);
            value2.set(s);
        }
    });
    s1.unsubscribe();
    // let time pass that equals the default delay/period
    timer.incrementTime(10);
    assertTrue(latch1.await(1000, TimeUnit.MILLISECONDS));
    assertTrue(latch2.await(1000, TimeUnit.MILLISECONDS));
    assertNull(value1.get());
    assertEquals("2", value2.get());
    System.out.println("ReqLog : " + HystrixRequestLog.getCurrentRequest().getExecutedCommandsAsString());
    assertEquals(1, HystrixRequestLog.getCurrentRequest().getAllExecutedCommands().size());
    HystrixCollapserMetrics metrics = collapser1.getMetrics();
    assertSame(metrics, collapser2.getMetrics());
    HystrixInvokableInfo<?> command = HystrixRequestLog.getCurrentRequest().getAllExecutedCommands().iterator().next();
    //1 should have been removed from batch
    assertEquals(1, command.getNumberCollapsed());
}
Also used : Action0(rx.functions.Action0) AtomicReference(java.util.concurrent.atomic.AtomicReference) CountDownLatch(java.util.concurrent.CountDownLatch) ArrayList(java.util.ArrayList) List(java.util.List) Subscription(rx.Subscription) Test(org.junit.Test)

Aggregations

Subscription (rx.Subscription)275 Test (org.junit.Test)82 List (java.util.List)66 Action0 (rx.functions.Action0)45 ArrayList (java.util.ArrayList)40 CompositeSubscription (rx.subscriptions.CompositeSubscription)40 CountDownLatch (java.util.concurrent.CountDownLatch)38 Func1 (rx.functions.Func1)24 AtomicReference (java.util.concurrent.atomic.AtomicReference)20 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)19 Observable (rx.Observable)18 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)17 LinkedList (java.util.LinkedList)13 CommandStreamTest (com.netflix.hystrix.metric.CommandStreamTest)11 PlayList (io.github.ryanhoo.music.data.model.PlayList)11 View (android.view.View)10 MetaChangedEvent (io.hefuyi.listener.event.MetaChangedEvent)10 Bundle (android.os.Bundle)9 IOException (java.io.IOException)9 AndroidSchedulers (rx.android.schedulers.AndroidSchedulers)9