本文整理了Java中io.reactivex.Flowable.doOnSubscribe()
方法的一些代码示例,展示了Flowable.doOnSubscribe()
的具体用法。这些代码示例主要来源于Github
/Stackoverflow
/Maven
等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。Flowable.doOnSubscribe()
方法的具体详情如下:
包路径:io.reactivex.Flowable
类名称:Flowable
方法名:doOnSubscribe
[英]Modifies the source Publisher so that it invokes the given action when it is subscribed from its subscribers. Each subscription will result in an invocation of the given action except when the source Publisher is reference counted, in which case the source Publisher will invoke the given action for the first subscription.
Backpressure: The operator doesn't interfere with backpressure which is determined by the source Publisher's backpressure behavior. Scheduler: doOnSubscribe does not operate by default on a particular Scheduler.
[中]修改源发布服务器,使其在从订阅服务器订阅时调用给定操作。每个订阅将导致调用给定操作,除非源发布服务器进行引用计数,在这种情况下,源发布服务器将为第一个订阅调用给定操作。
背压:操作员不会干扰由源发布者的背压行为确定的背压。调度器:默认情况下,doOnSubscribe不会在特定的调度器上运行。
代码示例来源:origin: ReactiveX/RxJava
@Test(expected = NullPointerException.class)
public void doOnSubscribeNull() {
just1.doOnSubscribe(null);
}
代码示例来源:origin: ReactiveX/RxJava
@Test
public void testDoOnSubscribe2() throws Exception {
final AtomicInteger count = new AtomicInteger();
Flowable<Integer> f = Flowable.just(1).doOnSubscribe(new Consumer<Subscription>() {
@Override
public void accept(Subscription s) {
count.incrementAndGet();
}
}).take(1).doOnSubscribe(new Consumer<Subscription>() {
@Override
public void accept(Subscription s) {
count.incrementAndGet();
}
});
f.subscribe();
assertEquals(2, count.get());
}
代码示例来源:origin: ReactiveX/RxJava
@Test
public void testDoOnSubscribe() throws Exception {
final AtomicInteger count = new AtomicInteger();
Flowable<Integer> f = Flowable.just(1).doOnSubscribe(new Consumer<Subscription>() {
@Override
public void accept(Subscription s) {
count.incrementAndGet();
}
});
f.subscribe();
f.subscribe();
f.subscribe();
assertEquals(3, count.get());
}
代码示例来源:origin: ReactiveX/RxJava
@Test
public void testSwitchWhenNotEmpty() throws Exception {
final AtomicBoolean subscribed = new AtomicBoolean(false);
final Flowable<Integer> flowable = Flowable.just(4)
.switchIfEmpty(Flowable.just(2)
.doOnSubscribe(new Consumer<Subscription>() {
@Override
public void accept(Subscription s) {
subscribed.set(true);
}
}));
assertEquals(4, flowable.blockingSingle().intValue());
assertFalse(subscribed.get());
}
代码示例来源:origin: ReactiveX/RxJava
private static <T> Flowable<T> composer(Flowable<T> source, final AtomicInteger subscriptionCount, final int m) {
return source.doOnSubscribe(new Consumer<Subscription>() {
@Override
public void accept(Subscription s) {
int n = subscriptionCount.getAndIncrement();
if (n >= m) {
Assert.fail("Too many subscriptions! " + (n + 1));
}
}
}).doOnComplete(new Action() {
@Override
public void run() {
int n = subscriptionCount.decrementAndGet();
if (n < 0) {
Assert.fail("Too many unsubscriptions! " + (n - 1));
}
}
});
}
代码示例来源:origin: ReactiveX/RxJava
final AtomicInteger nextCount = new AtomicInteger();
Flowable<Integer> r = Flowable.just(1, 2, 3, 4, 5, 6, 7, 8, 9)
.doOnSubscribe(new Consumer<Subscription>() {
@Override
public void accept(Subscription s) {
代码示例来源:origin: ReactiveX/RxJava
final AtomicInteger unsubscribeCount = new AtomicInteger();
Flowable<Long> r = Flowable.interval(0, 1, TimeUnit.MILLISECONDS)
.doOnSubscribe(new Consumer<Subscription>() {
@Override
public void accept(Subscription s) {
代码示例来源:origin: ReactiveX/RxJava
@Test
public void disposedUpfrontFallback() {
PublishProcessor<Object> pp = PublishProcessor.create();
final AtomicInteger counter = new AtomicInteger();
Flowable<Object> timeoutAndFallback = Flowable.never().doOnSubscribe(new Consumer<Subscription>() {
@Override
public void accept(Subscription s) throws Exception {
counter.incrementAndGet();
}
});
pp
.timeout(timeoutAndFallback, Functions.justFunction(timeoutAndFallback), timeoutAndFallback)
.test(1, true)
.assertEmpty();
assertEquals(0, counter.get());
}
代码示例来源:origin: ReactiveX/RxJava
@Test
public void disposedUpfront() {
PublishProcessor<Integer> pp = PublishProcessor.create();
final AtomicInteger counter = new AtomicInteger();
Flowable<Object> timeoutAndFallback = Flowable.never().doOnSubscribe(new Consumer<Subscription>() {
@Override
public void accept(Subscription s) throws Exception {
counter.incrementAndGet();
}
});
pp
.timeout(timeoutAndFallback, Functions.justFunction(timeoutAndFallback))
.test(1, true)
.assertEmpty();
assertEquals(0, counter.get());
}
代码示例来源:origin: ReactiveX/RxJava
@Test
public void delayAndTakeUntilNeverSubscribeToSource() {
PublishProcessor<Integer> delayUntil = PublishProcessor.create();
PublishProcessor<Integer> interrupt = PublishProcessor.create();
final AtomicBoolean subscribed = new AtomicBoolean(false);
Flowable.just(1)
.doOnSubscribe(new Consumer<Subscription>() {
@Override
public void accept(Subscription s) {
subscribed.set(true);
}
})
.delaySubscription(delayUntil)
.takeUntil(interrupt)
.subscribe();
interrupt.onNext(9000);
delayUntil.onNext(1);
Assert.assertFalse(subscribed.get());
}
代码示例来源:origin: ReactiveX/RxJava
@Test
public void delayAndTakeUntilNeverSubscribeToSource() {
PublishProcessor<Integer> delayUntil = PublishProcessor.create();
PublishProcessor<Integer> interrupt = PublishProcessor.create();
final AtomicBoolean subscribed = new AtomicBoolean(false);
Flowable.just(1)
.doOnSubscribe(new Consumer<Object>() {
@Override
public void accept(Object o) {
subscribed.set(true);
}
})
.delaySubscription(delayUntil)
.takeUntil(interrupt)
.subscribe();
interrupt.onNext(9000);
delayUntil.onNext(1);
Assert.assertFalse(subscribed.get());
}
代码示例来源:origin: ReactiveX/RxJava
@SuppressWarnings("unchecked")
@Test
public void dontSubscribeIfDone2() {
List<Throwable> errors = TestHelper.trackPluginErrors();
try {
final int[] count = { 0 };
Flowable.combineLatestDelayError(
Arrays.asList(Flowable.empty(),
Flowable.error(new TestException())
.doOnSubscribe(new Consumer<Subscription>() {
@Override
public void accept(Subscription s) throws Exception {
count[0]++;
}
})
),
new Function<Object[], Object>() {
@Override
public Object apply(Object[] a) throws Exception {
return 0;
}
})
.test()
.assertResult();
assertEquals(0, count[0]);
assertTrue(errors.toString(), errors.isEmpty());
} finally {
RxJavaPlugins.reset();
}
}
代码示例来源:origin: ReactiveX/RxJava
@SuppressWarnings("unchecked")
@Test
public void testSubscriptionOnlyHappensOnce() throws InterruptedException {
final AtomicLong count = new AtomicLong();
Consumer<Subscription> incrementer = new Consumer<Subscription>() {
@Override
public void accept(Subscription s) {
count.incrementAndGet();
}
};
//this aync stream should emit first
Flowable<Integer> f1 = Flowable.just(1).doOnSubscribe(incrementer)
.delay(100, TimeUnit.MILLISECONDS).subscribeOn(Schedulers.computation());
//this stream emits second
Flowable<Integer> f2 = Flowable.just(1).doOnSubscribe(incrementer)
.delay(100, TimeUnit.MILLISECONDS).subscribeOn(Schedulers.computation());
TestSubscriber<Integer> ts = new TestSubscriber<Integer>();
Flowable.ambArray(f1, f2).subscribe(ts);
ts.request(1);
ts.awaitTerminalEvent(5, TimeUnit.SECONDS);
ts.assertNoErrors();
assertEquals(2, count.get());
}
代码示例来源:origin: ReactiveX/RxJava
@Test
public void dontSubscribeIfDone() {
List<Throwable> errors = TestHelper.trackPluginErrors();
try {
final int[] count = { 0 };
Flowable.combineLatest(Flowable.empty(),
Flowable.error(new TestException())
.doOnSubscribe(new Consumer<Subscription>() {
@Override
public void accept(Subscription s) throws Exception {
count[0]++;
}
}),
new BiFunction<Object, Object, Object>() {
@Override
public Object apply(Object a, Object b) throws Exception {
return 0;
}
})
.test()
.assertResult();
assertEquals(0, count[0]);
assertTrue(errors.toString(), errors.isEmpty());
} finally {
RxJavaPlugins.reset();
}
}
代码示例来源:origin: ReactiveX/RxJava
@Test
public void testCompleteTriggersSubscription() {
PublishProcessor<Object> other = PublishProcessor.create();
TestSubscriber<Integer> ts = new TestSubscriber<Integer>();
final AtomicInteger subscribed = new AtomicInteger();
Flowable.just(1)
.doOnSubscribe(new Consumer<Subscription>() {
@Override
public void accept(Subscription s) {
subscribed.getAndIncrement();
}
})
.delaySubscription(other)
.subscribe(ts);
ts.assertNotComplete();
ts.assertNoErrors();
ts.assertNoValues();
Assert.assertEquals("Premature subscription", 0, subscribed.get());
other.onComplete();
Assert.assertEquals("No subscription", 1, subscribed.get());
ts.assertValue(1);
ts.assertNoErrors();
ts.assertComplete();
}
代码示例来源:origin: ReactiveX/RxJava
@Test
public void testNoPrematureSubscription() {
PublishProcessor<Object> other = PublishProcessor.create();
TestSubscriber<Integer> ts = new TestSubscriber<Integer>();
final AtomicInteger subscribed = new AtomicInteger();
Flowable.just(1)
.doOnSubscribe(new Consumer<Subscription>() {
@Override
public void accept(Subscription s) {
subscribed.getAndIncrement();
}
})
.delaySubscription(other)
.subscribe(ts);
ts.assertNotComplete();
ts.assertNoErrors();
ts.assertNoValues();
Assert.assertEquals("Premature subscription", 0, subscribed.get());
other.onNext(1);
Assert.assertEquals("No subscription", 1, subscribed.get());
ts.assertValue(1);
ts.assertNoErrors();
ts.assertComplete();
}
代码示例来源:origin: ReactiveX/RxJava
@Test
public void testNoPrematureSubscriptionToError() {
PublishProcessor<Object> other = PublishProcessor.create();
TestSubscriber<Integer> ts = new TestSubscriber<Integer>();
final AtomicInteger subscribed = new AtomicInteger();
Flowable.<Integer>error(new TestException())
.doOnSubscribe(new Consumer<Subscription>() {
@Override
public void accept(Subscription s) {
subscribed.getAndIncrement();
}
})
.delaySubscription(other)
.subscribe(ts);
ts.assertNotComplete();
ts.assertNoErrors();
ts.assertNoValues();
Assert.assertEquals("Premature subscription", 0, subscribed.get());
other.onComplete();
Assert.assertEquals("No subscription", 1, subscribed.get());
ts.assertNoValues();
ts.assertNotComplete();
ts.assertError(TestException.class);
}
代码示例来源:origin: ReactiveX/RxJava
@Test
public void testNoSubscriptionIfOtherErrors() {
PublishProcessor<Object> other = PublishProcessor.create();
TestSubscriber<Integer> ts = new TestSubscriber<Integer>();
final AtomicInteger subscribed = new AtomicInteger();
Flowable.<Integer>error(new TestException())
.doOnSubscribe(new Consumer<Subscription>() {
@Override
public void accept(Subscription s) {
subscribed.getAndIncrement();
}
})
.delaySubscription(other)
.subscribe(ts);
ts.assertNotComplete();
ts.assertNoErrors();
ts.assertNoValues();
Assert.assertEquals("Premature subscription", 0, subscribed.get());
other.onError(new TestException());
Assert.assertEquals("Premature subscription", 0, subscribed.get());
ts.assertNoValues();
ts.assertNotComplete();
ts.assertError(TestException.class);
}
代码示例来源:origin: ReactiveX/RxJava
@Test
public void testNoMultipleSubscriptions() {
PublishProcessor<Object> other = PublishProcessor.create();
TestSubscriber<Integer> ts = new TestSubscriber<Integer>();
final AtomicInteger subscribed = new AtomicInteger();
Flowable.just(1)
.doOnSubscribe(new Consumer<Subscription>() {
@Override
public void accept(Subscription s) {
subscribed.getAndIncrement();
}
})
.delaySubscription(other)
.subscribe(ts);
ts.assertNotComplete();
ts.assertNoErrors();
ts.assertNoValues();
Assert.assertEquals("Premature subscription", 0, subscribed.get());
other.onNext(1);
other.onNext(2);
Assert.assertEquals("No subscription", 1, subscribed.get());
ts.assertValue(1);
ts.assertNoErrors();
ts.assertComplete();
}
代码示例来源:origin: ReactiveX/RxJava
@Test
public void byCount() {
final int[] subscriptions = { 0 };
Flowable<Integer> source = Flowable.range(1, 5)
.doOnSubscribe(new Consumer<Subscription>() {
@Override
public void accept(Subscription s) throws Exception {
subscriptions[0]++;
}
})
.publish()
.refCount(2);
for (int i = 0; i < 3; i++) {
TestSubscriber<Integer> ts1 = source.test();
ts1.assertEmpty();
TestSubscriber<Integer> ts2 = source.test();
ts1.assertResult(1, 2, 3, 4, 5);
ts2.assertResult(1, 2, 3, 4, 5);
}
assertEquals(3, subscriptions[0]);
}
内容来源于网络,如有侵权,请联系作者删除!