ObservableExtensions { @Deprecated @Nonnull public static <T> Observable.Transformer<T, T> behaviorRefCount() { return new Observable.Transformer<T, T>() { @Override public Observable<T> call(final Observable<T> tObservable) { return tObservable.replay(1).refCount(); } }; }  @Deprecated @Nonnull static ConnectableObservable<T> behavior(@Nonnull Observable<T> observable); @Deprecated @Nonnull static Observable.Transformer<T, T> behaviorRefCount(); @Deprecated @Nonnull static Observable.Transformer<T, T> behaviorConnected();  }
@Test public void testBehaviorRefCountAfterGettingTwoDataOnFirstSubscription_newWillOnlyGetLastData() throws Exception { final Observable<String> refCount = subject.compose(ObservableExtensions.<String>behaviorRefCount()); final TestSubscriber<String> stringSubscriber1 = new TestSubscriber<>(); final TestSubscriber<String> stringSubscriber2 = new TestSubscriber<>(); refCount.subscribe(stringSubscriber1); subject.onNext("test1"); subject.onNext("test2"); assert_().that(stringSubscriber1.getOnNextEvents()).isEqualTo(ImmutableList.of("test1", "test2")); stringSubscriber1.assertNoErrors(); refCount.subscribe(stringSubscriber2); assert_().that(stringSubscriber2.getOnNextEvents()).isEqualTo(ImmutableList.of("test2")); stringSubscriber2.assertNoErrors(); }
@Test public void testBehaviorRefCountWithTwoParallelSchedulers_returnsSameValue() throws Exception { final Observable<String> refCount = subject.compose(ObservableExtensions.<String>behaviorRefCount()); final TestSubscriber<String> stringSubscriber1 = new TestSubscriber<>(); final TestSubscriber<String> stringSubscriber2 = new TestSubscriber<>(); refCount.subscribe(stringSubscriber1); refCount.subscribe(stringSubscriber2); subject.onNext("test1"); assert_().that(stringSubscriber1.getOnNextEvents()).isEqualTo(ImmutableList.of("test1")); assert_().that(stringSubscriber2.getOnNextEvents()).isEqualTo(ImmutableList.of("test1")); stringSubscriber1.assertNoErrors(); stringSubscriber2.assertNoErrors(); }
@Test public void testBehaviorRefCountNextValueBeforeSubscription_isIgnoredAfterSubscribe() throws Exception { final Observable<String> connected = subject.compose(ObservableExtensions.<String>behaviorRefCount()); final TestSubscriber<String> stringSubscriber = new TestSubscriber<>(); subject.onNext("test1"); connected.subscribe(stringSubscriber); assert_().that(stringSubscriber.getOnNextEvents()).isEqualTo(ImmutableList.of()); }
@Test public void testBehaviorRefCountAfterUnsubscribinAllSubscribers_newSchedulerWillNotGetData() throws Exception { final Observable<String> refCount = subject.compose(ObservableExtensions.<String>behaviorRefCount()); final TestSubscriber<String> stringSubscriber1 = new TestSubscriber<>(); final TestSubscriber<String> stringSubscriber2 = new TestSubscriber<>(); final Subscription s1 = refCount.subscribe(stringSubscriber1); subject.onNext("test1"); assert_().that(stringSubscriber1.getOnNextEvents()).isEqualTo(ImmutableList.of("test1")); s1.unsubscribe(); refCount.subscribe(stringSubscriber2); assert_().that(stringSubscriber2.getOnNextEvents()).isEmpty(); stringSubscriber1.assertNoErrors(); stringSubscriber2.assertNoErrors(); }
@Test public void testBehaviorRefCountAfterSubsribingToAlreadySubscribedScheduler_getSameData() throws Exception { final Observable<String> refCount = subject.compose(ObservableExtensions.<String>behaviorRefCount()); final TestSubscriber<String> stringSubscriber1 = new TestSubscriber<>(); final TestSubscriber<String> stringSubscriber2 = new TestSubscriber<>(); refCount.subscribe(stringSubscriber1); subject.onNext("test1"); assert_().that(stringSubscriber1.getOnNextEvents()).isEqualTo(ImmutableList.of("test1")); stringSubscriber1.assertNoErrors(); refCount.subscribe(stringSubscriber2); assert_().that(stringSubscriber2.getOnNextEvents()).isEqualTo(ImmutableList.of("test1")); stringSubscriber2.assertNoErrors(); }