-
Notifications
You must be signed in to change notification settings - Fork 28
/
Copy pathAsyncSubscriberTest.cs
62 lines (53 loc) · 1.85 KB
/
AsyncSubscriberTest.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
using System;
using System.Threading;
using NUnit.Framework;
using Reactive.Streams.TCK;
using Reactive.Streams.TCK.Support;
namespace Reactive.Streams.Example.Unicast.Tests
{
[TestFixture]
public class AsyncSubscriberTest : SubscriberBlackboxVerification<int?>
{
public AsyncSubscriberTest() : base(new TestEnvironment())
{
}
public override int? CreateElement(int element) => element;
public override ISubscriber<int?> CreateSubscriber() => new Suscriber();
private sealed class Suscriber : AsyncSubscriber<int?>
{
protected override bool WhenNext(int? element) => true;
}
[Test]
public void TestAccumulation()
{
var i = new AtomicCounterLong(0);
var latch = new CountdownEvent(1);
var subscriber = new AccSubscriber(i, latch);
new NumberIterablePublisher(0,10).Subscribe(subscriber);
latch.Wait(TimeSpan.FromMilliseconds(Environment.DefaultTimeoutMilliseconds*10));
Assert.AreEqual(45, i.Current);
}
private sealed class AccSubscriber : AsyncSubscriber<int?>
{
private readonly AtomicCounterLong _counter;
private readonly CountdownEvent _latch;
private long _acc;
public AccSubscriber(AtomicCounterLong counter, CountdownEvent latch)
{
_counter = counter;
_latch = latch;
}
protected override bool WhenNext(int? element)
{
// no need for null check, OnNext handles this case
_acc += element.Value;
return true;
}
protected override void WhenComplete()
{
_counter.GetAndAdd(_acc);
_latch.Signal();
}
}
}
}