Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Polishing #25

Merged
merged 24 commits into from
Jan 14, 2024
Merged
Show file tree
Hide file tree
Changes from 19 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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", "0x0000" }
};

private readonly IConfiguration _configuration = new ConfigurationBuilder()
Expand Down Expand Up @@ -64,17 +66,66 @@ public async Task Test_Initialization()

// Act
connection.Connect(mockConnection.Object);
await connection.InitializeConnection(new GenericPointState(LastCommandedPointPosition: null, PointPosition: GenericPointPosition.Left, DegradedPointPosition: GenericDegradedPointPosition.NotDegraded, AbilityToMove: GenericAbilityToMove.AbleToMove), true, CancellationToken.None);
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, 2, new byte[] { 0x00, 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(){
Scretch9 marked this conversation as resolved.
Show resolved Hide resolved
// 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 connection = new PointToInterlockingConnection(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, 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.PDIVersionsFromReceiverAndSenderDoMatch, 1, 2, new byte[] { 0x00, 0x00 }).ToByteArray(), receivedMessages[0]);
}

/// <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 connection = new PointToInterlockingConnection(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, 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 @@ -89,7 +140,7 @@ public async Task Test_Send_Position()

// Act
connection.Connect(mockConnection.Object);
await connection.InitializeConnection(new GenericPointState(LastCommandedPointPosition: null, PointPosition: GenericPointPosition.Left, DegradedPointPosition: GenericDegradedPointPosition.NotDegraded, AbilityToMove: GenericAbilityToMove.AbleToMove), true, CancellationToken.None);
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 List<GenericPointPosition>() { GenericPointPosition.Left, GenericPointPosition.Right, GenericPointPosition.UnintendedPosition, GenericPointPosition.NoEndPosition })
{
await connection.SendPointPosition(new GenericPointState(LastCommandedPointPosition: null, PointPosition: position, DegradedPointPosition: GenericDegradedPointPosition.NotDegraded, AbilityToMove: GenericAbilityToMove.AbleToMove));
Expand Down Expand Up @@ -118,7 +169,7 @@ public async Task Test_Receive_Position()

// Act
connection.Connect(mockConnection.Object);
await connection.InitializeConnection(new GenericPointState(LastCommandedPointPosition: null, PointPosition: GenericPointPosition.Left, DegradedPointPosition: GenericDegradedPointPosition.NotDegraded, AbilityToMove: GenericAbilityToMove.AbleToMove), true, CancellationToken.None);
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 @@ -141,7 +192,7 @@ public async Task Test_TimeoutMessage()

// Act
connection.Connect(mockConnection.Object);
await connection.InitializeConnection(new GenericPointState(LastCommandedPointPosition: null, PointPosition: GenericPointPosition.Left, DegradedPointPosition: GenericDegradedPointPosition.NotDegraded, AbilityToMove: GenericAbilityToMove.AbleToMove), true, CancellationToken.None);
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
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ public class PointToInterlockingConnectionTest
{"PointSettings:SimulatedTransitioningTimeSeconds", "0" },
{"PointSettings:AllPointMachinesCrucial", "false" },
{"PointSettings:ObserveAbilityToMove", "true" },
{"PointSettings:PDIVersion", "1" },
{"PointSettings:PDIChecksum", "0x0000" }
};

private readonly IConfiguration _configuration = new ConfigurationBuilder()
Expand Down Expand Up @@ -58,18 +60,67 @@ public async Task Test_Initialization()

// Act
connection.Connect(mockConnection.Object);
await connection.InitializeConnection(new GenericPointState(LastCommandedPointPosition: null, PointPosition: GenericPointPosition.Left, DegradedPointPosition: GenericDegradedPointPosition.NotDegraded, AbilityToMove: GenericAbilityToMove.AbleToMove), true, CancellationToken.None);
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, 2, new byte[] { 0x00, 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 connection = new PointToInterlockingConnection(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, 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.PDIVersionsFromReceiverAndSenderDoMatch, 1, 2, new byte[] { 0x00, 0x00 }).ToByteArray(), receivedMessages[0]);
}

