-
Notifications
You must be signed in to change notification settings - Fork 6
/
tests.cpp
170 lines (132 loc) · 5.18 KB
/
tests.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
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
#include "cppq.hpp"
#include <nlohmann/json.hpp>
#undef NDEBUG
const std::string TypeEmailDelivery = "email:deliver";
typedef struct {
int UserID;
std::string TemplateID;
} EmailDeliveryPayload;
void to_json(nlohmann::json& j, const EmailDeliveryPayload& p) {
j = nlohmann::json{{"UserID", p.UserID}, {"TemplateID", p.TemplateID}};
}
cppq::Task NewEmailDeliveryTask(EmailDeliveryPayload payload) {
nlohmann::json j = payload;
return cppq::Task{TypeEmailDelivery, j.dump(), 10};
}
void HandleEmailDeliveryTask(cppq::Task& task) {
nlohmann::json parsedPayload = nlohmann::json::parse(task.payload);
int userID = parsedPayload["UserID"];
std::string templateID = parsedPayload["TemplateID"];
nlohmann::json r;
r["Sent"] = true;
task.result = r.dump();
return;
}
void testEnqueue() {
cppq::registerHandler(TypeEmailDelivery, &HandleEmailDeliveryTask);
redisOptions options = {0};
REDIS_OPTIONS_SET_TCP(&options, "127.0.0.1", 6379);
redisContext *c = redisConnectWithOptions(&options);
if (c == NULL || c->err) {
std::cerr << "Failed to connect to Redis" << std::endl;
assert(false);
}
redisCommand(c, "FLUSHALL");
cppq::Task task = NewEmailDeliveryTask(EmailDeliveryPayload{.UserID = 666, .TemplateID = "AH"});
cppq::enqueue(c, task, "default");
redisReply *reply = (redisReply *)redisCommand(c, "LRANGE cppq:default:pending -1 -1");
if (reply->type != REDIS_REPLY_ARRAY)
assert(false);
if (reply->elements == 0)
assert(false);
reply = reply->element[0];
std::string uuid = reply->str;
redisCommand(c, "MULTI");
redisCommand(c, "LREM cppq:default:pending 1 %s", uuid.c_str());
redisCommand(c, "HGET cppq:default:task:%s type", uuid.c_str());
redisCommand(c, "HGET cppq:default:task:%s payload", uuid.c_str());
redisCommand(c, "HGET cppq:default:task:%s state", uuid.c_str());
redisCommand(c, "HGET cppq:default:task:%s maxRetry", uuid.c_str());
redisCommand(c, "HGET cppq:default:task:%s retried", uuid.c_str());
redisCommand(c, "HGET cppq:default:task:%s dequeuedAtMs", uuid.c_str());
reply = (redisReply *)redisCommand(c, "EXEC");
if (reply->type != REDIS_REPLY_ARRAY || reply->elements != 7)
assert(false);
task = cppq::Task(
uuid,
reply->element[1]->str,
reply->element[2]->str,
reply->element[3]->str,
strtoull(reply->element[4]->str, NULL, 0),
strtoull(reply->element[5]->str, NULL, 0),
strtoull(reply->element[6]->str, NULL, 0)
);
assert(task.type.compare(TypeEmailDelivery) == 0);
assert(task.payload.compare("{\"TemplateID\":\"AH\",\"UserID\":666}") == 0);
assert(task.state == cppq::TaskState::Pending);
assert(task.maxRetry == 10);
assert(task.retried == 0);
assert(task.dequeuedAtMs == 0);
}
void testDequeue() {
cppq::registerHandler(TypeEmailDelivery, &HandleEmailDeliveryTask);
redisOptions options = {0};
REDIS_OPTIONS_SET_TCP(&options, "127.0.0.1", 6379);
redisContext *c = redisConnectWithOptions(&options);
if (c == NULL || c->err) {
std::cerr << "Failed to connect to Redis" << std::endl;
assert(false);
}
redisCommand(c, "FLUSHALL");
cppq::Task task = NewEmailDeliveryTask(EmailDeliveryPayload{.UserID = 666, .TemplateID = "AH"});
cppq::enqueue(c, task, "default");
std::optional<cppq::Task> dequeued = cppq::dequeue(c, "default");
assert(dequeued.value().type.compare(TypeEmailDelivery) == 0);
assert(dequeued.value().payload.compare("{\"TemplateID\":\"AH\",\"UserID\":666}") == 0);
assert(dequeued.value().state == cppq::TaskState::Active);
assert(dequeued.value().maxRetry == 10);
assert(dequeued.value().retried == 0);
assert(dequeued.value().dequeuedAtMs != 0);
redisReply *reply = (redisReply *)redisCommand(c, "LRANGE cppq:default:active -1 -1");
if (reply->type != REDIS_REPLY_ARRAY)
assert(false);
if (reply->elements == 0)
assert(false);
reply = reply->element[0];
std::string uuid = reply->str;
assert(uuid.compare(cppq::uuidToString(dequeued.value().uuid)) == 0);
}
void testRecovery() {
cppq::registerHandler(TypeEmailDelivery, &HandleEmailDeliveryTask);
redisOptions options = {0};
REDIS_OPTIONS_SET_TCP(&options, "127.0.0.1", 6379);
redisContext *c = redisConnectWithOptions(&options);
if (c == NULL || c->err) {
std::cerr << "Failed to connect to Redis" << std::endl;
assert(false);
}
redisCommand(c, "FLUSHALL");
cppq::Task task = NewEmailDeliveryTask(EmailDeliveryPayload{.UserID = 666, .TemplateID = "AH"});
cppq::enqueue(c, task, "default");
std::optional<cppq::Task> dequeued = cppq::dequeue(c, "default");
redisReply *reply = (redisReply *)redisCommand(c, "LRANGE cppq:default:active -1 -1");
if (reply->type != REDIS_REPLY_ARRAY)
assert(false);
if (reply->elements == 0)
assert(false);
cppq::thread_pool pool;
pool.push_task(cppq::recovery, options, (std::map<std::string, int>){{"default", 5}}, 1, 10);
std::this_thread::sleep_for(std::chrono::milliseconds(20));
reply = (redisReply *)redisCommand(c, "LRANGE cppq:default:pending -1 -1");
if (reply->type != REDIS_REPLY_ARRAY)
assert(false);
if (reply->elements == 0)
assert(false);
exit(0);
}
int main(int argc, char *argv[]) {
testEnqueue();
testDequeue();
// TODO: Add scheduled task test
testRecovery();
}