本文整理汇总了Java中rx.observables.ConnectableObservable.subscribe方法的典型用法代码示例。如果您正苦于以下问题:Java ConnectableObservable.subscribe方法的具体用法?Java ConnectableObservable.subscribe怎么用?Java ConnectableObservable.subscribe使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类rx.observables.ConnectableObservable
的用法示例。
在下文中一共展示了ConnectableObservable.subscribe方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: testExpectedReplayBehavior
import rx.observables.ConnectableObservable; //导入方法依赖的package包/类
@Test
public void testExpectedReplayBehavior() {
final TestScheduler scheduler = new TestScheduler();
final TestSubject<Integer> subject = TestSubject.create(scheduler);
final TestSubscriber<Integer> subscriber = new TestSubscriber<>();
final ConnectableObservable<Integer> sums = subject.scan((a, b) -> a + b).replay(1);
sums.connect();
subject.onNext(1);
subject.onNext(2);
subject.onNext(3);
scheduler.triggerActions();
sums.subscribe(subscriber);
subscriber.assertValueCount(1);
subscriber.assertValues(6);
}
示例2: testFlakyReplayBehavior
import rx.observables.ConnectableObservable; //导入方法依赖的package包/类
@Test
public void testFlakyReplayBehavior() {
final TestScheduler scheduler = new TestScheduler();
final TestSubject<Integer> subject = TestSubject.create(scheduler);
final TestSubscriber<Integer> subscriber = new TestSubscriber<>();
final ConnectableObservable<Integer> sums = subject.scan(1, (a, b) -> a + b).replay(1);
sums.connect();
subject.onNext(2);
subject.onNext(3);
scheduler.triggerActions();
sums.subscribe(subscriber);
// subscriber.assertValueCount(1);
subscriber.assertValues(6);
}
示例3: testFlakyReplayBehavior2
import rx.observables.ConnectableObservable; //导入方法依赖的package包/类
@Test
public void testFlakyReplayBehavior2() {
final PublishSubject<Integer> subject = PublishSubject.create();
final TestSubscriber<Integer> subscriber = new TestSubscriber<>();
final ConnectableObservable<Integer> sums = subject.scan(1, (a, b) -> a + b).replay(1);
sums.connect();
subject.onNext(2);
subject.onNext(3);
sums.subscribe(subscriber);
// subscriber.assertValueCount(1);
subscriber.assertValues(6);
}
示例4: hotObservable
import rx.observables.ConnectableObservable; //导入方法依赖的package包/类
public static void hotObservable() {
Observable<Integer> observable = Observable.create(subscriber -> {
Log.i("Sample", "Creating obseravble");
subscriber.onNext(10);
subscriber.onCompleted();
});
observable.subscribe();
ConnectableObservable<Integer> hot = observable.publish();
hot.subscribe(integer -> Log.i("Sample", "aaa"));
hot.subscribe(integer -> Log.i("Sample", "bbb"));
hot.subscribe(integer -> Log.i("Sample", "ccc"));
//call connect to emit the same stream for all subscribers
hot.connect();
}
示例5: testMulticast
import rx.observables.ConnectableObservable; //导入方法依赖的package包/类
@Test
public void testMulticast() {
Subject<String, String> source = PublishSubject.create();
ConnectableObservable<String> multicasted = new OperatorMulticast<String, String>(source, new PublishSubjectFactory());
@SuppressWarnings("unchecked")
Observer<String> observer = mock(Observer.class);
multicasted.subscribe(observer);
source.onNext("one");
source.onNext("two");
multicasted.connect();
source.onNext("three");
source.onNext("four");
source.onComplete();
verify(observer, never()).onNext("one");
verify(observer, never()).onNext("two");
verify(observer, times(1)).onNext("three");
verify(observer, times(1)).onNext("four");
verify(observer, times(1)).onComplete();
}
示例6: testMulticastConnectTwice
import rx.observables.ConnectableObservable; //导入方法依赖的package包/类
@Test
public void testMulticastConnectTwice() {
Subject<String, String> source = PublishSubject.create();
ConnectableObservable<String> multicasted = new OperatorMulticast<String, String>(source, new PublishSubjectFactory());
@SuppressWarnings("unchecked")
Observer<String> observer = mock(Observer.class);
multicasted.subscribe(observer);
source.onNext("one");
multicasted.connect();
multicasted.connect();
source.onNext("two");
source.onComplete();
verify(observer, never()).onNext("one");
verify(observer, times(1)).onNext("two");
verify(observer, times(1)).onComplete();
}
示例7: testHot
import rx.observables.ConnectableObservable; //导入方法依赖的package包/类
public void testHot() throws Exception {
ConnectableObservable<Long> o = Observable.interval(200, TimeUnit.MILLISECONDS).publish();
Subscription s = o.connect();
o.subscribe(i -> System.out.println("first: " + i));
// Thread.sleep(500);
Thread.sleep(1000);
System.out.println("unsubscribe");
s.unsubscribe();
// o.subscribe(i -> System.out.println("second: " + i));
Thread.sleep(1000);
System.out.println("connect");
s = o.connect();
System.in.read();
}
示例8: addTextListeners
import rx.observables.ConnectableObservable; //导入方法依赖的package包/类
private void addTextListeners(final EditText et) {
final ConnectableObservable<String> textSource =
RxTextView.textChanges(et)
.skip(1)
.map(CharSequence::toString)
.publish();
addSpaceHandler(et);
final Subscription suggestionSub =
textSource
.filter(input -> input.length() > 0)
.flatMap(this::getWordSuggestion)
.observeOn(AndroidSchedulers.mainThread())
.subscribeOn(Schedulers.io())
.subscribe(
this::handleWordSuggestion,
throwable -> LogUtil.e(getClass(), throwable.toString())
);
final Subscription uiSub =
textSource
.subscribe(
this::updateUi,
throwable -> LogUtil.e(getClass(), throwable.toString())
);
final Subscription connectSub = textSource.connect();
this.subscriptions.addAll(
suggestionSub,
uiSub,
connectSub
);
}
示例9: getObservable
import rx.observables.ConnectableObservable; //导入方法依赖的package包/类
private ConnectableObservable<String> getObservable() {
ConnectableObservable<String> observable = Observable
.create(new Observable.OnSubscribe<String>() {
public void call(Subscriber<? super String> subscriber) {
subscriber.onStart();
try {
while (true) {
String _s = readMessage(System.in);
subscriber.onNext(_s);
}
} catch (InterruptedIOException ioe) {
log("Blocked communication");
} catch (Exception e) {
subscriber.onError(e);
}
subscriber.onCompleted();
}
}).subscribeOn(Schedulers.io()).publish();
observable.subscribe(new Observer<String>() {
public void onCompleted() {
log("App closed.");
interrompe.set(true);
}
public void onError(Throwable throwable) {
log("Unexpected error!");
interrompe.set(true);
}
public void onNext(String s) {
}
});
return observable;
}
示例10: testPublishLast
import rx.observables.ConnectableObservable; //导入方法依赖的package包/类
@Test
public void testPublishLast() throws InterruptedException {
final AtomicInteger count = new AtomicInteger();
ConnectableObservable<String> connectable = Observable.create(new OnSubscribe<String>() {
@Override
public void call(final Subscriber<? super String> observer) {
count.incrementAndGet();
new Thread(new Runnable() {
@Override
public void run() {
observer.onNext("first");
observer.onNext("last");
observer.onComplete();
}
}).start();
}
}).takeLast(1).publish();
// subscribe once
final CountDownLatch latch = new CountDownLatch(1);
connectable.subscribe(new Action1<String>() {
@Override
public void call(String value) {
assertEquals("last", value);
latch.countDown();
}
});
// subscribe twice
connectable.subscribe(new Action1<String>() {
@Override
public void call(String ignored) {
}
});
Subscription subscription = connectable.connect();
assertTrue(latch.await(1000, TimeUnit.MILLISECONDS));
assertEquals(1, count.get());
subscription.unsubscribe();
}
示例11: testPublish
import rx.observables.ConnectableObservable; //导入方法依赖的package包/类
@Test
public void testPublish() throws InterruptedException {
final AtomicInteger counter = new AtomicInteger();
Observable<String> f = Observable.create(observer ->
new Thread(() -> {
counter.incrementAndGet();
AbstractSubscription.setEmptyOn(observer);
observer.onNext("one");
observer.onComplete();
}).start()
);
ConnectableObservable<String> o = f.publish();
final CountDownLatch latch = new CountDownLatch(2);
// subscribe once
o.subscribe(v -> {
assertEquals("one", v);
latch.countDown();
});
// subscribe again
o.subscribe(v -> {
assertEquals("one", v);
latch.countDown();
});
Disposable s = o.connect();
try {
if (!latch.await(1000, TimeUnit.MILLISECONDS)) {
fail("subscriptions did not receive values");
}
assertEquals(1, counter.get());
} finally {
s.dispose();
}
}
示例12: testMulticastDisconnect
import rx.observables.ConnectableObservable; //导入方法依赖的package包/类
@Test
public void testMulticastDisconnect() {
Subject<String, String> source = PublishSubject.create();
ConnectableObservable<String> multicasted = new OperatorMulticast<String, String>(source, new PublishSubjectFactory());
@SuppressWarnings("unchecked")
Observer<String> observer = mock(Observer.class);
multicasted.subscribe(observer);
source.onNext("one");
Subscription connection = multicasted.connect();
source.onNext("two");
connection.unsubscribe();
source.onNext("three");
// subscribe again
multicasted.subscribe(observer);
// reconnect
multicasted.connect();
source.onNext("four");
source.onComplete();
verify(observer, never()).onNext("one");
verify(observer, times(1)).onNext("two");
verify(observer, never()).onNext("three");
verify(observer, times(1)).onNext("four");
verify(observer, times(1)).onComplete();
}
示例13: testIssue2191_UnsubscribeSource
import rx.observables.ConnectableObservable; //导入方法依赖的package包/类
/**
* test the basic expectation of OperatorMulticast via replay
*/
@Test
public void testIssue2191_UnsubscribeSource() {
// setup mocks
Action1 sourceNext = mock(Action1.class);
Action0 sourceCompleted = mock(Action0.class);
Action0 sourceUnsubscribed = mock(Action0.class);
Observer spiedSubscriberBeforeConnect = mock(Observer.class);
Observer spiedSubscriberAfterConnect = mock(Observer.class);
// Observable under test
Observable<Integer> source = Observable.just(1,2);
ConnectableObservable<Integer> replay = source
.doOnNext(sourceNext)
.doOnUnsubscribe(sourceUnsubscribed)
.doOnComplete(sourceCompleted)
.replay();
replay.subscribe(spiedSubscriberBeforeConnect);
replay.subscribe(spiedSubscriberBeforeConnect);
replay.connect();
replay.subscribe(spiedSubscriberAfterConnect);
replay.subscribe(spiedSubscriberAfterConnect);
// verify interactions
verify(sourceNext, times(1)).call(1);
verify(sourceNext, times(1)).call(2);
verify(sourceCompleted, times(1)).call();
verifyObserverMock(spiedSubscriberBeforeConnect, 2, 4);
verifyObserverMock(spiedSubscriberAfterConnect, 2, 4);
verify(sourceUnsubscribed, times(1)).call();
verifyNoMoreInteractions(sourceNext);
verifyNoMoreInteractions(sourceCompleted);
verifyNoMoreInteractions(sourceUnsubscribed);
verifyNoMoreInteractions(spiedSubscriberBeforeConnect);
verifyNoMoreInteractions(spiedSubscriberAfterConnect);
}
示例14: testIssue2191_SchedulerUnsubscribe
import rx.observables.ConnectableObservable; //导入方法依赖的package包/类
/**
* Specifically test interaction with a Scheduler with subscribeOn
*
* @throws Exception
*/
@Test
public void testIssue2191_SchedulerUnsubscribe() throws Exception {
// setup mocks
Action1 sourceNext = mock(Action1.class);
Action0 sourceCompleted = mock(Action0.class);
Action0 sourceUnsubscribed = mock(Action0.class);
final Scheduler mockScheduler = mock(Scheduler.class);
final Subscription mockSubscription = mock(Subscription.class);
Worker spiedWorker = workerSpy(mockSubscription);
Observer mockObserverBeforeConnect = mock(Observer.class);
Observer mockObserverAfterConnect = mock(Observer.class);
when(mockScheduler.createWorker()).thenReturn(spiedWorker);
// Observable under test
ConnectableObservable<Integer> replay = Observable.just(1, 2, 3)
.doOnNext(sourceNext)
.doOnUnsubscribe(sourceUnsubscribed)
.doOnComplete(sourceCompleted)
.subscribeOn(mockScheduler).replay();
replay.subscribe(mockObserverBeforeConnect);
replay.subscribe(mockObserverBeforeConnect);
replay.connect();
replay.subscribe(mockObserverAfterConnect);
replay.subscribe(mockObserverAfterConnect);
// verify interactions
verify(sourceNext, times(1)).call(1);
verify(sourceNext, times(1)).call(2);
verify(sourceNext, times(1)).call(3);
verify(sourceCompleted, times(1)).call();
verify(mockScheduler, times(1)).createWorker();
verify(spiedWorker, times(1)).schedule((Action0)notNull());
verifyObserverMock(mockObserverBeforeConnect, 2, 6);
verifyObserverMock(mockObserverAfterConnect, 2, 6);
verify(spiedWorker, times(1)).isUnsubscribed();
verify(spiedWorker, times(1)).unsubscribe();
verify(sourceUnsubscribed, times(1)).call();
verifyNoMoreInteractions(sourceNext);
verifyNoMoreInteractions(sourceCompleted);
verifyNoMoreInteractions(sourceUnsubscribed);
verifyNoMoreInteractions(spiedWorker);
verifyNoMoreInteractions(mockSubscription);
verifyNoMoreInteractions(mockScheduler);
verifyNoMoreInteractions(mockObserverBeforeConnect);
verifyNoMoreInteractions(mockObserverAfterConnect);
}
示例15: testIssue2191_SchedulerUnsubscribeOnError
import rx.observables.ConnectableObservable; //导入方法依赖的package包/类
/**
* Specifically test interaction with a Scheduler with subscribeOn
*
* @throws Exception
*/
@Test
public void testIssue2191_SchedulerUnsubscribeOnError() throws Exception {
// setup mocks
Action1 sourceNext = mock(Action1.class);
Action0 sourceCompleted = mock(Action0.class);
Action1 sourceError = mock(Action1.class);
Action0 sourceUnsubscribed = mock(Action0.class);
final Scheduler mockScheduler = mock(Scheduler.class);
final Subscription mockSubscription = mock(Subscription.class);
Worker spiedWorker = workerSpy(mockSubscription);
Observer mockObserverBeforeConnect = mock(Observer.class);
Observer mockObserverAfterConnect = mock(Observer.class);
when(mockScheduler.createWorker()).thenReturn(spiedWorker);
// Observable under test
Function<Integer, Integer> mockFunc = mock(Function.class);
IllegalArgumentException illegalArgumentException = new IllegalArgumentException();
when(mockFunc.call(1)).thenReturn(1);
when(mockFunc.call(2)).thenThrow(illegalArgumentException);
ConnectableObservable<Integer> replay = Observable.just(1, 2, 3).map(mockFunc)
.doOnNext(sourceNext)
.doOnUnsubscribe(sourceUnsubscribed)
.doOnComplete(sourceCompleted)
.doOnError(sourceError)
.subscribeOn(mockScheduler).replay();
replay.subscribe(mockObserverBeforeConnect);
replay.subscribe(mockObserverBeforeConnect);
replay.connect();
replay.subscribe(mockObserverAfterConnect);
replay.subscribe(mockObserverAfterConnect);
// verify interactions
verify(mockScheduler, times(1)).createWorker();
verify(spiedWorker, times(1)).schedule((Action0)notNull());
verify(sourceNext, times(1)).call(1);
verify(sourceError, times(1)).call(illegalArgumentException);
verifyObserver(mockObserverBeforeConnect, 2, 2, illegalArgumentException);
verifyObserver(mockObserverAfterConnect, 2, 2, illegalArgumentException);
verify(spiedWorker, times(1)).isUnsubscribed();
verify(spiedWorker, times(1)).unsubscribe();
verify(sourceUnsubscribed, times(1)).call();
verifyNoMoreInteractions(sourceNext);
verifyNoMoreInteractions(sourceCompleted);
verifyNoMoreInteractions(sourceError);
verifyNoMoreInteractions(sourceUnsubscribed);
verifyNoMoreInteractions(spiedWorker);
verifyNoMoreInteractions(mockSubscription);
verifyNoMoreInteractions(mockScheduler);
verifyNoMoreInteractions(mockObserverBeforeConnect);
verifyNoMoreInteractions(mockObserverAfterConnect);
}