X-Git-Url: https://git.m6w6.name/?a=blobdiff_plain;f=tests%2Fmem_functions.cc;h=d6aaa7cbf6451c7904386125ae8a0fe531cc3aa6;hb=9428b4c34783838b84181abb0a5335ccd9db72ed;hp=b9acb87ef882a7c03504765aeb4921be715bf216;hpb=bd53173d0a23c8c2a0dac68056cbd2cc52d5a6ef;p=awesomized%2Flibmemcached diff --git a/tests/mem_functions.cc b/tests/mem_functions.cc index b9acb87e..d6aaa7cb 100644 --- a/tests/mem_functions.cc +++ b/tests/mem_functions.cc @@ -910,6 +910,7 @@ static test_return_t bad_key_test(memcached_st *memc) memcached_callback_set(memc_clone, MEMCACHED_CALLBACK_NAMESPACE, NULL)); std::vector longkey; + longkey.reserve(MEMCACHED_MAX_KEY); longkey.insert(longkey.end(), MEMCACHED_MAX_KEY, 'a'); test_compare(longkey.size(), size_t(MEMCACHED_MAX_KEY)); { @@ -1054,15 +1055,12 @@ static test_return_t get_test2(memcached_st *memc) static test_return_t set_test2(memcached_st *memc) { - const char *key= "foo"; - const char *value= "train in the brain"; - size_t value_length= strlen(value); - for (uint32_t x= 0; x < 10; x++) { - memcached_return_t rc= memcached_set(memc, key, strlen(key), - value, value_length, - (time_t)0, (uint32_t)0); + memcached_return_t rc= memcached_set(memc, + test_literal_param("foo"), + test_literal_param("train in the brain"), + time_t(0), uint32_t(0)); test_true(rc == MEMCACHED_SUCCESS or rc == MEMCACHED_BUFFERED); } @@ -1073,12 +1071,11 @@ static test_return_t set_test3(memcached_st *memc) { size_t value_length= 8191; - char *value= (char*)malloc(value_length); - test_true(value); - + std::vector value; + value.reserve(value_length); for (uint32_t x= 0; x < value_length; x++) { - value[x] = (char) (x % 127); + value.push_back(char(x % 127)); } /* The dump test relies on there being at least 32 items in memcached */ @@ -1090,14 +1087,12 @@ static test_return_t set_test3(memcached_st *memc) uint64_t query_id= memcached_query_id(memc); memcached_return_t rc= memcached_set(memc, key, strlen(key), - value, value_length, + &value[0], value.size(), (time_t)0, (uint32_t)0); test_true_got(rc == MEMCACHED_SUCCESS or rc == MEMCACHED_BUFFERED, memcached_strerror(NULL, rc)); test_compare(query_id +1, memcached_query_id(memc)); } - free(value); - return TEST_SUCCESS; } @@ -1106,17 +1101,16 @@ static test_return_t get_test3(memcached_st *memc) const char *key= "foo"; size_t value_length= 8191; - char *value= (char*)malloc(value_length); - test_true(value); - + std::vector value; + value.reserve(value_length); for (uint32_t x= 0; x < value_length; x++) { - value[x] = (char) (x % 127); + value.push_back(char(x % 127)); } memcached_return_t rc; rc= memcached_set(memc, key, strlen(key), - value, value_length, + &value[0], value.size(), (time_t)0, (uint32_t)0); test_true(rc == MEMCACHED_SUCCESS or rc == MEMCACHED_BUFFERED); @@ -1128,10 +1122,9 @@ static test_return_t get_test3(memcached_st *memc) test_compare(MEMCACHED_SUCCESS, rc); test_true(string); test_compare(string_length, value_length); - test_memcmp(string, value, string_length); + test_memcmp(string, &value[0], string_length); free(string); - free(value); return TEST_SUCCESS; } @@ -1141,16 +1134,15 @@ static test_return_t get_test4(memcached_st *memc) const char *key= "foo"; size_t value_length= 8191; - char *value= (char*)malloc(value_length); - test_true(value); - + std::vector value; + value.reserve(value_length); for (uint32_t x= 0; x < value_length; x++) { - value[x] = (char) (x % 127); + value.push_back(char(x % 127)); } memcached_return_t rc= memcached_set(memc, key, strlen(key), - value, value_length, + &value[0], value.size(), (time_t)0, (uint32_t)0); test_true(rc == MEMCACHED_SUCCESS or rc == MEMCACHED_BUFFERED); @@ -1164,12 +1156,10 @@ static test_return_t get_test4(memcached_st *memc) test_compare(MEMCACHED_SUCCESS, rc); test_true(string); test_compare(string_length, value_length); - test_memcmp(string, value, string_length); + test_memcmp(string, &value[0], string_length); free(string); } - free(value); - return TEST_SUCCESS; } @@ -2275,8 +2265,9 @@ static test_return_t user_supplied_bug2(memcached_st *memc) /* Do a large mget() over all the keys we think exist */ static test_return_t user_supplied_bug3(memcached_st *memc) { - test_compare(true, memcached_behavior_set(memc, MEMCACHED_BEHAVIOR_NO_BLOCK, 1)); - test_compare(true, memcached_behavior_set(memc, MEMCACHED_BEHAVIOR_TCP_NODELAY, 1)); + test_compare(MEMCACHED_SUCCESS, memcached_behavior_set(memc, MEMCACHED_BEHAVIOR_NO_BLOCK, 1)); + test_compare(MEMCACHED_SUCCESS, memcached_behavior_set(memc, MEMCACHED_BEHAVIOR_TCP_NODELAY, 1)); + #ifdef NOT_YET setter = 20 * 1024576; memcached_behavior_set(memc, MEMCACHED_BEHAVIOR_SOCKET_SEND_SIZE, setter); @@ -2623,18 +2614,19 @@ static test_return_t user_supplied_bug10(memcached_st *memc) memcached_behavior_set(mclone, MEMCACHED_BEHAVIOR_TCP_NODELAY, set); memcached_behavior_set(mclone, MEMCACHED_BEHAVIOR_POLL_TIMEOUT, uint64_t(0)); - char *value= (char*)malloc(value_length * sizeof(char)); - - for (unsigned int x= 0; x < value_length; x++) + std::vector value; + value.reserve(value_length); + for (uint32_t x= 0; x < value_length; x++) { - value[x]= (char) (x % 127); + value.push_back(char(x % 127)); } for (unsigned int x= 1; x <= 100000; ++x) { memcached_return_t rc= memcached_set(mclone, test_literal_param("foo"), - value, value_length, 0, 0); + &value[0], value.size(), + 0, 0); test_true_got((rc == MEMCACHED_SUCCESS or rc == MEMCACHED_WRITE_FAILURE or rc == MEMCACHED_BUFFERED or rc == MEMCACHED_TIMEOUT or rc == MEMCACHED_CONNECTION_FAILURE or rc == MEMCACHED_SERVER_TEMPORARILY_DISABLED), @@ -2646,7 +2638,6 @@ static test_return_t user_supplied_bug10(memcached_st *memc) } } - free(value); memcached_free(mclone); return TEST_SUCCESS; @@ -2657,35 +2648,28 @@ static test_return_t user_supplied_bug10(memcached_st *memc) */ static test_return_t user_supplied_bug11(memcached_st *memc) { - const char *key= "foo"; - size_t value_length= 512; - size_t key_len= 3; - unsigned int set= 1; memcached_st *mclone= memcached_clone(NULL, memc); - memcached_behavior_set(mclone, MEMCACHED_BEHAVIOR_NO_BLOCK, set); - memcached_behavior_set(mclone, MEMCACHED_BEHAVIOR_TCP_NODELAY, set); - int32_t timeout= -1; - memcached_behavior_set(mclone, MEMCACHED_BEHAVIOR_POLL_TIMEOUT, (size_t)timeout); - - timeout= (int32_t)memcached_behavior_get(mclone, MEMCACHED_BEHAVIOR_POLL_TIMEOUT); + memcached_behavior_set(mclone, MEMCACHED_BEHAVIOR_NO_BLOCK, true); + memcached_behavior_set(mclone, MEMCACHED_BEHAVIOR_TCP_NODELAY, true); + memcached_behavior_set(mclone, MEMCACHED_BEHAVIOR_POLL_TIMEOUT, size_t(-1)); - test_true(timeout == -1); + test_compare(-1, int32_t(memcached_behavior_get(mclone, MEMCACHED_BEHAVIOR_POLL_TIMEOUT))); - char *value= (char*)malloc(value_length * sizeof(char)); - for (unsigned int x= 0; x < value_length; x++) + std::vector value; + value.reserve(512); + for (unsigned int x= 0; x < 512; x++) { - value[x]= (char) (x % 127); + value.push_back(char(x % 127)); } for (unsigned int x= 1; x <= 100000; ++x) { - memcached_return_t rc= memcached_set(mclone, key, key_len,value, value_length, 0, 0); + memcached_return_t rc= memcached_set(mclone, test_literal_param("foo"), &value[0], value.size(), 0, 0); (void)rc; } - free(value); memcached_free(mclone); return TEST_SUCCESS; @@ -2709,7 +2693,6 @@ static test_return_t user_supplied_bug12(memcached_st *memc) rc= memcached_increment(memc, "autoincrement", strlen("autoincrement"), 1, &number_value); - test_true(value == NULL); /* The binary protocol will set the key if it doesn't exist */ if (memcached_behavior_get(memc, MEMCACHED_BEHAVIOR_BINARY_PROTOCOL) == 1) @@ -2783,6 +2766,7 @@ static test_return_t user_supplied_bug14(memcached_st *memc) memcached_behavior_set(memc, MEMCACHED_BEHAVIOR_TCP_NODELAY, true); std::vector value; + value.reserve(18000); for (size_t x= 0; x < 18000; x++) { value.push_back((char) (x % 127)); @@ -4162,240 +4146,6 @@ static test_return_t dump_test(memcached_st *memc) return TEST_SUCCESS; } - -#define POOL_SIZE 10 -static test_return_t connection_pool_test(memcached_st *memc) -{ - memcached_pool_st* pool= memcached_pool_create(memc, 5, POOL_SIZE); - test_true(pool); - memcached_st *mmc[POOL_SIZE]; - - // Fill up our array that we will store the memc that are in the pool - for (size_t x= 0; x < POOL_SIZE; ++x) - { - memcached_return_t rc; - mmc[x]= memcached_pool_fetch(pool, NULL, &rc); - test_compare(MEMCACHED_SUCCESS, rc); - test_true(mmc[x]); - } - - // All memc should be gone - { - memcached_return_t rc; - test_null(memcached_pool_fetch(pool, NULL, &rc)); - test_compare(MEMCACHED_NOTFOUND, rc); - } - - // Release them.. - for (size_t x= 0; x < POOL_SIZE; ++x) - { - if (mmc[x]) - { - test_compare(MEMCACHED_SUCCESS, memcached_pool_release(pool, mmc[x])); - } - } - test_true(memcached_pool_destroy(pool) == memc); - - return TEST_SUCCESS; -} - -static test_return_t connection_pool2_test(memcached_st *memc) -{ - memcached_pool_st* pool= memcached_pool_create(memc, 5, POOL_SIZE); - test_true(pool); - memcached_st *mmc[POOL_SIZE]; - - // Fill up our array that we will store the memc that are in the pool - for (size_t x= 0; x < POOL_SIZE; ++x) - { - memcached_return_t rc; - mmc[x]= memcached_pool_fetch(pool, NULL, &rc); - test_compare(MEMCACHED_SUCCESS, rc); - test_true(mmc[x]); - } - - // All memc should be gone - { - memcached_return_t rc; - test_null(memcached_pool_fetch(pool, NULL, &rc)); - test_compare(MEMCACHED_NOTFOUND, rc); - } - - // verify that I can do ops with all connections - test_compare(MEMCACHED_SUCCESS, - memcached_set(mmc[0], - test_literal_param("key"), - "0", 1, 0, 0)); - - for (uint64_t x= 0; x < POOL_SIZE; ++x) - { - uint64_t number_value; - test_compare(MEMCACHED_SUCCESS, - memcached_increment(mmc[x], - test_literal_param("key"), - 1, &number_value)); - test_compare(number_value, (x+1)); - } - - // Release them.. - for (size_t x= 0; x < POOL_SIZE; ++x) - { - test_compare(MEMCACHED_SUCCESS, memcached_pool_release(pool, mmc[x])); - } - - - /* verify that I can set behaviors on the pool when I don't have all - * of the connections in the pool. It should however be enabled - * when I push the item into the pool - */ - mmc[0]= memcached_pool_fetch(pool, NULL, NULL); - test_true(mmc[0]); - - test_compare(MEMCACHED_SUCCESS, - memcached_pool_behavior_set(pool, MEMCACHED_BEHAVIOR_IO_MSG_WATERMARK, 9999)); - - { - memcached_return_t rc; - mmc[1]= memcached_pool_fetch(pool, NULL, &rc); - test_true(mmc[1]); - test_compare(MEMCACHED_SUCCESS, rc); - } - - test_compare(UINT64_C(9999), memcached_behavior_get(mmc[1], MEMCACHED_BEHAVIOR_IO_MSG_WATERMARK)); - test_compare(MEMCACHED_SUCCESS, memcached_pool_release(pool, mmc[1])); - test_compare(MEMCACHED_SUCCESS, memcached_pool_release(pool, mmc[0])); - - { - memcached_return_t rc; - mmc[0]= memcached_pool_fetch(pool, NULL, &rc); - test_true(mmc[0]); - test_compare(MEMCACHED_SUCCESS, rc); - } - - test_compare(UINT64_C(9999), memcached_behavior_get(mmc[0], MEMCACHED_BEHAVIOR_IO_MSG_WATERMARK)); - test_compare(MEMCACHED_SUCCESS, memcached_pool_release(pool, mmc[0])); - - test_true(memcached_pool_destroy(pool) == memc); - - return TEST_SUCCESS; -} - -struct test_pool_context_st { - volatile memcached_return_t rc; - memcached_pool_st* pool; - memcached_st* mmc; - sem_t _lock; - - test_pool_context_st(memcached_pool_st *pool_arg, memcached_st *memc_arg): - rc(MEMCACHED_FAILURE), - pool(pool_arg), - mmc(memc_arg) - { - sem_init(&_lock, 0, 0); - } - - void wait() - { - sem_wait(&_lock); - } - - void release() - { - sem_post(&_lock); - } - - ~test_pool_context_st() - { - sem_destroy(&_lock); - } -}; - -static void* connection_release(void *arg) -{ - test_pool_context_st *resource= static_cast(arg); - assert(resource); - if (resource == NULL) - { - abort(); - } - - // Release all of the memc we are holding - resource->rc= memcached_pool_release(resource->pool, resource->mmc); - resource->release(); - - pthread_exit(arg); -} - -static test_return_t connection_pool3_test(memcached_st *memc) -{ -#ifdef __APPLE__ - return TEST_SKIPPED; -#endif - - memcached_pool_st* pool= memcached_pool_create(memc, 1, 1); - test_true(pool); - - memcached_st *pool_memc; - { - memcached_return_t rc; - pool_memc= memcached_pool_fetch(pool, NULL, &rc); - test_compare(MEMCACHED_SUCCESS, rc); - test_true(pool_memc); - } - - /* - @note This comment was written to describe what was believed to be the original authors intent. - - This portion of the test creates a thread that will wait until told to free a memcached_st - that will be grabbed by the main thread. - - It is believed that this tests whether or not we are handling ownership correctly. - */ - pthread_t tid; - test_pool_context_st item(pool, pool_memc); - - test_zero(pthread_create(&tid, NULL, connection_release, &item)); - item.wait(); - - memcached_return_t rc; - memcached_st *pop_memc; - // We do a hard loop, and try N times - int counter= 5; - do - { - struct timespec relative_time= { 0, 0 }; - pop_memc= memcached_pool_fetch(pool, &relative_time, &rc); - - if (memcached_success(rc)) - { - break; - } - - if (memcached_failed(rc)) - { - test_null(pop_memc); - test_true(rc != MEMCACHED_TIMEOUT); // As long as relative_time is zero, MEMCACHED_TIMEOUT is invalid - } - } while (--counter); - - if (memcached_failed(rc)) // Cleanup thread since we will exit once we test. - { - pthread_join(tid, NULL); - test_compare(MEMCACHED_SUCCESS, rc); - } - - { - int pthread_ret= pthread_join(tid, NULL); - test_true(pthread_ret == 0 or pthread_ret == ESRCH); - } - test_compare(MEMCACHED_SUCCESS, rc); - test_true(pool_memc == pop_memc); - - test_true(memcached_pool_destroy(pool) == memc); - - return TEST_SUCCESS; -} - static test_return_t util_version_test(memcached_st *memc) { test_compare_hint(MEMCACHED_SUCCESS, memcached_version(memc), memcached_last_error_message(memc));