Skip to content

Commit

Permalink
Fix tests
Browse files Browse the repository at this point in the history
  • Loading branch information
rs22 committed Dec 3, 2023
1 parent f570596 commit e5848ad
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 23 deletions.
34 changes: 16 additions & 18 deletions src/FieldElementSubsystems.Test/Point/PointTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ namespace FieldElementSubsystems.Test;
public class PointTest
{
private EulynxLive.Point.Point CreateDefaultPoint(IPointToInterlockingConnection? connection = null) =>
new EulynxLive.Point.Point(_logger, _configuration, connection ?? Mock.Of<IPointToInterlockingConnection>(), async () => {});
new(_logger, _configuration, connection ?? Mock.Of<IPointToInterlockingConnection>(), () => Task.CompletedTask);

private Mock<IPointToInterlockingConnection> CreateDefaultMockConnection() {
var mockConnection = new Mock<IPointToInterlockingConnection>();
Expand All @@ -22,18 +22,18 @@ private Mock<IPointToInterlockingConnection> CreateDefaultMockConnection() {
return mockConnection;
}

private static IDictionary<string, string?> _testSettings = new Dictionary<string, string?> {
{"PointSettings:LocalId", "99W1" },
{"PointSettings:LocalRastaId", "100" },
{"PointSettings:RemoteId", "INTERLOCKING" },
{"PointSettings:RemoteEndpoint", "http://localhost:50051" },
{"PointSettings:AllPointMachinesCrucial", "true" },
{"PointSettings:SimulateRandomTimeouts", "false" },
};
private IConfiguration _configuration = new ConfigurationBuilder()
private static readonly IDictionary<string, string?> _testSettings = new Dictionary<string, string?> {
{"PointSettings:LocalId", "99W1" },
{"PointSettings:LocalRastaId", "100" },
{"PointSettings:RemoteId", "INTERLOCKING" },
{"PointSettings:RemoteEndpoint", "http://localhost:50051" },
{"PointSettings:AllPointMachinesCrucial", "true" },
{"PointSettings:SimulateRandomTimeouts", "false" },
};
private readonly IConfiguration _configuration = new ConfigurationBuilder()
.AddInMemoryCollection(_testSettings)
.Build();
private ILogger<EulynxLive.Point.Point> _logger = Mock.Of<ILogger<EulynxLive.Point.Point>>();
private readonly ILogger<EulynxLive.Point.Point> _logger = Mock.Of<ILogger<EulynxLive.Point.Point>>();

[Fact]
public void Test_Parse_Configuration()
Expand All @@ -44,11 +44,9 @@ public void Test_Parse_Configuration()
}

[Fact]
public async Task Test_Default_Position()
public void Test_Default_Position()
{
var point = CreateDefaultPoint();
await point.StartAsync(CancellationToken.None);

Assert.Equal(IPointToInterlockingConnection.PointPosition.NoEndPosition, point.PointState.PointPosition);
}

Expand Down Expand Up @@ -82,10 +80,10 @@ public async Task Test_Turn_Left()
[Fact]
public async Task Test_Turn_Right()
{
var cancel = new CancellationTokenSource();
// Arrange
var point = CreateDefaultPoint();
var mockConnection = CreateDefaultMockConnection();
var cancel = new CancellationTokenSource();

mockConnection
.SetupSequence(m => m.ReceivePointPosition(It.IsAny<CancellationToken>()))
Expand All @@ -99,7 +97,7 @@ public async Task Test_Turn_Right()
point = CreateDefaultPoint(mockConnection.Object);

// Act
await point.StartAsync(CancellationToken.None);
await point.StartAsync(cancel.Token);

// Assert
mockConnection.Verify(v => v.InitializeConnection(It.IsAny<IPointToInterlockingConnection.PointState>(), It.IsAny<CancellationToken>()));
Expand All @@ -109,10 +107,10 @@ public async Task Test_Turn_Right()
[Fact]
public async Task Test_Turnover()
{
var cancel = new CancellationTokenSource();
// Arrange
var point = CreateDefaultPoint();
var mockConnection = CreateDefaultMockConnection();
var cancel = new CancellationTokenSource();

mockConnection
.SetupSequence(m => m.ReceivePointPosition(It.IsAny<CancellationToken>()))
Expand All @@ -128,7 +126,7 @@ public async Task Test_Turnover()
point = CreateDefaultPoint(mockConnection.Object);

// Act
await point.StartAsync(CancellationToken.None);
await point.StartAsync(cancel.Token);

// Assert
mockConnection.Verify(v => v.InitializeConnection(It.IsAny<IPointToInterlockingConnection.PointState>(), It.IsAny<CancellationToken>()));
Expand Down
2 changes: 1 addition & 1 deletion src/Point/Interfaces/IPointToInterlockingConnection.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
namespace EulynxLive.Point.Interfaces;

public interface IPointToInterlockingConnection: IDisposable {
public interface IPointToInterlockingConnection : IDisposable {
Task SendPointPosition(PointState state);
Task SendTimeoutMessage();
void Connect();
Expand Down
8 changes: 4 additions & 4 deletions src/Point/Point.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ public class Point : BackgroundService
private readonly ILogger<Point> _logger;
private readonly Func<Task> _simulateTimeout;
private readonly List<WebSocket> _webSockets;
private IPointToInterlockingConnection _connection;
private readonly IPointToInterlockingConnection _connection;
private readonly Random _random;
private readonly bool _simulateRandomTimeouts;
private bool _initialized;
Expand Down Expand Up @@ -190,7 +190,7 @@ protected override async Task ExecuteAsync(CancellationToken stoppingToken)
var success = await _connection.InitializeConnection(PointState, stoppingToken);
if (!success)
{
continue;
throw new Exception("Unable to initialize connection");
}
await UpdateConnectedWebClients();

Expand All @@ -215,14 +215,14 @@ protected override async Task ExecuteAsync(CancellationToken stoppingToken)

// Simulate point movement
var transitioningTime = _random.Next(1, 5);
var transitioningTask = Task.Delay(transitioningTime * 1000);
var transitioningTask = Task.Delay(transitioningTime * 1000, CancellationToken.None);
var pointMovementTimeout = 3 * 1000;

_logger.LogDebug("Moving to {}.", commandedPointPosition);

if (_simulateRandomTimeouts)
{
if (await Task.WhenAny(transitioningTask, Task.Delay(pointMovementTimeout)) == transitioningTask)
if (await Task.WhenAny(transitioningTask, Task.Delay(pointMovementTimeout, CancellationToken.None)) == transitioningTask)
{
// transition completed within timeout
UpdatePointState(commandedPointPosition.Value);
Expand Down

0 comments on commit e5848ad

Please sign in to comment.