forked from facebookincubator/velox
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add CallbackOnLastSignal (facebookincubator#9506)
Summary: Pull Request resolved: facebookincubator#9506 Utility to emit a signal just once, and make sure that it's emitted (from the destructor) even if never called. Reviewed By: bikramSingh91, helfman Differential Revision: D56210938 fbshipit-source-id: a877473f318c03c836dc937e8ae016b818104711
- Loading branch information
1 parent
e18a4cf
commit f8fde93
Showing
3 changed files
with
221 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,137 @@ | ||
/* | ||
* Copyright (c) Facebook, Inc. and its affiliates. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
#include <gtest/gtest.h> | ||
|
||
#include "velox/dwio/common/UnitLoaderTools.h" | ||
|
||
using namespace ::testing; | ||
using namespace ::facebook::velox::dwio::common; | ||
using namespace ::facebook::velox::dwio::common::unit_loader_tools; | ||
|
||
TEST(UnitLoaderTestToolsTests, NoCallbacksCreated) { | ||
std::atomic_size_t callCount = 0; | ||
{ | ||
CallbackOnLastSignal callback([&callCount]() { ++callCount; }); | ||
EXPECT_EQ(callCount, 0); | ||
} | ||
EXPECT_EQ(callCount, 1); | ||
} | ||
|
||
TEST(UnitLoaderTestToolsTests, NoExplicitCalls) { | ||
std::atomic_size_t callCount = 0; | ||
{ | ||
CallbackOnLastSignal callback([&callCount]() { ++callCount; }); | ||
EXPECT_EQ(callCount, 0); | ||
{ | ||
auto c1 = callback.getCallback(); | ||
auto c4 = callback.getCallback(); | ||
EXPECT_EQ(callCount, 0); | ||
|
||
auto c2 = std::move(c1); | ||
auto c3(c2); | ||
EXPECT_EQ(callCount, 0); | ||
|
||
auto c5 = std::move(c4); | ||
auto c6(c5); | ||
EXPECT_EQ(callCount, 0); | ||
} | ||
EXPECT_EQ(callCount, 1); | ||
} | ||
EXPECT_EQ(callCount, 1); | ||
} | ||
|
||
TEST(UnitLoaderTestToolsTests, NoExplicitCallsFactoryDeletedFirst) { | ||
std::atomic_size_t callCount = 0; | ||
{ | ||
std::function<void()> c1, c2; | ||
{ | ||
CallbackOnLastSignal callback([&callCount]() { ++callCount; }); | ||
EXPECT_EQ(callCount, 0); | ||
|
||
c1 = callback.getCallback(); | ||
c2 = callback.getCallback(); | ||
EXPECT_EQ(callCount, 0); | ||
} | ||
EXPECT_EQ(callCount, 0); | ||
} | ||
EXPECT_EQ(callCount, 1); | ||
} | ||
|
||
TEST(UnitLoaderTestToolsTests, ExplicitCalls) { | ||
std::atomic_size_t callCount = 0; | ||
{ | ||
CallbackOnLastSignal callback([&callCount]() { ++callCount; }); | ||
EXPECT_EQ(callCount, 0); | ||
{ | ||
auto c1 = callback.getCallback(); | ||
auto c4 = callback.getCallback(); | ||
EXPECT_EQ(callCount, 0); | ||
|
||
c1(); | ||
auto c2 = std::move(c1); | ||
c2(); | ||
auto c3(c2); | ||
c3(); | ||
EXPECT_EQ(callCount, 0); | ||
|
||
c4(); | ||
EXPECT_EQ(callCount, 1); | ||
auto c5 = std::move(c4); | ||
c5(); | ||
auto c6(c2); | ||
c6(); | ||
EXPECT_EQ(callCount, 1); | ||
} | ||
EXPECT_EQ(callCount, 1); | ||
} | ||
EXPECT_EQ(callCount, 1); | ||
} | ||
|
||
TEST(UnitLoaderTestToolsTests, WillOnlyCallbackOnce) { | ||
std::atomic_size_t callCount = 0; | ||
{ | ||
CallbackOnLastSignal callback([&callCount]() { ++callCount; }); | ||
EXPECT_EQ(callCount, 0); | ||
{ | ||
auto c1 = callback.getCallback(); | ||
auto c4 = callback.getCallback(); | ||
EXPECT_EQ(callCount, 0); | ||
|
||
c1(); | ||
auto c2 = std::move(c1); | ||
c2(); | ||
auto c3(c2); | ||
c3(); | ||
EXPECT_EQ(callCount, 0); | ||
|
||
c4(); | ||
EXPECT_EQ(callCount, 1); | ||
auto c5 = std::move(c4); | ||
c5(); | ||
auto c6(c2); | ||
c6(); | ||
EXPECT_EQ(callCount, 1); | ||
|
||
// This won't emit a new call | ||
auto c7 = callback.getCallback(); | ||
c7(); | ||
EXPECT_EQ(callCount, 1); | ||
} | ||
EXPECT_EQ(callCount, 1); | ||
} | ||
EXPECT_EQ(callCount, 1); | ||
} |