| | | 1 | | using Microsoft.Extensions.Time.Testing; |
| | | 2 | | |
| | | 3 | | namespace Anichron.Testing.Time; |
| | | 4 | | |
| | | 5 | | /// <summary> |
| | | 6 | | /// An ordered record of what time-dependent code did: the timers it armed on <see cref="Clock"/>, |
| | | 7 | | /// the timers that fired, the collaborator calls the test <see cref="Record"/>s, and the end of |
| | | 8 | | /// every <see cref="Watch"/>ed task. A test states what happens next with <see cref="ExpectAsync"/>, |
| | | 9 | | /// which fails at the first event that differs instead of waiting a guessed amount of real time. |
| | | 10 | | /// </summary> |
| | | 11 | | public sealed class Timeline : IAsyncDisposable |
| | | 12 | | { |
| | 142 | 13 | | private readonly Lock gate = new(); |
| | 142 | 14 | | private readonly List<TimelineEvent> events = []; |
| | 142 | 15 | | private readonly List<TaskCompletionSource<TimelineEvent>> slots = []; |
| | 142 | 16 | | private readonly List<(Task Ended, Func<Task>? Stop)> watched = []; |
| | 142 | 17 | | private readonly CancellationTokenSource lifetime = new(); |
| | | 18 | | private readonly TimeSpan silenceLimit; |
| | | 19 | | private int cursor; |
| | | 20 | | |
| | | 21 | | public Timeline() |
| | 127 | 22 | | : this(DefaultSilenceLimit) |
| | 127 | 23 | | { |
| | 127 | 24 | | } |
| | | 25 | | |
| | 142 | 26 | | internal Timeline(TimeSpan silenceLimit) |
| | 142 | 27 | | { |
| | 142 | 28 | | this.silenceLimit = silenceLimit; |
| | 142 | 29 | | Clock = new TimelineClock(this); |
| | 142 | 30 | | } |
| | | 31 | | |
| | 133 | 32 | | public static TimeSpan DefaultSilenceLimit => TimeSpan.FromSeconds(10); |
| | | 33 | | |
| | | 34 | | public FakeTimeProvider Clock { get; } |
| | | 35 | | |
| | 24 | 36 | | public CancellationToken Lifetime => lifetime.Token; |
| | | 37 | | |
| | | 38 | | public IReadOnlyList<TimelineEvent> Unread |
| | | 39 | | { |
| | | 40 | | get |
| | 9 | 41 | | { |
| | | 42 | | lock (gate) |
| | 9 | 43 | | return events[cursor..]; |
| | 9 | 44 | | } |
| | | 45 | | } |
| | | 46 | | |
| | 233 | 47 | | public void Record(string what) => Append(new Called(what)); |
| | | 48 | | |
| | | 49 | | public void Watch(Task subject, Func<Task>? stop = null) |
| | 79 | 50 | | { |
| | 79 | 51 | | var ended = subject.ContinueWith( |
| | 79 | 52 | | task => Append(new Finished(task.Status)), |
| | 79 | 53 | | CancellationToken.None, |
| | 79 | 54 | | TaskContinuationOptions.ExecuteSynchronously, |
| | 79 | 55 | | TaskScheduler.Default); |
| | | 56 | | lock (gate) |
| | 79 | 57 | | watched.Add((ended, stop)); |
| | 79 | 58 | | } |
| | | 59 | | |
| | | 60 | | public async Task ExpectAsync(params TimelineEvent[] expected) |
| | 170 | 61 | | { |
| | 170 | 62 | | ArgumentOutOfRangeException.ThrowIfZero(expected.Length); |
| | | 63 | | |
| | 1332 | 64 | | for (var position = 0; position < expected.Length; position++) |
| | 499 | 65 | | { |
| | 499 | 66 | | var actual = await ReadAsync(expected[position]); |
| | 498 | 67 | | if (actual != expected[position]) |
| | 1 | 68 | | { |
| | 1 | 69 | | throw new TimelineMismatchException( |
| | 1 | 70 | | $"Expected {expected[position]} as event {position + 1} of {expected.Length}, but the timeline recor |
| | | 71 | | } |
| | 497 | 72 | | } |
| | 167 | 73 | | } |
| | | 74 | | |
| | | 75 | | public async ValueTask DisposeAsync() |
| | 142 | 76 | | { |
| | 142 | 77 | | await lifetime.CancelAsync(); |
| | | 78 | | |
| | | 79 | | List<(Task Ended, Func<Task>? Stop)> owned; |
| | | 80 | | lock (gate) |
| | 142 | 81 | | owned = [.. watched]; |
| | | 82 | | |
| | 142 | 83 | | var ends = new List<Task>(owned.Count); |
| | 582 | 84 | | foreach (var (ended, stop) in owned) |
| | 79 | 85 | | { |
| | 79 | 86 | | if (stop is not null) |
| | 49 | 87 | | await stop().WaitAsync(silenceLimit, CancellationToken.None); |
| | 77 | 88 | | ends.Add(ended); |
| | 77 | 89 | | } |
| | | 90 | | |
| | 140 | 91 | | await Task.WhenAll(ends).WaitAsync(silenceLimit, CancellationToken.None); |
| | 139 | 92 | | lifetime.Dispose(); |
| | 139 | 93 | | } |
| | | 94 | | |
| | | 95 | | internal void Append(TimelineEvent timelineEvent) |
| | 575 | 96 | | { |
| | | 97 | | TaskCompletionSource<TimelineEvent> slot; |
| | | 98 | | lock (gate) |
| | 575 | 99 | | { |
| | 575 | 100 | | slot = SlotAt(events.Count); |
| | 575 | 101 | | events.Add(timelineEvent); |
| | 575 | 102 | | } |
| | | 103 | | |
| | 575 | 104 | | slot.SetResult(timelineEvent); |
| | 575 | 105 | | } |
| | | 106 | | |
| | | 107 | | private async Task<TimelineEvent> ReadAsync(TimelineEvent awaited) |
| | 499 | 108 | | { |
| | | 109 | | Task<TimelineEvent> next; |
| | | 110 | | lock (gate) |
| | 499 | 111 | | { |
| | 499 | 112 | | if (cursor < events.Count) |
| | 390 | 113 | | return events[cursor++]; |
| | 109 | 114 | | next = SlotAt(cursor).Task; |
| | 109 | 115 | | } |
| | | 116 | | |
| | | 117 | | try |
| | 109 | 118 | | { |
| | 109 | 119 | | var read = await next.WaitAsync(silenceLimit, CancellationToken.None); |
| | | 120 | | lock (gate) |
| | 108 | 121 | | cursor++; |
| | 108 | 122 | | return read; |
| | | 123 | | } |
| | 1 | 124 | | catch (TimeoutException ex) |
| | 1 | 125 | | { |
| | 1 | 126 | | throw new TimeoutException($"Expected {awaited}, but the timeline went silent for {silenceLimit}.", ex); |
| | | 127 | | } |
| | 498 | 128 | | } |
| | | 129 | | |
| | | 130 | | private TaskCompletionSource<TimelineEvent> SlotAt(int position) |
| | 684 | 131 | | { |
| | 684 | 132 | | if (slots.Count == position) |
| | 576 | 133 | | slots.Add(new TaskCompletionSource<TimelineEvent>(TaskCreationOptions.RunContinuationsAsynchronously)); |
| | 684 | 134 | | return slots[position]; |
| | 684 | 135 | | } |
| | | 136 | | } |