2019-07-28 10:31:56 +08:00
|
|
|
#include "stdafx.h"
|
2015-02-02 00:13:26 +08:00
|
|
|
|
2015-04-07 23:31:31 +08:00
|
|
|
static bool test_auth(acl::redis_connection& redis)
|
2015-02-02 00:13:26 +08:00
|
|
|
{
|
|
|
|
acl::string passwd("hello");
|
|
|
|
|
2015-04-07 23:31:31 +08:00
|
|
|
redis.clear();
|
|
|
|
if (redis.auth(passwd.c_str()) == false)
|
2015-02-02 00:13:26 +08:00
|
|
|
{
|
|
|
|
printf("auth failed, passwd: %s, eof: %s\r\n",
|
2015-04-07 23:31:31 +08:00
|
|
|
passwd.c_str(), redis.eof() ? "yes" : "no");
|
2015-02-02 00:13:26 +08:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
printf("auth ok, passwd: %s\r\n", passwd.c_str());
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2015-04-07 23:31:31 +08:00
|
|
|
static bool test_echo(acl::redis_connection& redis, int n)
|
2015-02-02 00:13:26 +08:00
|
|
|
{
|
|
|
|
acl::string buf("hello world!");
|
|
|
|
|
|
|
|
for (int i = 0; i < n; i++)
|
|
|
|
{
|
2015-04-07 23:31:31 +08:00
|
|
|
redis.clear();
|
|
|
|
if (redis.echo(buf.c_str()) == false)
|
2015-02-02 00:13:26 +08:00
|
|
|
{
|
2015-08-09 22:42:51 +08:00
|
|
|
printf("echo error: %s\r\n", redis.result_error());
|
2015-02-02 00:13:26 +08:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
else if (i >= 10)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
printf("echo ok\r\n");
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2015-04-07 23:31:31 +08:00
|
|
|
static bool test_ping(acl::redis_connection& redis, int n)
|
2015-02-02 00:13:26 +08:00
|
|
|
{
|
|
|
|
for (int i = 0; i < n; i++)
|
|
|
|
{
|
2015-04-07 23:31:31 +08:00
|
|
|
redis.clear();
|
|
|
|
if (redis.ping() == false)
|
2015-02-02 00:13:26 +08:00
|
|
|
{
|
2015-08-09 22:42:51 +08:00
|
|
|
printf("ping failed: %s\r\n", redis.result_error());
|
2015-02-02 00:13:26 +08:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
else if (i < 10)
|
|
|
|
printf("ping ok\r\n");
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2015-04-07 23:31:31 +08:00
|
|
|
static bool test_quit(acl::redis_connection& redis)
|
2015-02-02 00:13:26 +08:00
|
|
|
{
|
2015-04-07 23:31:31 +08:00
|
|
|
redis.clear();
|
|
|
|
if (redis.quit() == false)
|
2015-02-02 00:13:26 +08:00
|
|
|
{
|
2015-08-09 22:42:51 +08:00
|
|
|
printf("quit error: %s\r\n", redis.result_error());
|
2015-02-02 00:13:26 +08:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
printf("quit ok\r\n");
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-04-07 23:31:31 +08:00
|
|
|
static bool test_select(acl::redis_connection& redis, int n)
|
2015-02-02 00:13:26 +08:00
|
|
|
{
|
|
|
|
for (int i = 0; i < n; i++)
|
|
|
|
{
|
2015-04-07 23:31:31 +08:00
|
|
|
redis.clear();
|
|
|
|
if (redis.select(i % 2) == false)
|
2015-02-02 00:13:26 +08:00
|
|
|
{
|
2015-08-09 22:42:51 +08:00
|
|
|
printf("select %d error: %s\r\n",
|
|
|
|
i % 2, redis.result_error());
|
2015-02-02 00:13:26 +08:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
else if (i < 10)
|
|
|
|
printf("select %d ok\r\n", i % 2);
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void usage(const char* procname)
|
|
|
|
{
|
|
|
|
printf("usage: %s -h[help]\r\n"
|
|
|
|
"-s redis_addr[127.0.0.1:6379]\r\n"
|
|
|
|
"-n count\r\n"
|
|
|
|
"-C connect_timeout[default: 10]\r\n"
|
|
|
|
"-I rw_timeout[default: 10]\r\n"
|
|
|
|
"-S [if slice request, default: no]\r\n"
|
|
|
|
"-a cmd[auth|echo|ping|quit|select]\r\n",
|
|
|
|
procname);
|
|
|
|
}
|
|
|
|
|
|
|
|
int main(int argc, char* argv[])
|
|
|
|
{
|
|
|
|
int ch, n = 1, conn_timeout = 10, rw_timeout = 10;
|
|
|
|
acl::string addr("127.0.0.1:6379"), cmd;
|
|
|
|
bool slice_req = false;
|
|
|
|
|
|
|
|
while ((ch = getopt(argc, argv, "hs:n:C:I:a:S")) > 0)
|
|
|
|
{
|
|
|
|
switch (ch)
|
|
|
|
{
|
|
|
|
case 'h':
|
|
|
|
usage(argv[0]);
|
|
|
|
return 0;
|
|
|
|
case 's':
|
|
|
|
addr = optarg;
|
|
|
|
break;
|
|
|
|
case 'n':
|
|
|
|
n = atoi(optarg);
|
|
|
|
break;
|
|
|
|
case 'C':
|
|
|
|
conn_timeout = atoi(optarg);
|
|
|
|
break;
|
|
|
|
case 'I':
|
|
|
|
rw_timeout = atoi(optarg);
|
|
|
|
break;
|
|
|
|
case 'a':
|
|
|
|
cmd = optarg;
|
|
|
|
break;
|
|
|
|
case 'S':
|
|
|
|
slice_req = true;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
acl::acl_cpp_init();
|
|
|
|
acl::redis_client client(addr.c_str(), conn_timeout, rw_timeout);
|
|
|
|
client.set_slice_request(slice_req);
|
2015-04-07 23:31:31 +08:00
|
|
|
acl::redis_connection redis(&client);
|
2015-02-02 00:13:26 +08:00
|
|
|
|
|
|
|
bool ret;
|
|
|
|
|
|
|
|
if (cmd == "auth")
|
2015-04-07 23:31:31 +08:00
|
|
|
ret = test_auth(redis);
|
2015-02-02 00:13:26 +08:00
|
|
|
else if (cmd == "echo")
|
2015-04-07 23:31:31 +08:00
|
|
|
ret = test_echo(redis, n);
|
2015-02-02 00:13:26 +08:00
|
|
|
else if (cmd == "ping")
|
2015-04-07 23:31:31 +08:00
|
|
|
ret = test_ping(redis, n);
|
2015-02-02 00:13:26 +08:00
|
|
|
else if (cmd == "quit")
|
2015-04-07 23:31:31 +08:00
|
|
|
ret = test_quit(redis);
|
2015-02-02 00:13:26 +08:00
|
|
|
else if (cmd == "select")
|
2015-04-07 23:31:31 +08:00
|
|
|
ret = test_select(redis, n);
|
2015-02-02 00:13:26 +08:00
|
|
|
else if (cmd == "all")
|
|
|
|
{
|
2015-04-07 23:31:31 +08:00
|
|
|
ret = test_auth(redis)
|
|
|
|
&& test_echo(redis, n)
|
|
|
|
&& test_ping(redis, n)
|
|
|
|
&& test_select(redis, n)
|
|
|
|
&& test_quit(redis);
|
2015-02-02 00:13:26 +08:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
printf("unknown cmd: %s\r\n", cmd.c_str());
|
|
|
|
ret = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
printf("cmd: %s %s\r\n", cmd.c_str(), ret ? "ok" : "failed");
|
|
|
|
|
|
|
|
#ifdef WIN32
|
|
|
|
printf("enter any key to exit\r\n");
|
|
|
|
getchar();
|
|
|
|
#endif
|
|
|
|
return 0;
|
|
|
|
}
|