diff --git a/src/FieldElementSubsystems.Test/Point/PointTest.cs b/src/FieldElementSubsystems.Test/Point/PointTest.cs index 203872e..9d6565e 100644 --- a/src/FieldElementSubsystems.Test/Point/PointTest.cs +++ b/src/FieldElementSubsystems.Test/Point/PointTest.cs @@ -33,7 +33,7 @@ private Mock CreateDefaultMockConnection() { private IConfiguration _configuration = new ConfigurationBuilder() .AddInMemoryCollection(_testSettings) .Build(); - private ILogger _logger = Mock.Of>(); + private ILogger _logger = Mock.Of>(); [Fact] public void Test_Parse_Configuration() @@ -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] @@ -62,7 +62,7 @@ public async Task Test_Turn_Left() mockConnection .SetupSequence(m => m.ReceivePointPosition(It.IsAny())) - .Returns(Task.FromResult(IPointToInterlockingConnection.PointPosition.LEFT)) + .Returns(Task.FromResult(IPointToInterlockingConnection.PointPosition.Left)) .Returns(() => { cancel.Cancel(); @@ -76,73 +76,62 @@ public async Task Test_Turn_Left() // Assert mockConnection.Verify(v => v.InitializeConnection(It.IsAny(), It.IsAny())); - 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.RIGHT)) - // .Returns(() => - // { - // finished = true; - // return Task.FromResult(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())); - // 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.RIGHT)) - // .Returns(Task.FromResult(null)) - // .Returns(Task.FromResult(IPointToInterlockingConnection.PointPosition.LEFT)) - // .Returns(() => - // { - // finished = true; - // return Task.FromResult(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())); - // 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())) + .Returns(Task.FromResult(IPointToInterlockingConnection.PointPosition.Right)) + .Returns(() => + { + cancel.Cancel(); + return new TaskCompletionSource().Task; + }); + + point = CreateDefaultPoint(mockConnection.Object); + + // Act + await point.StartAsync(CancellationToken.None); + + // Assert + mockConnection.Verify(v => v.InitializeConnection(It.IsAny(), It.IsAny())); + 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())) + .Returns(Task.FromResult(IPointToInterlockingConnection.PointPosition.Right)) + .Returns(Task.FromResult(null)) + .Returns(Task.FromResult(IPointToInterlockingConnection.PointPosition.Left)) + .Returns(() => + { + cancel.Cancel(); + return new TaskCompletionSource().Task; + }); + + point = CreateDefaultPoint(mockConnection.Object); + + // Act + await point.StartAsync(CancellationToken.None); + + // Assert + mockConnection.Verify(v => v.InitializeConnection(It.IsAny(), It.IsAny())); + Assert.Equal(IPointToInterlockingConnection.PointPosition.Left, point.PointState.PointPosition); + } } diff --git a/src/Point/Point.cs b/src/Point/Point.cs index b13d42c..c10b993 100644 --- a/src/Point/Point.cs +++ b/src/Point/Point.cs @@ -15,7 +15,7 @@ public class Point : BackgroundService { public bool AllPointMachinesCrucial { get; } - private readonly ILogger _logger; + private readonly ILogger _logger; private readonly List _webSockets; private IPointToInterlockingConnection _connection; private readonly Random _random; @@ -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 logger, IConfiguration configuration, IPointToInterlockingConnection connection) { _logger = logger;