summaryrefslogtreecommitdiff
path: root/test/test-e2e.cpp
blob: 443499506e541d4560c4b7627d3f9fe4ce6359be (plain)
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
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
#define BOOST_TEST_MODULE EndToEnd
#include <boost/test/data/test_case.hpp>
#include <boost/test/unit_test.hpp>

#include "helpers.h"
#include "testdb-mysql.h"
#include "testdb-postgresql.h"
#include <compileTimeFormatter.h>
#include <condition_variable>
#include <dbConn.h>
#include <input/replStream.h>
#include <output/pq/updateDatabase.h>
#include <sql/createTestTable.h>
#include <sql/fillTestTable.h>
#include <sql/selectTestTable.h>
#include <sql/simpleCreate.h>
#include <sql/simpleDeleteSome.h>
#include <sql/simpleInsert.h>
#include <sql/simpleSelectAll.h>
#include <sql/simpleUpdate.h>
#include <sql/simpleUpdateAll.h>
#include <streamSupport.h>
#include <thread>

class TestUpdateDatabase : public MyGrate::Output::Pq::UpdateDatabase {
public:
	using MyGrate::Output::Pq::UpdateDatabase::UpdateDatabase;
	void
	afterEvent(const MyGrate::MariaDB_Event_Ptr & e) override
	{
		{
			std::lock_guard<std::mutex> lk(m);
			UpdateDatabase::afterEvent(std::move(e));
		}
		cv.notify_all();
	}

	void
	waitFor(EventCounterTarget ect)
	{
		std::unique_lock<std::mutex> lk(m);
		cv.wait(lk, [&] {
			return getProcessedCounts() >= ect;
		});
	}

private:
	std::condition_variable cv;
	std::mutex m;
};

using namespace MyGrate::Testing;
class MockSetup {
public:
	static constexpr const char * const target_schema {"testout"};
	MockSetup() : pq {ROOT "/db/schema.sql"}, pqm {pq.mock()}, mym {my.mock()} { }
	virtual ~MockSetup()
	{
		if (src) {
			src->stopEvents();
		}
		if (repl) {
			repl->join();
			repl.reset();
		}
	}

	TestUpdateDatabase &
	getUpdateDatabase()
	{
		BOOST_REQUIRE(!out);
		auto & o = out.emplace(pqm.connstr.c_str(),
				MyGrate::Output::Pq::UpdateDatabase::createNew(&pqm, MySQLDB::SERVER, MySQLDB::USER, MySQLDB::PASSWORD,
						MySQLDB::PORT, my.mockname.c_str(), 100, target_schema)
						.source);
		BOOST_CHECK_EQUAL(o.source, 1);
		return o;
	}

	const MyGrate::EventSourceBasePtr &
	getSource()
	{
		BOOST_REQUIRE(out);
		if (!src) {
			src = out->getSource();
			BOOST_REQUIRE(src);
		}
		return src;
	}

	void
	run()
	{
		BOOST_REQUIRE(out);
		BOOST_REQUIRE(!repl);
		repl.emplace([&]() {
			try {
				getSource()->readEvents(*out);
			}
			catch (std::exception & ex) {
				failed = true;
				std::cerr << ex.what() << "\n";
			}
			catch (...) {
				failed = true;
				std::cerr << "bug\n";
			}
		});
	}

	void
	stopAfter(const EventCounterTarget & events)
	{
		BOOST_REQUIRE(out);
		BOOST_REQUIRE(src);
		BOOST_REQUIRE(repl);
		if (!failed) {
			out->waitFor(events);
		}
		src->stopEvents();
		repl->join();
		repl.reset();
		if (failed) {
			throw std::runtime_error("Replication thread failed");
		}
	}

	MySQLDB my;
	PqConnDB pq;
	MyGrate::Output::Pq::PqConn pqm;
	MyGrate::Input::MySQLConn mym;
	bool failed {false};

	std::optional<TestUpdateDatabase> out;
	MyGrate::EventSourceBasePtr src;
	std::optional<std::thread> repl;
};

BOOST_FIXTURE_TEST_SUITE(setup, MockSetup);

