Skip to content

Commit

Permalink
Deal with Observe failure
Browse files Browse the repository at this point in the history
Need to extend the timeout for the passive cancel because the RST needs to have time to get back to the server before the NON message is canceled out by the server.
  • Loading branch information
jimsch committed Sep 1, 2020
1 parent b93e1e0 commit d1b606e
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 5 deletions.
2 changes: 1 addition & 1 deletion CoAP.NET/Stack/ReliabilityLayer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -237,7 +237,7 @@ public override void ReceiveEmptyMessage(INextLayer nextLayer, Exchange exchange
exchange.CurrentRequest.IsRejected = true;
}
else {
exchange.CurrentResponse.IsRejected = true;
exchange.Response.IsRejected = true;
}

break;
Expand Down
22 changes: 18 additions & 4 deletions CoAP.Test/Observe/ObserveTests2.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
using CoAP.Test.Std10.MockDriver;
using CoAP.Test.Std10.MockItems;
using Com.AugustCellars.CoAP;
using Com.AugustCellars.CoAP.Log;
using Com.AugustCellars.CoAP.Net;
using Com.AugustCellars.CoAP.Server;
using Com.AugustCellars.CoAP.Server.Resources;
Expand Down Expand Up @@ -253,14 +254,18 @@ public void ObserveTest2()
[TestMethod]
public void ObserveTest3()
{
CoapConfig clientConfig = new CoapConfig();
CoapConfig clientConfig = new CoapConfig() {
MaxRetransmit = 0
};
LogManager.Level = LogLevel.Debug;

Pump = new MockMessagePump();

IEndPoint clientEndpoint = Pump.AddEndPoint("Client1", MockMessagePump.ClientAddress, clientConfig, new Type[] { typeof(ObserveLayer) });
IEndPoint clientEndpoint = Pump.AddEndPoint("Client1", MockMessagePump.ClientAddress, clientConfig, new Type[] { typeof(ObserveLayer), typeof(TokenLayer), typeof(ReliabilityLayer) });
clientEndpoint.Start();

CreateServer();
_config.NonTimeout = 100; // Change this value up - at 10 it cleans up the NON before the RST has a chance to get back.

CoapClient coapClient = new CoapClient {
EndPoint = clientEndpoint,
Expand Down Expand Up @@ -290,6 +295,7 @@ public void ObserveTest3()
switch (item.ItemType) {
case MockQueueItem.QueueType.NetworkSend:
if (item.Request != null) {
Debug.WriteLine($"Request: {item.Request}");
if (tokenBytes == null) {
tokenBytes = relation.Request.Token;
}
Expand All @@ -299,13 +305,16 @@ public void ObserveTest3()
CollectionAssert.AreEqual(tokenBytes, item.Request.Token);
}
else if (item.Response != null) {
Debug.WriteLine($"Response: {item.Response}");
Assert.IsTrue(item.Response.HasOption(OptionType.Observe));
Assert.AreEqual(count, item.Response.Observe);
CollectionAssert.AreEqual(tokenBytes, item.Response.Token);
dataSent = true;
}
else {
Debug.WriteLine($"RST: {item.EmptyMessage}");
emptyCount += 1;
dataSent = true;
}

break;
Expand All @@ -314,11 +323,12 @@ public void ObserveTest3()
Pump.Pump();
}
else if (dataSent) {
dataSent = false;
if (count >= 3) {
Assert.IsNull(lastResponse);
}
else {
Assert.IsTrue(trigger.WaitOne(1000));
Assert.IsTrue(trigger.WaitOne(10*1000));
Assert.IsNotNull(lastResponse);
Assert.IsTrue(lastResponse.HasOption(OptionType.Observe));
Assert.AreEqual(count, lastResponse.Observe);
Expand All @@ -339,9 +349,13 @@ public void ObserveTest3()
else if (count == 5) {
break;
}
else if (emptyCount != 0) {
_resource.UpdateContent($"New string {count}");
count += 1;
}
}

Assert.AreEqual(3, emptyCount);
Assert.AreEqual(1, emptyCount);
Assert.AreEqual(5, count);
}

Expand Down

0 comments on commit d1b606e

Please sign in to comment.