88330284f2919716e2af7b7641c741430fca68b5
1 #include "test/lib/common.hpp"
3 #include "libmemcachedutil-1.0/pool.h"
12 struct test_pool_context_st
{
13 volatile memcached_return_t rc
;
14 memcached_pool_st
*pool
;
18 test_pool_context_st(memcached_pool_st
*pool_arg
, memcached_st
*memc_arg
) :
19 rc(MEMCACHED_FAILURE
),
22 sem_init(&_lock
, 0, 0);
33 ~test_pool_context_st() {
38 static void *connection_release(void *arg
) {
39 assert(arg
!= nullptr);
41 this_thread::sleep_for(2s
);
42 auto res
= static_cast<test_pool_context_st
*>(arg
);
43 res
->rc
= memcached_pool_release(res
->pool
, res
->memc
);
50 TEST_CASE("memcached_util_pool_thread") {
52 SUCCEED("skip: pthreads");
55 auto pool
= memcached_pool_create(*memc
, 1, 1);
58 memcached_return_t rc
;
59 auto pool_memc
= memcached_pool_fetch(pool
, nullptr, &rc
);
60 REQUIRE(MEMCACHED_SUCCESS
== rc
);
64 @note This comment was written to describe what was believed to be the original authors intent.
66 This portion of the test creates a thread that will wait until told to free a memcached_st
67 that will be grabbed by the main thread.
69 It is believed that this tests whether or not we are handling ownership correctly.
72 test_pool_context_st
item(pool
, pool_memc
);
74 REQUIRE(0 == pthread_create(&tid
, nullptr, connection_release
, &item
));
77 memcached_st
*pop_memc
;
78 // We do a hard loop, and try N times
81 struct timespec relative_time
= {0, 0};
82 pop_memc
= memcached_pool_fetch(pool
, &relative_time
, &rc
);
84 if (memcached_success(rc
)) {
87 cerr
<< "rc == " << memcached_strerror(nullptr, rc
);
88 if (memcached_failed(rc
)) {
89 REQUIRE_FALSE(pop_memc
);
90 REQUIRE(rc
!= MEMCACHED_TIMEOUT
); // As long as relative_time is zero, MEMCACHED_TIMEOUT is invalid
94 // Cleanup thread since we will exit once we test.
95 REQUIRE(0 == pthread_join(tid
, nullptr));
96 REQUIRE(MEMCACHED_SUCCESS
== rc
);
97 REQUIRE(pool_memc
== pop_memc
);
98 REQUIRE(MEMCACHED_SUCCESS
== memcached_pool_release(pool
, pop_memc
));
99 REQUIRE(memcached_pool_destroy(pool
) == *memc
);
104 #endif // HAVE_SEMAPHORE_H