Test helper for creating a non-blocking connection

This commit is contained in:
Pieter Noordhuis 2010-10-19 21:00:13 +02:00
parent e3067fe231
commit bbe007a75a

25
test.c
View File

@ -211,62 +211,65 @@ static void __test_reply_callback(redisContext *c, redisReply *reply, void *priv
freeReplyObject(reply); freeReplyObject(reply);
} }
static redisContext *__connect_nonblock() {
/* Reset callback flags */
__test_callback_flags = __test_reply_callback_flags = 0;
return redisConnectNonBlock("127.0.0.1", 6379, NULL);
}
static void test_nonblocking_connection() { static void test_nonblocking_connection() {
redisContext *c; redisContext *c;
int wdone = 0; int wdone = 0;
__test_callback_flags = 0;
test("Calls command callback when command is issued: "); test("Calls command callback when command is issued: ");
c = redisConnectNonBlock("127.0.0.1", 6379, NULL); c = __connect_nonblock();
redisSetCommandCallback(c,__test_callback,(void*)1); redisSetCommandCallback(c,__test_callback,(void*)1);
redisCommand(c,"PING"); redisCommand(c,"PING");
test_cond(__test_callback_flags == 1); test_cond(__test_callback_flags == 1);
redisFree(c); redisFree(c);
__test_callback_flags = 0;
test("Calls disconnect callback on redisDisconnect: "); test("Calls disconnect callback on redisDisconnect: ");
c = redisConnectNonBlock("127.0.0.1", 6379, NULL); c = __connect_nonblock();
redisSetDisconnectCallback(c,__test_callback,(void*)2); redisSetDisconnectCallback(c,__test_callback,(void*)2);
redisDisconnect(c); redisDisconnect(c);
test_cond(__test_callback_flags == 2); test_cond(__test_callback_flags == 2);
redisFree(c); redisFree(c);
__test_callback_flags = 0;
test("Calls disconnect callback and free callback on redisFree: "); test("Calls disconnect callback and free callback on redisFree: ");
c = redisConnectNonBlock("127.0.0.1", 6379, NULL); c = __connect_nonblock();
redisSetDisconnectCallback(c,__test_callback,(void*)2); redisSetDisconnectCallback(c,__test_callback,(void*)2);
redisSetFreeCallback(c,__test_callback,(void*)4); redisSetFreeCallback(c,__test_callback,(void*)4);
redisFree(c); redisFree(c);
test_cond(__test_callback_flags == ((2 << 8) | 4)); test_cond(__test_callback_flags == ((2 << 8) | 4));
test("redisBufferWrite against empty write buffer: "); test("redisBufferWrite against empty write buffer: ");
c = redisConnectNonBlock("127.0.0.1", 6379, NULL); c = __connect_nonblock();
test_cond(redisBufferWrite(c,&wdone) == REDIS_OK && wdone == 1); test_cond(redisBufferWrite(c,&wdone) == REDIS_OK && wdone == 1);
redisFree(c); redisFree(c);
test("redisBufferWrite against not yet connected fd: "); test("redisBufferWrite against not yet connected fd: ");
c = redisConnectNonBlock("127.0.0.1", 6379, NULL); c = __connect_nonblock();
redisCommand(c,"PING"); redisCommand(c,"PING");
test_cond(redisBufferWrite(c,NULL) == REDIS_ERR && test_cond(redisBufferWrite(c,NULL) == REDIS_ERR &&
strncmp(c->error,"write:",6) == 0); strncmp(c->error,"write:",6) == 0);
redisFree(c); redisFree(c);
test("redisBufferWrite against closed fd: "); test("redisBufferWrite against closed fd: ");
c = redisConnectNonBlock("127.0.0.1", 6379, NULL); c = __connect_nonblock();
redisCommand(c,"PING"); redisCommand(c,"PING");
redisDisconnect(c); redisDisconnect(c);
test_cond(redisBufferWrite(c,NULL) == REDIS_ERR && test_cond(redisBufferWrite(c,NULL) == REDIS_ERR &&
strncmp(c->error,"write:",6) == 0); strncmp(c->error,"write:",6) == 0);
redisFree(c); redisFree(c);
wdone = __test_reply_callback_flags = 0;
test("Process callbacks in the right sequence: "); test("Process callbacks in the right sequence: ");
c = redisConnectNonBlock("127.0.0.1", 6379, NULL); c = __connect_nonblock();
redisCommandWithCallback(c,__test_reply_callback,(void*)1,"PING"); redisCommandWithCallback(c,__test_reply_callback,(void*)1,"PING");
redisCommandWithCallback(c,__test_reply_callback,(void*)2,"PING"); redisCommandWithCallback(c,__test_reply_callback,(void*)2,"PING");
redisCommandWithCallback(c,__test_reply_callback,(void*)3,"PING"); redisCommandWithCallback(c,__test_reply_callback,(void*)3,"PING");
/* Write output buffer */ /* Write output buffer */
wdone = 0;
while(!wdone) { while(!wdone) {
usleep(500); usleep(500);
redisBufferWrite(c,&wdone); redisBufferWrite(c,&wdone);