proxygen
InterruptTest.cpp File Reference

Go to the source code of this file.

Functions

 TEST (Interrupt, raise)
 
 TEST (Interrupt, cancel)
 
 TEST (Interrupt, handleThenInterrupt)
 
 TEST (Interrupt, interruptThenHandle)
 
 TEST (Interrupt, interruptAfterFulfilNoop)
 
 TEST (Interrupt, secondInterruptNoop)
 
 TEST (Interrupt, futureWithinTimedOut)
 
 TEST (Interrupt, semiFutureWithinTimedOut)
 

Function Documentation

TEST ( Interrupt  ,
raise   
)

Definition at line 24 of file InterruptTest.cpp.

References EXPECT_THROW, folly::Promise< T >::getFuture(), folly::Promise< T >::setInterruptHandler(), and folly::exception_wrapper::throw_exception().

24  {
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 }
void setInterruptHandler(F &&fn)
Definition: Promise-inl.h:117
#define EXPECT_THROW(statement, expected_exception)
Definition: gtest.h:1843
Future< T > getFuture()
Definition: Promise-inl.h:97
FutureException eggs_t
Definition: CollectTest.cpp:28
TEST ( Interrupt  ,
cancel   
)

Definition at line 33 of file InterruptTest.cpp.

References EXPECT_THROW, folly::Promise< T >::getFuture(), folly::Promise< T >::setInterruptHandler(), and folly::exception_wrapper::throw_exception().

33  {
34  Promise<Unit> p;
35  p.setInterruptHandler([&](const exception_wrapper& e) {
37  });
38  p.getFuture().cancel();
39 }
void setInterruptHandler(F &&fn)
Definition: Promise-inl.h:117
#define EXPECT_THROW(statement, expected_exception)
Definition: gtest.h:1843
Future< T > getFuture()
Definition: Promise-inl.h:97
TEST ( Interrupt  ,
handleThenInterrupt   
)

Definition at line 41 of file InterruptTest.cpp.

References EXPECT_TRUE, flag, folly::Promise< T >::getFuture(), and folly::Promise< T >::setInterruptHandler().

41  {
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 }
void setInterruptHandler(F &&fn)
Definition: Promise-inl.h:117
static once_flag flag
Definition: Random.cpp:75
Future< T > getFuture()
Definition: Promise-inl.h:97
#define EXPECT_TRUE(condition)
Definition: gtest.h:1859
TEST ( Interrupt  ,
interruptThenHandle   
)

Definition at line 49 of file InterruptTest.cpp.

References EXPECT_TRUE, flag, folly::Promise< T >::getFuture(), and folly::Promise< T >::setInterruptHandler().

49  {
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 }
void setInterruptHandler(F &&fn)
Definition: Promise-inl.h:117
static once_flag flag
Definition: Random.cpp:75
Future< T > getFuture()
Definition: Promise-inl.h:97
#define EXPECT_TRUE(condition)
Definition: gtest.h:1859
TEST ( Interrupt  ,
interruptAfterFulfilNoop   
)

Definition at line 57 of file InterruptTest.cpp.

References EXPECT_FALSE, flag, folly::Promise< T >::getFuture(), folly::Promise< T >::setInterruptHandler(), and folly::Promise< T >::setValue().

57  {
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 }
void setInterruptHandler(F &&fn)
Definition: Promise-inl.h:117
static once_flag flag
Definition: Random.cpp:75
Future< T > getFuture()
Definition: Promise-inl.h:97
std::enable_if< std::is_same< Unit, B >::value, void >::type setValue()
Definition: Promise.h:326
#define EXPECT_FALSE(condition)
Definition: gtest.h:1862
TEST ( Interrupt  ,
secondInterruptNoop   
)

Definition at line 66 of file InterruptTest.cpp.

References count, EXPECT_EQ, f, folly::Promise< T >::getFuture(), and folly::Promise< T >::setInterruptHandler().

66  {
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 }
void setInterruptHandler(F &&fn)
Definition: Promise-inl.h:117
auto f
#define EXPECT_EQ(val1, val2)
Definition: gtest.h:1922
Future< T > getFuture()
Definition: Promise-inl.h:97
int * count
TEST ( Interrupt  ,
futureWithinTimedOut   
)

Definition at line 76 of file InterruptTest.cpp.

References EXPECT_TRUE, folly::Promise< T >::getFuture(), folly::Baton< MayBlock, Atom >::post(), folly::Promise< T >::setInterruptHandler(), and folly::Baton< MayBlock, Atom >::try_wait_for().

76  {
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 }
void setInterruptHandler(F &&fn)
Definition: Promise-inl.h:117
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
void post() noexcept
Definition: Baton.h:123
#define EXPECT_TRUE(condition)
Definition: gtest.h:1859
TEST ( Interrupt  ,
semiFutureWithinTimedOut   
)

Definition at line 85 of file InterruptTest.cpp.

References EXPECT_TRUE, folly::Promise< T >::getSemiFuture(), folly::Baton< MayBlock, Atom >::post(), folly::Promise< T >::setInterruptHandler(), and folly::Baton< MayBlock, Atom >::try_wait_for().

85  {
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
FOLLY_ALWAYS_INLINE bool try_wait_for(const std::chrono::duration< Rep, Period > &timeout, const WaitOptions &opt=wait_options()) noexcept
Definition: Baton.h:206
void post() noexcept
Definition: Baton.h:123
#define EXPECT_TRUE(condition)
Definition: gtest.h:1859
SemiFuture< T > getSemiFuture()
Definition: Promise-inl.h:88