/Users/deen/code/yugabyte-db/src/yb/util/object_pool-test.cc
Line | Count | Source |
1 | | // Licensed to the Apache Software Foundation (ASF) under one |
2 | | // or more contributor license agreements. See the NOTICE file |
3 | | // distributed with this work for additional information |
4 | | // regarding copyright ownership. The ASF licenses this file |
5 | | // to you under the Apache License, Version 2.0 (the |
6 | | // "License"); you may not use this file except in compliance |
7 | | // with the License. You may obtain a copy of the License at |
8 | | // |
9 | | // http://www.apache.org/licenses/LICENSE-2.0 |
10 | | // |
11 | | // Unless required by applicable law or agreed to in writing, |
12 | | // software distributed under the License is distributed on an |
13 | | // "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
14 | | // KIND, either express or implied. See the License for the |
15 | | // specific language governing permissions and limitations |
16 | | // under the License. |
17 | | // |
18 | | // The following only applies to changes made to this file as part of YugaByte development. |
19 | | // |
20 | | // Portions Copyright (c) YugaByte, Inc. |
21 | | // |
22 | | // Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except |
23 | | // in compliance with the License. You may obtain a copy of the License at |
24 | | // |
25 | | // http://www.apache.org/licenses/LICENSE-2.0 |
26 | | // |
27 | | // Unless required by applicable law or agreed to in writing, software distributed under the License |
28 | | // is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express |
29 | | // or implied. See the License for the specific language governing permissions and limitations |
30 | | // under the License. |
31 | | // |
32 | | |
33 | | #include <glog/logging.h> |
34 | | #include <gtest/gtest.h> |
35 | | |
36 | | #include "yb/util/object_pool.h" |
37 | | |
38 | | namespace yb { |
39 | | |
40 | | // Simple class which maintains a count of how many objects |
41 | | // are currently alive. |
42 | | class MyClass { |
43 | | public: |
44 | 4 | MyClass() { |
45 | 4 | instance_count_++; |
46 | 4 | } |
47 | | |
48 | 4 | ~MyClass() { |
49 | 4 | instance_count_--; |
50 | 4 | } |
51 | | |
52 | 10 | static int instance_count() { |
53 | 10 | return instance_count_; |
54 | 10 | } |
55 | | |
56 | 2 | static void ResetCount() { |
57 | 2 | instance_count_ = 0; |
58 | 2 | } |
59 | | |
60 | | private: |
61 | | static int instance_count_; |
62 | | }; |
63 | | int MyClass::instance_count_ = 0; |
64 | | |
65 | 1 | TEST(TestObjectPool, TestPooling) { |
66 | 1 | MyClass::ResetCount(); |
67 | 1 | { |
68 | 1 | ObjectPool<MyClass> pool; |
69 | 1 | ASSERT_EQ(0, MyClass::instance_count()); |
70 | 1 | MyClass *a = pool.Construct(); |
71 | 1 | ASSERT_EQ(1, MyClass::instance_count()); |
72 | 1 | MyClass *b = pool.Construct(); |
73 | 1 | ASSERT_EQ(2, MyClass::instance_count()); |
74 | 1 | ASSERT_TRUE(a != b); |
75 | 1 | pool.Destroy(b); |
76 | 1 | ASSERT_EQ(1, MyClass::instance_count()); |
77 | 1 | MyClass *c = pool.Construct(); |
78 | 1 | ASSERT_EQ(2, MyClass::instance_count()); |
79 | 2 | ASSERT_TRUE(c == b) << "should reuse instance"; |
80 | 1 | pool.Destroy(c); |
81 | | |
82 | 1 | ASSERT_EQ(1, MyClass::instance_count()); |
83 | 1 | } |
84 | | |
85 | 2 | ASSERT_EQ(0, MyClass::instance_count()) |
86 | 2 | << "destructing pool should have cleared instances"; |
87 | 1 | } |
88 | | |
89 | 1 | TEST(TestObjectPool, TestScopedPtr) { |
90 | 1 | MyClass::ResetCount(); |
91 | 1 | ASSERT_EQ(0, MyClass::instance_count()); |
92 | 1 | ObjectPool<MyClass> pool; |
93 | 1 | { |
94 | 1 | ObjectPool<MyClass>::scoped_ptr sptr( |
95 | 1 | pool.make_scoped_ptr(pool.Construct())); |
96 | 1 | ASSERT_EQ(1, MyClass::instance_count()); |
97 | 1 | } |
98 | 1 | ASSERT_EQ(0, MyClass::instance_count()); |
99 | 1 | } |
100 | | |
101 | | } // namespace yb |