-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.cpp
126 lines (99 loc) · 3.55 KB
/
main.cpp
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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
#define BOOST_TEST_MODULE MultiQueueProcessorTest
#include <iostream>
#include "MultiQueueProcessor.h"
#include <boost/test/unit_test.hpp>
class Consumer : public IConsumer<std::string, std::string>
{
public:
void Consume(const std::string & id, const std::string & value) noexcept override
{
++consumed;
}
size_t Consumed() const {return consumed;}
private:
size_t consumed = 0;
};
template <typename Key, typename Value>
class RecursiveConsumer : public IConsumer<std::string, std::string>
{
public:
RecursiveConsumer(MultiQueueProcessor<Key, Value> &processor_) : processor(processor_) {}
void Consume(const std::string & id, const std::string & value) noexcept override
{
processor.Unsubscribe("key");
processor.Enqueue("key", "value");
processor.Enqueue("key", "value");
BOOST_CHECK(!processor.Dequeue("key").empty());
}
private:
MultiQueueProcessor<Key, Value> &processor;
};
BOOST_AUTO_TEST_CASE(test_enque_deque)
{
MultiQueueProcessor<std::string, std::string> processor;
BOOST_CHECK(processor.Dequeue("key1").empty());
processor.Enqueue("key1", "value1");
BOOST_CHECK(processor.Dequeue("key1") == "value1");
BOOST_CHECK(processor.Dequeue("key1").empty());
}
BOOST_AUTO_TEST_CASE(test_max_capacity)
{
MultiQueueProcessor<std::string, std::string> processor;
for (size_t i = 0; i < 1001; ++i)
processor.Enqueue("key1", "value1");
for (size_t i = 0; i < 1000; ++i)
{
BOOST_CHECK(!processor.Dequeue("key1").empty());
}
BOOST_CHECK(processor.Dequeue("key1").empty());
}
BOOST_AUTO_TEST_CASE(test_out_of_scope)
{
{
MultiQueueProcessor<std::string, std::string> processor;
Consumer consumer;
processor.Subscribe("key", &consumer);
processor.Enqueue("key1", "value1");
}
BOOST_CHECK(true);
}
BOOST_AUTO_TEST_CASE(test_recursive)
{
MultiQueueProcessor<std::string, std::string> processor;
RecursiveConsumer<std::string, std::string> recursive_consumer(processor);
processor.Subscribe("key", &recursive_consumer);
processor.Enqueue("key", "value");
std::this_thread::sleep_for(std::chrono::seconds(1));
BOOST_CHECK(!processor.Dequeue("key").empty());
BOOST_CHECK(processor.Dequeue("key").empty());
}
BOOST_AUTO_TEST_CASE(test_produce_consume)
{
MultiQueueProcessor<std::string, std::string> processor;
std::array<Consumer, 32> consumers;
std::array<std::unique_ptr<std::thread>, 32> threads;
for (size_t i = 0; i < threads.size(); ++i)
{
threads[i] = std::make_unique<std::thread>(
[&, i]()
{
for (int item_id = 0; item_id < 765; ++item_id)
{
processor.Enqueue("key" + std::to_string(i), "value" + std::to_string(item_id));
if (item_id % 5 == 0)
processor.Subscribe("key" + std::to_string(i), &consumers[i]);
if (item_id % 10 == 0)
processor.Unsubscribe("key" + std::to_string(i));
}
});
}
for (size_t i = 0; i < threads.size(); ++i)
threads[i]->join();
for (size_t i = 0; i < consumers.size(); ++i)
processor.Subscribe("key" + std::to_string(i), &consumers[i]);
std::this_thread::sleep_for(std::chrono::seconds(1));
for (size_t i = 0; i < consumers.size(); ++i)
BOOST_CHECK(consumers[i].Consumed() == 765);
for (size_t i = 0; i < consumers.size(); ++i)
BOOST_CHECK(processor.Dequeue("key" + std::to_string(i)).empty());
}