memcache_test.c
author Tero Marttila <terom@fixme.fi>
Thu, 28 Aug 2008 03:12:11 +0300
changeset 46 8a832c0e01ee
parent 44 03a7e064f833
child 48 1c67f512779b
permissions -rw-r--r--
bugfixed, enum->string mappings, test does requests

#include <event2/event.h>
#include <event2/event_compat.h>
#include <event2/event_struct.h>

#include "memcache.h"
#include "config.h"
#include "common.h"

static struct memcache *mc;
static struct config_endpoint server_endpoint;
static char *data_1 = "rei4quohV8Oocio1ua0co8ni4Ae1re4houcheixahchoh3ioghie0aShooShoh6Ahboequ9eiX5eashuu6Chu1quo"
                            "o0suph7cheiyai1ea0ooh7Aevoo4feihubupohDeephahwee2Ooz7chiediec7neit7keTh6xuheash8chaeKa5vi"
                            "ekooqu7ooj6Eezooroi6Nequ9ca2yi6iSoigh3loowaey9eiphaphaiJ0souy7wohpa9eXo5Ahu2sa";
static char *data_2 = "iefaek7ighi5UpueThageish5ieshohyeil1raiceerahjahng5ui7vuzie9quu4dai5ar2aiXi5ieth4looweigi"
                            "e3fo5ieri1queengaiphuaghaic1xahvoo9joo6baiNaig8puCootheowah4moocohDoiquoh3quieka5ao3aeNg9"
                            "Aimei1soangu4Duch5pho5buu2ohzaich4chahz9iTh3Pei4beep1ongie6au1aafoosh2vierei5E";

void _memcache_cb (struct memcache_req *req, void *arg) {
    char *key = arg;
    const struct memcache_obj *obj;
    const struct memcache_buf *buf;

    INFO("[%s]: cmd=%15s state=%15s reply=%15s", key,
        memcache_command_str(memcache_req_cmd(req)),
        memcache_state_str(memcache_req_state(req)),
        memcache_reply_str(memcache_req_reply(req))
    );
    
    if ((obj = memcache_req_obj(req)))
        INFO("\tobj: flags=0x%04X exptime=%9zu bytes=%6zu cas=%llu", obj->flags, obj->exptime, obj->bytes, obj->cas);

    if ((buf = memcache_req_buf(req)))
        INFO("\tbuf: data=%p len=%6zu offset=%6zu", buf->data, buf->len, buf->offset);
    
    INFO("%s", "");
}

void begin_test () {
    struct memcache_key key_1, key_2;
    struct memcache_obj obj_1, obj_2;
    struct memcache_buf buf_1, buf_2;

    if ((mc = memcache_alloc(&_memcache_cb)) == NULL)
        ERROR("memcache_alloc");
    
    // fix up the endpoint
    endpoint_init(&server_endpoint, 11211);
    
    if (endpoint_parse(&server_endpoint, "localhost"))
        ERROR("config_endpoint_parse");
    
    // add the server
    if (memcache_add_server(mc, &server_endpoint, 1))
        ERROR("memcache_add_server");
   
    // add a request or two
    key_1.buf = "memcache_test_k1";
    key_2.buf = "memcache_test_k2";
    key_1.len = key_2.len = 0;

    obj_1.flags = 0x1A;
    obj_2.flags = 0x2B;

    obj_1.exptime = 0;
    obj_2.exptime = 3600;

    obj_1.bytes = strlen(data_1);
    obj_2.bytes = strlen(data_2);

    buf_1.data = data_1;
    buf_1.len = strlen(data_1);
    buf_1.offset = buf_1.len;

    buf_2.data = data_2;
    buf_2.len = strlen(data_2);
    buf_2.offset = buf_2.len;

    if (memcache_store(mc, MEMCACHE_CMD_STORE_SET, &key_1, &obj_1, &buf_1, key_1.buf))
        ERROR("memcache_store: key_1");
    
    if (memcache_store(mc, MEMCACHE_CMD_STORE_ADD, &key_2, &obj_2, &buf_2, key_2.buf))
        ERROR("memcache_store: key_2");
    
    if (memcache_fetch(mc, &key_1, key_1.buf))
        ERROR("memcache_fetch: key_1");
    
    if (memcache_fetch(mc, &key_2, key_2.buf))
        ERROR("memcache_fetch: key_2");

error:
    return;
}

int main (int argc, char **argv) {
    // libevent init
    struct event_base *ev_base = event_init();

    if (!ev_base)
        FATAL("event_init");
    
    begin_test();

    // run the libevent mainloop
    if (event_base_dispatch(ev_base))
        WARNING("event_dispatch");

    INFO("SHUTDOWN");
    
    // clean up
    event_base_free(ev_base);
    
    // successfull exit
    return 0;
}