Fix some bugs when testing opensds ansible
[stor4nfv.git] / src / ceph / src / dmclock / test / test_test_client.cc
1 // -*- mode:C++; tab-width:8; c-basic-offset:2; indent-tabs-mode:t -*-
2 // vim: ts=8 sw=2 smarttab
3 /*
4  * Copyright (C) 2016 Red Hat Inc.
5  */
6
7 #include <atomic>
8 #include <thread>
9 #include <chrono>
10 #include <iostream>
11
12 #include "gtest/gtest.h"
13
14 #include "sim_recs.h"
15 #include "sim_client.h"
16
17 #include "test_dmclock.h"
18
19
20 using namespace std::placeholders;
21
22 namespace dmc = crimson::dmclock;
23 namespace test = crimson::test_dmc;
24 namespace sim = crimson::qos_simulation;
25
26 using TimePoint = std::chrono::time_point<std::chrono::system_clock>;
27
28 static TimePoint now() { return std::chrono::system_clock::now(); }
29
30
31 TEST(test_client, full_bore_timing) {
32   std::atomic_ulong count(0);
33
34   ServerId server_id = 3;
35
36   sim::TestResponse resp(0);
37   dmc::PhaseType resp_params = dmc::PhaseType::priority;
38   test::DmcClient* client;
39
40   auto start = now();
41   client =
42     new test::DmcClient(ClientId(0),
43                         [&] (const ServerId& server,
44                              const sim::TestRequest& req,
45                              const ClientId& client_id,
46                              const dmc::ReqParams& req_params) {
47                           ++count;
48                           client->receive_response(resp, client_id, resp_params);
49                         },
50                         [&] (const uint64_t seed) -> ServerId& {
51                           return server_id;
52                         },
53                         test::dmc_client_accumulate_f,
54                         1000, // ops to run
55                         100, // iops goal
56                         5); // outstanding ops allowed
57   client->wait_until_done();
58   auto end = now();
59   EXPECT_EQ(1000u, count) << "didn't get right number of ops";
60
61   int milliseconds = (end - start) / std::chrono::milliseconds(1);
62   EXPECT_LT(10000, milliseconds) << "timing too fast to be correct";
63   EXPECT_GT(12000, milliseconds) << "timing suspiciously slow";
64
65   delete client;
66 }
67
68
69 TEST(test_client, paused_timing) {
70   std::atomic_ulong count(0);
71   std::atomic_ulong unresponded_count(0);
72   std::atomic_bool auto_respond(false);
73
74   ClientId my_client_id = 0;
75   ServerId server_id = 3;
76
77   sim::TestResponse resp(0);
78   dmc::PhaseType resp_params = dmc::PhaseType::priority;
79   test::DmcClient* client;
80
81   auto start = now();
82   client =
83     new test::DmcClient(my_client_id,
84                         [&] (const ServerId& server,
85                              const sim::TestRequest& req,
86                              const ClientId& client_id,
87                              const dmc::ReqParams& req_params) {
88                           ++count;
89                           if (auto_respond.load()) {
90                             client->receive_response(resp, client_id, resp_params);
91                           } else {
92                             ++unresponded_count;
93                           }
94                         },
95                         [&] (const uint64_t seed) -> ServerId& {
96                           return server_id;
97                         },
98                         test::dmc_client_accumulate_f,
99
100                         1000, // ops to run
101                         100, // iops goal
102                         50); // outstanding ops allowed
103   std::thread t([&]() {
104       std::this_thread::sleep_for(std::chrono::seconds(5));
105       EXPECT_EQ(50u, unresponded_count.load()) <<
106         "should have 50 unresponded calls";
107       auto_respond = true;
108       // respond to those 50 calls
109       for(int i = 0; i < 50; ++i) {
110         client->receive_response(resp, my_client_id, resp_params);
111         --unresponded_count;
112       }
113     });
114
115   client->wait_until_done();
116   auto end = now();
117   int milliseconds = (end - start) / std::chrono::milliseconds(1);
118
119   // the 50 outstanding ops allowed means the first half-second of
120   // requests get responded to during the 5 second pause. So we have
121   // to adjust our expectations by a half-second.
122   EXPECT_LT(15000 - 500, milliseconds) << "timing too fast to be correct";
123   EXPECT_GT(17000 - 500, milliseconds) << "timing suspiciously slow";
124   t.join();
125
126   delete client;
127 }