#include "include/rados/librados.hpp"
#include "test/librados/test_cxx.h"
-#include <semaphore.h>
#include <errno.h>
+
+#include <mutex>
+#include <condition_variable>
+#include <chrono>
#include <map>
-#include <sstream>
#include <iostream>
#include <string>
#include <stdlib.h>
using namespace librados;
using std::map;
-using std::ostringstream;
using std::string;
-static sem_t sem;
-
class WatchNotifyTestCtx : public WatchCtx
{
public:
- void notify(uint8_t opcode, uint64_t ver, bufferlist& bl) override
- {
- sem_post(&sem);
- }
+ WatchNotifyTestCtx(std::mutex &lock)
+ : lock{lock} {}
+ void notify(uint8_t opcode, uint64_t ver, bufferlist &bl) override {
+ std::unique_lock locker {lock};
+ notified = true;
+ cond.notify_one();
+ }
+ bool wait() {
+ std::unique_lock locker {lock};
+ return cond.wait_for(locker, std::chrono::seconds(1200),
+ [this] { return notified; });
+ }
+
+private:
+ bool notified = false;
+ std::mutex& lock;
+ std::condition_variable cond;
};
#pragma GCC diagnostic ignored "-Wpragmas"
exit(1);
}
- for (int i = 0; i < 10000; ++i) {
- std::cerr << "Iteration " << i << std::endl;
+ std::mutex lock;
+ constexpr int NR_ITERATIONS = 10000;
+ for (int i = 0; i < NR_ITERATIONS; ++i) {
+ std::cout << "Iteration " << i << std::endl;
uint64_t handle;
- WatchNotifyTestCtx ctx;
+ WatchNotifyTestCtx ctx{lock};
ret = ioctx.watch(obj_name, 0, &handle, &ctx);
ceph_assert(!ret);
bufferlist bl2;
ret = ioctx.notify(obj_name, 0, bl2);
ceph_assert(!ret);
- TestAlarm alarm;
- sem_wait(&sem);
+ ceph_assert_always(ctx.wait());
ioctx.unwatch(obj_name, handle);
}
-
ioctx.close();
ret = cluster.pool_delete(pool_name.c_str());
if (ret < 0) {
pool_name = argv[2];
obj_name = argv[3];
}
- std::cerr << "Test type " << type << std::endl;
- std::cerr << "pool_name, obj_name are " << pool_name << ", " << obj_name << std::endl;
+ std::cout << "Test type " << type << std::endl;
+ std::cout << "pool_name, obj_name are " << pool_name << ", " << obj_name << std::endl;
if (type != "ec" && type != "rep") {
std::cerr << "Error: " << argv[0] << " Invalid arg must be 'ec' or 'rep' saw " << type << std::endl;
else if (type == "ec")
test_erasure(cluster, pool_name, obj_name);
- sem_destroy(&sem);
return 0;
}