X-Git-Url: https://git.m6w6.name/?a=blobdiff_plain;f=tests%2Ffunction.c;h=e72dc7eed64de3f8998f88a86d013ccc997f0727;hb=b0e52309fcc12077506ff7d0d7d55311d64a073e;hp=4483232c6b6666a24e80af0d8f577ea70453d32c;hpb=a9d5b6c6db3a99ba92ef4b298702ecfbbd5145af;p=m6w6%2Flibmemcached diff --git a/tests/function.c b/tests/function.c index 4483232c..e72dc7ee 100644 --- a/tests/function.c +++ b/tests/function.c @@ -48,13 +48,13 @@ static test_return server_list_null_test(memcached_st *ptr __attribute__((unuse memcached_server_st *server_list; memcached_return rc; - server_list= memcached_server_list_append(NULL, NULL, 0, NULL); + server_list= memcached_server_list_append_with_weight(NULL, NULL, 0, 0, NULL); assert(server_list == NULL); - server_list= memcached_server_list_append(NULL, "localhost", 0, NULL); + server_list= memcached_server_list_append_with_weight(NULL, "localhost", 0, 0, NULL); assert(server_list == NULL); - server_list= memcached_server_list_append(NULL, NULL, 0, &rc); + server_list= memcached_server_list_append_with_weight(NULL, NULL, 0, 0, &rc); assert(server_list == NULL); return 0; @@ -88,7 +88,7 @@ static test_return server_sort_test(memcached_st *ptr __attribute__((unused))) for (x= 0; x < TEST_PORT_COUNT; x++) { test_ports[x]= random() % 64000; - rc= memcached_server_add(local_memc, "localhost", test_ports[x]); + rc= memcached_server_add_with_weight(local_memc, "localhost", test_ports[x], 0); assert(local_memc->number_of_hosts == x + 1); assert(local_memc->hosts[0].count == x+1); assert(rc == MEMCACHED_SUCCESS); @@ -115,11 +115,11 @@ static test_return server_sort2_test(memcached_st *ptr __attribute__((unused))) rc= memcached_behavior_set(local_memc, MEMCACHED_BEHAVIOR_SORT_HOSTS, 1); assert(rc == MEMCACHED_SUCCESS); - rc= memcached_server_add(local_memc, "MEMCACHED_BEHAVIOR_SORT_HOSTS", 43043); + rc= memcached_server_add_with_weight(local_memc, "MEMCACHED_BEHAVIOR_SORT_HOSTS", 43043, 0); assert(rc == MEMCACHED_SUCCESS); assert(local_memc->hosts[0].port == 43043); - rc= memcached_server_add(local_memc, "MEMCACHED_BEHAVIOR_SORT_HOSTS", 43042); + rc= memcached_server_add_with_weight(local_memc, "MEMCACHED_BEHAVIOR_SORT_HOSTS", 43042, 0); assert(rc == MEMCACHED_SUCCESS); assert(local_memc->hosts[0].port == 43042); assert(local_memc->hosts[1].port == 43043); @@ -159,7 +159,7 @@ static test_return server_unsort_test(memcached_st *ptr __attribute__((unused)) for (x= 0; x < TEST_PORT_COUNT; x++) { test_ports[x]= random() % 64000; - rc= memcached_server_add(local_memc, "localhost", test_ports[x]); + rc= memcached_server_add_with_weight(local_memc, "localhost", test_ports[x], 0); assert(local_memc->number_of_hosts == x+1); assert(local_memc->hosts[0].count == x+1); assert(rc == MEMCACHED_SUCCESS); @@ -234,7 +234,7 @@ static test_return connection_test(memcached_st *memc) { memcached_return rc; - rc= memcached_server_add(memc, "localhost", 0); + rc= memcached_server_add_with_weight(memc, "localhost", 0, 0); assert(rc == MEMCACHED_SUCCESS); return 0; @@ -395,10 +395,13 @@ static test_return cas2_test(memcached_st *memc) static test_return cas_test(memcached_st *memc) { memcached_return rc; - char *key= "fun"; - size_t key_length= strlen("fun"); - char *value= "we the people"; - size_t value_length= strlen("we the people"); + const char *key= "fun"; + size_t key_length= strlen(key); + const char *value= "we the people"; + size_t value_length= strlen(value); + const char *value2= "change the value"; + size_t value2_length= strlen(value2); + memcached_result_st results_obj; memcached_result_st *results; unsigned int set= 1; @@ -421,24 +424,27 @@ static test_return cas_test(memcached_st *memc) assert(results); assert(rc == MEMCACHED_SUCCESS); WATCHPOINT_ASSERT(memcached_result_cas(results)); - - assert(!memcmp(value, "we the people", strlen("we the people"))); - assert(strlen("we the people") == value_length); + assert(!memcmp(value, memcached_result_value(results), value_length)); + assert(strlen(memcached_result_value(results)) == value_length); assert(rc == MEMCACHED_SUCCESS); + uint64_t cas = memcached_result_cas(results); - rc= memcached_cas(memc, key, key_length, - "change the value", strlen("change the value"), - 0, 0, memcached_result_cas(results)); - + #if 0 + results= memcached_fetch_result(memc, &results_obj, &rc); + assert(rc == MEMCACHED_END); + assert(results == NULL); +#endif + + rc= memcached_cas(memc, key, key_length, value2, value2_length, 0, 0, cas); assert(rc == MEMCACHED_SUCCESS); - rc= memcached_cas(memc, key, key_length, - "change the value", strlen("change the value"), - 0, 0, 23); - + /* + * The item will have a new cas value, so try to set it again with the old + * value. This should fail! + */ + rc= memcached_cas(memc, key, key_length, value2, value2_length, 0, 0, cas); assert(rc == MEMCACHED_DATA_EXISTS); - memcached_result_free(&results_obj); return 0; @@ -597,6 +603,7 @@ static test_return bad_key_test(memcached_st *memc) uint32_t flags; memcached_st *clone; unsigned int set= 1; + size_t max_keylen= 0xffff; clone= memcached_clone(NULL, memc); assert(clone); @@ -604,23 +611,25 @@ static test_return bad_key_test(memcached_st *memc) rc= memcached_behavior_set(clone, MEMCACHED_BEHAVIOR_VERIFY_KEY, set); assert(rc == MEMCACHED_SUCCESS); - string= memcached_get(clone, key, strlen(key), - &string_length, &flags, &rc); - assert(rc == MEMCACHED_BAD_KEY_PROVIDED); - assert(string_length == 0); - assert(!string); + /* All keys are valid in the binary protocol (except for length) */ + if (memcached_behavior_get(clone, MEMCACHED_BEHAVIOR_BINARY_PROTOCOL) == 0) + { + string= memcached_get(clone, key, strlen(key), + &string_length, &flags, &rc); + assert(rc == MEMCACHED_BAD_KEY_PROVIDED); + assert(string_length == 0); + assert(!string); - set= 0; - rc= memcached_behavior_set(clone, MEMCACHED_BEHAVIOR_VERIFY_KEY, set); - assert(rc == MEMCACHED_SUCCESS); - string= memcached_get(clone, key, strlen(key), - &string_length, &flags, &rc); - assert(rc == MEMCACHED_NOTFOUND); - assert(string_length == 0); - assert(!string); + set= 0; + rc= memcached_behavior_set(clone, MEMCACHED_BEHAVIOR_VERIFY_KEY, set); + assert(rc == MEMCACHED_SUCCESS); + string= memcached_get(clone, key, strlen(key), + &string_length, &flags, &rc); + assert(rc == MEMCACHED_NOTFOUND); + assert(string_length == 0); + assert(!string); - /* Test multi key for bad keys */ - { + /* Test multi key for bad keys */ char *keys[] = { "GoodKey", "Bad Key", "NotMine" }; size_t key_lengths[] = { 7, 7, 7 }; set= 1; @@ -632,6 +641,34 @@ static test_return bad_key_test(memcached_st *memc) rc= memcached_mget_by_key(clone, "foo daddy", 9, keys, key_lengths, 1); assert(rc == MEMCACHED_BAD_KEY_PROVIDED); + + max_keylen= 250; + + /* The following test should be moved to the end of this function when the + memcached server is updated to allow max size length of the keys in the + binary protocol + */ + rc= memcached_callback_set(clone, MEMCACHED_CALLBACK_PREFIX_KEY, NULL); + assert(rc == MEMCACHED_SUCCESS); + + char *longkey= malloc(max_keylen + 1); + if (longkey != NULL) + { + memset(longkey, 'a', max_keylen + 1); + string= memcached_get(clone, longkey, max_keylen, + &string_length, &flags, &rc); + assert(rc == MEMCACHED_NOTFOUND); + assert(string_length == 0); + assert(!string); + + string= memcached_get(clone, longkey, max_keylen + 1, + &string_length, &flags, &rc); + assert(rc == MEMCACHED_BAD_KEY_PROVIDED); + assert(string_length == 0); + assert(!string); + + free(longkey); + } } /* Make sure zero length keys are marked as bad */ @@ -1238,7 +1275,7 @@ static test_return add_host_test(memcached_st *memc) memcached_return rc; char servername[]= "0.example.com"; - servers= memcached_server_list_append(NULL, servername, 400, &rc); + servers= memcached_server_list_append_with_weight(NULL, servername, 400, 0, &rc); assert(servers); assert(1 == memcached_server_list_count(servers)); @@ -1247,7 +1284,7 @@ static test_return add_host_test(memcached_st *memc) char buffer[SMALL_STRING_LEN]; snprintf(buffer, SMALL_STRING_LEN, "%u.example.com", 400+x); - servers= memcached_server_list_append(servers, buffer, 401, + servers= memcached_server_list_append_with_weight(servers, buffer, 401, 0, &rc); assert(rc == MEMCACHED_SUCCESS); assert(x == memcached_server_list_count(servers)); @@ -1397,6 +1434,7 @@ static test_return user_supplied_bug1(memcached_st *memc) sprintf(key, "%d", x); rc = memcached_set(memc, key, strlen(key), randomstuff, strlen(randomstuff), 10, 0); + assert(rc == MEMCACHED_SUCCESS || rc == MEMCACHED_BUFFERED); /* If we fail, lets try again */ if (rc != MEMCACHED_SUCCESS && rc != MEMCACHED_BUFFERED) rc = memcached_set(memc, key, strlen(key), @@ -1928,7 +1966,11 @@ static test_return user_supplied_bug12(memcached_st *memc) 1, &number_value); assert(value == NULL); - assert(rc == MEMCACHED_NOTFOUND); + /* The binary protocol will set the key if it doesn't exist */ + if (memcached_behavior_get(memc, MEMCACHED_BEHAVIOR_BINARY_PROTOCOL) == 1) + assert(rc == MEMCACHED_SUCCESS); + else + assert(rc == MEMCACHED_NOTFOUND); rc= memcached_set(memc, "autoincrement", strlen("autoincrement"), "1", 1, 0, 0); @@ -2123,6 +2165,121 @@ static test_return user_supplied_bug17(memcached_st *memc) return 0; } +/* + From Andrei on IRC +*/ + +test_return user_supplied_bug19(memcached_st *memc) +{ + memcached_st *m; + memcached_server_st *s; + memcached_return res; + + (void)memc; + + m= memcached_create(NULL); + memcached_server_add_with_weight(m, "localhost", 11311, 100); + memcached_server_add_with_weight(m, "localhost", 11312, 100); + + s= memcached_server_by_key(m, "a", 1, &res); + memcached_server_free(s); + + memcached_free(m); + + return 0; +} + +/* CAS test from Andei */ +test_return user_supplied_bug20(memcached_st *memc) +{ + memcached_return status; + memcached_result_st *result, result_obj; + char *key = "abc"; + size_t key_len = strlen("abc"); + char *value = "foobar"; + size_t value_len = strlen(value); + + memcached_behavior_set(memc, MEMCACHED_BEHAVIOR_SUPPORT_CAS, 1); + + status = memcached_set(memc, key, key_len, value, value_len, (time_t)0, (uint32_t)0); + assert(status == MEMCACHED_SUCCESS); + + status = memcached_mget(memc, &key, &key_len, 1); + assert(status == MEMCACHED_SUCCESS); + + result= memcached_result_create(memc, &result_obj); + assert(result); + + memcached_result_create(memc, &result_obj); + result= memcached_fetch_result(memc, &result_obj, &status); + + assert(result); + assert(status == MEMCACHED_SUCCESS); + + memcached_result_free(result); + + return 0; +} + +#include "ketama_test_cases.h" +test_return user_supplied_bug18(memcached_st *trash) +{ + memcached_return rc; + int value; + int i; + memcached_server_st *server_pool; + memcached_st *memc; + + (void)trash; + + memc= memcached_create(NULL); + assert(memc); + + rc= memcached_behavior_set(memc, MEMCACHED_BEHAVIOR_KETAMA_WEIGHTED, 1); + assert(rc == MEMCACHED_SUCCESS); + + value= memcached_behavior_get(memc, MEMCACHED_BEHAVIOR_KETAMA_WEIGHTED); + assert(value == 1); + + rc= memcached_behavior_set(memc, MEMCACHED_BEHAVIOR_KETAMA_HASH, MEMCACHED_HASH_MD5); + assert(rc == MEMCACHED_SUCCESS); + + value= memcached_behavior_get(memc, MEMCACHED_BEHAVIOR_KETAMA_HASH); + assert(value == MEMCACHED_HASH_MD5); + + server_pool = memcached_servers_parse("10.0.1.1:11211 600,10.0.1.2:11211 300,10.0.1.3:11211 200,10.0.1.4:11211 350,10.0.1.5:11211 1000,10.0.1.6:11211 800,10.0.1.7:11211 950,10.0.1.8:11211 100"); + memcached_server_push(memc, server_pool); + + /* verify that the server list was parsed okay. */ + assert(memc->number_of_hosts == 8); + assert(strcmp(server_pool[0].hostname, "10.0.1.1") == 0); + assert(server_pool[0].port == 11211); + assert(server_pool[0].weight == 600); + assert(strcmp(server_pool[2].hostname, "10.0.1.3") == 0); + assert(server_pool[2].port == 11211); + assert(server_pool[2].weight == 200); + assert(strcmp(server_pool[7].hostname, "10.0.1.8") == 0); + assert(server_pool[7].port == 11211); + assert(server_pool[7].weight == 100); + + /* VDEAAAAA hashes to fffcd1b5, after the last continuum point, and lets + * us test the boundary wraparound. + */ + assert(memcached_generate_hash(memc, (char *)"VDEAAAAA", 8) == memc->continuum[0].index); + + /* verify the standard ketama set. */ + for (i= 0; i < 99; i++) + { + uint32_t server_idx = memcached_generate_hash(memc, test_cases[i].key, strlen(test_cases[i].key)); + char *hostname = memc->hosts[server_idx].hostname; + assert(strcmp(hostname, test_cases[i].server) == 0); + } + + memcached_server_list_free(server_pool); + memcached_free(memc); + + return 0; +} static test_return result_static(memcached_st *memc) { @@ -2130,7 +2287,7 @@ static test_return result_static(memcached_st *memc) memcached_result_st *result_ptr; result_ptr= memcached_result_create(memc, &result); - assert(result.is_allocated == MEMCACHED_NOT_ALLOCATED); + assert(result.is_allocated == false); assert(result_ptr); memcached_result_free(&result); @@ -2154,7 +2311,7 @@ static test_return string_static_null(memcached_st *memc) memcached_string_st *string_ptr; string_ptr= memcached_string_create(memc, &string, 0); - assert(string.is_allocated == MEMCACHED_NOT_ALLOCATED); + assert(string.is_allocated == false); assert(string_ptr); memcached_string_free(&string); @@ -2325,7 +2482,7 @@ static test_return get_read_count(memcached_st *memc) clone= memcached_clone(NULL, memc); assert(clone); - memcached_server_add(clone, "localhost", 6666); + memcached_server_add_with_weight(clone, "localhost", 6666, 0); { char *return_value; @@ -2487,7 +2644,7 @@ static test_return add_host_test1(memcached_st *memc) char servername[]= "0.example.com"; memcached_server_st *servers; - servers= memcached_server_list_append(NULL, servername, 400, &rc); + servers= memcached_server_list_append_with_weight(NULL, servername, 400, 0, &rc); assert(servers); assert(1 == memcached_server_list_count(servers)); @@ -2496,7 +2653,7 @@ static test_return add_host_test1(memcached_st *memc) char buffer[SMALL_STRING_LEN]; snprintf(buffer, SMALL_STRING_LEN, "%u.example.com", 400+x); - servers= memcached_server_list_append(servers, buffer, 401, + servers= memcached_server_list_append_with_weight(servers, buffer, 401, 0, &rc); assert(rc == MEMCACHED_SUCCESS); assert(x == memcached_server_list_count(servers)); @@ -2519,6 +2676,29 @@ static memcached_return pre_nonblock(memcached_st *memc) return MEMCACHED_SUCCESS; } +static memcached_return pre_nonblock_binary(memcached_st *memc) +{ + memcached_return rc= MEMCACHED_FAILURE; + memcached_st *clone; + + clone= memcached_clone(NULL, memc); + assert(clone); + // The memcached_version needs to be done on a clone, because the server + // will not toggle protocol on an connection. + memcached_version(clone); + + if (clone->hosts[0].major_version >= 1 && clone->hosts[0].minor_version > 2) + { + memcached_behavior_set(memc, MEMCACHED_BEHAVIOR_NO_BLOCK, 0); + rc = memcached_behavior_set(memc, MEMCACHED_BEHAVIOR_BINARY_PROTOCOL, 1); + assert(rc == MEMCACHED_SUCCESS); + assert(memcached_behavior_get(memc, MEMCACHED_BEHAVIOR_BINARY_PROTOCOL) == 1); + } + + memcached_free(clone); + return rc; +} + static memcached_return pre_murmur(memcached_st *memc) { memcached_behavior_set(memc, MEMCACHED_BEHAVIOR_HASH, (uint64_t)MEMCACHED_HASH_MURMUR); @@ -2653,7 +2833,7 @@ static void *my_realloc(memcached_st *ptr __attribute__((unused)), void *mem, co return realloc(mem, size); } -static memcached_return set_prefix(memcached_st *memc) +static memcached_return set_prefix(memcached_st *memc) { memcached_return rc; const char *key= "mine"; @@ -2697,9 +2877,11 @@ static memcached_return set_prefix(memcached_st *memc) assert(value == NULL); /* Test a long key for failure */ + /* TODO, extend test to determine based on setting, what result should be */ long_key= "Thisismorethentheallottednumberofcharacters"; rc= memcached_callback_set(memc, MEMCACHED_CALLBACK_PREFIX_KEY, long_key); - assert(rc == MEMCACHED_BAD_KEY_PROVIDED); + //assert(rc == MEMCACHED_BAD_KEY_PROVIDED); + assert(rc == MEMCACHED_SUCCESS); /* Now test a key with spaces (which will fail from long key, since bad key is not set) */ long_key= "This is more then the allotted number of characters"; @@ -2813,7 +2995,7 @@ static memcached_return pre_unix_socket(memcached_st *memc) if (stat("/tmp/memcached.socket", &buf)) return MEMCACHED_FAILURE; - rc= memcached_server_add_unix_socket(memc, "/tmp/memcached.socket"); + rc= memcached_server_add_unix_socket_with_weight(memc, "/tmp/memcached.socket", 0); return rc; } @@ -2849,6 +3031,44 @@ static memcached_return poll_timeout(memcached_st *memc) return MEMCACHED_SUCCESS; } +static memcached_return noreply_test(memcached_st *memc) +{ + memcached_return ret; + ret= memcached_behavior_set(memc, MEMCACHED_BEHAVIOR_NOREPLY, 1); + assert(ret == MEMCACHED_SUCCESS); + ret= memcached_behavior_set(memc, MEMCACHED_BEHAVIOR_BUFFER_REQUESTS, 1); + assert(ret == MEMCACHED_SUCCESS); + assert(memcached_behavior_get(memc, MEMCACHED_BEHAVIOR_NOREPLY) == 1); + assert(memcached_behavior_get(memc, MEMCACHED_BEHAVIOR_BUFFER_REQUESTS) == 1); + + for (int x= 0; x < 100; ++x) { + char key[10]; + size_t len= sprintf(key, "%d", x); + ret= memcached_set(memc, key, len, key, len, 0, 0); + assert(ret == MEMCACHED_SUCCESS || ret == MEMCACHED_BUFFERED); + } + + /* + ** NOTE: Don't ever do this in your code! this is not a supported use of the + ** API and is _ONLY_ done this way to verify that the library works the + ** way it is supposed to do!!!! + */ + int no_msg= 0; + for (int x= 0; x < memc->number_of_hosts; ++x) { + no_msg+= memc->hosts[x].cursor_active; + } + + /* + ** The binary protocol does not implement quiet commands yet. Fix this + ** test they are implemented! + */ + if (memcached_behavior_get(memc, MEMCACHED_BEHAVIOR_BINARY_PROTOCOL) == 1) + assert(no_msg == 100); + else + assert(no_msg == 0); + + return MEMCACHED_SUCCESS; +} /* Clean the server before beginning testing */ test_st tests[] ={ @@ -2890,6 +3110,7 @@ test_st tests[] ={ {"memcached_server_cursor", 1, memcached_server_cursor_test }, {"read_through", 1, read_through }, {"delete_through", 1, delete_through }, + {"noreply", 1, noreply_test}, {0, 0, 0} }; @@ -2940,7 +3161,19 @@ test_st user_tests[] ={ {"user_supplied_bug14", 1, user_supplied_bug14 }, {"user_supplied_bug15", 1, user_supplied_bug15 }, {"user_supplied_bug16", 1, user_supplied_bug16 }, +#ifndef __sun + /* + ** It seems to be something weird with the character sets.. + ** value_fetch is unable to parse the value line (iscntrl "fails"), so I + ** guess I need to find out how this is supposed to work.. Perhaps I need + ** to run the test in a specific locale (I tried zh_CN.UTF-8 without success, + ** so just disable the code for now...). + */ {"user_supplied_bug17", 1, user_supplied_bug17 }, +#endif + {"user_supplied_bug18", 1, user_supplied_bug18 }, + {"user_supplied_bug19", 1, user_supplied_bug19 }, + {"user_supplied_bug20", 1, user_supplied_bug20 }, {0, 0, 0} }; @@ -3005,6 +3238,7 @@ collection_st collection[] ={ {"string", 0, 0, string_tests}, {"result", 0, 0, result_tests}, {"async", pre_nonblock, 0, async_tests}, + {"async_binary", pre_nonblock_binary, 0, async_tests}, {"user", 0, 0, user_tests}, {"generate", 0, 0, generate_tests}, {"generate_hsieh", pre_hsieh, 0, generate_tests},