-
Notifications
You must be signed in to change notification settings - Fork 3.2k
/
Copy pathredis.cpp
88 lines (74 loc) · 3.27 KB
/
redis.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
/*
+----------------------------------------------------------------------+
| Swoole |
+----------------------------------------------------------------------+
| This source file is subject to version 2.0 of the Apache license, |
| that is bundled with this package in the file LICENSE, and is |
| available through the world-wide-web at the following url: |
| http://www.apache.org/licenses/LICENSE-2.0.html |
| If you did not receive a copy of the Apache2.0 license and are unable|
| to obtain it through the world-wide-web, please send a note to |
| [email protected] so we can mail you a copy immediately. |
+----------------------------------------------------------------------+
| @link https://www.swoole.com/ |
| @contact [email protected] |
| @license https://github.com/swoole/swoole-src/blob/master/LICENSE |
| @Author Tianfeng Han <[email protected]> |
+----------------------------------------------------------------------+
*/
#include "test_core.h"
#include "test_coroutine.h"
#include "redis_client.h"
#include "swoole_redis.h"
using namespace swoole;
using namespace std;
const std::string REDIS_TEST_KEY = "key-swoole";
const std::string REDIS_TEST_VALUE = "value-swoole";
TEST(redis, get) {
test::coroutine::run([](void *arg) {
RedisClient redis;
ASSERT_TRUE(redis.Connect("127.0.0.1", 6379));
ASSERT_TRUE(redis.Set(REDIS_TEST_KEY, REDIS_TEST_VALUE));
ASSERT_EQ(redis.Get(REDIS_TEST_KEY), REDIS_TEST_VALUE);
});
}
TEST(redis, server) {
Server serv(Server::MODE_BASE);
serv.worker_num = 1;
serv.enable_static_handler = true;
sw_logger()->set_level(SW_LOG_WARNING);
ListenPort *port = serv.add_port(SW_SOCK_TCP, TEST_HOST, 0);
ASSERT_TRUE(port);
port->open_redis_protocol = true;
serv.create();
serv.onWorkerStart = [&](Server *serv, Worker *worker) {
if (worker->id != 0) {
return;
}
swoole::Coroutine::create(
[](void *arg) {
Server *serv = reinterpret_cast<Server *>(arg);
RedisClient redis;
ASSERT_TRUE(redis.Connect("127.0.0.1", serv->get_primary_port()->port));
ASSERT_TRUE(redis.Set(REDIS_TEST_KEY, REDIS_TEST_VALUE));
ASSERT_EQ(redis.Get(REDIS_TEST_KEY), REDIS_TEST_VALUE);
kill(serv->gs->master_pid, SIGTERM);
},
serv);
};
serv.onReceive = [](Server *serv, RecvData *req) -> int {
int session_id = req->info.fd;
auto list = redis::parse(req->data, req->info.len);
String *buffer = sw_tg_buffer();
buffer->clear();
if (strcasecmp(list[0].c_str(), "GET") == 0) {
redis::format(buffer, redis::REPLY_STRING, REDIS_TEST_VALUE);
serv->send(session_id, buffer->str, buffer->length);
} else if (strcasecmp(list[0].c_str(), "SET") == 0) {
redis::format(buffer, redis::REPLY_STATUS, "OK");
serv->send(session_id, buffer->str, buffer->length);
}
return SW_OK;
};
serv.start();
}