Impala
Impalaistheopensource,nativeanalyticdatabaseforApacheHadoop.
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
thread-resource-mgr-test.cc
Go to the documentation of this file.
1 // Copyright 2012 Cloudera Inc.
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 // http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14 
15 #include <string>
16 #include <boost/bind.hpp>
17 #include <gtest/gtest.h>
18 
20 #include "util/cpu-info.h"
21 
22 #include "common/names.h"
23 
24 namespace impala {
25 
27  public:
29  }
30 
32  DCHECK(consumer != NULL);
33  DCHECK_LT(consumer->num_threads(), consumer->quota());
34  ++counter_;
35  }
36 
37  int counter() const { return counter_; }
38 
39  private:
40  int counter_;
41 };
42 
43 TEST(ThreadResourceMgr, BasicTest) {
44  ThreadResourceMgr mgr(5);
45  NotifiedCounter counter1, counter2;
46 
48  c1->SetThreadAvailableCb(bind<void>(mem_fn(&NotifiedCounter::Notify), &counter1, _1));
49  c1->AcquireThreadToken();
50  c1->AcquireThreadToken();
51  c1->AcquireThreadToken();
52  EXPECT_EQ(c1->num_threads(), 3);
53  EXPECT_EQ(c1->num_required_threads(), 3);
54  EXPECT_EQ(c1->num_optional_threads(), 0);
55  EXPECT_EQ(counter1.counter(), 0);
56  c1->ReleaseThreadToken(true);
57  EXPECT_EQ(c1->num_threads(), 2);
58  EXPECT_EQ(c1->num_required_threads(), 2);
59  EXPECT_EQ(c1->num_optional_threads(), 0);
60  EXPECT_EQ(counter1.counter(), 1);
61  bool is_reserved = false;
62  c1->ReserveOptionalTokens(1);
63  EXPECT_TRUE(c1->TryAcquireThreadToken(&is_reserved));
64  EXPECT_TRUE(is_reserved);
65  EXPECT_TRUE(c1->TryAcquireThreadToken(&is_reserved));
66  EXPECT_FALSE(is_reserved);
67  EXPECT_TRUE(c1->TryAcquireThreadToken(&is_reserved));
68  EXPECT_FALSE(is_reserved);
69  EXPECT_FALSE(c1->TryAcquireThreadToken(&is_reserved));
70  EXPECT_FALSE(is_reserved);
71  EXPECT_EQ(c1->num_threads(), 5);
72  EXPECT_EQ(c1->num_required_threads(), 2);
73  EXPECT_EQ(c1->num_optional_threads(), 3);
74  c1->ReleaseThreadToken(true);
75  c1->ReleaseThreadToken(false);
76  EXPECT_EQ(counter1.counter(), 3);
77 
78  // Register a new consumer, quota is cut in half
80  c2->SetThreadAvailableCb(bind<void>(mem_fn(&NotifiedCounter::Notify), &counter2, _1));
81  EXPECT_FALSE(c1->TryAcquireThreadToken());
82  EXPECT_EQ(c1->num_threads(), 3);
83  c1->AcquireThreadToken();
84  EXPECT_EQ(c1->num_threads(), 4);
85  EXPECT_EQ(c1->num_required_threads(), 2);
86  EXPECT_EQ(c1->num_optional_threads(), 2);
87 
88  mgr.UnregisterPool(c1);
89  mgr.UnregisterPool(c2);
90  EXPECT_EQ(counter1.counter(), 3);
91  EXPECT_EQ(counter2.counter(), 1);
92 }
93 
94 }
95 
96 int main(int argc, char **argv) {
97  ::testing::InitGoogleTest(&argc, argv);
99  return RUN_ALL_TESTS();
100 }
bool TryAcquireThreadToken(bool *is_reserved=NULL)
int num_required_threads() const
Returns the number of threads that are from AcquireThreadToken.
void SetThreadAvailableCb(ThreadAvailableCb fn)
TEST(AtomicTest, Basic)
Definition: atomic-test.cc:28
void UnregisterPool(ResourcePool *pool)
void Notify(ThreadResourceMgr::ResourcePool *consumer)
int main(int argc, char **argv)
static void Init()
Initialize CpuInfo.
Definition: cpu-info.cc:75