mirror of
https://github.com/deavmi/doap
synced 2024-07-04 15:50:15 +02:00
CoapClient
- Disabled normal `get()`-based tests for now (whilst I test the timeout-based ones) - Added unit test to test timeout-based `get(Duration)` CoapRequestFuture - Added documentation for `get(Duration)` - Fixed bug whereby the `Mutex` was NOT unlock after a failed `wait(Duration)` (a timedout one)
This commit is contained in:
parent
89cc55f266
commit
f58f3cdb5c
|
@ -112,12 +112,16 @@ public class CoapClient
|
|||
this.messaging.close();
|
||||
|
||||
// Cancel all active request futures
|
||||
writeln("Awaking any sleeping futures.... (for shutdown)");
|
||||
this.requestsLock.lock();
|
||||
foreach(CoapRequest curReq; outgoingRequests)
|
||||
{
|
||||
writeln("Awaking any sleeping futures.... (for shutdown): ", curReq);
|
||||
curReq.future.cancel();
|
||||
writeln("Awaking any sleeping futures.... (for shutdown): ", curReq, " [done]");
|
||||
}
|
||||
this.requestsLock.unlock();
|
||||
writeln("Awaking any sleeping futures.... (for shutdown) [done]");
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -301,6 +305,37 @@ version(unittest)
|
|||
* and `client` module.
|
||||
*/
|
||||
unittest
|
||||
{
|
||||
// CoapClient client = new CoapClient("coap.me", 5683);
|
||||
|
||||
|
||||
// CoapRequestFuture future = client.newRequestBuilder()
|
||||
// .payload(cast(ubyte[])"Hello this is Tristan!")
|
||||
// .token([69])
|
||||
// .post();
|
||||
|
||||
|
||||
// writeln("Future start");
|
||||
// CoapPacket response = future.get();
|
||||
// writeln("Future done");
|
||||
// writeln("Got response: ", response);
|
||||
|
||||
// client.close();
|
||||
}
|
||||
|
||||
version(unittest)
|
||||
{
|
||||
import core.time : dur;
|
||||
import doap.client.exceptions : RequestTimeoutException;
|
||||
}
|
||||
|
||||
/**
|
||||
* Client testing
|
||||
*
|
||||
* See above except we test a timeout-based
|
||||
* request future here.
|
||||
*/
|
||||
unittest
|
||||
{
|
||||
CoapClient client = new CoapClient("coap.me", 5683);
|
||||
|
||||
|
@ -310,11 +345,21 @@ unittest
|
|||
.token([69])
|
||||
.post();
|
||||
|
||||
|
||||
try
|
||||
{
|
||||
writeln("Future start");
|
||||
CoapPacket response = future.get();
|
||||
writeln("Future done");
|
||||
writeln("Got response: ", response);
|
||||
CoapPacket response = future.get(dur!("msecs")(10));
|
||||
|
||||
// We should timeout and NOT get here
|
||||
assert(false);
|
||||
}
|
||||
catch(RequestTimeoutException e)
|
||||
{
|
||||
// We SHOULD time out
|
||||
assert(true);
|
||||
}
|
||||
|
||||
writeln("Closing client...");
|
||||
client.close();
|
||||
writeln("Client closed");
|
||||
}
|
|
@ -400,18 +400,32 @@ public class CoapRequestFuture
|
|||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Blocks until the response is received
|
||||
* but will unbllock if the timeout given
|
||||
* is exceeded
|
||||
*
|
||||
* Returns: the response as a `CoapPacket`
|
||||
* Throws:
|
||||
* RequestTimeoutException = on the
|
||||
* future request timing out
|
||||
*/
|
||||
public CoapPacket get(Duration timeout)
|
||||
{
|
||||
// We can only wait on a condition if we
|
||||
// ... first have a-hold of the lock
|
||||
this.mutex.lock();
|
||||
|
||||
scope(exit)
|
||||
{
|
||||
// Unlock the lock (either from successfully
|
||||
// ... waiting or timing out)
|
||||
this.mutex.unlock();
|
||||
}
|
||||
|
||||
// Await a response
|
||||
if(this.condition.wait(timeout))
|
||||
{
|
||||
// Upon waking up release lock
|
||||
this.mutex.unlock();
|
||||
|
||||
return this.response;
|
||||
}
|
||||
else
|
||||
|
|
Loading…
Reference in New Issue
Block a user