Skip to content

Commit

Permalink
Merge pull request #25 from eulynx-live/cmdline-args
Browse files Browse the repository at this point in the history
Polishing
  • Loading branch information
rs22 authored Jan 14, 2024
2 parents a41cc2c + ebd8603 commit 94a1713
Show file tree
Hide file tree
Showing 38 changed files with 6,234 additions and 18,761 deletions.
8 changes: 8 additions & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"eslint.workingDirectories": [
"./src/Point/rasta-point-web",
],
"editor.codeActionsOnSave": {
"source.fixAll.eslint": "explicit"
},
}
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@ private static Mock<IConnection> CreateDefaultMockConnection()
{"PointSettings:SimulatedTransitioningTimeSeconds", "0" },
{"PointSettings:AllPointMachinesCrucial", "false" },
{"PointSettings:ObserveAbilityToMove", "true" },
{"PointSettings:PDIVersion", "1" },
{"PointSettings:PDIChecksum", "0x00" }
};

private readonly IConfiguration _configuration = new ConfigurationBuilder()
Expand All @@ -42,13 +44,13 @@ public void Test_Connect()
{
// Arrange
var mockConnection = new Mock<IConnection>();
var connection = new PointToInterlockingConnection(Mock.Of<ILogger<PointToInterlockingConnection>>(), _configuration, CancellationToken.None);
var builder = new PointToInterlockingConnectionBuilder(Mock.Of<ILogger<PointToInterlockingConnection>>(), _configuration, CancellationToken.None);

// Act
connection.Connect(mockConnection.Object);
var connection = builder.Connect(mockConnection.Object);

// Assert
Assert.Equal(connection.CurrentConnection, mockConnection.Object);
Assert.Equal(((PointToInterlockingConnection)connection).CurrentConnection, mockConnection.Object);
}

[Fact]
Expand All @@ -60,22 +62,71 @@ public async Task Test_Initialization()
mockConnection.Setup(x => x.SendAsync(Capture.In(receivedMessages)))
.Returns(Task.FromResult(0));

var connection = new PointToInterlockingConnection(Mock.Of<ILogger<PointToInterlockingConnection>>(), _configuration, CancellationToken.None);
var builder = new PointToInterlockingConnectionBuilder(Mock.Of<ILogger<PointToInterlockingConnection>>(), _configuration, CancellationToken.None);

// Act
connection.Connect(mockConnection.Object);
await connection.InitializeConnection(new GenericPointState(LastCommandedPointPosition: null, PointPosition: GenericPointPosition.Left, DegradedPointPosition: GenericDegradedPointPosition.NotDegraded, AbilityToMove: GenericAbilityToMove.AbleToMove), true, CancellationToken.None);
var connection = builder.Connect(mockConnection.Object);
await connection.InitializeConnection(new GenericPointState(LastCommandedPointPosition: null, PointPosition: GenericPointPosition.Left, DegradedPointPosition: GenericDegradedPointPosition.NotDegraded, AbilityToMove: GenericAbilityToMove.AbleToMove), true, false, CancellationToken.None);

// Assert
mockConnection.Verify(v => v.ReceiveAsync(It.IsAny<CancellationToken>()), Times.Exactly(2));
mockConnection.Verify(v => v.SendAsync(It.IsAny<byte[]>()), Times.Exactly(5));
Assert.Equal(new PointPdiVersionCheckMessage("99W1________________", "INTERLOCKING________", PointPdiVersionCheckMessageResultPdiVersionCheck.PDIVersionsFromReceiverAndSenderDoMatch, /* TODO */ 0, 0, Array.Empty<byte>()).ToByteArray(), receivedMessages[0]);
Assert.Equal(new PointPdiVersionCheckMessage("99W1________________", "INTERLOCKING________", PointPdiVersionCheckMessageResultPdiVersionCheck.PDIVersionsFromReceiverAndSenderDoMatch, 1, 1, new byte[] { 0x00 }).ToByteArray(), receivedMessages[0]);
Assert.Equal(new PointStartInitialisationMessage("99W1________________", "INTERLOCKING________").ToByteArray(), receivedMessages[1]);
Assert.Equal(new PointPointPositionMessage("99W1________________", "INTERLOCKING________", PointPointPositionMessageReportedPointPosition.PointIsInALeftHandPositionDefinedEndPosition, PointPointPositionMessageReportedDegradedPointPosition.PointIsNotInADegradedPosition).ToByteArray(), receivedMessages[2]);
Assert.Equal(new PointAbilityToMovePointMessage("99W1________________", "INTERLOCKING________", PointAbilityToMovePointMessageReportedAbilityToMovePointStatus.PointIsAbleToMove).ToByteArray(), receivedMessages[3]);
Assert.Equal(new PointInitialisationCompletedMessage("99W1________________", "INTERLOCKING________").ToByteArray(), receivedMessages[4]);
}

