summaryrefslogtreecommitdiffstats
path: root/src/test/librados/asio.cc
blob: 9f86b4472b5a0621a5d815dd5bb0ca008d8546b5 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
// -*- mode:C++; tab-width:8; c-basic-offset:2; indent-tabs-mode:t -*-
// vim: ts=8 sw=2 smarttab
/*
 * Ceph - scalable distributed file system
 *
 * Copyright (C) 2017 Red Hat, Inc.
 *
 * This is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License version 2.1, as published by the Free Software
 * Foundation.  See file COPYING.
 */

#include "librados/librados_asio.h"
#include <gtest/gtest.h>

#include "common/ceph_argparse.h"
#include "common/debug.h"
#include "common/errno.h"
#include "global/global_init.h"

#include <boost/range/begin.hpp>
#include <boost/range/end.hpp>
#include <spawn/spawn.hpp>
#include <boost/asio/use_future.hpp>

#define dout_subsys ceph_subsys_rados
#define dout_context g_ceph_context

using namespace std;

// test fixture for global setup/teardown
class AsioRados : public ::testing::Test {
  static constexpr auto poolname = "ceph_test_rados_api_asio";

 protected:
  static librados::Rados rados;
  static librados::IoCtx io;
  // writes to snapio fail immediately with -EROFS. this is used to test errors
  // that come from inside the initiating function, rather than passed to the
  // AioCompletion callback
  static librados::IoCtx snapio;

 public:
  static void SetUpTestCase() {
    ASSERT_EQ(0, rados.init_with_context(g_ceph_context));
    ASSERT_EQ(0, rados.connect());
    // open/create test pool
    int r = rados.ioctx_create(poolname, io);
    if (r == -ENOENT) {
      r = rados.pool_create(poolname);
      if (r == -EEXIST) {
        r = 0;
      } else if (r == 0) {
        r = rados.ioctx_create(poolname, io);
      }
    }
    ASSERT_EQ(0, r);
    ASSERT_EQ(0, rados.ioctx_create(poolname, snapio));
    snapio.snap_set_read(1);
    // initialize the "exist" object
    bufferlist bl;
    bl.append("hello");
    ASSERT_EQ(0, io.write_full("exist", bl));
  }

  static void TearDownTestCase() {
    rados.shutdown();
  }
};
librados::Rados AsioRados::rados;
librados::IoCtx AsioRados::io;
librados::IoCtx AsioRados::snapio;

TEST_F(AsioRados, AsyncReadCallback)
{
  boost::asio::io_service service;

  auto success_cb = [&] (boost::system::error_code ec, bufferlist bl) {
    EXPECT_FALSE(ec);
    EXPECT_EQ("hello", bl.to_str());
  };
  librados::async_read(service, io, "exist", 256, 0, success_cb);

  auto failure_cb = [&] (boost::system::error_code ec, bufferlist bl) {
    EXPECT_EQ(boost::system::errc::no_such_file_or_directory, ec);
  };
  librados::async_read(service, io, "noexist", 256, 0, failure_cb);

  service.run();
}

TEST_F(AsioRados, AsyncReadFuture)
{
  boost::asio::io_service service;

  std::future<bufferlist> f1 = librados::async_read(service, io, "exist", 256,
                                                    0, boost::asio::use_future);
  std::future<bufferlist> f2 = librados::async_read(service, io, "noexist", 256,
                                                    0, boost::asio::use_future);

  service.run();

  EXPECT_NO_THROW({
    auto bl = f1.get();
    EXPECT_EQ("hello", bl.to_str());
  });
  EXPECT_THROW(f2.get(), boost::system::system_error);
}

