-
Notifications
You must be signed in to change notification settings - Fork 28
/
Copy pathSubscriberBlackboxVerification.cs
470 lines (385 loc) · 19.5 KB
/
SubscriberBlackboxVerification.cs
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
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
using System;
using System.Diagnostics;
using System.Linq;
using Xunit;
using Xunit.Abstractions;
using Reactive.Streams.TCK.Support;
namespace Reactive.Streams.TCK
{
/// <summary>
/// Provides tests for verifying <see cref="ISubscriber{T}"/> and <see cref="ISubscription"/>
/// specification rules, without any modifications to the tested implementation (also known as "Black Box" testing).
///
/// This verification is NOT able to check many of the rules of the spec, and if you want more
/// verification of your implementation you'll have to implement <see cref="SubscriberWhiteboxVerification{T}"/>
/// instead.
/// </summary>
public abstract class SubscriberBlackboxVerification<T> : WithHelperPublisher<T>,
ISubscriberBlackboxVerificationRules
{
protected readonly TestEnvironment Environment;
protected SubscriberBlackboxVerification(TestEnvironment environment)
{
Environment = environment;
Setup();
}
// USER API
/// <summary>
/// This is the main method you must implement in your test incarnation.
/// It must create a new <see cref="ISubscriber{T}"/> instance to be subjected to the testing logic.
/// </summary>
public abstract ISubscriber<T> CreateSubscriber();
/// <summary>
/// Override this method if the Subscriber implementation you are verifying
/// needs an external signal before it signals demand to its Publisher.
///
/// By default this method does nothing.
/// </summary>
public virtual void TriggerRequest(ISubscriber<T> subscriber)
{
}
////////////////////// TEST ENV CLEANUP /////////////////////////////////////
public void Setup() => Environment.ClearAsyncErrors();
////////////////////// SPEC RULE VERIFICATION ///////////////////////////////
// Verifies rule: https://github.com/reactive-streams/reactive-streams-jvm#2.1
[SkippableFact]
public void Required_spec201_blackbox_mustSignalDemandViaSubscriptionRequest()
=> BlackboxSubscriberTest(stage =>
{
TriggerRequest(stage.SubscriberProxy.Sub);
var n = stage.ExpectRequest(); // assuming subscriber wants to consume elements...
// should cope with up to requested number of elements
for (var i = 0; i < n; i++)
stage.SignalNext();
});
// Verifies rule: https://github.com/reactive-streams/reactive-streams-jvm#2.2
[SkippableFact]
public void Untested_spec202_blackbox_shouldAsynchronouslyDispatch()
=> NotVerified(); // cannot be meaningfully tested, or can it?
// Verifies rule: https://github.com/reactive-streams/reactive-streams-jvm#2.3
[SkippableFact]
public void Required_spec203_blackbox_mustNotCallMethodsOnSubscriptionOrPublisherInOnComplete()
=> BlackboxSubscriberWithoutSetupTest(stage =>
{
var subscriber = CreateSubscriber();
var subscription = new Spec203Subscription(Environment, "OnComplete");
subscriber.OnSubscribe(subscription);
subscriber.OnComplete();
Environment.VerifyNoAsyncErrorsNoDelay();
});
// Verifies rule: https://github.com/reactive-streams/reactive-streams-jvm#2.3
[SkippableFact]
public void Required_spec203_blackbox_mustNotCallMethodsOnSubscriptionOrPublisherInOnError()
=> BlackboxSubscriberWithoutSetupTest(stage =>
{
var subscriber = CreateSubscriber();
var subscription = new Spec203Subscription(Environment, "OnError");
subscriber.OnSubscribe(subscription);
subscriber.OnError(new TestException());
Environment.VerifyNoAsyncErrorsNoDelay();
});
private sealed class Spec203Subscription : ISubscription
{
private readonly TestEnvironment _environment;
private readonly string _method;
public Spec203Subscription(TestEnvironment environment, string method)
{
_environment = environment;
_method = method;
}
public void Request(long n)
{
var stack = new StackTrace();
var stackFrames = stack.GetFrames();
if (stackFrames != null && stackFrames.Any(f => f.GetMethod().Name.Equals(_method)))
_environment.Flop($"Subscription.Request MUST NOT be called from Subscriber.{_method} (Rule 2.3)!" +
$"Caller: {stack}");
}
public void Cancel()
{
var stack = new StackTrace();
var stackFrames = stack.GetFrames();
if (stackFrames != null && stackFrames.Any(f => f.GetMethod().Name.Equals(_method)))
_environment.Flop($"Subscription.Cancel MUST NOT be called from Subscriber.{_method} (Rule 2.3)!" +
$"Caller: {stack}");
}
}
// Verifies rule: https://github.com/reactive-streams/reactive-streams-jvm#2.4
[SkippableFact]
public void Untested_spec204_blackbox_mustConsiderTheSubscriptionAsCancelledInAfterRecievingOnCompleteOrOnError()
=> NotVerified(); // cannot be meaningfully tested, or can it?
// Verifies rule: https://github.com/reactive-streams/reactive-streams-jvm#2.5
[SkippableFact]
public void
Required_spec205_blackbox_mustCallSubscriptionCancelIfItAlreadyHasAnSubscriptionAndReceivesAnotherOnSubscribeSignal
()
{
var stage = new BlackBoxTestStage<T>(Environment, this);
// try to subscribe another time, if the subscriber calls `probe.RegisterOnSubscribe` the test will fail
var secondSubscriptionCancelled = new Latch(Environment);
stage.Sub.OnSubscribe(new Spec205Subscription(Environment, secondSubscriptionCancelled, stage.Sub));
secondSubscriptionCancelled.ExpectClose(
"Expected SecondSubscription given to subscriber to be cancelled, but `Subscription.cancel()` was not called.");
Environment.VerifyNoAsyncErrorsNoDelay();
}
private sealed class Spec205Subscription : ISubscription
{
private readonly TestEnvironment _environment;
private readonly Latch _secondSubscriptionCancelled;
private readonly ISubscriber<T> _subscriber;
public Spec205Subscription(TestEnvironment environment, Latch secondSubscriptionCancelled,
ISubscriber<T> subscriber)
{
_environment = environment;
_secondSubscriptionCancelled = secondSubscriptionCancelled;
_subscriber = subscriber;
}
public void Request(long n)
=> _environment.Flop($"Subscriber {_subscriber} illegally called `Subscription.Request({n})`!");
public void Cancel() => _secondSubscriptionCancelled.Close();
public override string ToString() => "SecondSubscription(should get cancelled)";
}
// Verifies rule: https://github.com/reactive-streams/reactive-streams-jvm#2.6
[SkippableFact]
public void Untested_spec206_blackbox_mustCallSubscriptionCancelIfItIsNoLongerValid()
=> NotVerified(); // cannot be meaningfully tested, or can it?
// Verifies rule: https://github.com/reactive-streams/reactive-streams-jvm#2.7
[SkippableFact]
public void
Untested_spec207_blackbox_mustEnsureAllCallsOnItsSubscriptionTakePlaceFromTheSameThreadOrTakeCareOfSynchronization
()
=> NotVerified(); // cannot be meaningfully tested, or can it?
// the same thread part of the clause can be verified but that is not very useful, or is it?
// Verifies rule: https://github.com/reactive-streams/reactive-streams-jvm#2.8
[SkippableFact]
public void Untested_spec208_blackbox_mustBePreparedToReceiveOnNextSignalsAfterHavingCalledSubscriptionCancel()
=> NotVerified(); // cannot be meaningfully tested, or can it?
// Verifies rule: https://github.com/reactive-streams/reactive-streams-jvm#2.9
[SkippableFact]
public void Required_spec209_blackbox_mustBePreparedToReceiveAnOnCompleteSignalWithPrecedingRequestCall()
=> BlackboxSubscriberWithoutSetupTest(stage =>
{
var publisher = new Spec209WithPublisher();
var subscriber = CreateSubscriber();
var probe = stage.CreateBlackboxSubscriberProxy(Environment, subscriber);
publisher.Subscribe(probe);
TriggerRequest(subscriber);
probe.ExpectCompletion();
probe.ExpectNone();
Environment.VerifyNoAsyncErrorsNoDelay();
});
private sealed class Spec209WithPublisher : IPublisher<T>
{
private sealed class Subscription : ISubscription
{
private readonly Spec209WithPublisher _publisher;
private bool _completed;
public Subscription(Spec209WithPublisher publisher)
{
_publisher = publisher;
}
public void Request(long n)
{
if (!_completed)
{
_completed = true;
_publisher._subscriber.OnComplete();
// Publisher now realises that it is in fact already completed
}
}
public void Cancel()
{
// noop, ignore
}
}
private ISubscriber<T> _subscriber;
public void Subscribe(ISubscriber<T> subscriber)
{
_subscriber = subscriber;
subscriber.OnSubscribe(new Subscription(this));
}
}
// Verifies rule: https://github.com/reactive-streams/reactive-streams-jvm#2.9
[SkippableFact]
public void Required_spec209_blackbox_mustBePreparedToReceiveAnOnCompleteSignalWithoutPrecedingRequestCall()
=> BlackboxSubscriberWithoutSetupTest(stage =>
{
var publisher = new Spec209WithoutPublisher();
var subscriber = CreateSubscriber();
var probe = stage.CreateBlackboxSubscriberProxy(Environment, subscriber);
publisher.Subscribe(probe);
probe.ExpectCompletion();
Environment.VerifyNoAsyncErrorsNoDelay();
});
private sealed class Spec209WithoutPublisher : IPublisher<T>
{
public void Subscribe(ISubscriber<T> subscriber) => subscriber.OnComplete();
}
// Verifies rule: https://github.com/reactive-streams/reactive-streams-jvm#2.10
[SkippableFact]
public void Required_spec210_blackbox_mustBePreparedToReceiveAnOnErrorSignalWithPrecedingRequestCall()
=> BlackboxSubscriberTest(stage =>
{
stage.Sub.OnError(new TestException());
stage.SubscriberProxy.ExpectError<TestException>();
});
// Verifies rule: https://github.com/reactive-streams/reactive-streams-jvm#2.11
[SkippableFact]
public void
Untested_spec211_blackbox_mustMakeSureThatAllCallsOnItsMethodsHappenBeforeTheProcessingOfTheRespectiveEvents
()
=> NotVerified(); // cannot be meaningfully tested, or can it?
// Verifies rule: https://github.com/reactive-streams/reactive-streams-jvm#2.12
[SkippableFact]
public void Untested_spec212_blackbox_mustNotCallOnSubscribeMoreThanOnceBasedOnObjectEquality()
=> NotVerified(); // cannot be meaningfully tested, or can it?
// Verifies rule: https://github.com/reactive-streams/reactive-streams-jvm#2.13
[SkippableFact]
public void Untested_spec213_blackbox_failingOnSignalInvocation()
=> NotVerified(); // cannot be meaningfully tested, or can it?
// Verifies rule: https://github.com/reactive-streams/reactive-streams-jvm#2.13
[SkippableFact]
public void Required_spec213_blackbox_onSubscribe_mustThrowNullPointerExceptionWhenParametersAreNull()
=> BlackboxSubscriberWithoutSetupTest(stage =>
{
var subscriber = CreateSubscriber();
var gotNpe = false;
subscriber.OnSubscribe(new Spec213DummySubscription());
try
{
subscriber.OnSubscribe(null);
}
catch (ArgumentNullException)
{
gotNpe = true;
}
Assert.True(gotNpe, "OnSubscribe(null) did not throw ArgumentNullException");
Environment.VerifyNoAsyncErrorsNoDelay();
});
// Verifies rule: https://github.com/reactive-streams/reactive-streams-jvm#2.13
[SkippableFact]
public void Required_spec213_blackbox_onNext_mustThrowNullPointerExceptionWhenParametersAreNull()
=> BlackboxSubscriberWithoutSetupTest(stage =>
{
var element = default(T);
if(element != null)
throw new SkipException("Can't verify behavior for value types");
var subscriber = CreateSubscriber();
var gotNpe = false;
subscriber.OnSubscribe(new Spec213DummySubscription());
try
{
subscriber.OnNext(element);
}
catch (ArgumentNullException)
{
gotNpe = true;
}
Assert.True(gotNpe, "OnNext(null) did not throw ArgumentNullException");
Environment.VerifyNoAsyncErrorsNoDelay();
});
// Verifies rule: https://github.com/reactive-streams/reactive-streams-jvm#2.13
[SkippableFact]
public void Required_spec213_blackbox_onError_mustThrowNullPointerExceptionWhenParametersAreNull()
=> BlackboxSubscriberWithoutSetupTest(stage =>
{
var subscriber = CreateSubscriber();
var gotNpe = false;
subscriber.OnSubscribe(new Spec213DummySubscription());
try
{
subscriber.OnError(null);
}
catch (ArgumentNullException)
{
gotNpe = true;
}
Assert.True(gotNpe, "OnError(null) did not throw ArgumentNullException");
Environment.VerifyNoAsyncErrorsNoDelay();
});
private sealed class Spec213DummySubscription : ISubscription
{
public void Request(long n)
{
}
public void Cancel()
{
}
}
////////////////////// SUBSCRIPTION SPEC RULE VERIFICATION //////////////////
// Verifies rule: https://github.com/reactive-streams/reactive-streams-jvm#3.1
[SkippableFact]
public void Untested_spec301_blackbox_mustNotBeCalledOutsideSubscriberContext()
=> NotVerified(); // cannot be meaningfully tested, or can it?
// Verifies rule: https://github.com/reactive-streams/reactive-streams-jvm#3.8
[SkippableFact]
public void Untested_spec308_blackbox_requestMustRegisterGivenNumberElementsToBeProduced()
=> NotVerified(); // cannot be meaningfully tested, or can it?
// Verifies rule: https://github.com/reactive-streams/reactive-streams-jvm#3.10
[SkippableFact]
public void Untested_spec310_blackbox_requestMaySynchronouslyCallOnNextOnSubscriber()
=> NotVerified(); // cannot be meaningfully tested, or can it?
// Verifies rule: https://github.com/reactive-streams/reactive-streams-jvm#3.11
[SkippableFact]
public void Untested_spec311_blackbox_requestMaySynchronouslyCallOnCompleteOrOnError()
=> NotVerified(); // cannot be meaningfully tested, or can it?
// Verifies rule: https://github.com/reactive-streams/reactive-streams-jvm#3.14
[SkippableFact]
public void Untested_spec314_blackbox_cancelMayCauseThePublisherToShutdownIfNoOtherSubscriptionExists()
=> NotVerified(); // cannot be meaningfully tested, or can it?
// Verifies rule: https://github.com/reactive-streams/reactive-streams-jvm#3.15
[SkippableFact]
public void Untested_spec315_blackbox_cancelMustNotThrowExceptionAndMustSignalOnError()
=> NotVerified(); // cannot be meaningfully tested, or can it?
// Verifies rule: https://github.com/reactive-streams/reactive-streams-jvm#3.16
[SkippableFact]
public void Untested_spec316_blackbox_requestMustNotThrowExceptionAndMustOnErrorTheSubscriber()
=> NotVerified(); // cannot be meaningfully tested, or can it?
/////////////////////// ADDITIONAL "COROLLARY" TESTS ////////////////////////
/////////////////////// TEST INFRASTRUCTURE /////////////////////////////////
public void BlackboxSubscriberTest(Action<BlackBoxTestStage<T>> body)
=> body(new BlackBoxTestStage<T>(Environment, this));
public void BlackboxSubscriberWithoutSetupTest(Action<BlackBoxTestStage<T>> body)
=> body(new BlackBoxTestStage<T>(Environment, this, false));
public void NotVerified() => NotVerified("Not verified using this TCK.");
public void NotVerified(string message) => Assert.Ignore(message);
}
public class BlackBoxTestStage<T> : ManualPublisher<T>
{
private readonly WithHelperPublisher<T> _verification;
public BlackBoxTestStage(TestEnvironment environment, SubscriberBlackboxVerification<T> verification, bool runDefaultInit = true) : base(environment)
{
_verification = verification;
if (runDefaultInit)
{
Publisher = CreateHelperPublisher(long.MaxValue);
Tees = Environment.NewManualSubscriber(Publisher);
var subscriber = verification.CreateSubscriber();
SubscriberProxy = CreateBlackboxSubscriberProxy(Environment, subscriber);
Subscribe(SubscriberProxy);
}
}
public IPublisher<T> Publisher { get; set; }
public ManualSubscriber<T> Tees { get; set; } // gives us access to a stream T values
public T LastT { get; private set; }
/// <summary>
/// Proxy for the <see cref="Sub"/> Subscriber, providing certain assertions on methods being called on the Subscriber.
/// </summary>
public BlackboxSubscriberProxy<T> SubscriberProxy { get; set; }
public ISubscriber<T> Sub => Subscriber.Value;
public IPublisher<T> CreateHelperPublisher(long elements) => _verification.CreateHelperPublisher(elements);
public BlackboxSubscriberProxy<T> CreateBlackboxSubscriberProxy(TestEnvironment environment, ISubscriber<T> subscriber)
=> new BlackboxSubscriberProxy<T>(environment, subscriber);
public T SignalNext()
{
var element = NextT();
SendNext(element);
return element;
}
public T NextT()
{
LastT = Tees.RequestNextElement();
return LastT;
}
}
}