Skip to content

Commit

Permalink
Remove CancellationToken from ICoapEndpoint
Browse files Browse the repository at this point in the history
  • Loading branch information
NZSmartie committed Aug 29, 2017
1 parent 0b9642e commit 0595303
Show file tree
Hide file tree
Showing 6 changed files with 37 additions and 38 deletions.
44 changes: 22 additions & 22 deletions CoAPNet.Tests/CoapClientTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ public void TestClientRequest()
var mockClientEndpoint = new Mock<ICoapEndpoint>();

mockClientEndpoint
.Setup(c => c.SendAsync(It.IsAny<CoapPacket>(), It.IsAny<CancellationToken>()))
.Setup(c => c.SendAsync(It.IsAny<CoapPacket>()))
.Returns(Task.CompletedTask);

// Act
Expand All @@ -41,7 +41,7 @@ public void TestClientRequest()
}

// Assert
mockClientEndpoint.Verify(cep => cep.SendAsync(It.IsAny<CoapPacket>(), It.IsAny<CancellationToken>()));
mockClientEndpoint.Verify(cep => cep.SendAsync(It.IsAny<CoapPacket>()));
}

[Test]
Expand All @@ -64,10 +64,10 @@ public void TestClientResponse()
};

mockClientEndpoint
.Setup(c => c.SendAsync(It.IsAny<CoapPacket>(), It.IsAny<CancellationToken>()))
.Setup(c => c.SendAsync(It.IsAny<CoapPacket>()))
.Returns(Task.CompletedTask);
mockClientEndpoint
.SetupSequence(c => c.ReceiveAsync(It.IsAny<CancellationToken>()))
.SetupSequence(c => c.ReceiveAsync())
.Returns(Task.FromResult(new CoapPacket{Payload = expected.Serialise()}))
.Throws(new CoapEndpointException("Endpoint closed"));

Expand All @@ -92,7 +92,7 @@ public void TestClientResponse()
}

// Assert
mockClientEndpoint.Verify(x => x.ReceiveAsync(It.IsAny<CancellationToken>()), Times.AtLeastOnce);
mockClientEndpoint.Verify(x => x.ReceiveAsync(), Times.AtLeastOnce);
}

[Test]
Expand All @@ -115,10 +115,10 @@ public void TestClientResponseWithDelay()
};

mockClientEndpoint
.Setup(c => c.SendAsync(It.IsAny<CoapPacket>(), It.IsAny<CancellationToken>()))
.Setup(c => c.SendAsync(It.IsAny<CoapPacket>()))
.Returns(Task.CompletedTask);
mockClientEndpoint
.SetupSequence(c => c.ReceiveAsync(It.IsAny<CancellationToken>()))
.SetupSequence(c => c.ReceiveAsync())
.Returns(Task.Delay(500).ContinueWith(t => new CoapPacket { Payload = expected.Serialise() }))
.Throws(new CoapEndpointException("Endpoint closed"));

Expand Down Expand Up @@ -146,7 +146,7 @@ public void TestClientResponseWithDelay()
}

// Assert
mockClientEndpoint.Verify(x => x.ReceiveAsync(It.IsAny<CancellationToken>()), Times.AtLeastOnce);
mockClientEndpoint.Verify(x => x.ReceiveAsync(), Times.AtLeastOnce);
}

[Test]
Expand All @@ -163,7 +163,7 @@ public void TestRejectEmptyMessageWithFormatError()

