2 * Copyright 2004-present Facebook, Inc.
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 #include <folly/io/async/DelayedDestructionBase.h>
21 #include <folly/portability/GTest.h>
23 using namespace folly;
25 class DestructionOnCallback : public DelayedDestructionBase {
27 DestructionOnCallback() : state_(0), deleted_(false) {
30 void onComplete(int n, int& state) {
31 DestructorGuard dg(this);
32 for (auto i = n; i >= 0; --i) {
38 int state() const { return state_; }
39 bool deleted() const { return deleted_; }
42 void onStackedComplete(int recur) {
43 DestructorGuard dg(this);
48 onStackedComplete(--recur);
54 void onDelayedDestroy(bool delayed) override {
57 (void)delayed; // prevent unused variable warnings
61 struct DelayedDestructionBaseTest : public ::testing::Test {
64 TEST_F(DelayedDestructionBaseTest, basic) {
65 DestructionOnCallback* d = new DestructionOnCallback();
66 EXPECT_NE(d, nullptr);
68 d->onComplete(3, state);
69 EXPECT_EQ(state, 10); // 10 = 6 + 3 + 1