From abe64f7701c22680b721bb33811961e91af5f08a Mon Sep 17 00:00:00 2001 From: "Tristan B. Velloza Kildaire" Date: Wed, 3 May 2023 23:10:45 +0200 Subject: [PATCH 1/2] Manager - Added a TODO for the future `removeQueue(Queue)` and `removeQueu_nothrow(Queue)` --- source/tristanable/manager/manager.d | 2 ++ 1 file changed, 2 insertions(+) diff --git a/source/tristanable/manager/manager.d b/source/tristanable/manager/manager.d index ec0c047..32af8a7 100644 --- a/source/tristanable/manager/manager.d +++ b/source/tristanable/manager/manager.d @@ -265,6 +265,8 @@ public class Manager return true; } + // TODO: Impkement releaseQueu_nothrow and releaseQueue + /** * Sets the default queue * From 798acba4aafa1b1da46d9bb1da68eea0713026a7 Mon Sep 17 00:00:00 2001 From: "Tristan B. Velloza Kildaire" Date: Thu, 4 May 2023 09:45:06 +0200 Subject: [PATCH 2/2] Manager - Implemented `releaseQueue(Queue)` - Implemented `releaseQueue_nothrow(Queue)` Unit tests - Added unit test for `releaseQueue(Queue)` --- source/tristanable/manager/manager.d | 87 +++++++++++++++++++++++++++- 1 file changed, 86 insertions(+), 1 deletion(-) diff --git a/source/tristanable/manager/manager.d b/source/tristanable/manager/manager.d index 32af8a7..33de7f9 100644 --- a/source/tristanable/manager/manager.d +++ b/source/tristanable/manager/manager.d @@ -265,7 +265,61 @@ public class Manager return true; } - // TODO: Impkement releaseQueu_nothrow and releaseQueue + /** + * De-registers the given queue from the manager + * + * Params: + * queue = the queue to de-register + * Throws: + * TristanableException if a queue with the provided id cannot be found + */ + public void releaseQueue(Queue queue) + { + /* Try to de-register the queue */ + bool status = releaseQueue_nothrow(queue); + + /* If de-registration was not successful */ + if(!status) + { + throw new TristanableException(ErrorType.QUEUE_NOT_FOUND); + } + } + + /** + * De-registers the given queue from the manager + * + * Params: + * queue = the queue to de-register + * Returns: true if de-registration was successful, false otherwise + */ + public bool releaseQueue_nothrow(Queue queue) + { + /* Lock the queue of queues */ + queuesLock.lock(); + + /* On return or error */ + scope(exit) + { + /* Unlock the queue of queues */ + queuesLock.unlock(); + } + + /* Search for the queue, return false if it does NOT exist */ + foreach(Queue curQueue; queues) + { + if(curQueue.getID() == queue.getID()) + { + /* Remove the queue */ + queues.linearRemoveElement(queue); + + /* De-registration succeeded */ + return true; + } + } + + /* De-registration failed */ + return false; + } /** * Sets the default queue @@ -441,6 +495,37 @@ unittest } } +/** + * Tests registering a queue, de-registering it and + * then registering it again + */ +unittest +{ + /* Create a manager */ + Manager manager = new Manager(nullSock); + + /* Create a new queue with tag 69 */ + Queue queue = new Queue(69); + + /* Register the queue */ + manager.registerQueue(queue); + + /* Ensure it is registered */ + assert(queue == manager.getQueue(69)); + + /* De-register the queue */ + manager.releaseQueue(queue); + + /* Ensure it is de-registered */ + assert(manager.getQueue_nothrow(69) is null); + + /* Register the queue (again) */ + manager.registerQueue(queue); + + /* Ensure it is registered (again) */ + assert(queue == manager.getQueue(69)); +} + /** * Tests registering a queue using the "next available queue" * method