54 lines
1.6 KiB
C#
54 lines
1.6 KiB
C#
using System.Text.Json.Nodes;
|
|
using System.Threading.Channels;
|
|
using backend.Realtime;
|
|
|
|
namespace backend.tests;
|
|
|
|
public class ScriptedRealtimeUpstream : IRealtimeUpstream
|
|
{
|
|
private readonly Channel<JsonObject> _toRelay =
|
|
Channel.CreateUnbounded<JsonObject>(new UnboundedChannelOptions { SingleReader = true });
|
|
|
|
private readonly Channel<JsonObject> _fromRelay =
|
|
Channel.CreateUnbounded<JsonObject>(new UnboundedChannelOptions { SingleWriter = true });
|
|
|
|
public IList<JsonObject> Sent { get; } = new List<JsonObject>();
|
|
|
|
public Task SendJsonAsync(JsonObject envelope, CancellationToken ct)
|
|
{
|
|
Sent.Add(envelope);
|
|
return _fromRelay.Writer.WriteAsync(envelope, ct).AsTask();
|
|
}
|
|
|
|
public async Task<JsonObject?> ReceiveJsonAsync(CancellationToken ct)
|
|
{
|
|
try
|
|
{
|
|
return await _toRelay.Reader.ReadAsync(ct);
|
|
}
|
|
catch (ChannelClosedException)
|
|
{
|
|
return null;
|
|
}
|
|
}
|
|
|
|
public void Push(JsonObject evt) => _toRelay.Writer.TryWrite(evt);
|
|
public void CloseUpstream() => _toRelay.Writer.TryComplete();
|
|
|
|
public async Task<JsonObject> WaitForSentAsync(string type, CancellationToken ct)
|
|
{
|
|
await foreach (var item in _fromRelay.Reader.ReadAllAsync(ct))
|
|
{
|
|
if ((string?)item["type"] == type) return item;
|
|
}
|
|
throw new TimeoutException($"never saw outbound {type}");
|
|
}
|
|
|
|
public ValueTask DisposeAsync()
|
|
{
|
|
_toRelay.Writer.TryComplete();
|
|
_fromRelay.Writer.TryComplete();
|
|
return ValueTask.CompletedTask;
|
|
}
|
|
}
|