39 lines
1.1 KiB
C#
39 lines
1.1 KiB
C#
using EonaCat.Json;
|
|
using System;
|
|
using System.Collections.Generic;
|
|
using System.Diagnostics;
|
|
using System.IO;
|
|
|
|
namespace EonaCat.DeterministicTime;
|
|
|
|
// This file is part of the EonaCat project(s) which is released under the Apache License.
|
|
// See the LICENSE file or go to https://EonaCat.com/License for full license details.
|
|
|
|
public sealed class TimeReplay : ITimeSource, IDisposable
|
|
{
|
|
private readonly Queue<DateTime> _queue;
|
|
private readonly IDisposable _scope;
|
|
|
|
private TimeReplay(IEnumerable<DateTime> times)
|
|
{
|
|
_queue = new Queue<DateTime>(times);
|
|
_scope = DeterministicTime.Push(this);
|
|
}
|
|
|
|
public static IDisposable Load(string path)
|
|
{
|
|
var times = JsonHelper.ToObject<List<DateTime>>(
|
|
File.ReadAllText(path))!;
|
|
return new TimeReplay(times);
|
|
}
|
|
|
|
public DateTime UtcNow =>
|
|
_queue.Count > 0
|
|
? _queue.Dequeue()
|
|
: throw new InvalidOperationException("Replay exhausted");
|
|
|
|
public long Timestamp => Stopwatch.GetTimestamp();
|
|
|
|
public void Dispose() => _scope.Dispose();
|
|
}
|