struct worker_ctx {
memcached_pool_st *pool;
- vector<stringstream> errors;
explicit worker_ctx(memcached_st *memc)
: pool{memcached_pool_create(memc, 5, 10)}
- , errors{}
{
}
~worker_ctx() {
memcached_pool_destroy(pool);
- for (const auto &err : errors) {
- cerr << err.str() << endl;
- }
- }
-
- stringstream &err() {
- errors.resize(errors.size()+1);
- return errors[errors.size()-1];
}
};
cerr << "failed to fetch connection from pool: "
<< memcached_strerror(nullptr, rc)
<< endl;
- this_thread::sleep_for(10ms);
continue;
}
REQUIRE(0 == pthread_create(&t, nullptr, worker, &ctx));
}
- this_thread::sleep_for(5s);
+ this_thread::sleep_for(1s);
set_running(false);
for (auto t : tid) {
#include "test/lib/common.hpp"
-#include "libmemcachedutil-1.0/pool.h"
-
-#include <cassert>
-
#if HAVE_SEMAPHORE_H
+#include "libmemcachedutil-1.0/pool.h"
+#include <cassert>
#include "semaphore.h"
-#ifndef __APPLE__
struct test_pool_context_st {
volatile memcached_return_t rc;
memcached_pool_st *pool;
rc(MEMCACHED_FAILURE),
pool(pool_arg),
memc(memc_arg) {
- sem_init(&_lock, 0, 0);
+ if (sem_init(&_lock, 0, 0)) {
+ perror("sem_init()");
+ }
}
void wait() {
static void *connection_release(void *arg) {
assert(arg != nullptr);
- this_thread::sleep_for(2s);
+ this_thread::sleep_for(200ms);
auto res = static_cast<test_pool_context_st *>(arg);
res->rc = memcached_pool_release(res->pool, res->memc);
res->release();
pthread_exit(arg);
}
-#endif
TEST_CASE("memcached_util_pool_thread") {
-#ifdef __APPLE__
- SUCCEED("skip: pthreads");
-#else
MemcachedPtr memc;
auto pool = memcached_pool_create(*memc, 1, 1);
REQUIRE(pool);
item.wait();
memcached_st *pop_memc;
- // We do a hard loop, and try N times
- int counter = 5;
do {
struct timespec relative_time = {0, 0};
pop_memc = memcached_pool_fetch(pool, &relative_time, &rc);
REQUIRE_FALSE(pop_memc);
REQUIRE(rc != MEMCACHED_TIMEOUT); // As long as relative_time is zero, MEMCACHED_TIMEOUT is invalid
}
- } while (--counter);
+ } while (rc == MEMCACHED_NOTFOUND);
// Cleanup thread since we will exit once we test.
REQUIRE(0 == pthread_join(tid, nullptr));
REQUIRE(MEMCACHED_SUCCESS == memcached_pool_release(pool, pop_memc));
REQUIRE(memcached_pool_destroy(pool) == *memc);
-#endif // __APPLE__
}
#endif // HAVE_SEMAPHORE_H