using System.Collections.Concurrent;
using System.Runtime.CompilerServices;
using System.Threading.Channels;
using Febe.Web.Connection;
namespace Febe.Tests.Server.Support;
/// A remote view without browser: the test writes what goes to the page and reads what comes back.
internal sealed class FakeRemoteViewHost : IRemoteViewHost
{
private readonly Lock _gate = new();
private readonly List _opened = [];
///
/// The views opened so far, copied under the lock the server writes with: the tests read it from their own thread
/// while the server may be adding to it (#46).
///
public IReadOnlyList Opened
{
get
{
lock (_gate)
{
return [.. _opened];
}
}
}
public bool Unavailable { get; set; }
public Task OpenAsync(CancellationToken ct)
{
if (Unavailable)
{
throw new RemoteViewUnavailableException("Il browser di prova non c'รจ.");
}
var connection = new FakeRemoteViewConnection();
lock (_gate)
{
if (_opened.LastOrDefault() is { Disposed: false } previous)
{
previous.End(RemoteViewEnd.Replaced);
}
_opened.Add(connection);
}
return Task.FromResult(connection);
}
}
internal sealed class FakeRemoteViewConnection : IRemoteViewConnection
{
private readonly Channel _output = Channel.CreateUnbounded();
private volatile bool _disposed;
private volatile object? _ended;
public ConcurrentQueue