File tree Expand file tree Collapse file tree 2 files changed +81
-6
lines changed Expand file tree Collapse file tree 2 files changed +81
-6
lines changed Original file line number Diff line number Diff line change @@ -60,18 +60,18 @@ class Queue {
60
60
return data;
61
61
}
62
62
63
- std::shared_lock<std::shared_mutex> read_lock () {
63
+ std::shared_lock<std::shared_mutex> read_lock () const {
64
64
return std::shared_lock (mx_);
65
65
}
66
66
67
- std::unique_lock<std::shared_mutex> write_lock () {
67
+ std::unique_lock<std::shared_mutex> write_lock () const {
68
68
return std::unique_lock (mx_);
69
69
}
70
70
71
71
private:
72
72
std::queue<T> inner_;
73
73
74
- std::shared_mutex mx_;
74
+ mutable std::shared_mutex mx_;
75
75
};
76
76
77
77
template <class T >
@@ -81,7 +81,7 @@ class Producer {
81
81
82
82
~Producer () { Stop (); }
83
83
84
- virtual T next ();
84
+ virtual T next () = 0 ;
85
85
86
86
void Start () {
87
87
thread_.Start ([this ] { Run (); });
@@ -110,7 +110,7 @@ class Consumer {
110
110
111
111
~Consumer () { Stop (); }
112
112
113
- virtual void handle (const T& event);
113
+ virtual void handle (const T& event) = 0 ;
114
114
115
115
void Start () {
116
116
thread_.Start ([this ] { Run (); });
@@ -140,7 +140,7 @@ class Transformer {
140
140
141
141
~Transformer () { Stop (); }
142
142
143
- virtual std::optional<Out> transform (const In& event);
143
+ virtual std::optional<Out> transform (const In& event) = 0 ;
144
144
145
145
void Start () {
146
146
thread_.Start ([this ] { Run (); });
Original file line number Diff line number Diff line change
1
+ #include < chrono>
2
+ #include < memory>
3
+ #include < ratio>
4
+ #include < thread>
5
+ #include < vector>
6
+
7
+ #include " Pipeline.h"
8
+ #include " gmock/gmock.h"
9
+ #include " gtest/gtest.h"
10
+
11
+ namespace collector {
12
+
13
+ class IntProducer : public Producer <int > {
14
+ public:
15
+ IntProducer (std::shared_ptr<Queue<int >>& input, int limit) : Producer(input), limit_(limit) {}
16
+
17
+ int next () override {
18
+ n_++;
19
+ if (n_ > limit_) {
20
+ Stop ();
21
+ }
22
+ return n_;
23
+ }
24
+
25
+ private:
26
+ int n_ = 0 ;
27
+ int limit_;
28
+ };
29
+
30
+ class IntConsumer : public Consumer <int > {
31
+ public:
32
+ IntConsumer (std::shared_ptr<Queue<int >>& input, std::vector<int > output) : Consumer(input), events_(output) {}
33
+
34
+ void handle (const int & event) override {
35
+ events_.push_back (event);
36
+ }
37
+
38
+ std::vector<int >& Events () {
39
+ return events_;
40
+ }
41
+
42
+ private:
43
+ std::vector<int > events_;
44
+ };
45
+
46
+ class EvenIntFilter : public Filter <int > {
47
+ public:
48
+ std::optional<int > transform (const int & event) {
49
+ if (event % 2 == 0 ) {
50
+ return {event};
51
+ }
52
+ return std::nullopt;
53
+ }
54
+ };
55
+
56
+ TEST (PipelineTests, TestBasic) {
57
+ std::shared_ptr<Queue<int >> queue;
58
+
59
+ std::vector<int > output;
60
+
61
+ std::unique_ptr<Producer<int >> producer = std::make_unique<IntProducer>(queue, 10 );
62
+ std::unique_ptr<Consumer<int >> consumer = std::make_unique<IntConsumer>(queue, output);
63
+
64
+ producer->Start ();
65
+ consumer->Start ();
66
+
67
+ std::this_thread::sleep_for (std::chrono::milliseconds (200 ));
68
+
69
+ consumer->Stop ();
70
+ producer->Stop ();
71
+
72
+ EXPECT_TRUE (output.size () == 10 );
73
+ }
74
+
75
+ } // namespace collector
You can’t perform that action at this time.
0 commit comments