feat(usage): cache the TokenTracker export with single-flight refresh
This commit is contained in:
@@ -0,0 +1,47 @@
|
||||
using ClaudeDo.Worker.Usage.TokenTracker;
|
||||
using ClaudeDo.Worker.Usage.TokenTracker.Interfaces;
|
||||
|
||||
namespace ClaudeDo.Worker.Tests.Usage.TokenTracker;
|
||||
|
||||
public sealed class FakeTokenTrackerClient : ITokenTrackerClient
|
||||
{
|
||||
private readonly SemaphoreSlim _gate = new(0);
|
||||
|
||||
public TokenTrackerProbe Probe { get; set; } = new(true, "1.2.3", true, "v22.1.0", null);
|
||||
public TokenTrackerRunResult ExportResult { get; set; } =
|
||||
new(true, TokenTrackerFixtures.SessionsJson, null);
|
||||
public TokenTrackerRunResult InstallResult { get; set; } = new(true, "added 1 package", null);
|
||||
|
||||
public int ExportCalls;
|
||||
public int ProbeCalls;
|
||||
public int InstallCalls;
|
||||
public DateOnly? LastFrom;
|
||||
public DateOnly? LastTo;
|
||||
|
||||
/// <summary>When true, <see cref="ExportAsync"/> blocks until <see cref="Release"/> is called.</summary>
|
||||
public bool BlockExport { get; set; }
|
||||
|
||||
public void Release() => _gate.Release();
|
||||
|
||||
public Task<TokenTrackerProbe> ProbeAsync(CancellationToken ct = default)
|
||||
{
|
||||
Interlocked.Increment(ref ProbeCalls);
|
||||
return Task.FromResult(Probe);
|
||||
}
|
||||
|
||||
public async Task<TokenTrackerRunResult> ExportAsync(DateOnly from, DateOnly to, CancellationToken ct = default)
|
||||
{
|
||||
Interlocked.Increment(ref ExportCalls);
|
||||
LastFrom = from;
|
||||
LastTo = to;
|
||||
if (BlockExport) await _gate.WaitAsync(ct);
|
||||
return ExportResult;
|
||||
}
|
||||
|
||||
public Task<TokenTrackerRunResult> InstallAsync(IProgress<string>? output = null, CancellationToken ct = default)
|
||||
{
|
||||
Interlocked.Increment(ref InstallCalls);
|
||||
output?.Report("added 1 package");
|
||||
return Task.FromResult(InstallResult);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user