Skip to content

Commit

Permalink
ripped off websockettestclient to see if the timing is the problem
Browse files Browse the repository at this point in the history
  • Loading branch information
juuwel committed May 28, 2024
1 parent 80c9195 commit a559127
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 21 deletions.
56 changes: 56 additions & 0 deletions Tests/WebSocketTestClient.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
using System.Text.Json;
using lib;
using Websocket.Client;

namespace Tests
{
public class WebSocketTestClient
{
public readonly WebsocketClient Client;
public readonly List<BaseDto> ReceivedMessages = [];

public WebSocketTestClient(string? url = null)
{
Client = url == null ? new WebsocketClient(new Uri("ws://localhost:" + (Environment.GetEnvironmentVariable("FULLSTACK_API_PORT") ?? "8181"))) : new WebsocketClient(new Uri(url));
Client.MessageReceived.Subscribe((Action<ResponseMessage>) (msg =>
{
BaseDto baseDto = JsonSerializer.Deserialize<BaseDto>(msg.Text);
lock (ReceivedMessages)
ReceivedMessages.Add(baseDto);
}));
}

public async Task<WebSocketTestClient> ConnectAsync()
{
await Client.Start();
if (!Client.IsRunning)
throw new Exception("Could not start client!");
return this;
}

public void Send<T>(T dto) where T : BaseDto
{
Client.Send(JsonSerializer.Serialize(dto));
}

public async Task DoAndAssert<T>(T? action = null, Func<List<BaseDto>, bool>? condition = null) where T : BaseDto
{
if ((object) (T) action != null)
Send(action);
if (condition != null)
{
DateTime startTime = DateTime.UtcNow;
while (DateTime.UtcNow - startTime < TimeSpan.FromSeconds(10.0))
{
lock (ReceivedMessages)
{
if (condition(ReceivedMessages))
return;
}
await Task.Delay(100);
}
throw new TimeoutException("Condition not met: ");
}
}
}
}
21 changes: 0 additions & 21 deletions Tests/WebSocketTestClientExtension.cs

This file was deleted.

0 comments on commit a559127

Please sign in to comment.