BOOST_AUTO_TEST_CASE(e2e, *boost::unit_test::timeout(15))
{
	MyGrate::sql::createTestTable::execute(&mym);
	MyGrate::sql::fillTestTable::execute(&mym);

	TestUpdateDatabase & out {getUpdateDatabase()};

	out.addTable(&mym, "session");

	BOOST_CHECK_EQUAL(MyGrate::sql::selectTestTable::execute(&pqm)->rows(), 1);

	run();

	auto upd = mym.prepare("UPDATE session SET session_id = ? WHERE id = ?", 2);
	upd->execute({"food", 1});
	auto del = mym.prepare("DELETE FROM session WHERE id = ?", 2);
	del->execute({1});
	auto ins = mym.prepare("INSERT INTO session(session_id, username, user_lvl, ip_addr, port, created, modified) \
			VALUES(?, ?, ?, ?, ?, now(), now())",
			5);
	ins->execute({"hashyhash", "testuser", "groupadm", "10.10.0.1", 2433});
	mym.query("flush logs");

	stopAfter(EventCounterTarget {}
					  .add(UPDATE_ROWS_EVENT_V1, 1)
					  .add(DELETE_ROWS_EVENT_V1, 1)
					  .add(WRITE_ROWS_EVENT_V1, 1)
					  .add(ROTATE_EVENT, 1));
}

BOOST_AUTO_TEST_CASE(txns, *boost::unit_test::timeout(15))
{
	MyGrate::sql::simpleCreate::execute(&mym);

	TestUpdateDatabase & out {getUpdateDatabase()};

	out.addTable(&mym, "test");

	run();
	MyGrate::Tx {&mym}([&]() {
		for (unsigned int x = 0; x < 10; x += 1) {
			MyGrate::sql::simpleInsert::execute(&mym, MyGrate::scprintf<"some string %?">(x));
		}
		MyGrate::sql::simpleUpdateAll::execute(&mym, "Same");
		MyGrate::sql::simpleDeleteSome::execute(&mym, 5);
	});
	stopAfter(EventCounterTarget {}
					  .add(GTID_EVENT, 1)
					  .add(WRITE_ROWS_EVENT_V1, 10)
					  .add(UPDATE_ROWS_EVENT_V1, 1)
					  .add(DELETE_ROWS_EVENT_V1, 1)
					  .add(XID_EVENT, 1));

	auto recs = MyGrate::sql::simpleSelectAll::execute(&pqm);
	BOOST_REQUIRE_EQUAL(recs->rows(), 6);
	BOOST_CHECK_EQUAL(recs->at(0, 0).get<int>(), 5);
	BOOST_CHECK_EQUAL(recs->at(5, 0).get<int>(), 10);
	BOOST_CHECK_EQUAL(recs->at(1, 1).get<std::string_view>(), "Same");
	BOOST_CHECK_EQUAL(recs->at(3, 1).get<std::string_view>(), "Same");
}

template<int T> struct TypeTestDetail;
#define TEST_TYPE(MYSQL_TYPE, IN, OUT, MYTYPE) \
	template<> struct TypeTestDetail<MYSQL_TYPE> { \
		using OutType = OUT; \
		static constexpr auto mytype = #MYTYPE; \
		static IN generate(size_t n); \
	}; \
	IN TypeTestDetail<MYSQL_TYPE>::generate(size_t n)

TEST_TYPE(MYSQL_TYPE_STRING, std::string, std::string_view, text)
{
	return std::string(n * 200, 'f');
}
TEST_TYPE(MYSQL_TYPE_VARCHAR, std::string, std::string_view, varchar(2048))
{
	return std::string(n * 20, 'f');
}
TEST_TYPE(MYSQL_TYPE_JSON, std::string, std::string_view, json)
{
	return MyGrate::scprintf<"{ json: %? }">(n);
}
TEST_TYPE(MYSQL_TYPE_ENUM, std::string_view, std::string_view, enum('alpha', 'beta', 'gamma'))
{
	static constexpr std::array<std::string_view, 3> vals {"alpha", "beta", "gamma"};
	return vals[n % vals.size()];
}
TEST_TYPE(MYSQL_TYPE_TINY, int8_t, int8_t, tinyint)
{
	return (int8_t)n;
}
TEST_TYPE(MYSQL_TYPE_SHORT, int16_t, int16_t, smallint)
{
	return (int16_t)n;
}
TEST_TYPE(MYSQL_TYPE_INT24, int32_t, int32_t, int)
{
	return (int32_t)n;
}
TEST_TYPE(MYSQL_TYPE_YEAR, int16_t, int16_t, year)
{
	if (!n) {
		return 0;
	}
	return (int16_t)n + 1901;
}
TEST_TYPE(MYSQL_TYPE_LONG, int32_t, int32_t, int)
{
	return (int32_t)n;
}
TEST_TYPE(MYSQL_TYPE_LONGLONG, int64_t, int64_t, bigint)
{
	return (int64_t)n;
}
TEST_TYPE(MYSQL_TYPE_FLOAT, float, float, float)
{
	return (float)n;
}
TEST_TYPE(MYSQL_TYPE_DOUBLE, double, double, real)
{
	return (double)n;
}
TEST_TYPE(MYSQL_TYPE_DATETIME, MyGrate::DateTime, MyGrate::DateTime, datetime)
{
	time_t t = time(nullptr);
	t -= (n * 12345679);
	struct tm tm {
	};
	gmtime_r(&t, &tm);
	return MyGrate::DateTime {tm};
}
TEST_TYPE(MYSQL_TYPE_DATE, MyGrate::Date, MyGrate::Date, date)
{
	time_t t = time(nullptr);
	t -= (n * 12345679);
	struct tm tm {
	};
	gmtime_r(&t, &tm);
	return MyGrate::Date {tm};
}
TEST_TYPE(MYSQL_TYPE_TIME, MyGrate::Time, MyGrate::Time, time)
{
	time_t t = time(nullptr);
	t -= (n * 12345679);
	struct tm tm {
	};
	gmtime_r(&t, &tm);
	auto r = MyGrate::Time {tm};
	std::cerr << r << "\n";
	return r;
}

