Search in sources :

Example 1 with TestSubscriber

use of rx.observers.TestSubscriber in project MovieGuide by esoxjem.

the class MovieDetailsPresenterImplTest method shouldBeAbleToShowTrailers.

@Test
public void shouldBeAbleToShowTrailers() {
    TestScheduler testScheduler = new TestScheduler();
    TestSubscriber<List<Video>> testSubscriber = new TestSubscriber<>();
    Observable<List<Video>> responseObservable = Observable.just(videos).subscribeOn(testScheduler);
    responseObservable.subscribe(testSubscriber);
    when(movieDetailsInteractor.getTrailers(anyString())).thenReturn(responseObservable);
    movieDetailsPresenter.showTrailers(movie);
    testScheduler.triggerActions();
    testSubscriber.assertNoErrors();
    testSubscriber.assertCompleted();
    verify(view).showTrailers(videos);
}
Also used : TestSubscriber(rx.observers.TestSubscriber) List(java.util.List) TestScheduler(rx.schedulers.TestScheduler) Test(org.junit.Test)

Example 2 with TestSubscriber

use of rx.observers.TestSubscriber in project MovieGuide by esoxjem.

the class MoviesListingPresenterImplTest method shouldBeAbleToDisplayMovies.

@Test
public void shouldBeAbleToDisplayMovies() {
    TestScheduler testScheduler = new TestScheduler();
    TestSubscriber<List<Movie>> testSubscriber = new TestSubscriber<>();
    Observable<List<Movie>> responseObservable = Observable.just(movies).subscribeOn(testScheduler);
    responseObservable.subscribe(testSubscriber);
    when(interactor.fetchMovies()).thenReturn(responseObservable);
    presenter.setView(view);
    testScheduler.triggerActions();
    testSubscriber.assertNoErrors();
    testSubscriber.onCompleted();
    verify(view).showMovies(movies);
}
Also used : TestSubscriber(rx.observers.TestSubscriber) List(java.util.List) TestScheduler(rx.schedulers.TestScheduler) Test(org.junit.Test)

Example 3 with TestSubscriber

use of rx.observers.TestSubscriber 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 4 with TestSubscriber

use of rx.observers.TestSubscriber in project Hystrix by Netflix.

the class HystrixCommandTest method testObservableTimeoutNoFallbackThreadContext.

/**
     * See https://github.com/Netflix/Hystrix/issues/212
     */
@Test
public void testObservableTimeoutNoFallbackThreadContext() {
    TestSubscriber<Object> ts = new TestSubscriber<Object>();
    final AtomicReference<Thread> onErrorThread = new AtomicReference<Thread>();
    final AtomicBoolean isRequestContextInitialized = new AtomicBoolean();
    TestHystrixCommand<Integer> command = getCommand(ExecutionIsolationStrategy.THREAD, AbstractTestHystrixCommand.ExecutionResult.SUCCESS, 200, AbstractTestHystrixCommand.FallbackResult.UNIMPLEMENTED, 50);
    command.toObservable().doOnError(new Action1<Throwable>() {

        @Override
        public void call(Throwable t1) {
            System.out.println("onError: " + t1);
            System.out.println("onError Thread: " + Thread.currentThread());
            System.out.println("ThreadContext in onError: " + HystrixRequestContext.isCurrentThreadInitialized());
            onErrorThread.set(Thread.currentThread());
            isRequestContextInitialized.set(HystrixRequestContext.isCurrentThreadInitialized());
        }
    }).subscribe(ts);
    ts.awaitTerminalEvent();
    assertTrue(isRequestContextInitialized.get());
    assertTrue(onErrorThread.get().getName().startsWith("HystrixTimer"));
    List<Throwable> errors = ts.getOnErrorEvents();
    assertEquals(1, errors.size());
    Throwable e = errors.get(0);
    if (errors.get(0) instanceof HystrixRuntimeException) {
        HystrixRuntimeException de = (HystrixRuntimeException) e;
        assertNotNull(de.getFallbackException());
        assertTrue(de.getFallbackException() instanceof UnsupportedOperationException);
        assertNotNull(de.getImplementingClass());
        assertNotNull(de.getCause());
        assertTrue(de.getCause() instanceof TimeoutException);
    } else {
        fail("the exception should be ExecutionException with cause as HystrixRuntimeException");
    }
    assertTrue(command.getExecutionTimeInMilliseconds() > -1);
    assertTrue(command.isResponseTimedOut());
    assertCommandExecutionEvents(command, HystrixEventType.TIMEOUT, HystrixEventType.FALLBACK_MISSING);
    assertNotNull(command.getExecutionException());
    assertEquals(0, command.getBuilder().metrics.getCurrentConcurrentExecutionCount());
    assertSaneHystrixRequestLog(1);
}
Also used : Action1(rx.functions.Action1) AtomicReference(java.util.concurrent.atomic.AtomicReference) HystrixRuntimeException(com.netflix.hystrix.exception.HystrixRuntimeException) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) TestSubscriber(rx.observers.TestSubscriber) TimeoutException(java.util.concurrent.TimeoutException) Test(org.junit.Test)

