@Override public void destroy() { this.context = null; if (subscription != null && !subscription.isUnsubscribed()) subscription.unsubscribe(); }
@Override public void destroy() { if (subscription != null && !subscription.isUnsubscribed()) subscription.unsubscribe(); subscription = null; context = null; dataListener = null; }
public static void unsubscribeIfNotNull(Subscription subscription) { if (subscription != null && !subscription.isUnsubscribed()) { subscription.unsubscribe(); } } }
/** * Checks if a restartable is unsubscribed. * * @param restartableId id of the restartable. * @return true if the subscription is null or unsubscribed, false otherwise. */ public boolean isUnsubscribed(int restartableId) { Subscription subscription = restartableSubscriptions.get(restartableId); return subscription == null || subscription.isUnsubscribed(); }
/** * {@inheritDoc} */ @CallSuper @Override protected void onSave(Bundle state) { for (int i = requested.size() - 1; i >= 0; i--) { int restartableId = requested.get(i); Subscription subscription = restartableSubscriptions.get(restartableId); if (subscription != null && subscription.isUnsubscribed()) requested.remove(i); } state.putIntegerArrayList(REQUESTED_KEY, requested); }
@Test public void testAdd() throws Exception { RxPresenter presenter = new RxPresenter(); Subscription mock = Mockito.mock(Subscription.class); when(mock.isUnsubscribed()).thenReturn(false); presenter.add(mock); presenter.onDestroy(); verify(mock, times(1)).unsubscribe(); verify(mock, atLeastOnce()).isUnsubscribed(); verifyNoMoreInteractions(mock); }
@Test public void testAddRemove() throws Exception { RxPresenter presenter = new RxPresenter(); Subscription mock = Mockito.mock(Subscription.class); when(mock.isUnsubscribed()).thenReturn(false); presenter.add(mock); presenter.remove(mock); verify(mock, atLeastOnce()).isUnsubscribed(); verify(mock, times(1)).unsubscribe(); presenter.onDestroy(); verifyNoMoreInteractions(mock); }
recyclerViewVisibility.set(View.INVISIBLE); infoMessageVisibility.set(View.INVISIBLE); if (subscription != null && !subscription.isUnsubscribed()) subscription.unsubscribe(); ArchiApplication application = ArchiApplication.get(context); GithubService githubService = application.getGithubService();
@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); }
@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)); }
@Test public void testCompletedRestartableIsUnsubscribed() 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(true); presenter.restartable(1, restartable); assertTrue(presenter.isUnsubscribed(1)); presenter.start(1); assertTrue(presenter.isUnsubscribed(1)); }
@Test public void testStartedRestartableIsNotUnsubscribed() 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)); presenter.start(1); assertFalse(presenter.isUnsubscribed(1)); }
@Test(timeout = 60000) public void testRemoveNonDelayedTasks() throws Exception { RxJavaEventloopScheduler scheduler = new RxJavaEventloopScheduler(new NioEventLoopGroup()); final EventloopWorker worker = (EventloopWorker) scheduler.createWorker(); final EventloopWorker worker2 = (EventloopWorker) scheduler.createWorker(); assertThat("New worker already has subscriptions.", worker.hasScheduledSubscriptions(), is(false)); final AtomicBoolean isScheduledBeforeExecute = new AtomicBoolean(); final CountDownLatch executed = new CountDownLatch(1); Subscription subscription = worker.schedule(new Action0() { @Override public void call() { isScheduledBeforeExecute.set(worker.hasScheduledSubscriptions()); worker2.schedule(new Action0() { @Override public void call() { executed.countDown(); } }); } }); executed.await(); assertThat("No scheduled subscriptions on executing the action.", isScheduledBeforeExecute.get(), is(true)); assertThat("Action not unsubscribed.", subscription.isUnsubscribed(), is(true)); assertThat("Subscription not removed post execution.", worker.hasScheduledSubscriptions(), is(false)); }
@Test(timeout = 60000) public void testRemoveDelayedTasks() throws Exception { RxJavaEventloopScheduler scheduler = new RxJavaEventloopScheduler(new NioEventLoopGroup()); final EventloopWorker worker = (EventloopWorker) scheduler.createWorker(); final EventloopWorker worker2 = (EventloopWorker) scheduler.createWorker(); assertThat("New worker already has subscriptions.", worker.hasDelayScheduledSubscriptions(), is(false)); final AtomicBoolean isScheduledBeforeExecute = new AtomicBoolean(); final CountDownLatch executed = new CountDownLatch(1); Subscription subscription = worker.schedule(new Action0() { @Override public void call() { isScheduledBeforeExecute.set(worker.hasDelayScheduledSubscriptions()); worker2.schedule(new Action0() { @Override public void call() { executed.countDown(); } }); } }, 1, TimeUnit.MILLISECONDS); executed.await(); assertThat("No scheduled subscriptions on executing the action.", isScheduledBeforeExecute.get(), is(true)); assertThat("Action not unsubscribed.", subscription.isUnsubscribed(), is(true)); assertThat("Subscription not removed post execution.", worker.hasDelayScheduledSubscriptions(), is(false)); }
@Test @Category(ExperimentalTest.class ) public void testPublish() throws InterruptedException { final int count = 10; final CountDownLatch latch = new CountDownLatch( count+1 ); final Subscription connectedObservable = Observable.range( 0, count ) .doOnNext( integer -> latch.countDown() ) .doOnCompleted( () -> latch.countDown() ).subscribeOn( Schedulers.io() ) .subscribe(); final boolean completed = latch.await( 3, TimeUnit.SECONDS ); assertTrue( "publish1 behaves as expected", completed ); final boolean completedSubscription = connectedObservable.isUnsubscribed(); assertTrue( "Subscription complete", completedSubscription ); }
@Test public void testCompletedRestartableDoesNoRestart() 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); when(subscription.isUnsubscribed()).thenReturn(true); Bundle bundle = BundleMock.mock(); presenter.onSave(bundle); presenter = new RxPresenter(); presenter.onCreate(bundle); presenter.restartable(1, restartable); verifyNoMoreInteractions(restartable); }
@Test public void testRestartable() 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); verifyNoMoreInteractions(restartable); presenter.start(1); verify(restartable, times(1)).call(); verifyNoMoreInteractions(restartable); Bundle bundle = BundleMock.mock(); presenter.onSave(bundle); presenter = new RxPresenter(); presenter.create(bundle); presenter.restartable(1, restartable); verify(restartable, times(2)).call(); verifyNoMoreInteractions(restartable); }
@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); }
@Test(timeout = 60000) public void testDispose() throws Exception { ListenerWithSub l = holderRule.addAListener(); holderRule.getHolder().dispose(); assertThat("On complete not called on dispose.", l.listener.getOnCompletedCount(), is(1)); assertThat("Listener not unsubscribed on dispose.", l.subscription.isUnsubscribed(), is(true)); assertThat("Listener not removed on dispose.", holderRule.getHolder().getAllListeners(), not(contains(l.listener))); }
@Test(timeout = 60000) public void testDisposeWithExceptions() throws Exception { final MockEventListener listener1 = new MockEventListener(true); final MockEventListener listener2 = new MockEventListener(); Subscription subscription1 = holderRule.getHolder().subscribe(listener1); Subscription subscription2 = holderRule.getHolder().subscribe(listener2); assertThat("Listeners not added.", holderRule.getHolder().getAllListeners(), hasSize(2)); assertThat("Listeners not added.", holderRule.getHolder().getAllListeners(), contains(listener1, listener2)); try { holderRule.getHolder().dispose(); throw new AssertionError("Error not thrown on dispose."); } catch (Exception e) { // Expected. } assertThat("First listener not completed.", listener1.getOnCompletedCount(), is(1)); assertThat("Second listener not completed.", listener2.getOnCompletedCount(), is(1)); assertThat("First listener not unsubscribed.", subscription1.isUnsubscribed(), is(true)); assertThat("Second listener not unsubscribed.", subscription2.isUnsubscribed(), is(true)); assertThat("Listeners not removed post dispose.", holderRule.getHolder().getAllListeners(), is(empty())); }