Line data Source code
1 : /**
2 : *
3 : * @file FollyQueue_metric_test.cxx FollyQueue class Unit Tests to check if size() can be called in a thread
4 : * that is neither the producer nor the consumer
5 : *
6 : * This is part of the DUNE DAQ Application Framework, copyright 2020.
7 : * Licensing/copyright details are in the COPYING file that you should have
8 : * received with this code.
9 : */
10 :
11 : #include "iomanager/queue/FollyQueue.hpp"
12 :
13 : #define BOOST_TEST_MODULE FollyQueue_metric_test // NOLINT
14 : #include "boost/test/unit_test.hpp"
15 :
16 : #include <chrono>
17 : #include <future>
18 : #include <thread>
19 :
20 : // For a first look at the code, you may want to skip past the
21 : // contents of the unnamed namespace and move ahead to the actual test
22 : // cases
23 :
24 : namespace {
25 :
26 : constexpr int initial_size = 100; ///< the initial size of the queue ;
27 :
28 : constexpr auto test_time = std::chrono::milliseconds(500);
29 :
30 : constexpr auto timeout = std::chrono::milliseconds(5);
31 :
32 : /**
33 : * @brief Timeout to use for tests
34 : *
35 : * Don't set the timeout to zero, otherwise the tests will fail since they'd
36 : * expect the push/pop functions to execute instananeously
37 : */
38 :
39 : folly::DSPSCQueue<int, true> queue(initial_size); ///< Queue instance for the test
40 :
41 : } // namespace ""
42 :
43 : BOOST_AUTO_TEST_SUITE(FollyQueueMetricTest)
44 :
45 : // This test case should run first. Make sure all other test cases depend on
46 : // this.
47 :
48 2 : BOOST_AUTO_TEST_CASE(three_thread_test)
49 : {
50 :
51 3 : std::future<size_t> size_thread = std::async(std::launch::async, [&]() {
52 1 : size_t size = 0;
53 1 : auto start_time = std::chrono::steady_clock::now();
54 1 : auto stop_time = start_time + test_time / 2;
55 :
56 262506 : while (std::chrono::steady_clock::now() < stop_time) {
57 262505 : size_t temp = queue.size();
58 262505 : if (temp > size)
59 : size = temp;
60 : }
61 1 : return size;
62 1 : });
63 :
64 3 : std::future<int> push_thread = std::async(std::launch::async, [&]() {
65 1 : auto start_time = std::chrono::steady_clock::now();
66 1 : auto stop_time = start_time + test_time;
67 1 : int number = 0;
68 105796 : while (std::chrono::steady_clock::now() < stop_time) {
69 105795 : ++number;
70 211590 : queue.enqueue(number);
71 : }
72 1 : return number;
73 1 : });
74 :
75 3 : std::future<int> pop_thread = std::async(std::launch::async, [&]() {
76 1 : int memory = 0;
77 105797 : while (queue.try_dequeue_for(memory, timeout)) {
78 : ;
79 : }
80 1 : return memory;
81 1 : });
82 :
83 1 : auto last_pushed_entry = push_thread.get();
84 1 : auto last_popped_entry = pop_thread.get();
85 1 : auto read_size = size_thread.get();
86 :
87 1 : BOOST_TEST_MESSAGE("Last pushed value: " << last_pushed_entry);
88 1 : BOOST_TEST_MESSAGE("Last popped value: " << last_popped_entry);
89 1 : BOOST_TEST_MESSAGE("Temp size: " << read_size);
90 :
91 1 : BOOST_REQUIRE_EQUAL(queue.size(), 0);
92 1 : BOOST_REQUIRE_EQUAL(last_pushed_entry, last_popped_entry);
93 :
94 1 : BOOST_CHECK_GT(read_size, 0);
95 1 : }
96 :
97 : BOOST_AUTO_TEST_SUITE_END()
|