[Fact]
public async Task Test_Initialization_Timeout(){
// Arrange
var mockConnection = new Mock<IConnection>();
mockConnection.SetupSequence(x => x.ReceiveAsync(It.IsAny<CancellationToken>()))
.ReturnsAsync(new PointPdiVersionCheckCommand("99W1", "100", 0x01).ToByteArray())
.ReturnsAsync(new PointInitialisationRequestCommand("99W1", "100").ToByteArray());
var receivedMessages = new List<byte[]>();
mockConnection.Setup(x => x.SendAsync(Capture.In(receivedMessages)))
.Returns(Task.FromResult(0));

var builder = new PointToInterlockingConnectionBuilder(Mock.Of<ILogger<PointToInterlockingConnection>>(), _configuration, CancellationToken.None);

// Act
var connection = builder.Connect(mockConnection.Object);
await connection.InitializeConnection(new GenericPointState(LastCommandedPointPosition: null, PointPosition: GenericPointPosition.Left, DegradedPointPosition: GenericDegradedPointPosition.NotDegraded, AbilityToMove: GenericAbilityToMove.AbleToMove), true, true, CancellationToken.None);

// Assert
// Should not send/receive any more messages after version check as timeout is simulated
mockConnection.Verify(v => v.ReceiveAsync(It.IsAny<CancellationToken>()), Times.Exactly(1));
mockConnection.Verify(v => v.SendAsync(It.IsAny<byte[]>()), Times.Exactly(1));
}

/// <summary>
/// Eu.Gen-SCI.445 - PDI version is unequal, no retry
/// </summary>
[Fact]
public async Task Test_Initialization_Version_Mismatch(){
// Arrange
var mockConnection = new Mock<IConnection>();
mockConnection.SetupSequence(x => x.ReceiveAsync(It.IsAny<CancellationToken>()))
.ReturnsAsync(new PointPdiVersionCheckCommand("99W1", "100", 0x00).ToByteArray())
.ReturnsAsync(new PointInitialisationRequestCommand("99W1", "100").ToByteArray());
var receivedMessages = new List<byte[]>();
mockConnection.Setup(x => x.SendAsync(Capture.In(receivedMessages)))
.Returns(Task.FromResult(0));

var builder = new PointToInterlockingConnectionBuilder(Mock.Of<ILogger<PointToInterlockingConnection>>(), _configuration, CancellationToken.None);

// Act
var connection = builder.Connect(mockConnection.Object);
await connection.InitializeConnection(new GenericPointState(LastCommandedPointPosition: null, PointPosition: GenericPointPosition.Left, DegradedPointPosition: GenericDegradedPointPosition.NotDegraded, AbilityToMove: GenericAbilityToMove.AbleToMove), true, true, CancellationToken.None);

// Assert
mockConnection.Verify(v => v.ReceiveAsync(It.IsAny<CancellationToken>()), Times.Exactly(1));
mockConnection.Verify(v => v.SendAsync(It.IsAny<byte[]>()), Times.Exactly(1));
Assert.Equal(new PointPdiVersionCheckMessage("99W1________________", "INTERLOCKING________", PointPdiVersionCheckMessageResultPdiVersionCheck.PDIVersionsFromReceiverAndSenderDoNotMatch, 1, 0, []).ToByteArray(), receivedMessages[0]);
}

