1 #include "test/lib/common.hpp"
3 #include "libmemcachedutil-1.0/pool.h"
6 struct test_pool_context_st
{
7 volatile memcached_return_t rc
;
8 memcached_pool_st
*pool
;
11 test_pool_context_st(memcached_pool_st
*pool_arg
, memcached_st
*memc_arg
)
12 : rc(MEMCACHED_FAILURE
)
19 static void *connection_release(void *arg
) {
20 assert(arg
!= nullptr);
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
);
29 TEST_CASE("memcached_util_pool_thread") {
31 auto pool
= memcached_pool_create(*memc
, 1, 1);
34 memcached_return_t rc
;
35 auto pool_memc
= memcached_pool_fetch(pool
, nullptr, &rc
);
36 REQUIRE(MEMCACHED_SUCCESS
== rc
);
40 @note This comment was written to describe what was believed to be the original authors intent.
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.
45 It is believed that this tests whether or not we are handling ownership correctly.
48 test_pool_context_st
item(pool
, pool_memc
);
50 REQUIRE(0 == pthread_create(&tid
, nullptr, connection_release
, &item
));
52 memcached_st
*pop_memc
;
54 struct timespec relative_time
= {0, 0};
55 pop_memc
= memcached_pool_fetch(pool
, &relative_time
, &rc
);
57 if (memcached_success(rc
)) {
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
65 } while (rc
== MEMCACHED_NOTFOUND
);
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
);