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) 2011 Greg Farnum <gregory.farnum@dreamhost.com>
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.
14 #include "include/Context.h"
15 #include "gtest/gtest.h"
17 class C_Checker
: public Context
{
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
; }
26 TEST(ContextGather
, Constructor
) {
27 C_GatherBuilder
gather(g_ceph_context
);
28 EXPECT_FALSE(gather
.has_subs());
29 EXPECT_TRUE(gather
.get() == NULL
);
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());
38 bool finish_called
= false;
40 C_Checker
*checker
= new C_Checker(&finish_called
, &result
);
41 gather
.set_finisher(checker
);
44 EXPECT_TRUE(finish_called
);
48 TEST(ContextGather
, ManySubs
) {
49 bool finish_called
= false;
51 C_GatherBuilder
gather(g_ceph_context
, new C_Checker(&finish_called
, &result
));
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());
60 EXPECT_TRUE(gather
.has_subs());
63 //finish all except one sub
64 for (int j
= 0; j
< sub_count
- 1; ++j
) {
66 EXPECT_FALSE(finish_called
);
69 //finish last one and check asserts
70 subs
[sub_count
-1]->complete(0);
71 EXPECT_TRUE(finish_called
);
74 TEST(ContextGather
, AlternatingSubCreateFinish
) {
75 C_GatherBuilder
gather(g_ceph_context
);
77 bool finish_called
= false;
79 C_Checker
*checker
= new C_Checker(&finish_called
, &result
);
80 gather
.set_finisher(checker
);
81 Context
* subs
[sub_count
];
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());
90 //alternate finishing first half of subs and creating last half of subs
91 for (int j
= 0; j
< sub_count
/ 2; ++j
) {
93 subs
[sub_count
/ 2 + j
] = gather
.new_sub();
97 //finish last half of subs
98 for (int k
= sub_count
/ 2; k
< sub_count
; ++k
) {
102 EXPECT_TRUE(finish_called
);