Fix some bugs when testing opensds ansible
[stor4nfv.git] / src / ceph / src / test / rbd_mirror / test_mock_ImageSyncThrottler.cc
1 // -*- mode:C++; tab-width:8; c-basic-offset:2; indent-tabs-mode:t -*-
2 // vim: ts=8 sw=2 smarttab
3 /*
4  * Ceph - scalable distributed file system
5  *
6  * Copyright (C) 2016 SUSE LINUX GmbH
7  *
8  * This is free software; you can redistribute it and/or
9  * modify it under the terms of the GNU Lesser General Public
10  * License version 2.1, as published by the Free Software
11  * Foundation.  See file COPYING.
12  *
13  */
14
15 #include "test/rbd_mirror/test_mock_fixture.h"
16 #include "test/librbd/mock/MockImageCtx.h"
17
18 namespace librbd {
19
20 namespace {
21
22 struct MockTestImageCtx : public librbd::MockImageCtx {
23   MockTestImageCtx(librbd::ImageCtx &image_ctx)
24     : librbd::MockImageCtx(image_ctx) {
25   }
26 };
27
28 } // anonymous namespace
29
30 } // namespace librbd
31
32 // template definitions
33 #include "tools/rbd_mirror/ImageSyncThrottler.cc"
34
35 namespace rbd {
36 namespace mirror {
37
38 class TestMockImageSyncThrottler : public TestMockFixture {
39 public:
40   typedef ImageSyncThrottler<librbd::MockTestImageCtx> MockImageSyncThrottler;
41
42 };
43
44 TEST_F(TestMockImageSyncThrottler, Single_Sync) {
45   MockImageSyncThrottler throttler;
46   C_SaferCond on_start;
47   throttler.start_op("id", &on_start);
48   ASSERT_EQ(0, on_start.wait());
49   throttler.finish_op("id");
50 }
51
52 TEST_F(TestMockImageSyncThrottler, Multiple_Syncs) {
53   MockImageSyncThrottler throttler;
54   throttler.set_max_concurrent_syncs(2);
55
56   C_SaferCond on_start1;
57   throttler.start_op("id1", &on_start1);
58   C_SaferCond on_start2;
59   throttler.start_op("id2", &on_start2);
60   C_SaferCond on_start3;
61   throttler.start_op("id3", &on_start3);
62   C_SaferCond on_start4;
63   throttler.start_op("id4", &on_start4);
64
65   ASSERT_EQ(0, on_start2.wait());
66   throttler.finish_op("id2");
67   ASSERT_EQ(0, on_start3.wait());
68   throttler.finish_op("id3");
69   ASSERT_EQ(0, on_start1.wait());
70   throttler.finish_op("id1");
71   ASSERT_EQ(0, on_start4.wait());
72   throttler.finish_op("id4");
73 }
74
75 TEST_F(TestMockImageSyncThrottler, Cancel_Running_Sync) {
76   MockImageSyncThrottler throttler;
77   C_SaferCond on_start;
78   throttler.start_op("id", &on_start);
79   ASSERT_EQ(0, on_start.wait());
80   ASSERT_FALSE(throttler.cancel_op("id"));
81   throttler.finish_op("id");
82 }
83
84 TEST_F(TestMockImageSyncThrottler, Cancel_Waiting_Sync) {
85   MockImageSyncThrottler throttler;
86   throttler.set_max_concurrent_syncs(1);
87
88   C_SaferCond on_start1;
89   throttler.start_op("id1", &on_start1);
90   C_SaferCond on_start2;
91   throttler.start_op("id2", &on_start2);
92
93   ASSERT_EQ(0, on_start1.wait());
94   ASSERT_TRUE(throttler.cancel_op("id2"));
95   ASSERT_EQ(-ECANCELED, on_start2.wait());
96   throttler.finish_op("id1");
97 }
98
99
100 TEST_F(TestMockImageSyncThrottler, Cancel_Running_Sync_Start_Waiting) {
101   MockImageSyncThrottler throttler;
102   throttler.set_max_concurrent_syncs(1);
103
104   C_SaferCond on_start1;
105   throttler.start_op("id1", &on_start1);
106   C_SaferCond on_start2;
107   throttler.start_op("id2", &on_start2);
108
109   ASSERT_EQ(0, on_start1.wait());
110   ASSERT_FALSE(throttler.cancel_op("id1"));
111   throttler.finish_op("id1");
112   ASSERT_EQ(0, on_start2.wait());
113   throttler.finish_op("id2");
114 }
115
116 TEST_F(TestMockImageSyncThrottler, Increase_Max_Concurrent_Syncs) {
117   MockImageSyncThrottler throttler;
118   throttler.set_max_concurrent_syncs(2);
119
120   C_SaferCond on_start1;
121   throttler.start_op("id1", &on_start1);
122   C_SaferCond on_start2;
123   throttler.start_op("id2", &on_start2);
124   C_SaferCond on_start3;
125   throttler.start_op("id3", &on_start3);
126   C_SaferCond on_start4;
127   throttler.start_op("id4", &on_start4);
128   C_SaferCond on_start5;
129   throttler.start_op("id5", &on_start5);
130
131   ASSERT_EQ(0, on_start1.wait());
132   ASSERT_EQ(0, on_start2.wait());
133
134   throttler.set_max_concurrent_syncs(4);
135
136   ASSERT_EQ(0, on_start3.wait());
137   ASSERT_EQ(0, on_start4.wait());
138
139   throttler.finish_op("id4");
140   ASSERT_EQ(0, on_start5.wait());
141
142   throttler.finish_op("id1");
143   throttler.finish_op("id2");
144   throttler.finish_op("id3");
145   throttler.finish_op("id5");
146 }
147
148 TEST_F(TestMockImageSyncThrottler, Decrease_Max_Concurrent_Syncs) {
149   MockImageSyncThrottler throttler;
150   throttler.set_max_concurrent_syncs(4);
151
152   C_SaferCond on_start1;
153   throttler.start_op("id1", &on_start1);
154   C_SaferCond on_start2;
155   throttler.start_op("id2", &on_start2);
156   C_SaferCond on_start3;
157   throttler.start_op("id3", &on_start3);
158   C_SaferCond on_start4;
159   throttler.start_op("id4", &on_start4);
160   C_SaferCond on_start5;
161   throttler.start_op("id5", &on_start5);
162
163   ASSERT_EQ(0, on_start1.wait());
164   ASSERT_EQ(0, on_start2.wait());
165   ASSERT_EQ(0, on_start3.wait());
166   ASSERT_EQ(0, on_start4.wait());
167
168   throttler.set_max_concurrent_syncs(2);
169
170   throttler.finish_op("id1");
171   throttler.finish_op("id2");
172   throttler.finish_op("id3");
173
174   ASSERT_EQ(0, on_start5.wait());
175
176   throttler.finish_op("id4");
177   throttler.finish_op("id5");
178 }
179
180 } // namespace mirror
181 } // namespace rbd
182