folly copyright 2015 -> copyright 2016
[folly.git] / folly / io / async / test / DelayedDestructionBaseTest.cpp
1 /*
2  * Copyright 2016 Facebook, Inc.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *   http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 /*
17  * Licensed to the Apache Software Foundation (ASF) under one
18  * or more contributor license agreements. See the NOTICE file
19  * distributed with this work for additional information
20  * regarding copyright ownership. The ASF licenses this file
21  * to you under the Apache License, Version 2.0 (the
22  * "License"); you may not use this file except in compliance
23  * 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,
28  * software distributed under the License is distributed on an
29  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
30  * KIND, either express or implied. See the License for the
31  * specific language governing permissions and limitations
32  * under the License.
33  */
34 #include <folly/io/async/DelayedDestructionBase.h>
35
36 #include <functional>
37 #include <gtest/gtest.h>
38 #include <list>
39 #include <vector>
40
41 using namespace folly;
42
43 class DestructionOnCallback : public DelayedDestructionBase {
44  public:
45   DestructionOnCallback() : state_(0), deleted_(false) {
46     onDestroy_ = [this] (bool delayed) {
47       deleted_ = true;
48       delete this;
49       (void)delayed; // prevent unused variable warnings
50     };
51   }
52
53   void onComplete(int n, int& state) {
54     DestructorGuard dg(this);
55     for (auto i = n; i >= 0; --i) {
56       onStackedComplete(i);
57     }
58     state = state_;
59   }
60
61   void setOnDestroy(std::function<void(bool)> onDestroy) {
62     onDestroy_ = onDestroy;
63   }
64
65   int state() const { return state_; }
66   bool deleted() const { return deleted_; }
67
68  protected:
69   void onStackedComplete(int recur) {
70     DestructorGuard dg(this);
71     ++state_;
72     if (recur <= 0) {
73       return;
74     }
75     onStackedComplete(--recur);
76   }
77  private:
78   int state_;
79   bool deleted_;
80 };
81
82 struct DelayedDestructionBaseTest : public ::testing::Test {
83 };
84
85 TEST_F(DelayedDestructionBaseTest, basic) {
86   DestructionOnCallback* d = new DestructionOnCallback();
87   EXPECT_NE(d, nullptr);
88   int32_t state;
89   d->onComplete(3, state);
90   EXPECT_EQ(state, 10); // 10 = 6 + 3 + 1
91 }
92
93 TEST_F(DelayedDestructionBaseTest, destructFromContainer) {
94   std::list<DestructionOnCallback> l;
95   l.emplace_back();
96   l.back().setOnDestroy([&] (bool delayed) {
97     l.erase(l.begin());
98     (void)delayed;
99   });
100   EXPECT_NE(l.size(), 0);
101   int32_t state;
102   l.back().onComplete(3, state);
103   EXPECT_EQ(state, 10); // 10 = 6 + 3 + 1
104   EXPECT_EQ(l.size(), 0);
105 }