-
Notifications
You must be signed in to change notification settings - Fork 123
/
UnicastProcessorTest.java
156 lines (128 loc) · 5.42 KB
/
UnicastProcessorTest.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
package io.smallrye.mutiny.operators.multi.processors;
import static org.assertj.core.api.Assertions.assertThat;
import static org.awaitility.Awaitility.await;
import static org.mockito.Mockito.mock;
import java.io.IOException;
import java.util.Queue;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.ForkJoinPool;
import java.util.concurrent.atomic.AtomicInteger;
import org.junit.jupiter.api.RepeatedTest;
import org.junit.jupiter.api.Test;
import org.reactivestreams.Subscription;
import io.smallrye.mutiny.helpers.queues.Queues;
import io.smallrye.mutiny.helpers.test.AssertSubscriber;
import io.smallrye.mutiny.subscription.BackPressureFailure;
public class UnicastProcessorTest {
@Test
public void testTheProcessorCanGetOnlyOneSubscriber() {
UnicastProcessor<Integer> processor = UnicastProcessor.create();
processor.subscribe()
.withSubscriber(AssertSubscriber.create());
AssertSubscriber<Integer> second = processor.subscribe()
.withSubscriber(AssertSubscriber.create());
second.assertHasNotReceivedAnyItem()
.assertFailedWith(IllegalStateException.class, null);
}
@RepeatedTest(100)
public void testWithMultithreadedUpstream() {
UnicastProcessor<String> processor = UnicastProcessor.create();
ExecutorService executor = Executors.newFixedThreadPool(5);
for (int i = 0; i < 5; i++) {
int t = i;
Runnable produce = () -> {
for (int j = 0; j < 1000; j++) {
processor.onNext(t + "-" + j);
}
};
executor.submit(produce);
}
AssertSubscriber<Object> subscriber = AssertSubscriber.create(Long.MAX_VALUE);
processor.subscribe(subscriber);
await().until(() -> subscriber.getItems().size() == 5 * 1000);
processor.onComplete();
for (int i = 0; i < 5; i++) {
for (int j = 0; j < 1000; j++) {
assertThat(subscriber.getItems()).contains(i + "-" + j);
}
}
executor.shutdownNow();
}
@Test
public void testWithImmediateCancellationFromDownstream() {
UnicastProcessor<String> processor = UnicastProcessor.create();
AssertSubscriber<String> subscriber = processor
.subscribe().withSubscriber(new AssertSubscriber<>(50, true));
processor.onNext("a");
processor.onNext("b");
processor.onComplete();
subscriber.assertNotTerminated()
.assertSubscribed()
.assertHasNotReceivedAnyItem();
processor = UnicastProcessor.create();
subscriber = processor
.subscribe().withSubscriber(new AssertSubscriber<>(50, true));
processor.onNext("a");
processor.onNext("b");
processor.onError(new IOException("boom"));
subscriber.assertNotTerminated()
.assertSubscribed()
.assertHasNotReceivedAnyItem();
}
@Test
public void testWithImmediateCancellationFromDownstreamWhileWaitingForUpstreamSubscription() {
UnicastProcessor<String> processor = UnicastProcessor.create();
AssertSubscriber<String> subscriber = processor
.subscribe().withSubscriber(new AssertSubscriber<>(50, true));
processor.onSubscribe(mock(Subscription.class));
processor.onNext("a");
processor.onNext("b");
processor.onComplete();
subscriber.assertNotTerminated()
.assertSubscribed()
.assertHasNotReceivedAnyItem();
}
@RepeatedTest(10)
public void testWithConcurrentCancellations() {
AtomicInteger counter = new AtomicInteger();
Queue<Integer> queue = Queues.<Integer> get(1).get();
UnicastProcessor<Integer> processor = UnicastProcessor.create(queue, counter::incrementAndGet);
AssertSubscriber<Integer> sub = processor.subscribe().withSubscriber(AssertSubscriber.create(Long.MAX_VALUE));
ForkJoinPool pool = ForkJoinPool.commonPool();
CountDownLatch start = new CountDownLatch(10);
CountDownLatch done = new CountDownLatch(10);
for (int i = 0; i < 10; i++) {
pool.execute(() -> {
start.countDown();
try {
start.await();
} catch (InterruptedException e) {
e.printStackTrace();
}
processor.cancel();
done.countDown();
});
}
await().until(() -> done.getCount() == 0);
sub.assertSubscribed().assertHasNotReceivedAnyItem().assertNotTerminated();
assertThat(counter).hasValue(1);
}
@Test
public void testOverflow() {
Queue<Integer> queue = Queues.<Integer> get(1).get();
UnicastProcessor<Integer> processor = UnicastProcessor.create(queue, null);
AssertSubscriber<Integer> subscriber = processor.subscribe()
.withSubscriber(AssertSubscriber.create(2));
processor.onNext(1);
processor.onNext(2);
processor.onNext(3);
processor.onNext(4);
subscriber.assertSubscribed()
.assertItems(1, 2)
// The overflow is only propagated on the next request.
.request(2)
.assertFailedWith(BackPressureFailure.class, "");
}
}