Skip to content

Commit

Permalink
fix comments
Browse files Browse the repository at this point in the history
  • Loading branch information
Scretch9 committed Jan 8, 2024
1 parent 35b7b30 commit 3329c63
Show file tree
Hide file tree
Showing 19 changed files with 175 additions and 107 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ private static Mock<IConnection> CreateDefaultMockConnection()
{"PointSettings:AllPointMachinesCrucial", "false" },
{"PointSettings:ObserveAbilityToMove", "true" },
{"PointSettings:PDIVersion", "1" },
{"PointSettings:PDIChecksum", "0x0000" }
{"PointSettings:PDIChecksum", "0x00" }
};

private readonly IConfiguration _configuration = new ConfigurationBuilder()
Expand All @@ -44,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 @@ -62,16 +62,16 @@ 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);
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, 1, 2, new byte[] { 0x00, 0x00 }).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]);
Expand All @@ -89,16 +89,16 @@ public async Task Test_Initialization_Timeout(){
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);
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));
Assert.Equal(new PointPdiVersionCheckMessage("99W1________________", "INTERLOCKING________", PointPdiVersionCheckMessageResultPdiVersionCheck.PDIVersionsFromReceiverAndSenderDoMatch, 1, 2, new byte[] { 0x00, 0x00 }).ToByteArray(), receivedMessages[0]);
}

/// <summary>
Expand All @@ -115,10 +115,10 @@ public async Task Test_Initialization_Version_Mismatch(){
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);
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
Expand All @@ -136,10 +136,10 @@ 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);
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 List<GenericPointPosition>() { GenericPointPosition.Left, GenericPointPosition.Right, GenericPointPosition.UnintendedPosition, GenericPointPosition.NoEndPosition })
{
Expand All @@ -165,10 +165,10 @@ 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);
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 @@ -188,10 +188,10 @@ 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);
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();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ public class PointToInterlockingConnectionTest
{"PointSettings:AllPointMachinesCrucial", "false" },
{"PointSettings:ObserveAbilityToMove", "true" },
{"PointSettings:PDIVersion", "1" },
{"PointSettings:PDIChecksum", "0x0000" }
{"PointSettings:PDIChecksum", "0x00" }
};

private readonly IConfiguration _configuration = new ConfigurationBuilder()
Expand All @@ -35,13 +35,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 @@ -56,16 +56,15 @@ 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);
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, 1, 2, new byte[] { 0x00, 0x00 }).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]);
Expand All @@ -83,16 +82,16 @@ public async Task Test_Initialization_Timeout(){
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);
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));
Assert.Equal(new PointPdiVersionCheckMessage("99W1________________", "INTERLOCKING________", PointPdiVersionCheckMessageResultPdiVersionCheck.PDIVersionsFromReceiverAndSenderDoMatch, 1, 2, new byte[] { 0x00, 0x00 }).ToByteArray(), receivedMessages[0]);
}

/// <summary>
Expand All @@ -109,10 +108,10 @@ public async Task Test_Initialization_Version_Mismatch(){
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);
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
Expand All @@ -133,10 +132,10 @@ 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);
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.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));
Expand All @@ -162,10 +161,10 @@ 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);
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 @@ -188,10 +187,10 @@ 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);
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();

Expand Down
Loading

0 comments on commit 3329c63

Please sign in to comment.