Skip to content

Commit

Permalink
minor fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
Scretch9 committed Nov 28, 2023
1 parent 5576afb commit 7359cde
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 72 deletions.
129 changes: 59 additions & 70 deletions src/FieldElementSubsystems.Test/Point/PointTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ private Mock<IPointToInterlockingConnection> CreateDefaultMockConnection() {
private IConfiguration _configuration = new ConfigurationBuilder()
.AddInMemoryCollection(_testSettings)
.Build();
private ILogger _logger = Mock.Of<ILogger<EulynxLive.Point.Point>>();
private ILogger<EulynxLive.Point.Point> _logger = Mock.Of<ILogger<EulynxLive.Point.Point>>();

[Fact]
public void Test_Parse_Configuration()
Expand All @@ -49,7 +49,7 @@ public async Task Test_Default_Position()
var point = CreateDefaultPoint();
await point.StartAsync(CancellationToken.None);

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

[Fact]
Expand All @@ -62,7 +62,7 @@ public async Task Test_Turn_Left()

mockConnection
.SetupSequence(m => m.ReceivePointPosition(It.IsAny<CancellationToken>()))
.Returns(Task.FromResult<IPointToInterlockingConnection.PointPosition?>(IPointToInterlockingConnection.PointPosition.LEFT))
.Returns(Task.FromResult<IPointToInterlockingConnection.PointPosition?>(IPointToInterlockingConnection.PointPosition.Left))
.Returns(() =>
{
cancel.Cancel();
Expand All @@ -76,73 +76,62 @@ public async Task Test_Turn_Left()

// Assert
mockConnection.Verify(v => v.InitializeConnection(It.IsAny<IPointToInterlockingConnection.PointState>(), It.IsAny<CancellationToken>()));
Assert.Equal(IPointToInterlockingConnection.PointPosition.LEFT, point.PointState.PointPosition);
Assert.Equal(IPointToInterlockingConnection.PointPosition.Left, point.PointState.PointPosition);
}

// [Fact]
// public async Task Test_Turn_Right()
// {
// var cancel = new CancellationTokenSource();
// // Arrange
// var point = CreateDefaultPoint();
// var mockConnection = CreateDefaultMockConnection();

// var finished = false;
// mockConnection
// .SetupSequence(m => m.ReceivePointPosition())
// .Returns(Task.FromResult<IPointToInterlockingConnection.PointPosition?>(IPointToInterlockingConnection.PointPosition.RIGHT))
// .Returns(() =>
// {
// finished = true;
// return Task.FromResult<IPointToInterlockingConnection.PointPosition?>(null);
// });

// point = CreateDefaultPoint(mockConnection.Object);

// // Act
// await point.StartAsync(CancellationToken.None);
// while (!finished)
// {
// await Task.Delay(1000);
// }

// // Assert
// mockConnection.Verify(v => v.InitializeConnection(It.IsAny<IPointToInterlockingConnection.PointState>()));
// Assert.Equal(IPointToInterlockingConnection.PointPosition.RIGHT, point.PointState.PointPosition);
// }

// [Fact]
// public async Task Test_Turnover()
// {
// var cancel = new CancellationTokenSource();
// // Arrange
// var point = CreateDefaultPoint();
// var mockConnection = CreateDefaultMockConnection();

// var finished = false;
// mockConnection
// .SetupSequence(m => m.ReceivePointPosition())
// .Returns(Task.FromResult<IPointToInterlockingConnection.PointPosition?>(IPointToInterlockingConnection.PointPosition.RIGHT))
// .Returns(Task.FromResult<IPointToInterlockingConnection.PointPosition?>(null))
// .Returns(Task.FromResult<IPointToInterlockingConnection.PointPosition?>(IPointToInterlockingConnection.PointPosition.LEFT))
// .Returns(() =>
// {
// finished = true;
// return Task.FromResult<IPointToInterlockingConnection.PointPosition?>(null);
// });

// point = CreateDefaultPoint(mockConnection.Object);

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

// while (!finished)
// {
// await Task.Delay(1000);
// }

// // Assert
// mockConnection.Verify(v => v.InitializeConnection(It.IsAny<IPointToInterlockingConnection.PointState>()));
// Assert.Equal(IPointToInterlockingConnection.PointPosition.LEFT, point.PointState.PointPosition);
// }
[Fact]
public async Task Test_Turn_Right()
{
var cancel = new CancellationTokenSource();
// Arrange
var point = CreateDefaultPoint();
var mockConnection = CreateDefaultMockConnection();

mockConnection
.SetupSequence(m => m.ReceivePointPosition(It.IsAny<CancellationToken>()))
.Returns(Task.FromResult<IPointToInterlockingConnection.PointPosition?>(IPointToInterlockingConnection.PointPosition.Right))
.Returns(() =>
{
cancel.Cancel();
return new TaskCompletionSource<IPointToInterlockingConnection.PointPosition?>().Task;
});

point = CreateDefaultPoint(mockConnection.Object);

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

// Assert
mockConnection.Verify(v => v.InitializeConnection(It.IsAny<IPointToInterlockingConnection.PointState>(), It.IsAny<CancellationToken>()));
Assert.Equal(IPointToInterlockingConnection.PointPosition.Right, point.PointState.PointPosition);
}

[Fact]
public async Task Test_Turnover()
{
var cancel = new CancellationTokenSource();
// Arrange
var point = CreateDefaultPoint();
var mockConnection = CreateDefaultMockConnection();

mockConnection
.SetupSequence(m => m.ReceivePointPosition(It.IsAny<CancellationToken>()))
.Returns(Task.FromResult<IPointToInterlockingConnection.PointPosition?>(IPointToInterlockingConnection.PointPosition.Right))
.Returns(Task.FromResult<IPointToInterlockingConnection.PointPosition?>(null))
.Returns(Task.FromResult<IPointToInterlockingConnection.PointPosition?>(IPointToInterlockingConnection.PointPosition.Left))
.Returns(() =>
{
cancel.Cancel();
return new TaskCompletionSource<IPointToInterlockingConnection.PointPosition?>().Task;
});

point = CreateDefaultPoint(mockConnection.Object);

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

// Assert
mockConnection.Verify(v => v.InitializeConnection(It.IsAny<IPointToInterlockingConnection.PointState>(), It.IsAny<CancellationToken>()));
Assert.Equal(IPointToInterlockingConnection.PointPosition.Left, point.PointState.PointPosition);
}
}
4 changes: 2 additions & 2 deletions src/Point/Point.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ public class Point : BackgroundService
{
public bool AllPointMachinesCrucial { get; }

private readonly ILogger _logger;
private readonly ILogger<Point> _logger;
private readonly List<WebSocket> _webSockets;
private IPointToInterlockingConnection _connection;
private readonly Random _random;
Expand All @@ -24,7 +24,7 @@ public class Point : BackgroundService
private readonly PointState _pointState;
public PointState PointState { get { return _pointState; } }

public Point(ILogger logger, IConfiguration configuration, IPointToInterlockingConnection connection)
public Point(ILogger<Point> logger, IConfiguration configuration, IPointToInterlockingConnection connection)
{
_logger = logger;

Expand Down

0 comments on commit 7359cde

Please sign in to comment.