예제 #1
0
  @Test
  public void testStopRestartable() throws Exception {
    RxPresenter presenter = new RxPresenter();
    presenter.onCreate(null);

    Func0<Subscription> restartable = mock(Func0.class);
    Subscription subscription = mock(Subscription.class);
    when(restartable.call()).thenReturn(subscription);
    when(subscription.isUnsubscribed()).thenReturn(false);
    presenter.restartable(1, restartable);

    verifyNoMoreInteractions(restartable);

    presenter.start(1);

    verify(restartable, times(1)).call();
    verifyNoMoreInteractions(restartable);

    presenter.stop(1);

    Bundle bundle = BundleMock.mock();
    presenter.onSave(bundle);

    presenter = new RxPresenter();
    presenter.onCreate(bundle);
    presenter.restartable(1, restartable);

    verify(restartable, times(1)).call();
    verifyNoMoreInteractions(restartable);
  }
예제 #2
0
  @Test
  public void testRestartableIsUnsubscribed() throws Exception {
    RxPresenter presenter = new RxPresenter();
    presenter.create(null);

    Func0<Subscription> restartable = mock(Func0.class);
    Subscription subscription = mock(Subscription.class);
    when(restartable.call()).thenReturn(subscription);
    when(subscription.isUnsubscribed()).thenReturn(false);

    presenter.restartable(1, restartable);
    assertTrue(presenter.isUnsubscribed(1));
  }
예제 #3
0
  @Test
  public void testCompletedRestartable() throws Exception {
    Func0<Subscription> restartable = mock(Func0.class);
    Subscription subscription = mock(Subscription.class);

    RxPresenter presenter = new RxPresenter();
    presenter.create(null);

    when(restartable.call()).thenReturn(subscription);
    when(subscription.isUnsubscribed()).thenReturn(true);
    presenter.restartable(1, restartable);

    verifyNoMoreInteractions(restartable);

    presenter.start(1);
  }
예제 #4
0
 private void safelyWriteNumberField(
     JsonGenerator json, String name, Func0<Long> metricGenerator) throws IOException {
   try {
     json.writeNumberField(name, metricGenerator.call());
   } catch (NoSuchFieldError error) {
     logger.error(
         "While publishing Hystrix metrics stream, error looking up eventType for : {}.  Please check that all Hystrix versions are the same!",
         name);
     json.writeNumberField(name, 0L);
   }
 }
예제 #5
0
  @Override
  public void call(final Subscriber<? super T> subscriber) {

    try {

      // create the resource
      final Resource resource = resourceFactory.call();
      // create an action/subscription that disposes only once
      final DisposeAction<Resource> disposeOnceOnly =
          new DisposeAction<Resource>(dispose, resource);
      // dispose on unsubscription
      subscriber.add(disposeOnceOnly);
      // create the observable
      final Observable<? extends T> source =
          observableFactory
              // create the observable
              .call(resource);
      final Observable<? extends T> observable;
      // supplement with on termination disposal if requested
      if (disposeEagerly)
        observable =
            source
                // dispose on completion or error
                .doOnTerminate(disposeOnceOnly);
      else observable = source;
      try {
        // start
        observable.unsafeSubscribe(Subscribers.wrap(subscriber));
      } catch (Throwable e) {
        Throwable disposeError = disposeEagerlyIfRequested(disposeOnceOnly);
        Exceptions.throwIfFatal(e);
        Exceptions.throwIfFatal(disposeError);
        if (disposeError != null)
          subscriber.onError(new CompositeException(Arrays.asList(e, disposeError)));
        else
          // propagate error
          subscriber.onError(e);
      }
    } catch (Throwable e) {
      // then propagate error
      Exceptions.throwOrReport(e, subscriber);
    }
  }