Debian Codegraph @

src/test/java/io/reactivex/rxjava3/internal/subscribers/BlockingSubscriberTest.java

raw
1 /*
2 * Copyright (c) 2016-present, RxJava Contributors.
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in
5 * compliance with the License. You may obtain a copy of the License at
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software distributed under the License is
10 * distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See
11 * the License for the specific language governing permissions and limitations under the License.
12 */
13
14 package io.reactivex.rxjava3.internal.subscribers;
15
16 import static org.junit.Assert.*;
17
18 import java.util.ArrayDeque;
19 import java.util.concurrent.atomic.AtomicBoolean;
20
21 import org.junit.Test;
22 import org.reactivestreams.Subscription;
23
24 import io.reactivex.rxjava3.core.RxJavaTest;
25 import io.reactivex.rxjava3.internal.subscriptions.BooleanSubscription;
26 import io.reactivex.rxjava3.testsupport.TestHelper;
27
28 public class BlockingSubscriberTest extends RxJavaTest {
29
30 @Test
31 public void doubleOnSubscribe() {
32 TestHelper.doubleOnSubscribe(new BlockingSubscriber<Integer>(new ArrayDeque<>()));
33 }
34
35 @Test
36 public void cancel() {
37 BlockingSubscriber<Integer> bq = new BlockingSubscriber<>(new ArrayDeque<>());
38
39 assertFalse(bq.isCancelled());
40
41 bq.cancel();
42
43 assertTrue(bq.isCancelled());
44
45 bq.cancel();
46
47 assertTrue(bq.isCancelled());
48 }
49
50 @Test
51 public void blockingFirstDoubleOnSubscribe() {
52 TestHelper.doubleOnSubscribe(new BlockingFirstSubscriber<Integer>());
53 }
54
55 @Test
56 public void blockingFirstTimeout() {
57 BlockingFirstSubscriber<Integer> bf = new BlockingFirstSubscriber<>();
58
59 Thread.currentThread().interrupt();
60
61 try {
62 bf.blockingGet();
63 fail("Should have thrown!");
64 } catch (RuntimeException ex) {
65 assertTrue(ex.toString(), ex.getCause() instanceof InterruptedException);
66 }
67 }
68
69 @Test
70 public void blockingFirstTimeout2() {
71 BlockingFirstSubscriber<Integer> bf = new BlockingFirstSubscriber<>();
72
73 bf.onSubscribe(new BooleanSubscription());
74
75 Thread.currentThread().interrupt();
76
77 try {
78 bf.blockingGet();
79 fail("Should have thrown!");
80 } catch (RuntimeException ex) {
81 assertTrue(ex.toString(), ex.getCause() instanceof InterruptedException);
82 }
83 }
84
85 @Test
86 public void cancelOnRequest() {
87
88 final BlockingFirstSubscriber<Integer> bf = new BlockingFirstSubscriber<>();
89
90 final AtomicBoolean b = new AtomicBoolean();
91
92 Subscription s = new Subscription() {
93 @Override
94 public void request(long n) {
95 bf.cancelled = true;
96 }
97
98 @Override
99 public void cancel() {
100 b.set(true);
101 }
102 };
103
104 bf.onSubscribe(s);
105
106 assertTrue(b.get());
107 }
108
109 @Test
110 public void cancelUpfront() {
111
112 final BlockingFirstSubscriber<Integer> bf = new BlockingFirstSubscriber<>();
113
114 final AtomicBoolean b = new AtomicBoolean();
115
116 bf.cancelled = true;
117
118 Subscription s = new Subscription() {
119 @Override
120 public void request(long n) {
121 b.set(true);
122 }
123
124 @Override
125 public void cancel() {
126 }
127 };
128
129 bf.onSubscribe(s);
130
131 assertFalse(b.get());
132 }
133 }