]> git.proxmox.com Git - ceph.git/blob - ceph/src/test/test_stress_watch.cc
buildsys: switch source download to quincy
[ceph.git] / ceph / src / test / test_stress_watch.cc
1 #include "include/rados/librados.h"
2 #include "include/rados/librados.hpp"
3 #include "include/utime.h"
4 #include "common/Thread.h"
5 #include "common/Clock.h"
6 #include "test/librados/test_cxx.h"
7
8 #include "gtest/gtest.h"
9 #include <semaphore.h>
10 #include <errno.h>
11 #include <map>
12 #include <sstream>
13 #include <iostream>
14 #include <string>
15 #include <atomic>
16
17 #include "test/librados/testcase_cxx.h"
18
19
20 using namespace librados;
21 using std::map;
22 using std::ostringstream;
23 using std::string;
24
25 static sem_t sem;
26 static std::atomic<bool> stop_flag = { false };
27
28 class WatchNotifyTestCtx : public WatchCtx
29 {
30 public:
31 void notify(uint8_t opcode, uint64_t ver, bufferlist& bl) override
32 {
33 sem_post(&sem);
34 }
35 };
36
37 #pragma GCC diagnostic ignored "-Wpragmas"
38 #pragma GCC diagnostic push
39 #pragma GCC diagnostic ignored "-Wdeprecated-declarations"
40
41 struct WatcherUnwatcher : public Thread {
42 string pool;
43 explicit WatcherUnwatcher(string& _pool) : pool(_pool) {}
44
45 void *entry() override {
46 Rados cluster;
47 connect_cluster_pp(cluster);
48 while (!stop_flag) {
49 IoCtx ioctx;
50 cluster.ioctx_create(pool.c_str(), ioctx);
51
52 uint64_t handle;
53 WatchNotifyTestCtx watch_ctx;
54 int r = ioctx.watch("foo", 0, &handle, &watch_ctx);
55 if (r == 0)
56 ioctx.unwatch("foo", handle);
57 ioctx.close();
58 }
59 return NULL;
60 }
61 };
62
63 typedef RadosTestParamPP WatchStress;
64
65 INSTANTIATE_TEST_SUITE_P(WatchStressTests, WatchStress,
66 ::testing::Values("", "cache"));
67
68 TEST_P(WatchStress, Stress1) {
69 ASSERT_EQ(0, sem_init(&sem, 0, 0));
70 Rados ncluster;
71 std::string pool_name = get_temp_pool_name();
72 ASSERT_EQ("", create_one_pool_pp(pool_name, ncluster));
73 IoCtx nioctx;
74 ncluster.ioctx_create(pool_name.c_str(), nioctx);
75
76 WatcherUnwatcher *thr = new WatcherUnwatcher(pool_name);
77 thr->create("watcher_unwatch");
78 ASSERT_EQ(0, nioctx.create("foo", false));
79
80 for (unsigned i = 0; i < 75; ++i) {
81 std::cerr << "Iteration " << i << std::endl;
82 uint64_t handle;
83 Rados cluster;
84 IoCtx ioctx;
85 WatchNotifyTestCtx ctx;
86
87 connect_cluster_pp(cluster);
88 cluster.ioctx_create(pool_name.c_str(), ioctx);
89 ASSERT_EQ(0, ioctx.watch("foo", 0, &handle, &ctx));
90
91 bool do_blocklist = i % 2;
92 if (do_blocklist) {
93 cluster.test_blocklist_self(true);
94 std::cerr << "blocklisted" << std::endl;
95 sleep(1);
96 }
97
98 bufferlist bl2;
99 ASSERT_EQ(0, nioctx.notify("foo", 0, bl2));
100
101 if (do_blocklist) {
102 sleep(1); // Give a change to see an incorrect notify
103 } else {
104 TestAlarm alarm;
105 sem_wait(&sem);
106 }
107
108 if (do_blocklist) {
109 cluster.test_blocklist_self(false);
110 }
111
112 ioctx.unwatch("foo", handle);
113 ioctx.close();
114 }
115 stop_flag = true;
116 thr->join();
117 nioctx.close();
118 ASSERT_EQ(0, destroy_one_pool_pp(pool_name, ncluster));
119 sem_destroy(&sem);
120 }
121
122 #pragma GCC diagnostic pop
123 #pragma GCC diagnostic warning "-Wpragmas"