TEST_F(AsioRados, AsyncReadYield)
{
  boost::asio::io_service service;

  auto success_cr = [&] (spawn::yield_context yield) {
    boost::system::error_code ec;
    auto bl = librados::async_read(service, io, "exist", 256, 0, yield[ec]);
    EXPECT_FALSE(ec);
    EXPECT_EQ("hello", bl.to_str());
  };
  spawn::spawn(service, success_cr);

  auto failure_cr = [&] (spawn::yield_context yield) {
    boost::system::error_code ec;
    auto bl = librados::async_read(service, io, "noexist", 256, 0, yield[ec]);
    EXPECT_EQ(boost::system::errc::no_such_file_or_directory, ec);
  };
  spawn::spawn(service, failure_cr);

  service.run();
}

TEST_F(AsioRados, AsyncWriteCallback)
{
  boost::asio::io_service service;

  bufferlist bl;
  bl.append("hello");

  auto success_cb = [&] (boost::system::error_code ec) {
    EXPECT_FALSE(ec);
  };
  librados::async_write(service, io, "exist", bl, bl.length(), 0,
                        success_cb);

  auto failure_cb = [&] (boost::system::error_code ec) {
    EXPECT_EQ(boost::system::errc::read_only_file_system, ec);
  };
  librados::async_write(service, snapio, "exist", bl, bl.length(), 0,
                        failure_cb);

  service.run();
}

TEST_F(AsioRados, AsyncWriteFuture)
{
  boost::asio::io_service service;

  bufferlist bl;
  bl.append("hello");

  auto f1 = librados::async_write(service, io, "exist", bl, bl.length(), 0,
                                  boost::asio::use_future);
  auto f2 = librados::async_write(service, snapio, "exist", bl, bl.length(), 0,
                                  boost::asio::use_future);

  service.run();

  EXPECT_NO_THROW(f1.get());
  EXPECT_THROW(f2.get(), boost::system::system_error);
}

TEST_F(AsioRados, AsyncWriteYield)
{
  boost::asio::io_service service;

  bufferlist bl;
  bl.append("hello");

  auto success_cr = [&] (spawn::yield_context yield) {
    boost::system::error_code ec;
    librados::async_write(service, io, "exist", bl, bl.length(), 0,
                          yield[ec]);
    EXPECT_FALSE(ec);
    EXPECT_EQ("hello", bl.to_str());
  };
  spawn::spawn(service, success_cr);

  auto failure_cr = [&] (spawn::yield_context yield) {
    boost::system::error_code ec;
    librados::async_write(service, snapio, "exist", bl, bl.length(), 0,
                          yield[ec]);
    EXPECT_EQ(boost::system::errc::read_only_file_system, ec);
  };
  spawn::spawn(service, failure_cr);

  service.run();
}

TEST_F(AsioRados, AsyncReadOperationCallback)
{
  boost::asio::io_service service;
  {
    librados::ObjectReadOperation op;
    op.read(0, 0, nullptr, nullptr);
    auto success_cb = [&] (boost::system::error_code ec, bufferlist bl) {
      EXPECT_FALSE(ec);
      EXPECT_EQ("hello", bl.to_str());
    };
    librados::async_operate(service, io, "exist", &op, 0, success_cb);
  }
  {
    librados::ObjectReadOperation op;
    op.read(0, 0, nullptr, nullptr);
    auto failure_cb = [&] (boost::system::error_code ec, bufferlist bl) {
      EXPECT_EQ(boost::system::errc::no_such_file_or_directory, ec);
    };
    librados::async_operate(service, io, "noexist", &op, 0, failure_cb);
  }
  service.run();
}

TEST_F(AsioRados, AsyncReadOperationFuture)
{
  boost::asio::io_service service;
  std::future<bufferlist> f1;
  {
    librados::ObjectReadOperation op;
    op.read(0, 0, nullptr, nullptr);
    f1 = librados::async_operate(service, io, "exist", &op, 0,
                                 boost::asio::use_future);
  }
  std::future<bufferlist> f2;
  {
    librados::ObjectReadOperation op;
    op.read(0, 0, nullptr, nullptr);
    f2 = librados::async_operate(service, io, "noexist", &op, 0,
                                 boost::asio::use_future);
  }
  service.run();

  EXPECT_NO_THROW({
    auto bl = f1.get();
    EXPECT_EQ("hello", bl.to_str());
  });
  EXPECT_THROW(f2.get(), boost::system::system_error);
}

