0f90a008f17cce081aea94ec1c6bf47f77697f60
1 #include "test/lib/common.hpp"
5 #include "libmemcachedutil-1.0/pool.h"
9 struct test_pool_context_st
{
10 volatile memcached_return_t rc
;
11 memcached_pool_st
*pool
;
15 test_pool_context_st(memcached_pool_st
*pool_arg
, memcached_st
*memc_arg
) :
16 rc(MEMCACHED_FAILURE
),
19 if (sem_init(&_lock
, 0, 0)) {
32 ~test_pool_context_st() {
37 static void *connection_release(void *arg
) {
38 assert(arg
!= nullptr);
40 this_thread::sleep_for(200ms
);
41 auto res
= static_cast<test_pool_context_st
*>(arg
);
42 res
->rc
= memcached_pool_release(res
->pool
, res
->memc
);
48 TEST_CASE("memcached_util_pool_thread") {
50 auto pool
= memcached_pool_create(*memc
, 1, 1);
53 memcached_return_t rc
;
54 auto pool_memc
= memcached_pool_fetch(pool
, nullptr, &rc
);
55 REQUIRE(MEMCACHED_SUCCESS
== rc
);
59 @note This comment was written to describe what was believed to be the original authors intent.
61 This portion of the test creates a thread that will wait until told to free a memcached_st
62 that will be grabbed by the main thread.
64 It is believed that this tests whether or not we are handling ownership correctly.
67 test_pool_context_st
item(pool
, pool_memc
);
69 REQUIRE(0 == pthread_create(&tid
, nullptr, connection_release
, &item
));
72 memcached_st
*pop_memc
;
74 struct timespec relative_time
= {0, 0};
75 pop_memc
= memcached_pool_fetch(pool
, &relative_time
, &rc
);
77 if (memcached_success(rc
)) {
81 if (memcached_failed(rc
)) {
82 REQUIRE_FALSE(pop_memc
);
83 REQUIRE(rc
!= MEMCACHED_TIMEOUT
); // As long as relative_time is zero, MEMCACHED_TIMEOUT is invalid
85 } while (rc
== MEMCACHED_NOTFOUND
);
87 // Cleanup thread since we will exit once we test.
88 REQUIRE(0 == pthread_join(tid
, nullptr));
89 REQUIRE(MEMCACHED_SUCCESS
== rc
);
90 REQUIRE(pool_memc
== pop_memc
);
91 REQUIRE(MEMCACHED_SUCCESS
== memcached_pool_release(pool
, pop_memc
));
92 REQUIRE(memcached_pool_destroy(pool
) == *memc
);
96 #endif // HAVE_SEMAPHORE_H