X-Git-Url: https://git.m6w6.name/?a=blobdiff_plain;f=tests%2Fmem_functions.cc;h=cca23464e01ac16d9b63aa21e2c5cdef6ca3395d;hb=a60c98bb02391f995a03c3db5898e146ff2f6f3b;hp=1c5f7d4f6bc7038c95341f0f879d2fe368b1ab1c;hpb=98ae4d13d9d40e111791be650681ff08f10e61d5;p=awesomized%2Flibmemcached diff --git a/tests/mem_functions.cc b/tests/mem_functions.cc index 1c5f7d4f..cca23464 100644 --- a/tests/mem_functions.cc +++ b/tests/mem_functions.cc @@ -52,6 +52,7 @@ #include #include #include +#include #include #include #include @@ -824,7 +825,6 @@ static test_return_t bad_key_test(memcached_st *memc) const char *key= "foo bad"; uint32_t flags; memcached_st *memc_clone; - size_t max_keylen= 0xffff; uint64_t query_id= memcached_query_id(memc); @@ -886,8 +886,6 @@ static test_return_t bad_key_test(memcached_st *memc) memcached_mget_by_key(memc_clone, "foo daddy", 9, keys, key_lengths, 1)); test_compare(query_id +1, memcached_query_id(memc_clone)); - 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 @@ -896,15 +894,16 @@ static test_return_t bad_key_test(memcached_st *memc) memcached_callback_set(memc_clone, MEMCACHED_CALLBACK_NAMESPACE, NULL)); std::vector longkey; - longkey.insert(longkey.end(), max_keylen +1, 'a'); - test_compare(longkey.size(), max_keylen +1); + longkey.insert(longkey.end(), MEMCACHED_MAX_KEY, 'a'); + test_compare(longkey.size(), size_t(MEMCACHED_MAX_KEY)); { size_t string_length; - test_null(memcached_get(memc_clone, &longkey[0], max_keylen, &string_length, &flags, &rc)); + // We subtract 1 + test_null(memcached_get(memc_clone, &longkey[0], longkey.size() -1, &string_length, &flags, &rc)); test_compare(MEMCACHED_NOTFOUND, rc); test_zero(string_length); - test_null(memcached_get(memc_clone, &longkey[0], max_keylen +1, &string_length, &flags, &rc)); + test_null(memcached_get(memc_clone, &longkey[0], longkey.size(), &string_length, &flags, &rc)); test_compare(MEMCACHED_BAD_KEY_PROVIDED, rc); test_zero(string_length); } @@ -4159,10 +4158,9 @@ static test_return_t noreply_test(memcached_st *memc) static test_return_t analyzer_test(memcached_st *memc) { memcached_return_t rc; - memcached_stat_st *memc_stat; memcached_analysis_st *report; - memc_stat= memcached_stat(memc, NULL, &rc); + memcached_stat_st *memc_stat= memcached_stat(memc, NULL, &rc); test_compare(MEMCACHED_SUCCESS, rc); test_true(memc_stat); @@ -4194,68 +4192,85 @@ static test_return_t dump_test(memcached_st *memc) return TEST_SUCCESS; } -struct test_pool_context_st { - memcached_pool_st* pool; - memcached_st* mmc; -}; - -static void* connection_release(void *arg) -{ - test_pool_context_st *resource= static_cast(arg); - - usleep(250); - // Release all of the memc we are holding - assert(memcached_success(memcached_pool_push(resource->pool, resource->mmc))); - return arg; -} #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 != NULL); + test_true(pool); memcached_st *mmc[POOL_SIZE]; - memcached_return_t rc; // Fill up our array that we will store the memc that are in the pool for (size_t x= 0; x < POOL_SIZE; ++x) { - mmc[x]= memcached_pool_pop(pool, false, &rc); - test_true(mmc[x] != NULL); + 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 - test_true(memcached_pool_pop(pool, false, &rc) == NULL); - test_compare(MEMCACHED_SUCCESS, rc); + { + memcached_return_t rc; + test_null(memcached_pool_fetch(pool, NULL, &rc)); + test_compare(MEMCACHED_NOTFOUND, rc); + } - pthread_t tid; - test_pool_context_st item= { pool, mmc[9] }; + // 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); - pthread_create(&tid, NULL, connection_release, &item); - mmc[9]= memcached_pool_pop(pool, true, &rc); - test_compare(MEMCACHED_SUCCESS, rc); - pthread_join(tid, NULL); - test_true(mmc[9]); - const char *key= "key"; - size_t keylen= strlen(key); + 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], key, keylen, "0", 1, 0, 0)); + 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], key, keylen, 1, &number_value)); + 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_push(pool, mmc[x])); + test_compare(MEMCACHED_SUCCESS, memcached_pool_release(pool, mmc[x])); } @@ -4263,22 +4278,137 @@ static test_return_t connection_pool_test(memcached_st *memc) * of the connections in the pool. It should however be enabled * when I push the item into the pool */ - mmc[0]= memcached_pool_pop(pool, false, &rc); + mmc[0]= memcached_pool_fetch(pool, NULL, NULL); test_true(mmc[0]); - rc= memcached_pool_behavior_set(pool, MEMCACHED_BEHAVIOR_IO_MSG_WATERMARK, 9999); - test_compare(MEMCACHED_SUCCESS, rc); + test_compare(MEMCACHED_SUCCESS, + memcached_pool_behavior_set(pool, MEMCACHED_BEHAVIOR_IO_MSG_WATERMARK, 9999)); - mmc[1]= memcached_pool_pop(pool, false, &rc); - test_true(mmc[1]); + { + 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_push(pool, mmc[1])); - test_compare(MEMCACHED_SUCCESS, memcached_pool_push(pool, mmc[0])); + 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); + } - mmc[0]= memcached_pool_pop(pool, false, &rc); test_compare(UINT64_C(9999), memcached_behavior_get(mmc[0], MEMCACHED_BEHAVIOR_IO_MSG_WATERMARK)); - test_compare(MEMCACHED_SUCCESS, memcached_pool_push(pool, mmc[0])); + 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) +{ + 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; + int counter= 5; + do + { + struct timespec relative_time= { 0, 0 }; + pop_memc= memcached_pool_fetch(pool, &relative_time, &rc); + + if (memcached_failed(rc)) + { + test_null(pop_memc); + } + } while (rc == MEMCACHED_TIMEOUT and --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); @@ -4428,13 +4558,9 @@ static test_return_t hsieh_avaibility_test (memcached_st *memc) { test_skip(true, libhashkit_has_algorithm(HASHKIT_HASH_HSIEH)); - memcached_return_t expected_rc= MEMCACHED_INVALID_ARGUMENTS; -#ifdef HAVE_HSIEH_HASH - expected_rc= MEMCACHED_SUCCESS; -#endif - memcached_return_t rc= memcached_behavior_set(memc, MEMCACHED_BEHAVIOR_HASH, - (uint64_t)MEMCACHED_HASH_HSIEH); - test_compare(expected_rc, rc); + test_compare(MEMCACHED_SUCCESS, + memcached_behavior_set(memc, MEMCACHED_BEHAVIOR_HASH, + (uint64_t)MEMCACHED_HASH_HSIEH)); return TEST_SUCCESS; } @@ -4443,13 +4569,8 @@ static test_return_t murmur_avaibility_test (memcached_st *memc) { test_skip(true, libhashkit_has_algorithm(HASHKIT_HASH_MURMUR)); - memcached_return_t expected_rc= MEMCACHED_INVALID_ARGUMENTS; -#ifdef HAVE_MURMUR_HASH - expected_rc= MEMCACHED_SUCCESS; -#endif - memcached_return_t rc= memcached_behavior_set(memc, MEMCACHED_BEHAVIOR_HASH, - (uint64_t)MEMCACHED_HASH_MURMUR); - test_compare(expected_rc, rc); + test_compare(MEMCACHED_SUCCESS, + memcached_behavior_set(memc, MEMCACHED_BEHAVIOR_HASH, (uint64_t)MEMCACHED_HASH_MURMUR)); return TEST_SUCCESS; } @@ -5836,7 +5957,9 @@ test_st tests[] ={ {"delete_through", true, (test_callback_fn*)delete_through }, {"noreply", true, (test_callback_fn*)noreply_test}, {"analyzer", true, (test_callback_fn*)analyzer_test}, - {"connectionpool", true, (test_callback_fn*)connection_pool_test }, + {"memcached_pool_st", true, (test_callback_fn*)connection_pool_test }, + {"memcached_pool_st #2", true, (test_callback_fn*)connection_pool2_test }, + {"memcached_pool_st #3", true, (test_callback_fn*)connection_pool3_test }, {"memcached_pool_test", true, (test_callback_fn*)memcached_pool_test }, {"test_get_last_disconnect", true, (test_callback_fn*)test_get_last_disconnect}, {"verbosity", true, (test_callback_fn*)test_verbosity},