1 // -*- mode:C++; tab-width:8; c-basic-offset:2; indent-tabs-mode:t -*-
2 // vim: ts=8 sw=2 smarttab
4 * Ceph - scalable distributed file system
6 * Copyright (C) 2016 SUSE LINUX GmbH
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.
15 #include "test/rbd_mirror/test_mock_fixture.h"
16 #include "test/librbd/mock/MockImageCtx.h"
22 struct MockTestImageCtx : public librbd::MockImageCtx {
23 MockTestImageCtx(librbd::ImageCtx &image_ctx)
24 : librbd::MockImageCtx(image_ctx) {
28 } // anonymous namespace
32 // template definitions
33 #include "tools/rbd_mirror/ImageSyncThrottler.cc"
38 class TestMockImageSyncThrottler : public TestMockFixture {
40 typedef ImageSyncThrottler<librbd::MockTestImageCtx> MockImageSyncThrottler;
44 TEST_F(TestMockImageSyncThrottler, Single_Sync) {
45 MockImageSyncThrottler throttler;
47 throttler.start_op("id", &on_start);
48 ASSERT_EQ(0, on_start.wait());
49 throttler.finish_op("id");
52 TEST_F(TestMockImageSyncThrottler, Multiple_Syncs) {
53 MockImageSyncThrottler throttler;
54 throttler.set_max_concurrent_syncs(2);
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);
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");
75 TEST_F(TestMockImageSyncThrottler, Cancel_Running_Sync) {
76 MockImageSyncThrottler throttler;
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");
84 TEST_F(TestMockImageSyncThrottler, Cancel_Waiting_Sync) {
85 MockImageSyncThrottler throttler;
86 throttler.set_max_concurrent_syncs(1);
88 C_SaferCond on_start1;
89 throttler.start_op("id1", &on_start1);
90 C_SaferCond on_start2;
91 throttler.start_op("id2", &on_start2);
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");
100 TEST_F(TestMockImageSyncThrottler, Cancel_Running_Sync_Start_Waiting) {
101 MockImageSyncThrottler throttler;
102 throttler.set_max_concurrent_syncs(1);
104 C_SaferCond on_start1;
105 throttler.start_op("id1", &on_start1);
106 C_SaferCond on_start2;
107 throttler.start_op("id2", &on_start2);
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");
116 TEST_F(TestMockImageSyncThrottler, Increase_Max_Concurrent_Syncs) {
117 MockImageSyncThrottler throttler;
118 throttler.set_max_concurrent_syncs(2);
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);
131 ASSERT_EQ(0, on_start1.wait());
132 ASSERT_EQ(0, on_start2.wait());
134 throttler.set_max_concurrent_syncs(4);
136 ASSERT_EQ(0, on_start3.wait());
137 ASSERT_EQ(0, on_start4.wait());
139 throttler.finish_op("id4");
140 ASSERT_EQ(0, on_start5.wait());
142 throttler.finish_op("id1");
143 throttler.finish_op("id2");
144 throttler.finish_op("id3");
145 throttler.finish_op("id5");
148 TEST_F(TestMockImageSyncThrottler, Decrease_Max_Concurrent_Syncs) {
149 MockImageSyncThrottler throttler;
150 throttler.set_max_concurrent_syncs(4);
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);
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());
168 throttler.set_max_concurrent_syncs(2);
170 throttler.finish_op("id1");
171 throttler.finish_op("id2");
172 throttler.finish_op("id3");
174 ASSERT_EQ(0, on_start5.wait());
176 throttler.finish_op("id4");
177 throttler.finish_op("id5");
180 } // namespace mirror