EV-Hiredis

 view release on metacpan or  search on metacpan

deps/hiredis/test.c  view on Meta::CPAN

        redisAsyncCommand(c, commandCallback, NULL, "PING");
    }
}
static void disconnectCallback(const redisAsyncContext *c, int status) {
    assert(c->data == (void*)&astest);
    assert(astest.disconnects == 0);
    astest.err = c->err;
    strcpy(astest.errstr, c->errstr);
    astest.disconnects++;
    astest.disconnect_status = status;
    astest.connected = 0;
}

static void commandCallback(struct redisAsyncContext *ac, void* _reply, void* _privdata)
{
    redisReply *reply = (redisReply*)_reply;
    struct _astest *t = (struct _astest *)ac->data;
    assert(t == &astest);
    (void)_privdata;
    t->err = ac->err;
    strcpy(t->errstr, ac->errstr);
    t->counter++;
    if (t->testno == ASTEST_PINGPONG ||t->testno == ASTEST_ISSUE_931_PING)
    {
        assert(reply != NULL && reply->type == REDIS_REPLY_STATUS && strcmp(reply->str, "PONG") == 0);
        t->pongs++;
        redisAsyncFree(ac);
    }
    if (t->testno == ASTEST_PINGPONG_TIMEOUT)
    {
        /* two ping pongs */
        assert(reply != NULL && reply->type == REDIS_REPLY_STATUS && strcmp(reply->str, "PONG") == 0);
        t->pongs++;
        if (t->counter == 1) {
            int status = redisAsyncCommand(ac, commandCallback, NULL, "PING");
            assert(status == REDIS_OK);
        } else {
            redisAsyncFree(ac);
        }
    }
}

static redisAsyncContext *do_aconnect(struct config config, astest_no testno)
{
    redisOptions options = {0};
    memset(&astest, 0, sizeof(astest));

    astest.testno = testno;
    astest.connect_status = astest.disconnect_status = -2;

    if (config.type == CONN_TCP) {
        options.type = REDIS_CONN_TCP;
        options.connect_timeout = &config.connect_timeout;
        REDIS_OPTIONS_SET_TCP(&options, config.tcp.host, config.tcp.port);
    } else if (config.type == CONN_SSL) {
        options.type = REDIS_CONN_TCP;
        options.connect_timeout = &config.connect_timeout;
        REDIS_OPTIONS_SET_TCP(&options, config.ssl.host, config.ssl.port);
    } else if (config.type == CONN_UNIX) {
        options.type = REDIS_CONN_UNIX;
        options.endpoint.unix_socket = config.unix_sock.path;
    } else if (config.type == CONN_FD) {
        options.type = REDIS_CONN_USERFD;
        /* Create a dummy connection just to get an fd to inherit */
        redisContext *dummy_ctx = redisConnectUnix(config.unix_sock.path);
        if (dummy_ctx) {
            redisFD fd = disconnect(dummy_ctx, 1);
            printf("Connecting to inherited fd %d\n", (int)fd);
            options.endpoint.fd = fd;
        }
    }
    redisAsyncContext *c = redisAsyncConnectWithOptions(&options);
    assert(c);
    astest.ac = c;
    c->data = &astest;
    c->dataCleanup = asCleanup;
    redisPollAttach(c);
    redisAsyncSetConnectCallbackNC(c, connectCallback);
    redisAsyncSetDisconnectCallback(c, disconnectCallback);
    return c;
}

static void as_printerr(void) {
    printf("Async err %d : %s\n", astest.err, astest.errstr);
}

#define ASASSERT(e) do { \
    if (!(e)) \
        as_printerr(); \
    assert(e); \
} while (0);

static void test_async_polling(struct config config) {
    int status;
    redisAsyncContext *c;
    struct config defaultconfig = config;

    test("Async connect: ");
    c = do_aconnect(config, ASTEST_CONNECT);
    assert(c);
    while(astest.connected == 0)
        redisPollTick(c, 0.1);
    assert(astest.connects == 1);
    ASASSERT(astest.connect_status == REDIS_OK);
    assert(astest.disconnects == 0);
    test_cond(astest.connected == 1);

    test("Async free after connect: ");
    assert(astest.ac != NULL);
    redisAsyncFree(c);
    assert(astest.disconnects == 1);
    assert(astest.ac == NULL);
    test_cond(astest.disconnect_status == REDIS_OK);

    if (config.type == CONN_TCP || config.type == CONN_SSL) {
        /* timeout can only be simulated with network */
        test("Async connect timeout: ");
        config.tcp.host = "192.168.254.254";  /* blackhole ip */
        config.connect_timeout.tv_usec = 100000;
        c = do_aconnect(config, ASTEST_CONN_TIMEOUT);
        assert(c);
        assert(c->err == 0);
        while(astest.connected == 0)
            redisPollTick(c, 0.1);
        assert(astest.connected == -1);
        /*
         * freeing should not be done, clearing should have happened.
         *redisAsyncFree(c);
         */



( run in 2.342 seconds using v1.01-cache-2.11-cpan-140bd7fdf52 )