|
| 1 | +using Microsoft.Extensions.Time.Testing; |
| 2 | +using R3Async.Subjects; |
| 3 | +using Shouldly; |
| 4 | +#pragma warning disable CS1998 |
| 5 | + |
| 6 | +namespace R3Async.Tests.Operators; |
| 7 | + |
| 8 | +public class ThrottleFirstLastTest |
| 9 | +{ |
| 10 | + static readonly TimeSpan DueTime = TimeSpan.FromMilliseconds(100); |
| 11 | + |
| 12 | + [Fact] |
| 13 | + public async Task ThrottleFirstLast_EmitsFirstValueThenLastValueOfWindow() |
| 14 | + { |
| 15 | + var timeProvider = new FakeTimeProvider(); |
| 16 | + var subject = Subject.Create<int>(); |
| 17 | + |
| 18 | + var results = new List<int>(); |
| 19 | + var itemAdded = new SemaphoreSlim(0); |
| 20 | + |
| 21 | + await using var subscription = await subject.Values.ThrottleFirstLast(DueTime, timeProvider).SubscribeAsync( |
| 22 | + async (x, token) => { lock (results) results.Add(x); itemAdded.Release(); }, CancellationToken.None); |
| 23 | + |
| 24 | + // The first value of the window is emitted immediately. |
| 25 | + await subject.OnNextAsync(1, CancellationToken.None); |
| 26 | + await itemAdded.WaitAsync(); |
| 27 | + lock (results) results.ShouldBe(new[] { 1 }); |
| 28 | + |
| 29 | + // Values arriving inside the window are not emitted yet; only the latest is kept. |
| 30 | + timeProvider.Advance(TimeSpan.FromMilliseconds(50)); |
| 31 | + await subject.OnNextAsync(2, CancellationToken.None); |
| 32 | + await subject.OnNextAsync(3, CancellationToken.None); |
| 33 | + lock (results) results.ShouldBe(new[] { 1 }); |
| 34 | + |
| 35 | + // When the window elapses, the last pending value is emitted. |
| 36 | + timeProvider.Advance(DueTime); |
| 37 | + await itemAdded.WaitAsync(); |
| 38 | + lock (results) results.ShouldBe(new[] { 1, 3 }); |
| 39 | + |
| 40 | + // The window is now closed: the next value opens a new one and is emitted immediately. |
| 41 | + await subject.OnNextAsync(4, CancellationToken.None); |
| 42 | + await itemAdded.WaitAsync(); |
| 43 | + lock (results) results.ShouldBe(new[] { 1, 3, 4 }); |
| 44 | + } |
| 45 | + |
| 46 | + [Fact] |
| 47 | + public async Task ThrottleFirstLast_EmptyWindowDoesNotEmitOnTimerExpiry() |
| 48 | + { |
| 49 | + var timeProvider = new FakeTimeProvider(); |
| 50 | + var subject = Subject.Create<int>(); |
| 51 | + |
| 52 | + var results = new List<int>(); |
| 53 | + var itemAdded = new SemaphoreSlim(0); |
| 54 | + |
| 55 | + await using var subscription = await subject.Values.ThrottleFirstLast(DueTime, timeProvider).SubscribeAsync( |
| 56 | + async (x, token) => { lock (results) results.Add(x); itemAdded.Release(); }, CancellationToken.None); |
| 57 | + |
| 58 | + // A single value with no followers: nothing extra is emitted when the window closes. |
| 59 | + await subject.OnNextAsync(1, CancellationToken.None); |
| 60 | + await itemAdded.WaitAsync(); |
| 61 | + timeProvider.Advance(DueTime); |
| 62 | + |
| 63 | + lock (results) results.ShouldBe(new[] { 1 }); |
| 64 | + } |
| 65 | + |
| 66 | + [Fact] |
| 67 | + public async Task ThrottleFirstLast_FlushesPendingValueOnCompletion() |
| 68 | + { |
| 69 | + var timeProvider = new FakeTimeProvider(); |
| 70 | + var subject = Subject.Create<int>(); |
| 71 | + |
| 72 | + var results = new List<int>(); |
| 73 | + var completedTcs = new TaskCompletionSource<Result>(TaskCreationOptions.RunContinuationsAsynchronously); |
| 74 | + |
| 75 | + await using var subscription = await subject.Values.ThrottleFirstLast(DueTime, timeProvider).SubscribeAsync( |
| 76 | + async (x, token) => { lock (results) results.Add(x); }, |
| 77 | + async (ex, token) => { }, |
| 78 | + async result => completedTcs.TrySetResult(result), |
| 79 | + CancellationToken.None); |
| 80 | + |
| 81 | + await subject.OnNextAsync(1, CancellationToken.None); |
| 82 | + await subject.OnNextAsync(2, CancellationToken.None); |
| 83 | + // Complete before the window elapses: the pending value is flushed. |
| 84 | + await subject.OnCompletedAsync(Result.Success); |
| 85 | + |
| 86 | + var result = await completedTcs.Task; |
| 87 | + result.IsSuccess.ShouldBeTrue(); |
| 88 | + lock (results) results.ShouldBe(new[] { 1, 2 }); |
| 89 | + } |
| 90 | + |
| 91 | + [Fact] |
| 92 | + public async Task ThrottleFirstLast_ErrorDropsPendingValueAndCompletes() |
| 93 | + { |
| 94 | + var expected = new InvalidOperationException("boom"); |
| 95 | + var timeProvider = new FakeTimeProvider(); |
| 96 | + var subject = Subject.Create<int>(); |
| 97 | + |
| 98 | + var results = new List<int>(); |
| 99 | + var completedTcs = new TaskCompletionSource<Result>(TaskCreationOptions.RunContinuationsAsynchronously); |
| 100 | + |
| 101 | + await using var subscription = await subject.Values.ThrottleFirstLast(DueTime, timeProvider).SubscribeAsync( |
| 102 | + async (x, token) => { lock (results) results.Add(x); }, |
| 103 | + async (ex, token) => { }, |
| 104 | + async result => completedTcs.TrySetResult(result), |
| 105 | + CancellationToken.None); |
| 106 | + |
| 107 | + await subject.OnNextAsync(7, CancellationToken.None); |
| 108 | + await subject.OnNextAsync(8, CancellationToken.None); |
| 109 | + await subject.OnCompletedAsync(Result.Failure(expected)); |
| 110 | + |
| 111 | + var result = await completedTcs.Task; |
| 112 | + result.IsFailure.ShouldBeTrue(); |
| 113 | + result.Exception.ShouldBe(expected); |
| 114 | + lock (results) results.ShouldBe(new[] { 7 }); |
| 115 | + } |
| 116 | + |
| 117 | + [Fact] |
| 118 | + public async Task ThrottleFirstLast_DisposeStopsEmission() |
| 119 | + { |
| 120 | + var timeProvider = new FakeTimeProvider(); |
| 121 | + var subject = Subject.Create<int>(); |
| 122 | + |
| 123 | + var results = new List<int>(); |
| 124 | + |
| 125 | + var subscription = await subject.Values.ThrottleFirstLast(DueTime, timeProvider).SubscribeAsync( |
| 126 | + async (x, token) => { lock (results) results.Add(x); }, CancellationToken.None); |
| 127 | + |
| 128 | + await subject.OnNextAsync(1, CancellationToken.None); |
| 129 | + await subscription.DisposeAsync(); |
| 130 | + |
| 131 | + // Values after disposal must not be emitted, even after the window elapses. |
| 132 | + timeProvider.Advance(DueTime); |
| 133 | + await subject.OnNextAsync(2, CancellationToken.None); |
| 134 | + |
| 135 | + lock (results) results.ShouldBe(new[] { 1 }); |
| 136 | + } |
| 137 | +} |
0 commit comments