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_multi(acl::redis_transaction& redis)
|
2015-02-02 00:13:26 +08:00
|
|
|
{
|
2015-04-07 23:31:31 +08:00
|
|
|
redis.clear();
|
|
|
|
if (redis.multi() == false)
|
2015-02-02 00:13:26 +08:00
|
|
|
{
|
|
|
|
printf("multi error\r\n");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
printf("multi ok\r\n");
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2015-04-07 23:31:31 +08:00
|
|
|
static bool test_run_cmds(acl::redis_transaction& redis)
|
2015-02-02 00:13:26 +08:00
|
|
|
{
|
|
|
|
std::vector<acl::string> args;
|
|
|
|
const char* cmd = "SET";
|
|
|
|
|
|
|
|
args.push_back("multi_string_key");
|
|
|
|
args.push_back("multi_string_value");
|
|
|
|
|
2015-04-07 23:31:31 +08:00
|
|
|
if (redis.run_cmd(cmd, args) == false)
|
2015-02-02 00:13:26 +08:00
|
|
|
{
|
|
|
|
printf("run cmd: %s error\r\n", cmd);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
args.clear();
|
2015-04-07 23:31:31 +08:00
|
|
|
redis.clear();
|
2015-02-02 00:13:26 +08:00
|
|
|
|
|
|
|
cmd = "HMSET";
|
|
|
|
args.push_back("multi_hash_key");
|
|
|
|
args.push_back("name1");
|
|
|
|
args.push_back("value1");
|
|
|
|
args.push_back("name2");
|
|
|
|
args.push_back("values");
|
|
|
|
|
2015-04-07 23:31:31 +08:00
|
|
|
if (redis.run_cmd(cmd, args) == false)
|
2015-02-02 00:13:26 +08:00
|
|
|
{
|
|
|
|
printf("run cmd: %s error\r\n", cmd);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
args.clear();
|
2015-04-07 23:31:31 +08:00
|
|
|
redis.clear();
|
2015-02-02 00:13:26 +08:00
|
|
|
|
|
|
|
cmd = "GET";
|
|
|
|
args.push_back("multi_string_key");
|
|
|
|
|
2015-04-07 23:31:31 +08:00
|
|
|
if (redis.run_cmd(cmd, args) == false)
|
2015-02-02 00:13:26 +08:00
|
|
|
{
|
|
|
|
printf("run cmd: %s error\r\n", cmd);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
args.clear();
|
2015-04-07 23:31:31 +08:00
|
|
|
redis.clear();
|
2015-02-02 00:13:26 +08:00
|
|
|
|
|
|
|
cmd = "HGET";
|
|
|
|
args.push_back("multi_hash_key");
|
|
|
|
args.push_back("name1");
|
|
|
|
|
2015-04-07 23:31:31 +08:00
|
|
|
if (redis.run_cmd(cmd, args) == false)
|
2015-02-02 00:13:26 +08:00
|
|
|
{
|
|
|
|
printf("run cmd: %s error\r\n", cmd);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
printf("run all cmds ok\r\n");
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2015-04-07 23:31:31 +08:00
|
|
|
static bool test_exec(acl::redis_transaction& redis)
|
2015-02-02 00:13:26 +08:00
|
|
|
{
|
2015-04-07 23:31:31 +08:00
|
|
|
redis.clear();
|
|
|
|
if (redis.exec() == false)
|
2015-02-02 00:13:26 +08:00
|
|
|
{
|
|
|
|
printf("exec error\r\n");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
printf("exec ok\r\n");
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2015-04-07 23:31:31 +08:00
|
|
|
static bool get_results(acl::redis_transaction& redis)
|
2015-02-02 00:13:26 +08:00
|
|
|
{
|
2015-04-07 23:31:31 +08:00
|
|
|
const std::vector<acl::string>& cmds = redis.get_commands();
|
|
|
|
size_t size = redis.get_size();
|
2015-02-02 00:13:26 +08:00
|
|
|
|
|
|
|
if (size != cmds.size())
|
|
|
|
{
|
|
|
|
printf("invalid size: %lu != %lu\r\n",
|
|
|
|
(unsigned long) size, (unsigned long) cmds.size());
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
/////////////////////////////////////////////////////////////////////
|
|
|
|
// cmd: SET
|
|
|
|
|
2015-04-07 23:31:31 +08:00
|
|
|
const acl::redis_result* result = redis.get_child(0, NULL);
|
2015-02-02 00:13:26 +08:00
|
|
|
if (result == NULL)
|
|
|
|
{
|
|
|
|
printf("have no result for cmd %s\r\n", cmds[0].c_str());
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
if (result->get_type() != acl::REDIS_RESULT_STATUS)
|
|
|
|
{
|
|
|
|
printf("invalid result for cmd %s\r\n", cmds[0].c_str());
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
printf("run cmd %s ok\r\n", cmds[0].c_str());
|
|
|
|
|
|
|
|
/////////////////////////////////////////////////////////////////////
|
|
|
|
// cmd: HMSET
|
|
|
|
|
2015-04-07 23:31:31 +08:00
|
|
|
result = redis.get_child(1, NULL);
|
2015-02-02 00:13:26 +08:00
|
|
|
if (result == NULL)
|
|
|
|
{
|
|
|
|
printf("have no result for cmd %s\r\n", cmds[0].c_str());
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
if (result->get_type() != acl::REDIS_RESULT_STATUS)
|
|
|
|
{
|
|
|
|
printf("invalid result for cmd %s\r\n", cmds[0].c_str());
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
printf("run cmd %s ok\r\n", cmds[0].c_str());
|
|
|
|
|
|
|
|
/////////////////////////////////////////////////////////////////////
|
|
|
|
// cmd: GET
|
|
|
|
|
2015-04-07 23:31:31 +08:00
|
|
|
result = redis.get_child(2, NULL);
|
2015-02-02 00:13:26 +08:00
|
|
|
if (result == NULL)
|
|
|
|
{
|
|
|
|
printf("have no result for cmd %s\r\n", cmds[0].c_str());
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
if (result->get_type() != acl::REDIS_RESULT_STRING)
|
|
|
|
{
|
|
|
|
printf("invalid result for cmd %s\r\n", cmds[0].c_str());
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
printf("run cmd %s ok, result: %s\r\n", cmds[0].c_str(),
|
|
|
|
result->get(0));
|
|
|
|
|
|
|
|
/////////////////////////////////////////////////////////////////////
|
|
|
|
// cmd: HGET
|
|
|
|
|
2015-04-07 23:31:31 +08:00
|
|
|
result = redis.get_child(3, NULL);
|
2015-02-02 00:13:26 +08:00
|
|
|
if (result == NULL)
|
|
|
|
{
|
|
|
|
printf("have no result for cmd %s\r\n", cmds[0].c_str());
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
if (result->get_type() != acl::REDIS_RESULT_STRING)
|
|
|
|
{
|
|
|
|
printf("invalid result for cmd %s\r\n", cmds[0].c_str());
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
printf("run cmd %s ok, result: %s\r\n", cmds[0].c_str(),
|
|
|
|
result->get(0));
|
|
|
|
|
|
|
|
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"
|
|
|
|
"-C connect_timeout[default: 10]\r\n"
|
|
|
|
"-I rw_timeout[default: 10]\r\n"
|
|
|
|
"-S [if slice request, default: no]\r\n",
|
|
|
|
procname);
|
|
|
|
}
|
|
|
|
|
|
|
|
int main(int argc, char* argv[])
|
|
|
|
{
|
|
|
|
int ch, conn_timeout = 10, rw_timeout = 10;
|
|
|
|
acl::string addr("127.0.0.1:6379");
|
|
|
|
bool slice_req = false;
|
|
|
|
|
|
|
|
while ((ch = getopt(argc, argv, "hs:C:I:S")) > 0)
|
|
|
|
{
|
|
|
|
switch (ch)
|
|
|
|
{
|
|
|
|
case 'h':
|
|
|
|
usage(argv[0]);
|
|
|
|
return 0;
|
|
|
|
case 's':
|
|
|
|
addr = optarg;
|
|
|
|
break;
|
|
|
|
case 'C':
|
|
|
|
conn_timeout = atoi(optarg);
|
|
|
|
break;
|
|
|
|
case 'I':
|
|
|
|
rw_timeout = atoi(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);
|
|
|
|
client.set_slice_respond(false);
|
2015-04-07 23:31:31 +08:00
|
|
|
acl::redis_transaction redis(&client);
|
2015-02-02 00:13:26 +08:00
|
|
|
|
|
|
|
bool ret;
|
|
|
|
|
2015-04-07 23:31:31 +08:00
|
|
|
ret = test_multi(redis) && test_run_cmds(redis)
|
|
|
|
&& test_exec(redis) && get_results(redis);
|
2015-02-02 00:13:26 +08:00
|
|
|
|
|
|
|
printf("all cmds %s\r\n", ret ? "ok" : "failed");
|
|
|
|
|
|
|
|
#ifdef WIN32
|
|
|
|
printf("enter any key to exit\r\n");
|
|
|
|
getchar();
|
|
|
|
#endif
|
|
|
|
return 0;
|
|
|
|
}
|