/// <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 connection = new PointToInterlockingConnection(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, 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 @@ -86,7 +137,7 @@ public async Task Test_Send_Position()

// Act
connection.Connect(mockConnection.Object);
await connection.InitializeConnection(new GenericPointState(LastCommandedPointPosition: null, PointPosition: GenericPointPosition.Left, DegradedPointPosition: GenericDegradedPointPosition.NotDegraded, AbilityToMove: GenericAbilityToMove.AbleToMove), true, CancellationToken.None);
await connection.InitializeConnection(new GenericPointState(LastCommandedPointPosition: null, PointPosition: GenericPointPosition.Left, DegradedPointPosition: GenericDegradedPointPosition.NotDegraded, AbilityToMove: GenericAbilityToMove.AbleToMove), true, false, CancellationToken.None);
await connection.SendPointPosition(new GenericPointState(LastCommandedPointPosition: null, PointPosition: GenericPointPosition.Right, DegradedPointPosition: GenericDegradedPointPosition.NotDegraded, AbilityToMove: GenericAbilityToMove.AbleToMove));
await connection.SendPointPosition(new GenericPointState(LastCommandedPointPosition: null, PointPosition: GenericPointPosition.Left, DegradedPointPosition: GenericDegradedPointPosition.NotDegraded, AbilityToMove: GenericAbilityToMove.AbleToMove));
await connection.SendPointPosition(new GenericPointState(LastCommandedPointPosition: null, PointPosition: GenericPointPosition.UnintendedPosition, DegradedPointPosition: GenericDegradedPointPosition.NotDegraded, AbilityToMove: GenericAbilityToMove.AbleToMove));
Expand Down Expand Up @@ -115,7 +166,7 @@ public async Task Test_Receive_Position()

// Act
connection.Connect(mockConnection.Object);
await connection.InitializeConnection(new GenericPointState(LastCommandedPointPosition: null, PointPosition: GenericPointPosition.Left, DegradedPointPosition: GenericDegradedPointPosition.NotDegraded, AbilityToMove: GenericAbilityToMove.AbleToMove), true, CancellationToken.None);
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 @@ -141,7 +192,7 @@ public async Task Test_TimeoutMessage()

// Act
connection.Connect(mockConnection.Object);
await connection.InitializeConnection(new GenericPointState(LastCommandedPointPosition: null, PointPosition: GenericPointPosition.Left, DegradedPointPosition: GenericDegradedPointPosition.NotDegraded, AbilityToMove: GenericAbilityToMove.AbleToMove), true, CancellationToken.None);
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
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@ private static (EulynxLive.Point.Point point, Task pointTask, List<byte[]> recei
{"PointSettings:InitialPointPosition", initialPointState.PointPosition.ToString() },
{"PointSettings:InitialDegradedPointPosition", initialPointState.DegradedPointPosition.ToString() },
{"PointSettings:InitialAbilityToMove", initialPointState.AbilityToMove.ToString() },
{"PointSettings:PDIVersion", "1" },
{"PointSettings:PDIChecksum", "0x0000" }
};

var configuration = new ConfigurationBuilder()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@ private static (EulynxLive.Point.Point point, Task, List<byte[]> receivedBytes)
{"PointSettings:InitialPointPosition", initialPointState.PointPosition.ToString() },
{"PointSettings:InitialDegradedPointPosition", initialPointState.DegradedPointPosition.ToString() },
{"PointSettings:InitialAbilityToMove", initialPointState.AbilityToMove.ToString() },
{"PointSettings:PDIVersion", "1" },
{"PointSettings:PDIChecksum", "0x0000" }
};

var configuration = new ConfigurationBuilder()
Expand Down
Loading
Loading