proxygen
InterruptTest.cpp
Go to the documentation of this file.
1 /*
2  * Copyright 2014-present 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 #include <folly/futures/Future.h>
18 #include <folly/futures/Promise.h>
21 
22 using namespace folly;
23 
24 TEST(Interrupt, raise) {
25  using eggs_t = std::runtime_error;
26  Promise<Unit> p;
27  p.setInterruptHandler([&](const exception_wrapper& e) {
29  });
30  p.getFuture().raise(eggs_t("eggs"));
31 }
32 
33 TEST(Interrupt, cancel) {
34  Promise<Unit> p;
35  p.setInterruptHandler([&](const exception_wrapper& e) {
37  });
38  p.getFuture().cancel();
39 }
40 
41 TEST(Interrupt, handleThenInterrupt) {
42  Promise<int> p;
43  bool flag = false;
44  p.setInterruptHandler([&](const exception_wrapper& /* e */) { flag = true; });
45  p.getFuture().cancel();
46  EXPECT_TRUE(flag);
47 }
48 
49 TEST(Interrupt, interruptThenHandle) {
50  Promise<int> p;
51  bool flag = false;
52  p.getFuture().cancel();
53  p.setInterruptHandler([&](const exception_wrapper& /* e */) { flag = true; });
54  EXPECT_TRUE(flag);
55 }
56 
57 TEST(Interrupt, interruptAfterFulfilNoop) {
58  Promise<Unit> p;
59  bool flag = false;
60  p.setInterruptHandler([&](const exception_wrapper& /* e */) { flag = true; });
61  p.setValue();
62  p.getFuture().cancel();
63  EXPECT_FALSE(flag);
64 }
65 
66 TEST(Interrupt, secondInterruptNoop) {
67  Promise<Unit> p;
68  int count = 0;
69  p.setInterruptHandler([&](const exception_wrapper& /* e */) { count++; });
70  auto f = p.getFuture();
71  f.cancel();
72  f.cancel();
73  EXPECT_EQ(1, count);
74 }
75 
76 TEST(Interrupt, futureWithinTimedOut) {
77  Promise<int> p;
78  Baton<> done;
79  p.setInterruptHandler([&](const exception_wrapper& /* e */) { done.post(); });
80  p.getFuture().within(std::chrono::milliseconds(1));
81  // Give it 100ms to time out and call the interrupt handler
82  EXPECT_TRUE(done.try_wait_for(std::chrono::milliseconds(100)));
83 }
84 
85 TEST(Interrupt, semiFutureWithinTimedOut) {
86  Promise<int> p;
87  Baton<> done;
88  p.setInterruptHandler([&](const exception_wrapper& /* e */) { done.post(); });
89  p.getSemiFuture().within(std::chrono::milliseconds(1));
90  // Give it 100ms to time out and call the interrupt handler
91  EXPECT_TRUE(done.try_wait_for(std::chrono::milliseconds(100)));
92 }
void setInterruptHandler(F &&fn)
Definition: Promise-inl.h:117
auto f
#define EXPECT_THROW(statement, expected_exception)
Definition: gtest.h:1843
#define EXPECT_EQ(val1, val2)
Definition: gtest.h:1922
—— Concurrent Priority Queue Implementation ——
Definition: AtomicBitSet.h:29
static once_flag flag
Definition: Random.cpp:75
FOLLY_ALWAYS_INLINE bool try_wait_for(const std::chrono::duration< Rep, Period > &timeout, const WaitOptions &opt=wait_options()) noexcept
Definition: Baton.h:206
Future< T > getFuture()
Definition: Promise-inl.h:97
FutureException eggs_t
Definition: CollectTest.cpp:28
void post() noexcept
Definition: Baton.h:123
int * count
#define EXPECT_TRUE(condition)
Definition: gtest.h:1859
std::enable_if< std::is_same< Unit, B >::value, void >::type setValue()
Definition: Promise.h:326
SemiFuture< T > getSemiFuture()
Definition: Promise-inl.h:88
#define EXPECT_FALSE(condition)
Definition: gtest.h:1862
TEST(SequencedExecutor, CPUThreadPoolExecutor)