[Fact]
public async Task Test_Send_Position()
{
Expand All @@ -85,12 +136,12 @@ public async Task Test_Send_Position()
mockConnection.Setup(x => x.SendAsync(Capture.In(args)))
.Returns(Task.FromResult(0));

var connection = new PointToInterlockingConnection(Mock.Of<ILogger<PointToInterlockingConnection>>(), _configuration, CancellationToken.None);
var builder = new PointToInterlockingConnectionBuilder(Mock.Of<ILogger<PointToInterlockingConnection>>(), _configuration, CancellationToken.None);

// Act
connection.Connect(mockConnection.Object);
await connection.InitializeConnection(new GenericPointState(LastCommandedPointPosition: null, PointPosition: GenericPointPosition.Left, DegradedPointPosition: GenericDegradedPointPosition.NotDegraded, AbilityToMove: GenericAbilityToMove.AbleToMove), true, CancellationToken.None);
foreach (var position in new List<GenericPointPosition>() { GenericPointPosition.Left, GenericPointPosition.Right, GenericPointPosition.UnintendedPosition, GenericPointPosition.NoEndPosition })
var connection = builder.Connect(mockConnection.Object);
await connection.InitializeConnection(new GenericPointState(LastCommandedPointPosition: null, PointPosition: GenericPointPosition.Left, DegradedPointPosition: GenericDegradedPointPosition.NotDegraded, AbilityToMove: GenericAbilityToMove.AbleToMove), true, false, CancellationToken.None);
foreach (var position in new [] { GenericPointPosition.Left, GenericPointPosition.Right, GenericPointPosition.UnintendedPosition, GenericPointPosition.NoEndPosition })
{
await connection.SendPointPosition(new GenericPointState(LastCommandedPointPosition: null, PointPosition: position, DegradedPointPosition: GenericDegradedPointPosition.NotDegraded, AbilityToMove: GenericAbilityToMove.AbleToMove));
}
Expand All @@ -114,11 +165,11 @@ public async Task Test_Receive_Position()
.ReturnsAsync(new PointMovePointCommand("99W1", "100", PointMovePointCommandCommandedPointPosition.SubsystemElectronicInterlockingRequestsALeftHandPointMoving).ToByteArray())
.ReturnsAsync(new PointMovePointCommand("99W1", "100", PointMovePointCommandCommandedPointPosition.SubsystemElectronicInterlockingRequestsARightHandPointMoving).ToByteArray());

var connection = new PointToInterlockingConnection(Mock.Of<ILogger<PointToInterlockingConnection>>(), _configuration, CancellationToken.None);
var builder = new PointToInterlockingConnectionBuilder(Mock.Of<ILogger<PointToInterlockingConnection>>(), _configuration, CancellationToken.None);

// Act
connection.Connect(mockConnection.Object);
await connection.InitializeConnection(new GenericPointState(LastCommandedPointPosition: null, PointPosition: GenericPointPosition.Left, DegradedPointPosition: GenericDegradedPointPosition.NotDegraded, AbilityToMove: GenericAbilityToMove.AbleToMove), true, CancellationToken.None);
var connection = builder.Connect(mockConnection.Object);
await connection.InitializeConnection(new GenericPointState(LastCommandedPointPosition: null, PointPosition: GenericPointPosition.Left, DegradedPointPosition: GenericDegradedPointPosition.NotDegraded, AbilityToMove: GenericAbilityToMove.AbleToMove), true, false, CancellationToken.None);
var position1 = await connection.ReceiveMovePointCommand(CancellationToken.None);
var position2 = await connection.ReceiveMovePointCommand(CancellationToken.None);

Expand All @@ -137,11 +188,11 @@ public async Task Test_TimeoutMessage()
mockConnection.Setup(x => x.SendAsync(Capture.In(args)))
.Returns(Task.FromResult(0));

var connection = new PointToInterlockingConnection(Mock.Of<ILogger<PointToInterlockingConnection>>(), _configuration, CancellationToken.None);
var builder = new PointToInterlockingConnectionBuilder(Mock.Of<ILogger<PointToInterlockingConnection>>(), _configuration, CancellationToken.None);

// Act
connection.Connect(mockConnection.Object);
await connection.InitializeConnection(new GenericPointState(LastCommandedPointPosition: null, PointPosition: GenericPointPosition.Left, DegradedPointPosition: GenericDegradedPointPosition.NotDegraded, AbilityToMove: GenericAbilityToMove.AbleToMove), true, CancellationToken.None);
var connection = builder.Connect(mockConnection.Object);
await connection.InitializeConnection(new GenericPointState(LastCommandedPointPosition: null, PointPosition: GenericPointPosition.Left, DegradedPointPosition: GenericDegradedPointPosition.NotDegraded, AbilityToMove: GenericAbilityToMove.AbleToMove), true, false, CancellationToken.None);
await connection.SendTimeoutMessage();

// Assert
Expand Down
Loading

0 comments on commit 94a1713

Please sign in to comment.