File: test_event_bus.cc

package info (click to toggle)
logserver 1.13.2-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 720 kB
  • sloc: cpp: 10,815; makefile: 3
file content (181 lines) | stat: -rw-r--r-- 4,842 bytes parent folder | download
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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
#include "test.h"

#include <list>
#include <string>

#include "event_bus.h"
#include "i_log_lines_events.h"
#include "line.h"

using namespace std;
using namespace std::placeholders;

class MockEventReceiver : public ILogLinesEvents {
public:
	virtual ~MockEventReceiver() {
		CHECK(_expects.empty());
	}

	void expect(const string& s) {
		_expects.push_back(s);
	}
        // an existing line was changed
        virtual void edit_line([[maybe_unused]] LogLines* ll,
			       size_t pos, Line* line) {
		CHECK(pos == 10);
		CHECK(line->view() == "edit");
	}

        // a new line was added
        virtual void append_line([[maybe_unused]] LogLines* ll,
				 [[maybe_unused]] size_t pos,
				 Line* line) {
		CHECK(!_expects.empty());
		CHECK(line->view() == _expects.front());
		_expects.pop_front();
	}

        // an insertion event
        virtual void insertion([[maybe_unused]] LogLines* ll,
			       [[maybe_unused]] size_t pos,
			       [[maybe_unused]] size_t amount) {}

        // a deletion event
        virtual void deletion([[maybe_unused]] LogLines* ll,
			      [[maybe_unused]] size_t pos,
			      [[maybe_unused]] size_t amount) {}

	// clearing event
	virtual void clear_lines([[maybe_unused]] LogLines* ll) {}

protected:
	list<string> _expects;
};

TEST_CASE("eventbus enroll") {
	MockEventReceiver m, m_other;
	LogLines* ll = (LogLines*) 12;
	LogLines* ll_other = (LogLines*) 13;
	EventBus::_()->enregister(ll, &m);
	EventBus::_()->enregister(ll_other, &m_other);

	m.expect("hello");
	m.expect("there");
	m_other.expect("another");
	Line l1(string("hello"));
	Line l2(string("another"));
	Line l3(string("there"));
	Line l4(string("world"));
	Line l5(string("ignore"));
	EventBus::_()->append_line(ll, 0, &l1);
	EventBus::_()->append_line(ll_other, 1, &l2);
	EventBus::_()->append_line(ll, 1, &l3);
	m.expect("world");
	EventBus::_()->append_line(ll, 2, &l4);
	EventBus::_()->deregister(ll, &m);
	EventBus::_()->append_line(ll, 3, &l5);
	EventBus::_()->deregister(ll_other, &m_other);
}

TEST_CASE("eventbus eventmaker end") {
	MockEventReceiver m;
	LogLines* ll = (LogLines*) 42;
	EventBus::_()->enregister(ll, &m);
	m.expect("hello");
	m.expect("there");
	Line l1(string("hello"));
	Line l2(string("there"));
	Line l3(string("world"));
	EventBus::_()->append_line(ll, 3, &l1);
	EventBus::_()->append_line(ll, 4, &l2);
	EventBus::_()->eventmaker_finished(ll);
	EventBus::_()->append_line(ll, 5, &l3);
	EventBus::_()->deregister(ll, &m);

}

TEST_CASE("eventbus eventmaker end and deregister") {
	MockEventReceiver m;
	LogLines* ll = (LogLines*) 42;
	EventBus::_()->enregister(ll, &m);
	m.expect("hello");
	m.expect("there");
	Line l1(string("hello"));
	Line l2(string("there"));
	Line l3(string("world"));
	EventBus::_()->append_line(ll, 3, &l1);
	EventBus::_()->append_line(ll, 4, &l2);
	EventBus::_()->eventmaker_finished(ll);
	EventBus::_()->append_line(ll, 5, &l3);
	EventBus::_()->deregister(ll, &m);
}

TEST_CASE("eventbus deregister and eventmaker end") {
	MockEventReceiver m;
	LogLines* ll = (LogLines*) 42;
	EventBus::_()->enregister(ll, &m);
	m.expect("hello");
	m.expect("there");
	Line l1(string("hello"));
	Line l2(string("there"));
	Line l3(string("world"));
	EventBus::_()->append_line(ll, 3, &l1);
	EventBus::_()->append_line(ll, 4, &l2);
	EventBus::_()->deregister(ll, &m);
	EventBus::_()->append_line(ll, 4, &l3);
	EventBus::_()->eventmaker_finished(ll);
	EventBus::_()->deregister(ll, &m);
}

TEST_CASE("eventbus multiple enroll") {
	MockEventReceiver m1;
	MockEventReceiver m2;
	MockEventReceiver m3;
	LogLines* s1 = (LogLines*) 42;
	LogLines* s2 = (LogLines*) 44;
	CHECK(!EventBus::_()->present(&m1));
	EventBus::_()->enregister(s1, &m1);
	CHECK(EventBus::_()->present(&m1));
	EventBus::_()->enregister(s1, &m2);
	EventBus::_()->enregister(s1, &m3);
	EventBus::_()->enregister(s2, &m2);
	EventBus::_()->enregister(s2, &m3);

	m1.expect("s1_1");
	m1.expect("s1_2");
	m1.expect("s1_3");
	m2.expect("s1_1");
	m2.expect("s2_1");
	m2.expect("s1_2");
	m2.expect("s1_3");
	m2.expect("s2_2");
	m3.expect("s1_1");
	m3.expect("s2_1");
	m3.expect("s1_2");
	m3.expect("s1_3");
	m3.expect("s2_2");

	Line l1(string("s1_1"));
	Line l2(string("s2_1"));
	Line l3(string("s1_2"));
	Line l4(string("edit"));
	Line l5(string("s1_3"));
	Line l6(string("s2_2"));

	EventBus::_()->append_line(s1, 2, &l1);
	EventBus::_()->append_line(s2, 3, &l2);
	EventBus::_()->append_line(s1, 3, &l3);
	EventBus::_()->edit_line(s1, 10, &l4);
	EventBus::_()->append_line(s1, 4, &l5);
	EventBus::_()->append_line(s2, 4, &l6);

        EventBus::_()->deregister(s1, &m1);
	CHECK(!EventBus::_()->present(&m1));
	CHECK(EventBus::_()->present(&m2));

        EventBus::_()->deregister(s1, &m2);
        EventBus::_()->deregister(s1, &m3);
        EventBus::_()->deregister(s2, &m2);
        EventBus::_()->deregister(s2, &m3);
}