TEST_F(AsioRados, AsyncReadOperationYield)
{
  boost::asio::io_service service;

  auto success_cr = [&] (spawn::yield_context yield) {
    librados::ObjectReadOperation op;
    op.read(0, 0, nullptr, nullptr);
    boost::system::error_code ec;
    auto bl = librados::async_operate(service, io, "exist", &op, 0,
                                      yield[ec]);
    EXPECT_FALSE(ec);
    EXPECT_EQ("hello", bl.to_str());
  };
  spawn::spawn(service, success_cr);

  auto failure_cr = [&] (spawn::yield_context yield) {
    librados::ObjectReadOperation op;
    op.read(0, 0, nullptr, nullptr);
    boost::system::error_code ec;
    auto bl = librados::async_operate(service, io, "noexist", &op, 0,
                                      yield[ec]);
    EXPECT_EQ(boost::system::errc::no_such_file_or_directory, ec);
  };
  spawn::spawn(service, failure_cr);

  service.run();
}

TEST_F(AsioRados, AsyncWriteOperationCallback)
{
  boost::asio::io_service service;

  bufferlist bl;
  bl.append("hello");

  {
    librados::ObjectWriteOperation op;
    op.write_full(bl);
    auto success_cb = [&] (boost::system::error_code ec) {
      EXPECT_FALSE(ec);
    };
    librados::async_operate(service, io, "exist", &op, 0, success_cb);
  }
  {
    librados::ObjectWriteOperation op;
    op.write_full(bl);
    auto failure_cb = [&] (boost::system::error_code ec) {
      EXPECT_EQ(boost::system::errc::read_only_file_system, ec);
    };
    librados::async_operate(service, snapio, "exist", &op, 0, failure_cb);
  }
  service.run();
}

TEST_F(AsioRados, AsyncWriteOperationFuture)
{
  boost::asio::io_service service;

  bufferlist bl;
  bl.append("hello");

  std::future<void> f1;
  {
    librados::ObjectWriteOperation op;
    op.write_full(bl);
    f1 = librados::async_operate(service, io, "exist", &op, 0,
                                 boost::asio::use_future);
  }
  std::future<void> f2;
  {
    librados::ObjectWriteOperation op;
    op.write_full(bl);
    f2 = librados::async_operate(service, snapio, "exist", &op, 0,
                                 boost::asio::use_future);
  }
  service.run();

  EXPECT_NO_THROW(f1.get());
  EXPECT_THROW(f2.get(), boost::system::system_error);
}

TEST_F(AsioRados, AsyncWriteOperationYield)
{
  boost::asio::io_service service;

  bufferlist bl;
  bl.append("hello");

  auto success_cr = [&] (spawn::yield_context yield) {
    librados::ObjectWriteOperation op;
    op.write_full(bl);
    boost::system::error_code ec;
    librados::async_operate(service, io, "exist", &op, 0, yield[ec]);
    EXPECT_FALSE(ec);
  };
  spawn::spawn(service, success_cr);

  auto failure_cr = [&] (spawn::yield_context yield) {
    librados::ObjectWriteOperation op;
    op.write_full(bl);
    boost::system::error_code ec;
    librados::async_operate(service, snapio, "exist", &op, 0, yield[ec]);
    EXPECT_EQ(boost::system::errc::read_only_file_system, ec);
  };
  spawn::spawn(service, failure_cr);

  service.run();
}

int main(int argc, char **argv)
{
  auto args = argv_to_vec(argc, argv);
  env_to_vec(args);

  auto cct = global_init(NULL, args, CEPH_ENTITY_TYPE_CLIENT,
                         CODE_ENVIRONMENT_UTILITY, 0);
  common_init_finish(cct.get());

  ::testing::InitGoogleTest(&argc, argv);
  return RUN_ALL_TESTS();
}