Search in sources :

Example 1 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 2 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 3 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 4 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 5 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)44 Test (org.junit.Test)19 Consumer (io.reactivex.functions.Consumer)9 TestException (io.reactivex.exceptions.TestException)6 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)6 Disposable (io.reactivex.disposables.Disposable)5 IOException (java.io.IOException)4 NonNull (android.support.annotation.NonNull)3 View (android.view.View)3 NonNull (io.reactivex.annotations.NonNull)3 TestScheduler (io.reactivex.schedulers.TestScheduler)3 Uri (android.net.Uri)2 TextView (android.widget.TextView)2 BindView (butterknife.BindView)2 ActionBarProvider (com.bluelinelabs.conductor.demo.ActionBarProvider)2 ObservableScoper (com.uber.autodispose.ObservableScoper)2 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)2 ProgressDialog (android.app.ProgressDialog)1 Intent (android.content.Intent)1 Bitmap (android.graphics.Bitmap)1