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] hunyadi-dev commented on a change in pull request #773: MINIFICPP-1202 - Extend interface and add new tests for MinifiConcurrentQueue
Date Mon, 04 May 2020 12:45:53 GMT

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



##########
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));
+      }
 
-  std::thread consumer([&queue, &results]() {
-    while (results.size() < 3) {
-      std::string s;
-      if (queue.tryDequeue(s)) {
-        results.insert(s);
-        queue.enqueue(std::move(s));
-      } else {
-        std::this_thread::sleep_for(std::chrono::milliseconds(1));
+      SECTION("consume on empty queue returns false") {
+        bool ret = queue.consume([&s] (const std::string& elem) { s = elem; });
+        REQUIRE(false == ret);
       }
+      REQUIRE(s == "Unchanged");
     }
-  });
+  }
 
-  producer.join();
-  consumer.join();
+  SECTION("non-empty queue") {
+    SECTION("the queue is first-in-first-out") {
+      for (std::size_t i = 0; i < 20; ++i) {
+        queue.enqueue(std::to_string(i));
+      }
+      SECTION("tryDequeue preserves order") {
+        for (std::size_t i = 0; i < 20; ++i) {
+          std::string s;
+          queue.tryDequeue(s);
+          REQUIRE(s == std::to_string(i));
+        }
+        REQUIRE(queue.empty());
+      }
+      SECTION("consume preserves order") {
+        for (std::size_t i = 0; i < 20; ++i) {
+          std::string s;
+          queue.consume([&s] (const std::string& elem) { s = elem; });
+          REQUIRE(s == std::to_string(i));
+        }
+        REQUIRE(queue.empty());
+      }
+      SECTION("insertion does not reorder") {
+        for (std::size_t i = 0; i < 20; ++i) {
+          std::string s;
+          queue.tryDequeue(s);
+          queue.enqueue("0");
+          queue.enqueue("9");
+          REQUIRE(s == std::to_string(i));
+        }
+        REQUIRE(40 == queue.size());
+      }
+    }
+  }
+}
+
+TEST_CASE("TestConcurrentQueue::testProducerConsumer", "[TestConcurrentQueueProducerConsumer]")
{
+  using namespace MinifiConcurrentQueueTestProducersConsumers;
+  utils::ConcurrentQueue<std::string> queue;
+  std::vector<std::string> results;
+
+  SECTION("producers and consumers work synchronized") {
+    std::thread producer;
+    std::thread consumer;
+    SECTION("using tryDequeue") {
+        producer = getSimpleProducerThread(queue);
+        consumer = getSimpleTryDequeConsumerThread(queue, results);
+      }
+    SECTION("using consume") {
+        producer = getSimpleProducerThread(queue);
+        consumer = getSimpleConsumeConsumerThread(queue, results);
+    }
+    /* 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 */
+    SECTION("with readd") {
+      producer = getSimpleProducerThread(queue);
+      consumer = getSpinningReaddingDequeueConsumerThread(queue, results);
+    }
+    producer.join();
+    consumer.join();
+  }
 
   REQUIRE(utils::StringUtils::join("-", results) == "ba-dum-tss");
 }
 
-/* The same test as above, but covering the ConditionConcurrentQueue */
-TEST_CASE("TestConditionConqurrentQueue::testQueueWithReAdd", "[TestConditionQueueWithReAdd]")
{
+TEST_CASE("TestConditionConcurrentQueue::testProducerConsumer", "[TestConditionConcurrentQueueProducerConsumer]")
{
+  using namespace MinifiConcurrentQueueTestProducersConsumers;
   utils::ConditionConcurrentQueue<std::string> queue(true);
-  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");
-  });
+  std::vector<std::string> results;
 
-  std::thread consumer([&queue, &results]() {
-    std::string s;
-    while (queue.dequeueWait(s)) {
-      results.insert(s);
-      queue.enqueue(std::move(s));
+  SECTION("consumers fetching data from producers is synchronized and fifo") {
+    std::thread producer { getSimpleProducerThread(queue) };
+    std::thread consumer;
+    SECTION("using dequeueWait") {
+      consumer = getDequeueWaitConsumerThread(queue, results);
     }
-  });
-
-  producer.join();
+    SECTION("using consumeWait") {
+      consumer = getConsumeWaitConsumerThread(queue, results);
+    }
+    SECTION("using dequeueWaitFor") {
+      consumer = getDequeueWaitForConsumerThread(queue, results);
+    }
+    SECTION("using consumeWaitFor") {
+      consumer = getConsumeWaitForConsumerThread(queue, results);
+    }
+    producer.join();
+    std::this_thread::sleep_for(std::chrono::milliseconds(10));
+    queue.stop();
+    consumer.join();
 
-  std::this_thread::sleep_for(std::chrono::milliseconds(10));
+    REQUIRE(utils::StringUtils::join("-", results) == "ba-dum-tss");
+  }
 
-  queue.stop();
+  /* The same test as above, but covering the ConditionConcurrentQueue */
+  SECTION("with readd") {
+    std::thread consumer { getReaddingDequeueConsumerThread(queue, results) };
+    std::this_thread::sleep_for(std::chrono::milliseconds(1));
+    std::thread producer { getSimpleProducerThread(queue) };
+    std::this_thread::sleep_for(std::chrono::milliseconds(10));
+    producer.join();
+    queue.stop();
+    consumer.join();
+
+    REQUIRE(utils::StringUtils::join("-", results) == "ba-dum-tss");
+  }
 
-  consumer.join();
+  // Blocked producers
+  SECTION("consumer times out when using waitFor and time is up") {
+    std::mutex mutex;
+    std::unique_lock<std::mutex> lock(mutex);
+    std::thread producer{ getBlockedProducerThread(queue, mutex) };
+    std::thread consumer;
+    SECTION("using dequeueWaitFor") {
+      consumer = getDequeueWaitForConsumerThread(queue, results);
+    }
+    SECTION("using consumeWaitFor") {
+      consumer = getConsumeWaitForConsumerThread(queue, results);
+    }
+    consumer.join();
+    lock.unlock();
+    producer.join();
 
-  REQUIRE(utils::StringUtils::join("-", results) == "ba-dum-tss");
+    REQUIRE(0 == results.size());

Review comment:
       Updated as requested.

##########
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) {

Review comment:
       Updated as requested.

##########
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));
+      }
 
-  std::thread consumer([&queue, &results]() {
-    while (results.size() < 3) {
-      std::string s;
-      if (queue.tryDequeue(s)) {
-        results.insert(s);
-        queue.enqueue(std::move(s));
-      } else {
-        std::this_thread::sleep_for(std::chrono::milliseconds(1));
+      SECTION("consume on empty queue returns false") {
+        bool ret = queue.consume([&s] (const std::string& elem) { s = elem; });
+        REQUIRE(false == ret);

Review comment:
       Updated as requested.

##########
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:
       Updated as requested.




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