template<int MYSQL_TYPE, typename Test>
void
replication_data_type_impl(Test * test)
{
	using T = TypeTestDetail<MYSQL_TYPE>;
	using I = decltype(T::generate(0));
	using O = typename T::OutType;

	constexpr auto ROWS {100U};
	BOOST_TEST_INFO(T::mytype);
	test->mym.query(
			MyGrate::scprintf<"CREATE TABLE test(id INT AUTO_INCREMENT, val %?, PRIMARY KEY(id))">(T::mytype).c_str());
	TestUpdateDatabase & out {test->getUpdateDatabase()};
	out.addTable(&test->mym, "test");

	std::vector<I> vals;
	vals.reserve(ROWS);

	// insert test records
	test->run();
	MyGrate::Tx {&test->mym}([&vals, test] {
		MyGrate::DbStmt<"INSERT INTO test(val) VALUES(?)"> ins;
		for (size_t n {}; n < ROWS; n++) {
			vals.push_back(T::generate(n));
			ins.execute(&test->mym, vals.back());
		}
	});
	test->stopAfter(EventCounterTarget {}.add(WRITE_ROWS_EVENT_V1, ROWS));

	// read test records
	auto rs {MyGrate::DbStmt<"SELECT val FROM testout.test ORDER BY id">::execute(&test->pqm)};
	std::vector<O> outs;
	outs.reserve(ROWS);
	for (auto v : *rs) {
		outs.push_back(v[0]);
	}

	// Check values
	BOOST_CHECK_EQUAL_COLLECTIONS(vals.begin(), vals.end(), outs.begin(), outs.end());
}

#define TEST_MYSQL_TYPE(T) \
	BOOST_AUTO_TEST_CASE(replication_data_type_##T, *boost::unit_test::timeout(5)) \
	{ \
		replication_data_type_impl<T>(this); \
	}

TEST_MYSQL_TYPE(MYSQL_TYPE_STRING);
TEST_MYSQL_TYPE(MYSQL_TYPE_VARCHAR);
TEST_MYSQL_TYPE(MYSQL_TYPE_JSON);
// TEST_MYSQL_TYPE(MYSQL_TYPE_ENUM); // we don't have sufficient data available to support this
TEST_MYSQL_TYPE(MYSQL_TYPE_TINY);
TEST_MYSQL_TYPE(MYSQL_TYPE_SHORT);
TEST_MYSQL_TYPE(MYSQL_TYPE_LONG);
TEST_MYSQL_TYPE(MYSQL_TYPE_INT24);
TEST_MYSQL_TYPE(MYSQL_TYPE_LONGLONG);
TEST_MYSQL_TYPE(MYSQL_TYPE_FLOAT);
TEST_MYSQL_TYPE(MYSQL_TYPE_DOUBLE);
TEST_MYSQL_TYPE(MYSQL_TYPE_YEAR);
TEST_MYSQL_TYPE(MYSQL_TYPE_DATETIME);
TEST_MYSQL_TYPE(MYSQL_TYPE_DATE);
TEST_MYSQL_TYPE(MYSQL_TYPE_TIME);

BOOST_AUTO_TEST_SUITE_END();