static void knh_Actor_addMemcached(CTX ctx, knh_Actor_t *a) { const char *actor_name = DP(a)->actor_name; knh_String_t *p = ctx->script->ns->path->urn; const char *path = p->str.text; memcached_st *memc = new_memcached("localhost"); int port = knh_Actor_getPortNum(memc); port++; char port_str[5] = {0}; snprintf(port_str, 5, "%d", port); memcached_setValue(memc, "port", port_str); char buf[MAX_BUF_SIZE] = {0}; if (sizeof(path) + 6 > MAX_BUF_SIZE) { fprintf(stderr, "ERROR: too long name\n"); } snprintf(buf, MAX_BUF_SIZE, "%s:%s", path, port_str); memcached_setValue(memc, actor_name, buf); DP(a)->port = port; DP(a)->path = path; RETURNvoid_(); }
static const char *knh_Actor_getActorPathFromMemcached(const char *actor_name) { memcached_st *memc = new_memcached("localhost"); return memcached_getValue(memc, actor_name); }
int main(int argc, char **argv) { test_init(argc, argv); struct memcached_process_handle* mchandle = new_memcached(0, ""); // start the server if (!mchandle) { fprintf(stderr,"Could not start memcached process\n\n"); exit(0); } struct Memcache* memcache = libmemc_create(Automatic); if (libmemc_add_server(memcache, "127.0.0.1", mchandle->port) == -1) { fprintf(stderr,"Could not add server\n\n"); exit(0); } int usock = new_udp_sock(mchandle); char ok_str[50]; char notok_str[50]; if (libmemc_get_protocol(memcache) == Textual) { // set foo struct Item item = {0}; setItem(&item, 0, "foo", 3, 0, "fooval", 6, 0); ok_test(!libmemc_set(memcache, &item), "stored foo", "failed to store foo"); mem_get_is(memcache, &item, "foo == fooval", "foo != fooval"); test_single(usock); int offset[3] = {1, 1, 2}; for (int i=0; i<3; i++) { udp_data *res = send_udp_request(usock, 160 + offset[i], "get foo\r\n"); ok_test(res != NULL, "got result", "did not get result"); ok_test(res[0].packets == 1, "one packet", "not one packet"); ok_test(res[0].seq_num == 0, "only got seq number 0", "did not only get seq number 0"); ok_test(!memcmp(res[0].body, "VALUE foo 0 6\r\nfooval\r\nEND\r\n", 28), "payload is as expected", "payload is not as expected"); sprintf(ok_str, "request id in response %d is correct", res[0].request_id); sprintf(notok_str, "request id in response %d is not correct", res[0].request_id); ok_test(res[0].request_id == 160 + offset[i], ok_str, notok_str); if (res) free(res); } // testing non-existent stuff udp_data *res = send_udp_request(usock, 404, "get notexist\r\n"); ok_test(res != NULL, "got result", "did not get result"); ok_test(res[0].packets == 1, "one packet", "not one packet"); ok_test(res[0].seq_num == 0, "only got seq number 0", "did not only get seq number 0"); sprintf(ok_str, "request id %d correct", res[0].request_id); sprintf(notok_str, "request id %d not correct", res[0].request_id); ok_test(res[0].request_id == 404, ok_str, notok_str); ok_test(!memcmp(res[0].body, "END\r\n", 5), "payload is as expected", "payload is not as expected"); if (res) free(res); int size = 4096; // 256 kB char *big = malloc(size); for (int i=0; i<(size/4); i++) { memcpy(big + i*4, "abcd", 4); } setItem(&item, 0, "big", 3, 0, big, size, 0); ok_test(!libmemc_set(memcache, &item), "stored big", "failed to store big"); mem_get_is(memcache, &item, "big value matches", "big value does not match"); res = send_udp_request(usock, 999, "get big\r\n"); ok_test(res[0].packets == 3, "three packet response", "not three packet response"); ok_test(!memcmp(res[0].body, "VALUE big 0 4096\r\n", 18), "first packet has value line", "first packet is missing value line"); ok_test(strstr(res[2].body, "END\r\n") != NULL, "last packet has end", "last packet is missing end"); ok_test(res[1].request_id == 999, "request id of middle packet is correct", "request id of middle packet is incorrect"); if (res) free(res); } else { fprintf(stdout, "Tests not implemented for binary protocol\n"); } libmemc_destroy(memcache); test_report(); }
int main(int argc, char **argv) { test_init(argc, argv); // start the server struct memcached_process_handle* mchandle = new_memcached(0, ""); if (!mchandle) { fprintf(stderr,"Could not start memcached process\n\n"); exit(0); } struct Memcache* memcache = libmemc_create(Automatic); if (libmemc_add_server(memcache, "127.0.0.1", mchandle->port) == -1) { fprintf(stderr,"Could not add server\n\n"); exit(0); } struct Item item = {0}; setItem(&item, 0, "num", 3, 0, "1", 1, 0); ok_test(!libmemc_set(memcache, &item), "stored num", "failed to store num"); mem_get_is(memcache, &item, "num == '1'", "num != '1'"); ok_test(!libmemc_incr(memcache, &item, 1) && !memcmp(item.data, "2", item.size), "+ 1 = 2", "+ 1 != 2"); mem_get_is(memcache, &item, "num == '2'", "num != '2'"); ok_test(!libmemc_incr(memcache, &item, 8) && !memcmp(item.data, "10", item.size), "+ 8 = 10", "+ 8 != 10"); mem_get_is(memcache, &item, "num == '10'", "num != '10'"); ok_test(!libmemc_decr(memcache, &item, 1) && !memcmp(item.data, "9", item.size), "- 1 = 9", "- 1 != 9"); ok_test(!libmemc_decr(memcache, &item, 9) && !memcmp(item.data, "0", item.size), "- 9 = 0", "- 9 != 0"); ok_test(!libmemc_decr(memcache, &item, 5) && !memcmp(item.data, "0", item.size), "- 5 = 0", "- 5 != 0"); setItem(&item, 0, "num", 3, 0, "4294967296", 10, 0); ok_test(!libmemc_set(memcache, &item), "stored 2**32", "failed to store 2**32"); ok_test(!libmemc_incr(memcache, &item, 1) && !memcmp(item.data, "4294967297", item.size), "4294967296 + 1 = 4294967297", "4294967296 + 1 != 4294967297"); setItem(&item, 0, "num", 3, 0, "18446744073709551615", 20, 0); ok_test(!libmemc_set(memcache, &item), "stored 2**64-1", "failed to store 2**64-1"); ok_test(!libmemc_incr(memcache, &item, 1) && !memcmp(item.data, "0", item.size), "(2**64 - 1) + 1 = 0", "(2**64 - 1) + 1 != 0"); if (libmemc_get_protocol(memcache) == Textual) { setItem(&item, 0, "bogus1", 6, 0, "0", 1, 0); ok_test((libmemc_decr(memcache, &item, 5) == -1), "can't decr bogus key", "can decr bogus key"); setItem(&item, 0, "bogus2", 6, 0, "0", 1, 0); ok_test((libmemc_incr(memcache, &item, 5) == -1), "can't incr bogus key", "can incr bogus key"); } else { setItem(&item, 0, "bogus1", 6, 0, "4", 1, 0xffffffff); ok_test((libmemc_decr(memcache, &item, 5) == -1), "can't decr bogus key when expiry == 0xffffffff", "can decr bogus key when expiry == 0xffffffff"); setItem(&item, 0, "bogus2", 6, 0, "4", 1, 0xffffffff); ok_test((libmemc_incr(memcache, &item, 5) == -1), "can't incr bogus key when expiry == 0xffffffff", "can incr bogus key when expiry == 0xffffffff"); setItem(&item, 0, "bogus1", 6, 0, "4", 1, 0); ok_test(!libmemc_decr(memcache, &item, 5), "can decr bogus key when expiry != 0xffffffff", "can't decr bogus key when expiry != 0xffffffff"); setItem(&item, 0, "bogus2", 6, 0, "4", 1, 0); ok_test(!libmemc_incr(memcache, &item, 5), "can incr bogus key when expiry != 0xffffffff", "can't incr bogus key when expiry != 0xffffffff"); } setItem(&item, 0, "text", 4, 0, "hi", 2, 0); ok_test(!libmemc_set(memcache, &item), "stored text", "failed to store text"); ok_test(!libmemc_incr(memcache, &item, 1) && !memcmp(item.data, "1", item.size), "hi + 1 = 1", "hi + 1 != 1"); setItem(&item, 0, "text", 4, 0, "hi", 2, 0); ok_test(!libmemc_set(memcache, &item), "stored text", "failed to store text"); ok_test(!libmemc_decr(memcache, &item, 1) && !memcmp(item.data, "0", item.size), "hi - 1 = 0", "hi - 1 != 0"); libmemc_destroy(memcache); test_report(); }
int main(int argc, char **argv) { test_init(argc, argv); // start the server struct memcached_process_handle* mchandle = new_memcached(0, ""); if (!mchandle) { fprintf(stderr,"Could not start memcached process\n\n"); exit(0); } struct Memcache* memcache = libmemc_create(Automatic); if (libmemc_add_server(memcache, "127.0.0.1", mchandle->port) == -1) { fprintf(stderr,"Could not add server\n\n"); exit(0); } // wait for early second struct timeval tv; while (1) { gettimeofday(&tv, NULL); if (tv.tv_usec < 500000) break; usleep(100000); // 1/10th of a second sleeps until time changes. } struct Item item = {0}; // We choose expiry time = 2 secs instead of 1 sec. // 1 sec is too small for these tests since memcached updates current_time once a second // which means that an item will frequently expire in much less than a second if exptime=1 setItem(&item, 0, "foo", 3, 0, "fooval", 6, 2); ok_test(!libmemc_set(memcache, &item), "stored foo", "failed to store foo"); mem_get_is(memcache, &item, "foo == 'fooval'", "foo != 'fooval'"); usleep(2500000); // sleep 2.5 secs struct Item item2 = {0}; setItem(&item2, 0, "foo", 3, 0, NULL, 0, 0); mem_get_is(memcache, &item2, "foo == <undef>", "foo != <undef>"); gettimeofday(&tv, NULL); item.exptime = tv.tv_sec - 1; ok_test(!libmemc_set(memcache, &item), "stored foo", "failed to store foo"); mem_get_is(memcache, &item2, "already expired", "not expired"); struct Item item3 = {0}; setItem(&item3, 0, "foo", 3, 0, "foov+1", 6, 2); ok_test(!libmemc_set(memcache, &item3), "stored foo", "failed to store foo"); mem_get_is(memcache, &item3, "foo == 'foov+1'", "foo != 'foov+1'"); usleep(2500000); // sleep 2.5 secs struct Item item4 = {0}; setItem(&item4, 0, "foo", 3, 0, NULL, 0, 0); mem_get_is(memcache, &item4, "now expired", "not expired"); gettimeofday(&tv, NULL); struct Item item5 = {0}; setItem(&item5, 0, "boo", 3, 0, "booval", 6, tv.tv_sec - 20); ok_test(!libmemc_set(memcache, &item3), "stored boo", "failed to store boo"); struct Item item6 = {0}; setItem(&item6, 0, "boo", 3, 0, NULL, 0, 0); mem_get_is(memcache, &item6, "now expired", "not expired"); libmemc_destroy(memcache); test_report(); }