Max attempts for receive messages too

This commit is contained in:
Benito Palacios Sánchez 2014-12-08 15:39:18 +01:00
parent 4f698709a0
commit 3a06b1aa6b
2 changed files with 51 additions and 31 deletions

View File

@ -116,39 +116,46 @@ namespace UnitTests
() => SendCommand("test2", "arg1,arg2", "f5", 11)); () => SendCommand("test2", "arg1,arg2", "f5", 11));
} }
[Test] private void ReceiveMessageBad(int attempts)
public void ReceiveMessage()
{ {
// $ O K # 9 b
byte[] responseBad = new byte[] { 0x24, 0x4F, 0x4B, 0x23, 0x39, 0x62 };
// $ O K # 9 a // $ O K # 9 a
byte[] response = new byte[] { 0x24, 0x4F, 0x4B, 0x23, 0x39, 0x61 }; byte[] responseGood = new byte[] { 0x24, 0x4F, 0x4B, 0x23, 0x39, 0x61 };
connection.GetStream().Write(response, 0, response.Length);
for (int i = 0; i < attempts - 1; i++)
connection.GetStream().Write(responseBad, 0, responseBad.Length);
connection.GetStream().Write(responseGood, 0, responseGood.Length);
ReplyPacket responsePacket = presentation.ReceiveReply(); ReplyPacket responsePacket = presentation.ReceiveReply();
Assert.IsInstanceOf<OkReply>(responsePacket); Assert.IsInstanceOf<OkReply>(responsePacket);
for (int i = 0; i < attempts - 1; i++) {
byte nack = (byte)connection.GetStream().ReadByte();
Assert.AreEqual(RawPacket.Nack, nack);
}
byte ack = (byte)connection.GetStream().ReadByte(); byte ack = (byte)connection.GetStream().ReadByte();
Assert.AreEqual(RawPacket.Ack, ack); Assert.AreEqual(RawPacket.Ack, ack);
} }
[Test]
public void ReceiveMessage()
{
ReceiveMessageBad(1);
}
[Test] [Test]
public void RequestResent() public void RequestResent()
{ {
// $ O K # 9 b ReceiveMessageBad(3);
byte[] responseBad = new byte[] { 0x24, 0x4F, 0x4B, 0x23, 0x39, 0x62 }; }
connection.GetStream().Write(responseBad, 0, responseBad.Length);
// $ O K # 9 a [Test]
byte[] responseGood = new byte[] { 0x24, 0x4F, 0x4B, 0x23, 0x39, 0x61 }; public void ReceiveTooManyBadMessages()
connection.GetStream().Write(responseGood, 0, responseGood.Length); {
Assert.Throws<ProtocolViolationException>(() => ReceiveMessageBad(11));
ReplyPacket responsePacket = presentation.ReceiveReply();
Assert.IsInstanceOf<OkReply>(responsePacket);
byte firstNack = (byte)connection.GetStream().ReadByte();
Assert.AreEqual(RawPacket.Nack, firstNack);
byte ack = (byte)connection.GetStream().ReadByte();
Assert.AreEqual(RawPacket.Ack, ack);
} }
[Test] [Test]

View File

@ -73,8 +73,22 @@ namespace NitroDebugger.RSP
public ReplyPacket ReceiveReply() public ReplyPacket ReceiveReply()
{ {
ReplyPacket response = null; ReplyPacket response = null;
int count = 0;
while (response == null) { do {
if (count == MaxWriteAttemps)
throw new ProtocolViolationException("[PRES] Can not receive correctly");
count++;
response = this.NextReply();
} while (response == null);
return response;
}
private ReplyPacket NextReply()
{
ReplyPacket response = null;
try { try {
// Get data // Get data
byte[] packet = this.session.ReadPacket(PacketSeparator); byte[] packet = this.session.ReadPacket(PacketSeparator);
@ -86,7 +100,6 @@ namespace NitroDebugger.RSP
// Error... send NACK // Error... send NACK
this.session.Write(RawPacket.Nack); this.session.Write(RawPacket.Nack);
} }
}
return response; return response;
} }