]>
Commit | Line | Data |
---|---|---|
7c673cae FG |
1 | #include "include/rados/librados.h" |
2 | #include "include/rados/librados.hpp" | |
7c673cae FG |
3 | #include "include/utime.h" |
4 | #include "common/Thread.h" | |
5 | #include "common/Clock.h" | |
11fdf7f2 | 6 | #include "test/librados/test_cxx.h" |
7c673cae FG |
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> | |
31f18b77 | 15 | #include <atomic> |
7c673cae | 16 | |
11fdf7f2 | 17 | #include "test/librados/testcase_cxx.h" |
7c673cae FG |
18 | |
19 | ||
20 | using namespace librados; | |
21 | using std::map; | |
22 | using std::ostringstream; | |
23 | using std::string; | |
24 | ||
f67539c2 | 25 | static sem_t sem; |
31f18b77 | 26 | static std::atomic<bool> stop_flag = { false }; |
7c673cae FG |
27 | |
28 | class WatchNotifyTestCtx : public WatchCtx | |
29 | { | |
30 | public: | |
31 | void notify(uint8_t opcode, uint64_t ver, bufferlist& bl) override | |
32 | { | |
f67539c2 | 33 | sem_post(&sem); |
7c673cae FG |
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); | |
31f18b77 | 48 | while (!stop_flag) { |
7c673cae FG |
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 | ||
9f95a23c | 65 | INSTANTIATE_TEST_SUITE_P(WatchStressTests, WatchStress, |
7c673cae FG |
66 | ::testing::Values("", "cache")); |
67 | ||
68 | TEST_P(WatchStress, Stress1) { | |
f67539c2 | 69 | ASSERT_EQ(0, sem_init(&sem, 0, 0)); |
7c673cae FG |
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); | |
7c673cae FG |
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 | ||
f67539c2 TL |
91 | bool do_blocklist = i % 2; |
92 | if (do_blocklist) { | |
93 | cluster.test_blocklist_self(true); | |
94 | std::cerr << "blocklisted" << std::endl; | |
7c673cae FG |
95 | sleep(1); |
96 | } | |
97 | ||
98 | bufferlist bl2; | |
99 | ASSERT_EQ(0, nioctx.notify("foo", 0, bl2)); | |
100 | ||
f67539c2 | 101 | if (do_blocklist) { |
7c673cae FG |
102 | sleep(1); // Give a change to see an incorrect notify |
103 | } else { | |
104 | TestAlarm alarm; | |
f67539c2 | 105 | sem_wait(&sem); |
7c673cae FG |
106 | } |
107 | ||
f67539c2 TL |
108 | if (do_blocklist) { |
109 | cluster.test_blocklist_self(false); | |
7c673cae FG |
110 | } |
111 | ||
112 | ioctx.unwatch("foo", handle); | |
113 | ioctx.close(); | |
114 | } | |
31f18b77 | 115 | stop_flag = true; |
7c673cae FG |
116 | thr->join(); |
117 | nioctx.close(); | |
118 | ASSERT_EQ(0, destroy_one_pool_pp(pool_name, ncluster)); | |
f67539c2 | 119 | sem_destroy(&sem); |
7c673cae FG |
120 | } |
121 | ||
122 | #pragma GCC diagnostic pop | |
123 | #pragma GCC diagnostic warning "-Wpragmas" |