Fix some bugs when testing opensds ansible
[stor4nfv.git] / src / ceph / src / test / gather.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) 2011 Greg Farnum <gregory.farnum@dreamhost.com>
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 #include "include/Context.h"
15 #include "gtest/gtest.h"
16
17 class C_Checker : public Context {
18 public:
19   bool *finish_called;
20   int *result;
21   C_Checker(bool* _finish_called, int *r) :
22     finish_called(_finish_called), result(r) {}
23   void finish(int r) override { *finish_called = true; *result = r; }
24 };
25
26 TEST(ContextGather, Constructor) {
27   C_GatherBuilder gather(g_ceph_context);
28   EXPECT_FALSE(gather.has_subs());
29   EXPECT_TRUE(gather.get() == NULL);
30 }
31
32 TEST(ContextGather, OneSub) {
33   C_GatherBuilder gather(g_ceph_context);
34   Context *sub = gather.new_sub();
35   EXPECT_EQ(1, gather.num_subs_created());
36   EXPECT_EQ(1, gather.num_subs_remaining());
37
38   bool finish_called = false;
39   int result = 0;
40   C_Checker *checker = new C_Checker(&finish_called, &result);
41   gather.set_finisher(checker);
42   gather.activate();
43   sub->complete(0);
44   EXPECT_TRUE(finish_called);
45   EXPECT_EQ(0, result);
46 }
47
48 TEST(ContextGather, ManySubs) {
49   bool finish_called = false;
50   int result = 0;
51   C_GatherBuilder gather(g_ceph_context, new C_Checker(&finish_called, &result));
52   int sub_count = 8;
53   Context* subs[sub_count];
54   //create subs and test
55   for (int i = 0; i < sub_count; ++i) {
56     subs[i] = gather.new_sub();
57     EXPECT_EQ(i+1, gather.num_subs_created());
58     EXPECT_EQ(i+1, gather.num_subs_remaining());
59   }
60   EXPECT_TRUE(gather.has_subs());
61   gather.activate();
62
63   //finish all except one sub
64   for (int j = 0; j < sub_count - 1; ++j) {
65     subs[j]->complete(0);
66     EXPECT_FALSE(finish_called);
67   }
68
69   //finish last one and check asserts
70   subs[sub_count-1]->complete(0);
71   EXPECT_TRUE(finish_called);
72 }
73
74 TEST(ContextGather, AlternatingSubCreateFinish) {
75   C_GatherBuilder gather(g_ceph_context);
76   int sub_count = 8;
77   bool finish_called = false;
78   int result = 0;
79   C_Checker *checker = new C_Checker(&finish_called, &result);
80   gather.set_finisher(checker);
81   Context* subs[sub_count];
82
83   //create half the subs
84   for (int i = 0; i < sub_count / 2; ++i) {
85     subs[i] = gather.new_sub();
86     EXPECT_EQ(i + 1, gather.num_subs_created());
87     EXPECT_EQ(i + 1, gather.num_subs_remaining());
88   }
89
90   //alternate finishing first half of subs and creating last half of subs
91   for (int j = 0; j < sub_count / 2; ++j) {
92     subs[j]->complete(0);
93     subs[sub_count / 2 + j] = gather.new_sub();
94   }
95   gather.activate();
96
97   //finish last half of subs
98   for (int k = sub_count / 2; k < sub_count; ++k) {
99     subs[k]->complete(0);
100   }
101
102   EXPECT_TRUE(finish_called);
103 }