From 2fa77e639ff16ef07db55a5cc70433af3f03ef64 Mon Sep 17 00:00:00 2001 From: "Tristan B. Velloza Kildaire" Date: Thu, 6 Apr 2023 12:28:54 +0200 Subject: [PATCH] Queue MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - Added documentation for the constructor `this(ulong)` - Fixed issue #5 Unit tests - The `==` operator on strings does some normalization stuff which results in differing byte sequences and therefore inequality (see the `"Cucumber 😳️"` case), therefore casting to `byte[]` - Send another message tagged with `69` - Fixed comment for server code sending tagged `42` message - Call `manager.stop()` right at the end of the unit test --- source/tristanable/manager/watcher.d | 32 +++++++++++++++++++++++++--- source/tristanable/queue.d | 13 +++++++++++ 2 files changed, 42 insertions(+), 3 deletions(-) diff --git a/source/tristanable/manager/watcher.d b/source/tristanable/manager/watcher.d index 269d5dc..75483c1 100644 --- a/source/tristanable/manager/watcher.d +++ b/source/tristanable/manager/watcher.d @@ -126,7 +126,7 @@ unittest /** * Create a tagged message to send * - * tag 42 payload Hello + * tag 42 payload Cucumber 😳️ */ TaggedMessage message = new TaggedMessage(42, cast(byte[])"Cucumber 😳️"); byte[] tEncoded = message.encode(); @@ -144,6 +144,17 @@ unittest writeln("server send status: ", sendMessage(clientSocket, tEncoded)); writeln("server send [done]"); + + /** + * Create a tagged message to send + * + * tag 69 payload Bye + */ + message = new TaggedMessage(69, cast(byte[])"Bye"); + tEncoded = message.encode(); + writeln("server send status: ", sendMessage(clientSocket, tEncoded)); + + writeln("server send [done]"); } } @@ -176,9 +187,24 @@ unittest TaggedMessage dequeuedMessage = sixtyNine.dequeue(); writeln("unittest thread: Got '"~dequeuedMessage.toString()~"' decode payload to string '"~cast(string)dequeuedMessage.getPayload()~"'"); assert(dequeuedMessage.getTag() == 69); - assert(dequeuedMessage.getPayload() == "Hello"); + assert(dequeuedMessage.getPayload() == cast(byte[])"Hello"); + + /* Block on the unittest thread for a received message */ + writeln("unittest thread: Dequeue() blocking..."); + dequeuedMessage = sixtyNine.dequeue(); + writeln("unittest thread: Got '"~dequeuedMessage.toString()~"' decode payload to string '"~cast(string)dequeuedMessage.getPayload()~"'"); + assert(dequeuedMessage.getTag() == 69); + assert(dequeuedMessage.getPayload() == cast(byte[])"Bye"); + + /* Block on the unittest thread for a received message */ + writeln("unittest thread: Dequeue() blocking..."); + dequeuedMessage = fortyTwo.dequeue(); + writeln("unittest thread: Got '"~dequeuedMessage.toString()~"' decode payload to string '"~cast(string)dequeuedMessage.getPayload()~"'"); + assert(dequeuedMessage.getTag() == 42); + assert(dequeuedMessage.getPayload() == cast(byte[])"Cucumber 😳️"); - // while(true){} + /* Stop the manager */ + manager.stop(); } \ No newline at end of file diff --git a/source/tristanable/queue.d b/source/tristanable/queue.d index eec3efd..62e1010 100644 --- a/source/tristanable/queue.d +++ b/source/tristanable/queue.d @@ -10,6 +10,7 @@ import libsnooze; import core.sync.mutex : Mutex; import std.container.slist : SList; import tristanable.encoding; +import core.thread : dur; version(unittest) { @@ -34,6 +35,15 @@ public class Queue private ulong queueID; + /** + * Constructs a new Queue and immediately sets up the notification + * sub-system for the calling thread (the thread constructing this + * object) which ensures that a call to dequeue will immediately + * unblock on the first message received under this tag + * + * Params: + * queueID = the id to use for this queue + */ this(ulong queueID) { /* Initialize the queue lock */ @@ -44,6 +54,9 @@ public class Queue /* Set the queue id */ this.queueID = queueID; + + /* Ensure pipe existence (see https://deavmi.assigned.network/git/deavmi/tristanable/issues/5) */ + event.wait(dur!("seconds")(0)); } /**