nifi-issues mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From GitBox <...@apache.org>
Subject [GitHub] [nifi-minifi-cpp] szaszm commented on a change in pull request #773: MINIFICPP-1202 - Extend interface and add new tests for MinifiConcurrentQueue
Date Mon, 04 May 2020 14:25:33 GMT

szaszm commented on a change in pull request #773:
URL: https://github.com/apache/nifi-minifi-cpp/pull/773#discussion_r419476317



##########
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<std::string> queue;
-  std::vector<std::string> results;
+namespace {
+
+namespace MinifiConcurrentQueueTestProducersConsumers {
 
-  std::thread producer([&queue]() {
+  // Producers
+
+  template <typename Queue>
+  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<std::string>&
queue, std::mutex& mutex) {
+    return std::thread([&queue, &mutex] {
+      std::unique_lock<std::mutex> 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<std::string>&
queue, std::vector<std::string>& 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<std::string>&
queue, std::vector<std::string>& 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<std::string>&
queue, std::vector<std::string>& results) {
+    return std::thread([&queue, &results] {
+      std::string s;
+      while (queue.dequeueWait(s)) {
+        results.push_back(s);
+      }
+    });
+  }
 
-TEST_CASE("TestConditionConqurrentQueue::testQueue", "[TestConditionQueue]") {
-  utils::ConditionConcurrentQueue<std::string> queue(true);
-  std::vector<std::string> results;
+  std::thread getConsumeWaitConsumerThread(utils::ConditionConcurrentQueue<std::string>&
queue, std::vector<std::string>& 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<std::string>&
queue, std::vector<std::string>& 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<std::string>&
queue, std::vector<std::string>& 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<std::string>&
queue, std::vector<std::string>& 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<std::string>&
queue, std::vector<std::string>& 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<std::string> queue;
+  std::vector<std::string> 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<std::string> queue;
-  std::set<std::string> 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:
       Many of my code review comments point out things that can be done in a shorter/simpler
way if there are no reasons for doing otherwise. Your preference for the "expected, actual"
notation is a good enough reason for me to ignore this inspection. Feel free to question my
suggestions with valid points.




----------------------------------------------------------------
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



Mime
View raw message