/
LambdaMonoSubscriberTest.java
324 lines (267 loc) · 11 KB
/
LambdaMonoSubscriberTest.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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
/*
* Copyright (c) 2016-2022 VMware Inc. or its affiliates, All Rights Reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package reactor.core.publisher;
import java.time.Duration;
import java.util.concurrent.atomic.AtomicLong;
import java.util.concurrent.atomic.AtomicReference;
import java.util.function.Consumer;
import org.assertj.core.api.Assertions;
import org.junit.jupiter.api.Test;
import org.reactivestreams.Subscription;
import reactor.core.Scannable;
import reactor.core.TestLoggerExtension;
import reactor.test.util.LoggerUtils;
import reactor.test.util.TestLogger;
import reactor.util.context.Context;
import reactor.util.context.ContextView;
import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
import static org.assertj.core.api.Assertions.fail;
public class LambdaMonoSubscriberTest {
@Test
public void initialContextIsVisibleToUpstream() {
AtomicReference<ContextView> contextRef = new AtomicReference<>();
Mono.deferContextual(Mono::just)
.doOnNext(contextRef::set)
.subscribe(null, null, null, Context.of("subscriber", "context"));
assertThat(contextRef.get())
.isNotNull()
.matches(c -> c.hasKey("subscriber"));
}
@Test
public void initialContextIsUsedForOnErrorDropped() {
AtomicReference<Throwable> droppedRef = new AtomicReference<>();
Context ctx = Context.of(Hooks.KEY_ON_ERROR_DROPPED, (Consumer<Throwable>) droppedRef::set);
IllegalStateException expectDropped = new IllegalStateException("boom2");
LambdaMonoSubscriber<Object> sub = new LambdaMonoSubscriber<>(null, e -> { }, null, null, ctx);
sub.onError(new IllegalStateException("boom1"));
//now trigger drop
sub.onError(expectDropped);
assertThat(droppedRef).hasValue(expectDropped);
}
@Test
public void consumeOnSubscriptionNotifiesError() {
AtomicReference<Throwable> errorHolder = new AtomicReference<>(null);
LambdaMonoSubscriber<String> tested = new LambdaMonoSubscriber<>(
value -> {},
errorHolder::set,
() -> {},
subscription -> { throw new IllegalArgumentException(); });
TestSubscription testSubscription = new TestSubscription();
//the error is expected to be propagated through onError
tested.onSubscribe(testSubscription);
assertThat(errorHolder.get()).as("onError").isInstanceOf(IllegalArgumentException.class);
assertThat(testSubscription.isCancelled).as("subscription should be cancelled").isTrue();
assertThat(testSubscription.requested).as("request").isEqualTo(-1L);
}
@Test
public void consumeOnSubscriptionThrowsFatal() {
AtomicReference<Throwable> errorHolder = new AtomicReference<>(null);
LambdaMonoSubscriber<String> tested = new LambdaMonoSubscriber<>(
value -> {},
errorHolder::set,
() -> {},
subscription -> { throw new OutOfMemoryError(); });
TestSubscription testSubscription = new TestSubscription();
//the error is expected to be thrown as it is fatal
try {
tested.onSubscribe(testSubscription);
fail("Expected OutOfMemoryError to be thrown");
}
catch (OutOfMemoryError e) {
//expected
}
assertThat(errorHolder.get()).as("onError").isNull();
assertThat(testSubscription.isCancelled)
.as("subscription should not be cancelled due to fatal exception")
.isFalse();
assertThat(testSubscription.requested).as("unexpected request").isEqualTo(-1L);
}
@Test
public void consumeOnSubscriptionReceivesSubscriptionAndRequests32() {
AtomicReference<Throwable> errorHolder = new AtomicReference<>(null);
AtomicReference<Subscription> subscriptionHolder = new AtomicReference<>(null);
LambdaMonoSubscriber<String> tested = new LambdaMonoSubscriber<>(
value -> {},
errorHolder::set,
() -> { },
s -> {
subscriptionHolder.set(s);
s.request(32);
});
TestSubscription testSubscription = new TestSubscription();
tested.onSubscribe(testSubscription);
assertThat(errorHolder.get()).as("onError").isNull();
assertThat(testSubscription.isCancelled).as("subscription should not be cancelled").isFalse();
assertThat(subscriptionHolder.get()).as("subscription should be consumed").isEqualTo(testSubscription);
assertThat(testSubscription.requested).as("subscription should be requested").isEqualTo(32L);
}
@Test
public void noSubscriptionConsumerTriggersRequestOfMax() {
AtomicReference<Throwable> errorHolder = new AtomicReference<>(null);
LambdaMonoSubscriber<String> tested = new LambdaMonoSubscriber<>(
value -> {},
errorHolder::set,
() -> {},
null); //defaults to initial request of max
TestSubscription testSubscription = new TestSubscription();
tested.onSubscribe(testSubscription);
assertThat(errorHolder.get()).as("onError").isNull();
assertThat(testSubscription.isCancelled).as("subscription should not be cancelled").isFalse();
assertThat(testSubscription.requested)
.as("should request max")
.isNotEqualTo(-1L)
.isEqualTo(Long.MAX_VALUE);
}
@Test
public void onNextConsumerExceptionTriggersCancellation() {
AtomicReference<Throwable> errorHolder = new AtomicReference<>(null);
LambdaMonoSubscriber<String> tested = new LambdaMonoSubscriber<>(
value -> { throw new IllegalArgumentException(); },
errorHolder::set,
() -> {},
null);
TestSubscription testSubscription = new TestSubscription();
tested.onSubscribe(testSubscription);
//the error is expected to be propagated through doError
tested.onNext("foo");
assertThat(errorHolder.get()).as("onError").isInstanceOf(IllegalArgumentException.class);
assertThat(testSubscription.isCancelled).as("subscription isCancelled").isTrue();
}
@Test
@TestLoggerExtension.Redirect
void onNextConsumerExceptionNonFatalTriggersCancellation(TestLogger testLogger) {
LambdaMonoSubscriber<String> tested = new LambdaMonoSubscriber<>(
value -> { throw new IllegalArgumentException(); },
null, //no errorConsumer so that we use onErrorDropped
() -> { }, null);
TestSubscription testSubscription = new TestSubscription();
tested.onSubscribe(testSubscription);
//as Mono is single-value, it cancels early on onNext. this leads to an exception
//during onNext to be bubbled up as a BubbledException, not propagated through onNext
tested.onNext("foo");
Assertions.assertThat(testLogger.getErrContent())
.contains("Operator called default onErrorDropped")
.contains("IllegalArgumentException");
assertThat(testSubscription.isCancelled).as("subscription isCancelled")
.isTrue();
}
@Test
@TestLoggerExtension.Redirect
void onNextConsumerFatalDoesntTriggerCancellation(TestLogger testLogger) {
LambdaMonoSubscriber<String> tested = new LambdaMonoSubscriber<>(
value -> { throw new OutOfMemoryError(); },
null, //no errorConsumer so that we use onErrorDropped
() -> { }, null);
TestSubscription testSubscription = new TestSubscription();
tested.onSubscribe(testSubscription);
//the error is expected to be thrown as it is fatal, so it doesn't go through onErrorDropped. However, throwIfJvmFatal now logs it.
assertThatExceptionOfType(OutOfMemoryError.class).isThrownBy(() -> tested.onNext("foo"));
Assertions.assertThat(testLogger.getErrContent()).startsWith("[ WARN] throwIfFatal detected a jvm fatal exception, which is thrown and logged below: - java.lang.OutOfMemoryError");
assertThat(testSubscription.isCancelled).as("subscription isCancelled")
.isFalse();
}
@Test
public void emptyMonoState(){
assertThat(Mono.fromDirect(s -> {
assertThat(s).isInstanceOf(LambdaMonoSubscriber.class);
LambdaMonoSubscriber<?> bfs = (LambdaMonoSubscriber<?>) s;
assertThat(bfs.scan(Scannable.Attr.PREFETCH) == Integer.MAX_VALUE).isTrue();
assertThat(bfs.scan(Scannable.Attr.TERMINATED)).isFalse();
bfs.onSubscribe(Operators.emptySubscription());
bfs.onSubscribe(Operators.emptySubscription()); // noop
s.onComplete();
assertThat(bfs.scan(Scannable.Attr.TERMINATED)).isTrue();
bfs.dispose();
bfs.dispose();
}).subscribe(s -> {}, null, () -> {}).isDisposed()).isTrue();
assertThat(Mono.never().subscribe(null, null, () -> {}).isDisposed()).isFalse();
}
@Test
public void errorMonoState(){
Hooks.onErrorDropped(e -> assertThat(e).hasMessage("test2"));
Hooks.onNextDropped(d -> assertThat(d).isEqualTo("test2"));
Mono.fromDirect(s -> {
assertThat(s).isInstanceOf(LambdaMonoSubscriber.class);
LambdaMonoSubscriber<?> bfs = (LambdaMonoSubscriber<?>) s;
Operators.error(s, new Exception("test"));
s.onComplete();
s.onError(new Exception("test2"));
s.onNext("test2");
assertThat(bfs.scan(Scannable.Attr.TERMINATED)).isTrue();
bfs.dispose();
})
.subscribe(s -> {
}, e -> {
}, () -> {
});
}
@Test
public void completeHookErrorDropped() {
Hooks.onErrorDropped(e -> assertThat(e).hasMessage("complete"));
Mono.just("foo")
.subscribe(v -> {},
e -> {},
() -> { throw new IllegalStateException("complete");});
}
@Test
@TestLoggerExtension.Redirect
void noErrorHookThrowsCallbackNotImplemented(TestLogger testLogger) {
RuntimeException boom = new IllegalArgumentException("boom");
Mono.error(boom)
.subscribe(v -> {
});
assertThat(testLogger.getErrContent())
.contains("Operator called default onErrorDropped")
.contains(
"reactor.core.Exceptions$ErrorCallbackNotImplemented: java.lang.IllegalArgumentException: boom");
}
@Test
public void testCancel() {
AtomicLong cancelCount = new AtomicLong();
Mono.delay(Duration.ofMillis(500))
.doOnCancel(cancelCount::incrementAndGet)
.subscribe(v -> {})
.dispose();
assertThat(cancelCount).hasValue(1);
}
@Test
public void scan() {
LambdaMonoSubscriber<String> test = new LambdaMonoSubscriber<>(null, null, null, null);
Subscription parent = Operators.emptySubscription();
test.onSubscribe(parent);
assertThat(test.scan(Scannable.Attr.PARENT)).isSameAs(parent);
assertThat(test.scan(Scannable.Attr.PREFETCH)).isEqualTo(Integer.MAX_VALUE);
assertThat(test.scan(Scannable.Attr.RUN_STYLE)).isSameAs(Scannable.Attr.RunStyle.SYNC);
assertThat(test.scan(Scannable.Attr.TERMINATED)).isFalse();
assertThat(test.scan(Scannable.Attr.CANCELLED)).isFalse();
test.dispose();
assertThat(test.scan(Scannable.Attr.TERMINATED)).isTrue();
assertThat(test.scan(Scannable.Attr.CANCELLED)).isTrue();
}
private static class TestSubscription implements Subscription {
volatile boolean isCancelled = false;
volatile long requested = -1L;
@Override
public void request(long n) {
this.requested = n;
}
@Override
public void cancel() {
this.isCancelled = true;
}
}
}