forked from AliceO2Group/AliceO2
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_Fifo.cxx
More file actions
72 lines (62 loc) · 2 KB
/
test_Fifo.cxx
File metadata and controls
72 lines (62 loc) · 2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
// Copyright CERN and copyright holders of ALICE O2. This software is
// distributed under the terms of the GNU General Public License v3 (GPL
// Version 3), copied verbatim in the file "COPYING".
//
// See http://alice-o2.web.cern.ch/license for full licensing information.
//
// In applying this license CERN does not waive the privileges and immunities
// granted to it by virtue of its status as an Intergovernmental Organization
// or submit itself to any jurisdiction.
/// @file test_Fifo.cxx
/// @author Matthias Richter
/// @since 2016-12-06
/// @brief Test program for thread safe FIFO
#define BOOST_TEST_MODULE Utility test
#define BOOST_TEST_MAIN
#define BOOST_TEST_DYN_LINK
#include <boost/test/unit_test.hpp>
#include "Fifo.h"
#include <iostream>
#include <iomanip>
#include <unistd.h>
#include <thread>
const int nEvents = 6;
const int sleepTime = 1;
template <typename T>
bool processValue(T value)
{
std::cout << "processing " << value << std::endl;
sleep(sleepTime);
return (value + 1 < nEvents);
}
template <class FifoT, typename T>
void pushFifo(FifoT& fifo, T value = FifoT::value_type)
{
std::cout << "pushing " << value << std::endl;
fifo.push(value);
}
BOOST_AUTO_TEST_CASE(test_Fifo)
{
using value_type = unsigned int;
o2::test::Fifo<value_type> fifo;
// start a consumer thread which pulls from the FIFO to the function
// processValue with a simulated prcessing of one second.
std::thread consumer([&fifo]() {
do {
} while (fifo.pull([](value_type v) { return processValue(v); }));
});
// fill some values into the FIFO which the consumer can process
// immediately
unsigned int value = 0;
pushFifo(fifo, value++);
pushFifo(fifo, value++);
pushFifo(fifo, value++);
// now continue filling with a period longer than the consumer
// processing, consumer and producer are in sync once consumer
// has processed events which have been added to the FIFO before
while (value < nEvents) {
sleep(2 * sleepTime);
pushFifo(fifo, value++);
}
consumer.join();
}