var mockClientEndpoint = new Mock<ICoapEndpoint>();
mockClientEndpoint
.SetupSequence(c => c.ReceiveAsync(It.IsAny<CancellationToken>()))
.SetupSequence(c => c.ReceiveAsync())
.Returns(Task.FromResult(new CoapPacket
{
Payload = new byte[] { 0x40, 0x00, 0x12, 0x34, 0xFF, 0x12, 0x34 } // "Empty" Confirmable Message with a payload
Expand All @@ -186,7 +186,7 @@ public void TestRejectEmptyMessageWithFormatError()

// Assert
mockClientEndpoint.Verify(
cep => cep.SendAsync(It.Is<CoapPacket>(p => p.Payload.SequenceEqual(expected.Serialise())), It.IsAny<CancellationToken>()),
cep => cep.SendAsync(It.Is<CoapPacket>(p => p.Payload.SequenceEqual(expected.Serialise()))),
Times.Exactly(1));
}

Expand Down Expand Up @@ -217,7 +217,7 @@ public void TestRequestWithSeperateResponse()

var mockClientEndpoint = new Mock<ICoapEndpoint>();
mockClientEndpoint
.SetupSequence(c => c.ReceiveAsync(It.IsAny<CancellationToken>()))
.SetupSequence(c => c.ReceiveAsync())
.Returns(Task.FromResult(new CoapPacket
{
Payload = new CoapMessage
Expand Down Expand Up @@ -267,11 +267,11 @@ public void TestRequestWithSeperateResponse()
}
// Assert
mockClientEndpoint.Verify(
cep => cep.SendAsync(It.Is<CoapPacket>(p => p.Payload.SequenceEqual(requestMessage.Serialise())), It.IsAny<CancellationToken>()),
cep => cep.SendAsync(It.Is<CoapPacket>(p => p.Payload.SequenceEqual(requestMessage.Serialise()))),
Times.Exactly(1));

mockClientEndpoint.Verify(
cep => cep.SendAsync(It.Is<CoapPacket>(p => p.Payload.SequenceEqual(acknowledgeMessage.Serialise())), It.IsAny<CancellationToken>()),
cep => cep.SendAsync(It.Is<CoapPacket>(p => p.Payload.SequenceEqual(acknowledgeMessage.Serialise()))),
Times.Exactly(1));
}

Expand All @@ -293,7 +293,7 @@ public void TestRetransmissionAttempts()

var mockClientEndpoint = new Mock<ICoapEndpoint>();
mockClientEndpoint
.SetupSequence(c => c.ReceiveAsync(It.IsAny<CancellationToken>()))
.SetupSequence(c => c.ReceiveAsync())
.Returns(Task.Delay(500).ContinueWith(delayTask => new CoapPacket
{
Payload = new CoapMessage
Expand Down Expand Up @@ -324,7 +324,7 @@ public void TestRetransmissionAttempts()

// Assert
mockClientEndpoint.Verify(
cep => cep.SendAsync(It.Is<CoapPacket>(p => p.Payload.SequenceEqual(requestMessage.Serialise())), It.IsAny<CancellationToken>()),
cep => cep.SendAsync(It.Is<CoapPacket>(p => p.Payload.SequenceEqual(requestMessage.Serialise()))),
Times.Exactly(2));
}

Expand All @@ -346,7 +346,7 @@ public void TestRetransmissionMaxAttempts()

var mockClientEndpoint = new Mock<ICoapEndpoint>();
mockClientEndpoint
.SetupSequence(c => c.ReceiveAsync(It.IsAny<CancellationToken>()))
.SetupSequence(c => c.ReceiveAsync())
.Returns(Task.Delay(2000).ContinueWith<CoapPacket>(delayTask => throw new CoapEndpointException("Endpoint closed")));

// Act
Expand All @@ -369,7 +369,7 @@ public void TestRetransmissionMaxAttempts()

// Assert
mockClientEndpoint.Verify(
cep => cep.SendAsync(It.Is<CoapPacket>(p => p.Payload.SequenceEqual(requestMessage.Serialise())), It.IsAny<CancellationToken>()),
cep => cep.SendAsync(It.Is<CoapPacket>(p => p.Payload.SequenceEqual(requestMessage.Serialise()))),
Times.Exactly(3));
}

Expand Down Expand Up @@ -457,10 +457,10 @@ public void TestMulticastMessagFromMulticastEndpoint()

mockClientEndpoint.Setup(c => c.IsMulticast).Returns(true);
mockClientEndpoint
.Setup(c => c.SendAsync(It.IsAny<CoapPacket>(), It.IsAny<CancellationToken>()))
.Setup(c => c.SendAsync(It.IsAny<CoapPacket>()))
.Returns(Task.CompletedTask);
mockClientEndpoint
.SetupSequence(c => c.ReceiveAsync(It.IsAny<CancellationToken>()))
.SetupSequence(c => c.ReceiveAsync())
.Returns(Task.FromResult(mockPayload.Object))
.Throws(new CoapEndpointException("Endpoint closed"));

Expand Down Expand Up @@ -493,10 +493,10 @@ public void TestMulticastMessageIsNonConfirmable()

mockClientEndpoint.Setup(c => c.IsMulticast).Returns(true);
mockClientEndpoint
.Setup(c => c.SendAsync(It.IsAny<CoapPacket>(), It.IsAny<CancellationToken>()))
.Setup(c => c.SendAsync(It.IsAny<CoapPacket>()))
.Returns(Task.CompletedTask);
mockClientEndpoint
.SetupSequence(c => c.ReceiveAsync(It.IsAny<CancellationToken>()))
.SetupSequence(c => c.ReceiveAsync())
.Returns(Task.FromResult(new CoapPacket
{
Payload = new byte[] { 0x40, 0x00, 0x12, 0x34, 0xFF, 0x12, 0x34 } // "Empty" Confirmable Message with a payload
Expand All @@ -519,7 +519,7 @@ public void TestMulticastMessageIsNonConfirmable()
}

// Assert
mockClientEndpoint.Verify(x => x.SendAsync(It.IsAny<CoapPacket>(), It.IsAny<CancellationToken>()), Times.Never, "Multicast Message was responded to whenn it shouldn't");
mockClientEndpoint.Verify(x => x.SendAsync(It.IsAny<CoapPacket>()), Times.Never, "Multicast Message was responded to whenn it shouldn't");
}

// TODO: Test Multicast Message Error Does Not Reset
Expand Down
12 changes: 6 additions & 6 deletions CoAPNet.Tests/CoapResourceHandlerTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,15 +27,15 @@ public void Setup()
{
_endpoint = new Mock<ICoapEndpoint>();
_endpoint.Setup(e => e.BaseUri).Returns(_baseUri);
_endpoint.Setup(e => e.SendAsync(It.IsAny<CoapPacket>(), It.IsAny<CancellationToken>())).Returns(Task.FromResult(0));
_endpoint.Setup(e => e.SendAsync(It.IsAny<CoapPacket>())).Returns(Task.FromResult(0));
}

[Test]
public void TestResponse()
{
// Arrange
_endpoint
.Setup(c => c.SendAsync(It.IsAny<CoapPacket>(), It.IsAny<CancellationToken>()))
.Setup(c => c.SendAsync(It.IsAny<CoapPacket>()))
.Returns(Task.FromResult(0))
.Verifiable();

Expand Down Expand Up @@ -155,7 +155,7 @@ public void TestResourceMethodNotImplemented()
var expectedMessage = CoapMessageUtility
.FromException(new NotImplementedException()).Serialise();
_endpoint
.Setup(c => c.SendAsync(It.Is<CoapPacket>(p => p.Payload.SequenceEqual(expectedMessage)), It.IsAny<CancellationToken>()))
.Setup(c => c.SendAsync(It.Is<CoapPacket>(p => p.Payload.SequenceEqual(expectedMessage))))
.Returns(Task.FromResult(0))
.Verifiable();

Expand Down Expand Up @@ -184,7 +184,7 @@ public void TestResourceMethodBadOption()
var expectedMessage = CoapMessageUtility
.FromException(new CoapOptionException("Unsupported critical option (45575)")).Serialise();
_endpoint
.Setup(c => c.SendAsync(It.Is<CoapPacket>(p => p.Payload.SequenceEqual(expectedMessage)), It.IsAny<CancellationToken>()))
.Setup(c => c.SendAsync(It.Is<CoapPacket>(p => p.Payload.SequenceEqual(expectedMessage))))
.Returns(Task.FromResult(0))
.Verifiable();

Expand Down Expand Up @@ -213,7 +213,7 @@ public void TestResourceNotFound()
var expectedMessage = CoapMessageUtility.CreateMessage(CoapMessageCode.NotFound, $"Resouce {new Uri(_baseUri, "/test")} was not found", CoapMessageType.Acknowledgement).Serialise();

_endpoint
.Setup(e => e.SendAsync(It.Is<CoapPacket>(p => p.Payload.SequenceEqual(expectedMessage)), It.IsAny<CancellationToken>()))
.Setup(e => e.SendAsync(It.Is<CoapPacket>(p => p.Payload.SequenceEqual(expectedMessage))))
.Returns(Task.FromResult(0))
.Verifiable();

Expand All @@ -236,7 +236,7 @@ public void TestResourceInternalError()
var expectedMessage = CoapMessageUtility.CreateMessage(CoapMessageCode.InternalServerError, "An unexpected error occured", CoapMessageType.Reset).Serialise();

_endpoint
.Setup(e => e.SendAsync(It.Is<CoapPacket>(p => p.Payload.SequenceEqual(expectedMessage)), It.IsAny<CancellationToken>()))
.Setup(e => e.SendAsync(It.Is<CoapPacket>(p => p.Payload.SequenceEqual(expectedMessage))))
.Returns(Task.FromResult(0))
.Verifiable();

Expand Down
4 changes: 2 additions & 2 deletions CoAPNet.Udp/CoapUdpEndPoint.cs
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ public void Dispose()
Client?.Dispose();
}

public async Task<CoapPacket> ReceiveAsync(CancellationToken token = default (CancellationToken))
public async Task<CoapPacket> ReceiveAsync()
{
if (Client == null)
await BindAsync();
Expand All @@ -94,7 +94,7 @@ public void Dispose()
};
}

public async Task SendAsync(CoapPacket packet, CancellationToken token = default (CancellationToken))
public async Task SendAsync(CoapPacket packet)
{
if (Client == null)
await BindAsync();
Expand Down
4 changes: 2 additions & 2 deletions CoAPNet/CoapClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ private async Task ReceiveAsyncInternal()
{
while (true)
{
var payload = await Endpoint.ReceiveAsync(CancellationToken.None);
var payload = await Endpoint.ReceiveAsync();
var message = new CoapMessage(Endpoint.IsMulticast);
try
{
Expand Down Expand Up @@ -199,7 +199,7 @@ await Endpoint.SendAsync(new CoapPacket
{
Payload = message.Serialise(),
Endpoint = endpoint
}, token).ConfigureAwait(false);
}).ConfigureAwait(false);
}

internal void SetNextMessageId(int value)
Expand Down
3 changes: 1 addition & 2 deletions CoAPNet/CoapHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -169,8 +169,7 @@ await connection.LocalEndpoint.SendAsync(
{
Endpoint = connection.RemoteEndpoint,
Payload = result.Serialise()
},
CancellationToken.None);
});
}
}
}
Expand Down
8 changes: 4 additions & 4 deletions CoAPNet/ICoapEndpoint.cs
Original file line number Diff line number Diff line change
Expand Up @@ -48,13 +48,13 @@ public interface ICoapEndpoint : IDisposable
/// <param name="packet"></param>
/// <param name="token"></param>
/// <returns></returns>
Task SendAsync(CoapPacket packet, CancellationToken token);
Task SendAsync(CoapPacket packet);

/// <summary>
/// Called by [service] to receive data from the endpoint layer
/// </summary>
/// <returns></returns>
Task<CoapPacket> ReceiveAsync(CancellationToken token);
Task<CoapPacket> ReceiveAsync();
}

/// <summary>
Expand All @@ -69,12 +69,12 @@ public void Dispose()
public bool IsSecure { get; internal set; }
public bool IsMulticast { get; internal set; }
public Uri BaseUri { get; internal set; }
public Task SendAsync(CoapPacket packet, CancellationToken token)
public Task SendAsync(CoapPacket packet)
{
throw new InvalidOperationException($"{nameof(CoapEndpoint)} can not be used to send and receive");
}

public Task<CoapPacket> ReceiveAsync(CancellationToken token)
public Task<CoapPacket> ReceiveAsync()
{
throw new InvalidOperationException($"{nameof(CoapEndpoint)} can not be used to send and receive");
}
Expand Down

0 comments on commit 0595303

Please sign in to comment.