< Summary

Information
Class: Anichron.Testing.Time.Timeline
Assembly: Anichron.Testing
File(s): /home/runner/work/anichron/anichron/src/Anichron.Testing/Time/Timeline.cs
Tag: 19_45211875487
Line coverage
100%
Covered lines: 77
Uncovered lines: 0
Coverable lines: 77
Total lines: 136
Line coverage: 100%
Branch coverage
100%
Covered branches: 12
Total branches: 12
Branch coverage: 100%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity Line coverage
.ctor(...)100%11100%
.ctor()100%11100%
get_DefaultSilenceLimit()100%11100%
get_Lifetime()100%11100%
get_Unread()100%11100%
Record(...)100%11100%
Watch(...)100%11100%
ExpectAsync()100%44100%
DisposeAsync()100%44100%
Append(...)100%11100%
ReadAsync()100%22100%
SlotAt(...)100%22100%

File(s)

/home/runner/work/anichron/anichron/src/Anichron.Testing/Time/Timeline.cs

#LineLine coverage
 1using Microsoft.Extensions.Time.Testing;
 2
 3namespace 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>
 11public sealed class Timeline : IAsyncDisposable
 12{
 14213    private readonly Lock gate = new();
 14214    private readonly List<TimelineEvent> events = [];
 14215    private readonly List<TaskCompletionSource<TimelineEvent>> slots = [];
 14216    private readonly List<(Task Ended, Func<Task>? Stop)> watched = [];
 14217    private readonly CancellationTokenSource lifetime = new();
 18    private readonly TimeSpan silenceLimit;
 19    private int cursor;
 20
 21    public Timeline()
 12722        : this(DefaultSilenceLimit)
 12723    {
 12724    }
 25
 14226    internal Timeline(TimeSpan silenceLimit)
 14227    {
 14228        this.silenceLimit = silenceLimit;
 14229        Clock = new TimelineClock(this);
 14230    }
 31
 13332    public static TimeSpan DefaultSilenceLimit => TimeSpan.FromSeconds(10);
 33
 34    public FakeTimeProvider Clock { get; }
 35
 2436    public CancellationToken Lifetime => lifetime.Token;
 37
 38    public IReadOnlyList<TimelineEvent> Unread
 39    {
 40        get
 941        {
 42            lock (gate)
 943                return events[cursor..];
 944        }
 45    }
 46
 23347    public void Record(string what) => Append(new Called(what));
 48
 49    public void Watch(Task subject, Func<Task>? stop = null)
 7950    {
 7951        var ended = subject.ContinueWith(
 7952            task => Append(new Finished(task.Status)),
 7953            CancellationToken.None,
 7954            TaskContinuationOptions.ExecuteSynchronously,
 7955            TaskScheduler.Default);
 56        lock (gate)
 7957            watched.Add((ended, stop));
 7958    }
 59
 60    public async Task ExpectAsync(params TimelineEvent[] expected)
 17061    {
 17062        ArgumentOutOfRangeException.ThrowIfZero(expected.Length);
 63
 133264        for (var position = 0; position < expected.Length; position++)
 49965        {
 49966            var actual = await ReadAsync(expected[position]);
 49867            if (actual != expected[position])
 168            {
 169                throw new TimelineMismatchException(
 170                    $"Expected {expected[position]} as event {position + 1} of {expected.Length}, but the timeline recor
 71            }
 49772        }
 16773    }
 74
 75    public async ValueTask DisposeAsync()
 14276    {
 14277        await lifetime.CancelAsync();
 78
 79        List<(Task Ended, Func<Task>? Stop)> owned;
 80        lock (gate)
 14281            owned = [.. watched];
 82
 14283        var ends = new List<Task>(owned.Count);
 58284        foreach (var (ended, stop) in owned)
 7985        {
 7986            if (stop is not null)
 4987                await stop().WaitAsync(silenceLimit, CancellationToken.None);
 7788            ends.Add(ended);
 7789        }
 90
 14091        await Task.WhenAll(ends).WaitAsync(silenceLimit, CancellationToken.None);
 13992        lifetime.Dispose();
 13993    }
 94
 95    internal void Append(TimelineEvent timelineEvent)
 57596    {
 97        TaskCompletionSource<TimelineEvent> slot;
 98        lock (gate)
 57599        {
 575100            slot = SlotAt(events.Count);
 575101            events.Add(timelineEvent);
 575102        }
 103
 575104        slot.SetResult(timelineEvent);
 575105    }
 106
 107    private async Task<TimelineEvent> ReadAsync(TimelineEvent awaited)
 499108    {
 109        Task<TimelineEvent> next;
 110        lock (gate)
 499111        {
 499112            if (cursor < events.Count)
 390113                return events[cursor++];
 109114            next = SlotAt(cursor).Task;
 109115        }
 116
 117        try
 109118        {
 109119            var read = await next.WaitAsync(silenceLimit, CancellationToken.None);
 120            lock (gate)
 108121                cursor++;
 108122            return read;
 123        }
 1124        catch (TimeoutException ex)
 1125        {
 1126            throw new TimeoutException($"Expected {awaited}, but the timeline went silent for {silenceLimit}.", ex);
 127        }
 498128    }
 129
 130    private TaskCompletionSource<TimelineEvent> SlotAt(int position)
 684131    {
 684132        if (slots.Count == position)
 576133            slots.Add(new TaskCompletionSource<TimelineEvent>(TaskCreationOptions.RunContinuationsAsynchronously));
 684134        return slots[position];
 684135    }
 136}