update TODO
[awesomized/libmemcached] / test / tests / memcached / util_pool_thread.cpp
1 #include "test/lib/common.hpp"
2
3 #include "libmemcachedutil-1.0/pool.h"
4 #include <cassert>
5
6 struct test_pool_context_st {
7 volatile memcached_return_t rc;
8 memcached_pool_st *pool;
9 memcached_st *memc;
10
11 test_pool_context_st(memcached_pool_st *pool_arg, memcached_st *memc_arg)
12 : rc(MEMCACHED_FAILURE)
13 , pool(pool_arg)
14 , memc(memc_arg)
15 {
16 }
17 };
18
19 static void *connection_release(void *arg) {
20 assert(arg != nullptr);
21
22 this_thread::sleep_for(200ms);
23 auto res = static_cast<test_pool_context_st *>(arg);
24 res->rc = memcached_pool_release(res->pool, res->memc);
25
26 pthread_exit(arg);
27 }
28
29 TEST_CASE("memcached_util_pool_thread") {
30 MemcachedPtr memc;
31 auto pool = memcached_pool_create(*memc, 1, 1);
32 REQUIRE(pool);
33
34 memcached_return_t rc;
35 auto pool_memc = memcached_pool_fetch(pool, nullptr, &rc);
36 REQUIRE(MEMCACHED_SUCCESS == rc);
37 REQUIRE(pool_memc);
38
39 /*
40 @note This comment was written to describe what was believed to be the original authors intent.
41
42 This portion of the test creates a thread that will wait until told to free a memcached_st
43 that will be grabbed by the main thread.
44
45 It is believed that this tests whether or not we are handling ownership correctly.
46 */
47 pthread_t tid;
48 test_pool_context_st item(pool, pool_memc);
49
50 REQUIRE(0 == pthread_create(&tid, nullptr, connection_release, &item));
51
52 memcached_st *pop_memc;
53 do {
54 struct timespec relative_time = {0, 0};
55 pop_memc = memcached_pool_fetch(pool, &relative_time, &rc);
56
57 if (memcached_success(rc)) {
58 break;
59 }
60
61 if (memcached_failed(rc)) {
62 REQUIRE_FALSE(pop_memc);
63 REQUIRE(rc != MEMCACHED_TIMEOUT); // As long as relative_time is zero, MEMCACHED_TIMEOUT is invalid
64 }
65 } while (rc == MEMCACHED_NOTFOUND);
66
67 // Cleanup thread since we will exit once we test.
68 REQUIRE(0 == pthread_join(tid, nullptr));
69 REQUIRE(MEMCACHED_SUCCESS == rc);
70 REQUIRE(pool_memc == pop_memc);
71 REQUIRE(MEMCACHED_SUCCESS == memcached_pool_release(pool, pop_memc));
72 REQUIRE(memcached_pool_destroy(pool) == *memc);
73
74 }