Skip to content

Commit e76d093

Browse files
Unit tests for ranged GTID updates.
1 parent 23c153f commit e76d093

File tree

4 files changed

+129
-5
lines changed

4 files changed

+129
-5
lines changed

include/proxysql_gtid.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ class Gtid_Interval {
2929
const int cmp(const Gtid_Interval& other);
3030
const bool operator<(const Gtid_Interval& other);
3131
const bool operator==(const Gtid_Interval& other);
32+
const bool operator!=(const Gtid_Interval& other);
3233
};
3334
typedef Gtid_Interval gtid_interval_t;
3435

lib/proxysql_gtid.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -121,3 +121,7 @@ const bool Gtid_Interval::operator<(const Gtid_Interval& other) {
121121
const bool Gtid_Interval::operator==(const Gtid_Interval& other) {
122122
return cmp(other) == 0;
123123
}
124+
125+
const bool Gtid_Interval::operator!=(const Gtid_Interval& other) {
126+
return cmp(other) != 0;
127+
}

test/tap/tests/Makefile

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -93,11 +93,7 @@ CUSTOMARGS += -Wl,-Bdynamic -lcpp_dotenv -lcurl -lssl -lcrypto -lre2 -lpthread -
9393
.PHONY: all
9494
all: tests
9595

96-
debug: OPT := $(STDCPP) -O0 -DDEBUG -ggdb -Wl,--no-as-needed -Wl,-rpath,$(TAP_LDIR) $(WGCOV) $(WASAN) -DGITVERSION=\"$(GIT_VERSION)\"
97-
debug: tests
98-
99-
tests: CUSTOMARGS += $(OPT)
100-
tests: tests-cpp \
96+
tap-tests: tests-cpp \
10197
tests-php \
10298
tests-py \
10399
tests-sh \
@@ -125,6 +121,15 @@ tests: tests-cpp \
125121
fast_forward_switch_replication_deprecate_eof_libmysql-t \
126122
reg_test_mariadb_stmt_store_result_libmysql-t \
127123
reg_test_mariadb_stmt_store_result_async-t
124+
125+
unit-tests: \
126+
unit-proxysql_gtid-t
127+
128+
debug: OPT := $(STDCPP) -O0 -DDEBUG -ggdb -Wl,--no-as-needed -Wl,-rpath,$(TAP_LDIR) $(WGCOV) $(WASAN) -DGITVERSION=\"$(GIT_VERSION)\"
129+
debug: tests
130+
131+
tests: CUSTOMARGS += $(OPT)
132+
tests: tap-tests unit-tests
128133
tests:
129134
@echo "Removing empty .gcno files ..."
130135
find -L . -type f -name '*.gcno' -empty -ls -delete
Lines changed: 114 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,114 @@
1+
#include <stdlib.h>
2+
3+
#include "tap.h"
4+
#include "unit_test.h"
5+
#include "proxysql_gtid.h"
6+
7+
using std::string;
8+
9+
int testGtidIntervalFromString_Count() {
10+
return 2;
11+
}
12+
void testGtidIntervalFromString() {
13+
ok(gtid_interval_t("123-456") == gtid_interval_t(123, 456), "GTID interval from range string");
14+
ok(gtid_interval_t("111") == gtid_interval_t(111, 111), "GTID interval from single GTID string");
15+
}
16+
17+
int testGtidIntervalContains_Count() {
18+
return 8;
19+
}
20+
void testGtidIntervalContains() {
21+
auto iv = gtid_interval_t(123, 456);
22+
23+
ok(iv.contains(123), "GTID interval contains start");
24+
ok(iv.contains(456), "GTID interval contains end");
25+
ok(iv.contains(300), "GTID interval contains middle");
26+
ok(!iv.contains(100), "GTID interval doesn't contain before start");
27+
ok(!iv.contains(500), "GTID interval doesn't contain past end");
28+
ok(!iv.contains(gtid_interval_t(100, 300)), "GTID interval doesn't contain range before start");
29+
ok(!iv.contains(gtid_interval_t(300, 500)), "GTID interval doesn't contain range past end");
30+
ok(iv.contains(gtid_interval_t(150, 310)), "GTID interval contains range");
31+
}
32+
33+
int testGtidIntervalAppend_Count() {
34+
return 7;
35+
}
36+
void testGtidIntervalAppend() {
37+
auto iv = gtid_interval_t(123, 456);
38+
39+
ok(!iv.append(gtid_interval_t(90, 100)), "cannot append before range start");
40+
ok(!iv.append(gtid_interval_t(100, 200)), "cannot append at start");
41+
ok(!iv.append(gtid_interval_t(500, 600)), "cannot append past end");
42+
ok(iv.append(gtid_interval_t(457, 490)), "append");
43+
ok(iv.to_string() == "123-490", "append result");
44+
45+
iv = gtid_interval_t(123, 456);
46+
ok(iv.append(gtid_interval_t(200, 600)), "append with overlap");
47+
ok(iv.to_string() == "123-600", "append with overlap result");
48+
}
49+
50+
int testGtidIntervalMerge_Count() {
51+
return 14;
52+
}
53+
void testGtidIntervalMerge() {
54+
auto iv = gtid_interval_t(123, 456);
55+
ok(!iv.merge(gtid_interval_t(90, 100)), "cannot merge before range start");
56+
ok(!iv.merge(gtid_interval_t(500, 600)), "cannot merge past range end");
57+
ok(iv.merge(gtid_interval_t(90, 200)), "merge at start");
58+
auto want = gtid_interval_t(90, 456);
59+
ok(iv == want, "merge at start result");
60+
61+
iv = gtid_interval_t(123, 456);
62+
ok(iv.merge(gtid_interval_t(300, 500)), "merge at end");
63+
want = gtid_interval_t(123, 500);
64+
ok(iv == want, "merge at end result");
65+
66+
iv = gtid_interval_t(123, 456);
67+
ok(iv.merge(gtid_interval_t(200, 300)), "merge at middle");
68+
want = gtid_interval_t(123, 456);
69+
ok(iv == want, "merge at middle result");
70+
71+
iv = gtid_interval_t(123, 456);
72+
ok(iv.merge(gtid_interval_t(100, 500)), "merge overlap");
73+
want = gtid_interval_t(100, 500);
74+
ok(iv == want, "merge overlap result");
75+
76+
iv = gtid_interval_t(123, 456);
77+
ok(iv.merge(gtid_interval_t(100, 122)), "merge append at start");
78+
want = gtid_interval_t(100, 456);
79+
ok(iv == want, "merge append at start result");
80+
81+
iv = gtid_interval_t(123, 456);
82+
want = gtid_interval_t(123, 600);
83+
ok(iv.merge(gtid_interval_t(457, 600)), "merge append at end");
84+
ok(iv == want, "merge append at end result");
85+
}
86+
87+
std::function<int(void)> testFunctionCounts[] = {
88+
testGtidIntervalFromString_Count,
89+
testGtidIntervalContains_Count,
90+
testGtidIntervalAppend_Count,
91+
testGtidIntervalMerge_Count,
92+
};
93+
std::function<void(void)> testFunctions[] = {
94+
testGtidIntervalFromString,
95+
testGtidIntervalContains,
96+
testGtidIntervalAppend,
97+
testGtidIntervalMerge,
98+
};
99+
100+
int main(int argc, char** argv) {
101+
// Set up unit tests...
102+
int n = 0;
103+
for (auto f : testFunctionCounts) {
104+
n += f();
105+
}
106+
plan(n);
107+
108+
// ...and run them.
109+
for (auto f : testFunctions) {
110+
f();
111+
}
112+
113+
return exit_status();
114+
}

0 commit comments

Comments
 (0)