From issues-return-96714-archive-asf-public=cust-asf.ponee.io@nifi.apache.org Mon May 4 11:50:54 2020 Return-Path: X-Original-To: archive-asf-public@cust-asf.ponee.io Delivered-To: archive-asf-public@cust-asf.ponee.io Received: from mail.apache.org (hermes.apache.org [207.244.88.153]) by mx-eu-01.ponee.io (Postfix) with SMTP id F0D2F180608 for ; Mon, 4 May 2020 13:50:53 +0200 (CEST) Received: (qmail 51603 invoked by uid 500); 4 May 2020 11:50:53 -0000 Mailing-List: contact issues-help@nifi.apache.org; run by ezmlm Precedence: bulk List-Help: List-Unsubscribe: List-Post: List-Id: Reply-To: dev@nifi.apache.org Delivered-To: mailing list issues@nifi.apache.org Received: (qmail 51586 invoked by uid 99); 4 May 2020 11:50:53 -0000 Received: from ec2-52-202-80-70.compute-1.amazonaws.com (HELO gitbox.apache.org) (52.202.80.70) by apache.org (qpsmtpd/0.29) with ESMTP; Mon, 04 May 2020 11:50:53 +0000 From: =?utf-8?q?GitBox?= To: issues@nifi.apache.org Subject: =?utf-8?q?=5BGitHub=5D_=5Bnifi-minifi-cpp=5D_hunyadi-dev_commented_on_a_chan?= =?utf-8?q?ge_in_pull_request_=23773=3A_MINIFICPP-1202_-_Extend_interface_an?= =?utf-8?q?d_add_new_tests_for_MinifiConcurrentQueue?= Message-ID: <158859305326.26397.8622762876653705774.asfpy@gitbox.apache.org> Date: Mon, 04 May 2020 11:50:53 -0000 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: 8bit References: In-Reply-To: hunyadi-dev commented on a change in pull request #773: URL: https://github.com/apache/nifi-minifi-cpp/pull/773#discussion_r419379889 ########## File path: libminifi/test/unit/MinifiConcurrentQueueTests.cpp ########## @@ -29,132 +29,290 @@ namespace utils = org::apache::nifi::minifi::utils; -TEST_CASE("TestConqurrentQueue::testQueue", "[TestQueue]") { - utils::ConcurrentQueue queue; - std::vector results; +namespace { + +namespace MinifiConcurrentQueueTestProducersConsumers { - std::thread producer([&queue]() { + // Producers + + template + std::thread getSimpleProducerThread(Queue& queue) { + return std::thread([&queue] { queue.enqueue("ba"); std::this_thread::sleep_for(std::chrono::milliseconds(3)); queue.enqueue("dum"); std::this_thread::sleep_for(std::chrono::milliseconds(3)); queue.enqueue("tss"); }); + } - std::thread consumer([&queue, &results]() { - while (results.size() < 3) { - std::string s; - if (queue.tryDequeue(s)) { - results.push_back(s); - } else { - std::this_thread::sleep_for(std::chrono::milliseconds(1)); - } - } + std::thread getBlockedProducerThread(utils::ConditionConcurrentQueue& queue, std::mutex& mutex) { + return std::thread([&queue, &mutex] { + std::unique_lock lock(mutex); + queue.enqueue("ba"); + std::this_thread::sleep_for(std::chrono::milliseconds(3)); + queue.enqueue("dum"); + std::this_thread::sleep_for(std::chrono::milliseconds(3)); + queue.enqueue("tss"); }); + } - producer.join(); - consumer.join(); + // Consumers + + std::thread getSimpleTryDequeConsumerThread(utils::ConcurrentQueue& queue, std::vector& results) { + return std::thread([&queue, &results] { + while (results.size() < 3) { + std::string s; + if (queue.tryDequeue(s)) { + results.push_back(s); + } else { + std::this_thread::sleep_for(std::chrono::milliseconds(1)); + } + } + }); + } - REQUIRE(utils::StringUtils::join("-", results) == "ba-dum-tss"); -} + std::thread getSimpleConsumeConsumerThread(utils::ConcurrentQueue& queue, std::vector& results) { + return std::thread([&queue, &results] { + while (results.size() < 3) { + std::string s; + if (!queue.consume([&results] (const std::string& s) { results.push_back(s); })) { + std::this_thread::sleep_for(std::chrono::milliseconds(1)); + } + } + }); + } + std::thread getDequeueWaitConsumerThread(utils::ConditionConcurrentQueue& queue, std::vector& results) { + return std::thread([&queue, &results] { + std::string s; + while (queue.dequeueWait(s)) { + results.push_back(s); + } + }); + } -TEST_CASE("TestConditionConqurrentQueue::testQueue", "[TestConditionQueue]") { - utils::ConditionConcurrentQueue queue(true); - std::vector results; + std::thread getConsumeWaitConsumerThread(utils::ConditionConcurrentQueue& queue, std::vector& results) { + return std::thread([&queue, &results] { + while (results.size() < 3) { + std::string s; + if (!queue.consumeWait([&results] (const std::string& s) { results.push_back(s); })) { + std::this_thread::sleep_for(std::chrono::milliseconds(1)); + } + } + }); + } - std::thread producer([&queue]() { - queue.enqueue("ba"); - std::this_thread::sleep_for(std::chrono::milliseconds(3)); - queue.enqueue("dum"); - std::this_thread::sleep_for(std::chrono::milliseconds(3)); - queue.enqueue("tss"); - }); + std::thread getSpinningReaddingDequeueConsumerThread(utils::ConcurrentQueue& queue, std::vector& results) { + return std::thread([&queue, &results] { + while (results.size() < 3) { + std::string s; + if (queue.tryDequeue(s)) { + // Unique elements only + if (!std::count(results.begin(), results.end(), s)) { + results.push_back(s); + } + queue.enqueue(std::move(s)); + } else { + std::this_thread::sleep_for(std::chrono::milliseconds(1)); + } + } + }); + } - std::thread consumer([&queue, &results]() { - std::string s; - while (queue.dequeueWait(s)) { - results.push_back(s); - } - }); + std::thread getReaddingDequeueConsumerThread(utils::ConditionConcurrentQueue& queue, std::vector& results) { + return std::thread([&queue, &results] { + std::string s; + while (queue.dequeueWait(s)) { + if (!std::count(results.begin(), results.end(), s)) { + results.push_back(s); + } + // The consumer is busy enqueing so noone is waiting for this ;( + queue.enqueue(std::move(s)); + } + }); + } - producer.join(); + std::thread getDequeueWaitForConsumerThread(utils::ConditionConcurrentQueue& queue, std::vector& results) { + return std::thread([&queue, &results] { + const std::size_t max_read_attempts = 6; + std::size_t attemt_num = 0; + while (results.size() < 3 && attemt_num < max_read_attempts) { + ++attemt_num; + std::string s; + if (queue.dequeueWaitFor(s, std::chrono::milliseconds(3))) { + results.push_back(s); + } + } + }); + } + + std::thread getConsumeWaitForConsumerThread(utils::ConditionConcurrentQueue& queue, std::vector& results) { + return std::thread([&queue, &results]() { + const std::size_t max_read_attempts = 6; + std::size_t attemt_num = 0; + while (results.size() < 3 && attemt_num < max_read_attempts) { + ++attemt_num; + std::string s; + queue.consumeWaitFor([&results] (const std::string& s) { results.push_back(s); }, std::chrono::milliseconds(3)); + } + }); + } - std::this_thread::sleep_for(std::chrono::milliseconds(10)); +} // namespace MinifiConcurrentQueueTestProducersConsumers - queue.stop(); +TEST_CASE("TestConcurrentQueue", "[TestConcurrentQueue]") { + using namespace MinifiConcurrentQueueTestProducersConsumers; - consumer.join(); + utils::ConcurrentQueue queue; + std::vector results; - REQUIRE(utils::StringUtils::join("-", results) == "ba-dum-tss"); -} + SECTION("empty queue") { + SECTION("default initialized queue is empty") { + REQUIRE(queue.empty()); + } + SECTION("trying to update based on empty queue preserves original data") { + std::string s { "Unchanged" }; -/* In this testcase the consumer thread puts back all items to the queue to consume again - * Even in this case the ones inserted later by the producer should be consumed */ -TEST_CASE("TestConqurrentQueue::testQueueWithReAdd", "[TestQueueWithReAdd]") { - utils::ConcurrentQueue queue; - std::set results; - - std::thread producer([&queue]() { - queue.enqueue("ba"); - std::this_thread::sleep_for(std::chrono::milliseconds(3)); - queue.enqueue("dum"); - std::this_thread::sleep_for(std::chrono::milliseconds(3)); - queue.enqueue("tss"); - }); + SECTION("tryDequeue on empty queue returns false") { + REQUIRE(false == queue.tryDequeue(s)); Review comment: Sure, I'll replace this, I am just used to writing tests in `expected, actual` notation. ---------------------------------------------------------------- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. For queries about this service, please contact Infrastructure at: users@infra.apache.org