Example 5 with TestSubscriber

use of rx.observers.TestSubscriber in project Hystrix by Netflix.

the class HystrixObservableCommandTest method testObservableTimeoutNoFallbackThreadContext.

/**
     * See https://github.com/Netflix/Hystrix/issues/212
     */
@Test
public void testObservableTimeoutNoFallbackThreadContext() {
    TestSubscriber<Integer> ts = new TestSubscriber<Integer>();
    final AtomicReference<Thread> onErrorThread = new AtomicReference<Thread>();
    final AtomicBoolean isRequestContextInitialized = new AtomicBoolean();
    TestHystrixObservableCommand<Integer> command = getCommand(ExecutionIsolationStrategy.SEMAPHORE, AbstractTestHystrixCommand.ExecutionResult.SUCCESS, 200, AbstractTestHystrixCommand.FallbackResult.UNIMPLEMENTED, 100);
    command.toObservable().doOnError(new Action1<Throwable>() {

        @Override
        public void call(Throwable t1) {
            System.out.println("onError: " + t1);
            System.out.println("onError Thread: " + Thread.currentThread());
            System.out.println("ThreadContext in onError: " + HystrixRequestContext.isCurrentThreadInitialized());
            onErrorThread.set(Thread.currentThread());
            isRequestContextInitialized.set(HystrixRequestContext.isCurrentThreadInitialized());
        }
    }).subscribe(ts);
    ts.awaitTerminalEvent();
    assertTrue(isRequestContextInitialized.get());
    assertTrue(onErrorThread.get().getName().startsWith("HystrixTimer"));
    List<Throwable> errors = ts.getOnErrorEvents();
    assertEquals(1, errors.size());
    Throwable e = errors.get(0);
    if (errors.get(0) instanceof HystrixRuntimeException) {
        HystrixRuntimeException de = (HystrixRuntimeException) e;
        assertNotNull(de.getFallbackException());
        assertTrue(de.getFallbackException() instanceof UnsupportedOperationException);
        assertNotNull(de.getImplementingClass());
        assertNotNull(de.getCause());
        assertTrue(de.getCause() instanceof TimeoutException);
    } else {
        fail("the exception should be ExecutionException with cause as HystrixRuntimeException");
    }
    assertTrue(command.getExecutionTimeInMilliseconds() > -1);
    assertTrue(command.isResponseTimedOut());
    assertNotNull(command.getExecutionException());
    assertCommandExecutionEvents(command, HystrixEventType.TIMEOUT, HystrixEventType.FALLBACK_MISSING);
    assertEquals(0, command.metrics.getCurrentConcurrentExecutionCount());
    assertSaneHystrixRequestLog(1);
    assertFalse(command.isExecutedInThread());
}
Also used : Action1(rx.functions.Action1) AtomicReference(java.util.concurrent.atomic.AtomicReference) HystrixRuntimeException(com.netflix.hystrix.exception.HystrixRuntimeException) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) TestSubscriber(rx.observers.TestSubscriber) TimeoutException(java.util.concurrent.TimeoutException) Test(org.junit.Test)

Aggregations

TestSubscriber (rx.observers.TestSubscriber)238 Test (org.junit.Test)213 Intent (android.content.Intent)48 Environment (com.kickstarter.libs.Environment)43 Cursor (android.database.Cursor)36 Changes (com.pushtorefresh.storio.contentresolver.Changes)34 Project (com.kickstarter.models.Project)33 MockCurrentUser (com.kickstarter.libs.MockCurrentUser)21 MockApiClient (com.kickstarter.services.MockApiClient)21 ApiClientType (com.kickstarter.services.ApiClientType)20 TargetApi (android.annotation.TargetApi)18 CurrentUserType (com.kickstarter.libs.CurrentUserType)18 StorIOSQLite (com.pushtorefresh.storio.sqlite.StorIOSQLite)18 StorIOException (com.pushtorefresh.storio.StorIOException)17 TestCollapserTimer (com.netflix.hystrix.HystrixCollapserTest.TestCollapserTimer)16 Matchers.anyString (org.mockito.Matchers.anyString)16 User (com.kickstarter.models.User)15 PutResult (com.pushtorefresh.storio.contentresolver.operations.put.PutResult)15 Uri (android.net.Uri)14 NonNull (android.support.annotation.NonNull)14