Search in sources :

Example 6 with Action

use of io.reactivex.functions.Action in project RxJava by ReactiveX.

the class CompletableTimerTest method timer.

@Test
public void timer() {
    final TestScheduler testScheduler = new TestScheduler();
    final AtomicLong atomicLong = new AtomicLong();
    Completable.timer(2, TimeUnit.SECONDS, testScheduler).subscribe(new Action() {

        @Override
        public void run() throws Exception {
            atomicLong.incrementAndGet();
        }
    });
    assertEquals(0, atomicLong.get());
    testScheduler.advanceTimeBy(1, TimeUnit.SECONDS);
    assertEquals(0, atomicLong.get());
    testScheduler.advanceTimeBy(1, TimeUnit.SECONDS);
    assertEquals(1, atomicLong.get());
}
Also used : AtomicLong(java.util.concurrent.atomic.AtomicLong) Action(io.reactivex.functions.Action) TestScheduler(io.reactivex.schedulers.TestScheduler) Test(org.junit.Test)

Example 7 with Action

use of io.reactivex.functions.Action in project RxJava by ReactiveX.

the class CompletableDisposeOnTest method error.

@Test
public void error() {
    TestScheduler scheduler = new TestScheduler();
    final int[] call = { 0 };
    Completable.error(new TestException()).doOnDispose(new Action() {

        @Override
        public void run() throws Exception {
            call[0]++;
        }
    }).unsubscribeOn(scheduler).test().assertFailure(TestException.class);
    scheduler.triggerActions();
    assertEquals(0, call[0]);
}
Also used : Action(io.reactivex.functions.Action) TestException(io.reactivex.exceptions.TestException) TestScheduler(io.reactivex.schedulers.TestScheduler) Test(org.junit.Test)

Example 8 with Action

use of io.reactivex.functions.Action in project RxJava by ReactiveX.

the class CompletableDisposeOnTest method normal.

@Test
public void normal() {
    TestScheduler scheduler = new TestScheduler();
    final int[] call = { 0 };
    Completable.complete().doOnDispose(new Action() {

        @Override
        public void run() throws Exception {
            call[0]++;
        }
    }).unsubscribeOn(scheduler).test().assertResult();
    scheduler.triggerActions();
    assertEquals(0, call[0]);
}
Also used : Action(io.reactivex.functions.Action) TestScheduler(io.reactivex.schedulers.TestScheduler) Test(org.junit.Test)

Example 9 with Action

use of io.reactivex.functions.Action in project RxJava by ReactiveX.

the class CompletableFromActionTest method fromActionTwice.

@Test
public void fromActionTwice() {
    final AtomicInteger atomicInteger = new AtomicInteger();
    Action run = new Action() {

        @Override
        public void run() throws Exception {
            atomicInteger.incrementAndGet();
        }
    };
    Completable.fromAction(run).test().assertResult();
    assertEquals(1, atomicInteger.get());
    Completable.fromAction(run).test().assertResult();
    assertEquals(2, atomicInteger.get());
}
Also used : Action(io.reactivex.functions.Action) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Test(org.junit.Test)

Example 10 with Action

use of io.reactivex.functions.Action in project RxJava by ReactiveX.

the class FlowableDoAfterTerminateTest method ifFinallyActionThrowsExceptionShouldNotBeSwallowedAndActionShouldBeCalledOnce.

@Test
public void ifFinallyActionThrowsExceptionShouldNotBeSwallowedAndActionShouldBeCalledOnce() throws Exception {
    Action finallyAction = Mockito.mock(Action.class);
    doThrow(new IllegalStateException()).when(finallyAction).run();
    TestSubscriber<String> testSubscriber = new TestSubscriber<String>();
    Flowable.just("value").doAfterTerminate(finallyAction).subscribe(testSubscriber);
    testSubscriber.assertValue("value");
    verify(finallyAction).run();
// Actual result:
// Not only IllegalStateException was swallowed
// But finallyAction was called twice!
}
Also used : Action(io.reactivex.functions.Action) TestSubscriber(io.reactivex.subscribers.TestSubscriber)

Aggregations

Action (io.reactivex.functions.Action)31 Test (org.junit.Test)19 TestException (io.reactivex.exceptions.TestException)6 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)6 Consumer (io.reactivex.functions.Consumer)5 Disposable (io.reactivex.disposables.Disposable)4 TestScheduler (io.reactivex.schedulers.TestScheduler)3 View (android.view.View)2 NonNull (io.reactivex.annotations.NonNull)2 IOException (java.io.IOException)2 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)2 Intent (android.content.Intent)1 NonNull (android.support.annotation.NonNull)1 FloatingActionButton (android.support.design.widget.FloatingActionButton)1 Toolbar (android.support.v7.widget.Toolbar)1 TextView (android.widget.TextView)1 BindView (butterknife.BindView)1 ActionBarProvider (com.bluelinelabs.conductor.demo.ActionBarProvider)1 ActionBarOwner (com.example.mortar.android.ActionBarOwner)1 ChatListScreen (com.example.mortar.screen.ChatListScreen)1