proxygen
TimekeeperTest.cpp File Reference

Go to the source code of this file.

Classes

struct  TimekeeperFixture
 

Functions

std::chrono::milliseconds const zero_ms (0)
 
std::chrono::milliseconds const one_ms (1)
 
std::chrono::milliseconds const awhile (10)
 
std::chrono::seconds const too_long (10)
 
std::chrono::steady_clock::time_point now ()
 
 TEST_F (TimekeeperFixture, after)
 
 TEST (Timekeeper, futureGet)
 
 TEST (Timekeeper, futureGetBeforeTimeout)
 
 TEST (Timekeeper, futureGetTimeout)
 
 TEST (Timekeeper, futureSleep)
 
 TEST (Timekeeper, futureSleepHandlesNullTimekeeperSingleton)
 
 TEST (Timekeeper, futureWithinHandlesNullTimekeeperSingleton)
 
 TEST (Timekeeper, semiFutureWithinHandlesNullTimekeeperSingleton)
 
 TEST (Timekeeper, futureDelayed)
 
 TEST (Timekeeper, semiFutureDelayed)
 
 TEST (Timekeeper, futureDelayedUnsafe)
 
 TEST (Timekeeper, futureDelayedStickyExecutor)
 
 TEST (Timekeeper, futureWithinThrows)
 
 TEST (Timekeeper, semiFutureWithinThrows)
 
 TEST (Timekeeper, futureWithinAlreadyComplete)
 
 TEST (Timekeeper, semiFutureWithinAlreadyComplete)
 
 TEST (Timekeeper, futureWithinFinishesInTime)
 
 TEST (Timekeeper, semiFutureWithinFinishesInTime)
 
 TEST (Timekeeper, futureWithinVoidSpecialization)
 
 TEST (Timekeeper, semiFutureWithinVoidSpecialization)
 
 TEST (Timekeeper, futureWithinException)
 
 TEST (Timekeeper, semiFutureWithinException)
 
 TEST (Timekeeper, onTimeout)
 
 TEST (Timekeeper, onTimeoutComplete)
 
 TEST (Timekeeper, onTimeoutReturnsFuture)
 
 TEST (Timekeeper, onTimeoutVoid)
 
 TEST (Timekeeper, interruptDoesntCrash)
 
 TEST (Timekeeper, chainedInterruptTest)
 
 TEST (Timekeeper, futureWithinChainedInterruptTest)
 
 TEST (Timekeeper, semiFutureWithinChainedInterruptTest)
 
 TEST (Timekeeper, executor)
 
 TEST_F (TimekeeperFixture, atBeforeNow)
 
 TEST_F (TimekeeperFixture, howToCastDuration)
 
 TEST_F (TimekeeperFixture, destruction)
 

Function Documentation

std::chrono::milliseconds const awhile ( 10  )

Referenced by TEST(), and TEST_F().

std::chrono::steady_clock::time_point now ( )

Definition at line 31 of file TimekeeperTest.cpp.

Referenced by folly::addBenchmark(), folly::FunctionScheduler::addFunctionToHeap(), folly::BucketedTimeSeries< VT, CT >::addValueAggregated(), folly::MultiLevelTimeSeries< VT, CT >::addValueAggregated(), folly::pushmi::any_time_executor_ref< E, TP >::any_time_executor_ref(), folly::asm_rdtsc(), folly::Timekeeper::at(), basic_test(), folly::BenchmarkSuspender::BenchmarkSuspender(), folly::detail::BufferedStat< DigestT, ClockT >::BufferedStat(), folly::EventBase::bumpHandlingTime(), TryWriteUntilCaller< Q, Clock, Duration >::callWrite(), folly::DynamicBoundedQueue< T, SingleProducer, SingleConsumer, MayBlock, LgSegmentSize, LgAlign, WeightFn, Atom >::canEnqueue(), folly::logging::IntervalRateLimiter::checkSlow(), folly::AsyncSocket::connect(), wangle::Acceptor::connectionAccepted(), folly::MultiLevelTimeSeries< VT, CT >::count(), folly::SharedMutexImpl< ReaderPriority, Tag_, Atom, BlockImmediately, AnnotateForThreadSanitizer >::WaitForDuration< Rep, Period >::deadline(), folly::BasicDynamicTokenBucket< Clock >::defaultClockNow(), deterministicAtomicWaitUntilTests(), folly::detail::BufferedStat< DigestT, ClockT >::doUpdate(), duration_to_ts(), folly::custom_stop_watch< Clock, Duration >::elapsed(), estimateQuantile(), folly::test::fc_test(), TestAsyncTransport::fireNextReadEvent(), wangle::FizzAcceptorHandshakeHelper::fizzHandshakeError(), wangle::FizzAcceptorHandshakeHelper::fizzHandshakeSuccess(), folly::detail::BufferedStat< DigestT, ClockT >::flush(), funcHasDuration(), folly::detail::MemoryIdler::futexWaitPreIdle(), fizz::sm::generateTicket(), folly::UnboundedQueue< T, false, 6 >::getAllocNextSegment(), fizz::client::test::ClientProtocolTest::getCachedPsk(), fizz::sm::getClockSkew(), proxygen::getCurrentTime(), TestTimeoutDelayed::getCurTime(), folly::HHWheelTimer::Callback::getCurTime(), folly::HHWheelTimer::getCurTime(), proxygen::getDateTimeStr(), fizz::sm::getMaxEarlyDataSize(), folly::ThreadPoolExecutor::getPoolStats(), fizz::sm::getPskExtension(), wangle::SSLSessionPersistentCacheBase< K >::getSSLSession(), folly::getTimeDelta(), testing::internal::GetTimeInMillis(), proxygen::AsyncTimeoutSet::Callback::getTimeRemaining(), folly::HHWheelTimer::Callback::getTimeRemaining(), folly::detail::MemoryIdler::getVariationTimeout(), folly::AsyncServerSocket::handlerReady(), wangle::SSLAcceptorHandshakeHelper::handshakeErr(), wangle::FizzAcceptorHandshakeHelper::handshakeErr(), wangle::SSLAcceptorHandshakeHelper::handshakeSuc(), wangle::FizzAcceptorHandshakeHelper::handshakeSuc(), intervalTest(), folly::AsyncSSLSocket::invalidState(), folly::AsyncSocket::invalidState(), folly::AsyncSocket::invokeConnectErr(), folly::AsyncSocket::invokeConnectSuccess(), folly::AsyncSSLSocket::invokeHandshakeCB(), folly::AsyncSSLSocket::invokeHandshakeErr(), folly::custom_stop_watch< Clock, Duration >::lap(), liveClockWaitUntilTests(), folly::AutoTimer< Logger, Clock >::log(), folly::AutoTimer< Logger, Clock >::logFormat(), folly::AutoTimer< Logger, Clock >::logImpl(), folly::LogMessage::LogMessage(), folly::EventBase::loopBody(), merge(), FreeDigest::merge(), mergeDigests(), proxygen::millisecondsSinceEpoch(), move_test(), multiAttemptExpectDurationWithin(), TestAsyncTransport::WriteEvent::newEvent(), wangle::SSLSessionPersistentCacheBase< K >::TimeUtil::now(), folly::ScheduledExecutor::now(), folly::chrono::coarse_steady_clock::now(), folly::pushmi::detail::submit_after_fn::fn< D, AN >::operator()(), folly::pushmi::systemNowF::operator()(), folly::pushmi::passDNF::operator()(), folly::pushmi::detail::now_fn::impl::operator()(), folly::Codel::overloaded(), folly::ParkingLot< Data >::park_for(), std::chrono::PrintTo(), producer_consumer_test(), wangle::LRUPersistentCache< K, V, MutexT >::put(), folly::TimeseriesHistogram< T, CT, C >::rate(), folly::BucketedTimeSeries< VT, CT >::rateHelper(), ReadStats::ReadStats(), folly::fibers::TimeoutController::registerTimeout(), folly::BenchmarkSuspender::rehire(), folly::TimePoint::reset(), ConcurrentFlowSingleSender::reset(), ConcurrentFlowManySender::reset(), folly::custom_stop_watch< Clock, Duration >::reset(), folly::FunctionScheduler::resetFunctionTimer(), folly::rcu_domain< Tag >::retire(), folly::FunctionScheduler::run(), run_basic_test(), folly::test::run_basic_timed_wait_tests(), run_multi_poster_multi_waiter_test(), run_once(), run_steady_clock_test(), run_system_clock_test(), folly::test::run_timed_wait_regular_test(), folly::test::run_timed_wait_tmo_tests(), runAllAndValidate(), folly::runBenchmarkGetNSPerIteration(), runElementTypeTest(), runFailingTryTimeoutTest(), runFairness(), runNeverFailUntilThread(), folly::ThreadPoolExecutor::runTask(), runTimeoutInPastTest(), folly::fibers::TimeoutController::runTimeouts(), proxygen::secondsSinceEpoch(), fizz::server::test::MockTicketCipher::setDefaults(), fizz::client::test::SynchronizedLruPskCacheTest::SetUp(), PeekingAcceptorHandshakeHelperTest::SetUp(), testing::internal::ShouldRunTestCase(), folly::SharedMutexImpl< ReaderPriority, Tag_, Atom, BlockImmediately, AnnotateForThreadSanitizer >::WaitForDuration< Rep, Period >::shouldTimeOut(), folly::SharedMutexImpl< ReaderPriority, Tag_, Atom, BlockImmediately, AnnotateForThreadSanitizer >::WaitUntilDeadline< Clock, Duration >::shouldTimeOut(), ReadStats::shouldWriterStop(), folly::detail::distributed_mutex::spin(), folly::detail::spin_pause_until(), folly::detail::spin_yield_until(), folly::AsyncSSLSocket::sslAccept(), folly::FunctionScheduler::start(), TestAsyncTransport::startReadEvents(), folly::AsyncSSLSocket::startSSLConnect(), folly::BenchmarkSuspender::tally(), folly::ThreadPoolExecutor::Task::Task(), TEST(), test(), TEST_F(), fizz::server::test::TEST_F(), testTimeout(), folly::detail::time_point_conv(), time_single_sender_test(), folly::fibers::EventBaseLoopController::timedSchedule(), folly::fibers::Baton::timedWaitThread(), timeout_test(), proxygen::AsyncTimeoutSet::timeoutExpired(), folly::TimeoutQueue::TimeoutQueue(), wangle::AcceptorHandshakeManager::timeSinceAcceptMs(), folly::pushmi::inline_time_executor_t::top(), inline_time_executor::top(), proxygen::toSystemTimePoint(), folly::UnboundedQueue< T, false, 6 >::try_dequeue_for(), MPMC< T >::try_dequeue_for(), MPMC< T >::try_enqueue_for(), folly::detail::distributed_mutex::DistributedMutex< Atomic, TimePublishing >::try_lock_for(), folly::hazptr_domain< DeterministicAtomic >::try_timed_cleanup(), folly::SaturatingSemaphore< true, Atom >::try_wait_for(), folly::Baton< MayBlock, Atom >::try_wait_for(), folly::detail::LifoSemBase< BatonType, Atom >::try_wait_for(), folly::fibers::Baton::try_wait_until(), folly::DynamicBoundedQueue< T, SingleProducer, SingleConsumer, MayBlock, LgSegmentSize, LgAlign, WeightFn, Atom >::tryEnqueueForImpl(), folly::BucketedTimeSeries< VT, CT >::update(), folly::BucketedTimeSeries< VT, CT >::updateBuckets(), fizz::sm::validatePsk(), folly::SemiFuture< T >::wait(), folly::ThreadedRepeatingFunctionRunner::waitFor(), folly::futures::detail::waitViaImpl(), folly::pushmi::time_source_shared< E, time_point >::worker(), TestAsyncTransport::writesAllowed(), folly::PriorityMPMCQueue< T, Atom, Dynamic >::writeWithPriority(), and folly::detail::BufferedStat< folly::TDigest, ClockT >::~BufferedStat().

31  {
33 }
std::chrono::steady_clock::time_point now()
std::chrono::milliseconds const one_ms ( )

Referenced by TEST().

TEST ( Timekeeper  ,
futureGet   
)

Definition at line 51 of file TimekeeperTest.cpp.

References EXPECT_EQ, folly::Promise< T >::getFuture(), folly::Promise< T >::setValue(), and folly::pushmi::detail::t.

51  {
52  Promise<int> p;
53  auto t = std::thread([&] { p.setValue(42); });
54  EXPECT_EQ(42, p.getFuture().get());
55  t.join();
56 }
#define EXPECT_EQ(val1, val2)
Definition: gtest.h:1922
Future< T > getFuture()
Definition: Promise-inl.h:97
std::enable_if< std::is_same< Unit, B >::value, void >::type setValue()
Definition: Promise.h:326
TEST ( Timekeeper  ,
futureGetBeforeTimeout   
)

Definition at line 58 of file TimekeeperTest.cpp.

References EXPECT_EQ, folly::Promise< T >::getFuture(), folly::Promise< T >::setValue(), and folly::pushmi::detail::t.

58  {
59  Promise<int> p;
60  auto t = std::thread([&] { p.setValue(42); });
61  // Technically this is a race and if the test server is REALLY overloaded
62  // and it takes more than a second to do that thread it could be flaky. But
63  // I want a low timeout (in human terms) so if this regresses and someone
64  // runs it by hand they're not sitting there forever wondering why it's
65  // blocked, and get a useful error message instead. If it does get flaky,
66  // empirically increase the timeout to the point where it's very improbable.
67  EXPECT_EQ(42, p.getFuture().get(std::chrono::seconds(2)));
68  t.join();
69 }
#define EXPECT_EQ(val1, val2)
Definition: gtest.h:1922
Future< T > getFuture()
Definition: Promise-inl.h:97
std::enable_if< std::is_same< Unit, B >::value, void >::type setValue()
Definition: Promise.h:326
TEST ( Timekeeper  ,
futureGetTimeout   
)

Definition at line 71 of file TimekeeperTest.cpp.

References EXPECT_THROW, folly::Promise< T >::getFuture(), and one_ms().

71  {
72  Promise<int> p;
74 }
#define EXPECT_THROW(statement, expected_exception)
Definition: gtest.h:1843
std::chrono::milliseconds const one_ms(1)
Future< T > getFuture()
Definition: Promise-inl.h:97
TEST ( Timekeeper  ,
futureSleep   
)

Definition at line 76 of file TimekeeperTest.cpp.

References EXPECT_GE, now(), one_ms(), and folly::futures::sleep().

76  {
77  auto t1 = now();
78  futures::sleep(one_ms).get();
79  EXPECT_GE(now() - t1, one_ms);
80 }
Future< Unit > sleep(Duration dur, Timekeeper *tk)
Definition: Future.cpp:42
std::chrono::steady_clock::time_point now()
#define EXPECT_GE(val1, val2)
Definition: gtest.h:1932
std::chrono::milliseconds const one_ms(1)
TEST ( Timekeeper  ,
futureSleepHandlesNullTimekeeperSingleton   
)

Definition at line 82 of file TimekeeperTest.cpp.

References EXPECT_THROW, folly::Singleton< T, Tag, VaultTag >::make_mock(), one_ms(), SCOPE_EXIT, and folly::futures::sleep().

82  {
83  Singleton<ThreadWheelTimekeeper>::make_mock([] { return nullptr; });
84  SCOPE_EXIT {
86  };
88 }
#define EXPECT_THROW(statement, expected_exception)
Definition: gtest.h:1843
Future< Unit > sleep(Duration dur, Timekeeper *tk)
Definition: Future.cpp:42
#define SCOPE_EXIT
Definition: ScopeGuard.h:274
std::chrono::milliseconds const one_ms(1)
TEST ( Timekeeper  ,
futureWithinHandlesNullTimekeeperSingleton   
)

Definition at line 90 of file TimekeeperTest.cpp.

References EXPECT_THROW, f, folly::Promise< T >::getFuture(), folly::Singleton< T, Tag, VaultTag >::make_mock(), folly::gen::move, one_ms(), and SCOPE_EXIT.

90  {
91  Singleton<ThreadWheelTimekeeper>::make_mock([] { return nullptr; });
92  SCOPE_EXIT {
94  };
95  Promise<int> p;
96  auto f = p.getFuture().within(one_ms);
98 }
auto f
#define EXPECT_THROW(statement, expected_exception)
Definition: gtest.h:1843
constexpr detail::Map< Move > move
Definition: Base-inl.h:2567
#define SCOPE_EXIT
Definition: ScopeGuard.h:274
std::chrono::milliseconds const one_ms(1)
Future< T > getFuture()
Definition: Promise-inl.h:97
TEST ( Timekeeper  ,
semiFutureWithinHandlesNullTimekeeperSingleton   
)

Definition at line 100 of file TimekeeperTest.cpp.

References EXPECT_THROW, f, folly::Promise< T >::getSemiFuture(), folly::Singleton< T, Tag, VaultTag >::make_mock(), folly::gen::move, one_ms(), and SCOPE_EXIT.

100  {
101  Singleton<ThreadWheelTimekeeper>::make_mock([] { return nullptr; });
102  SCOPE_EXIT {
104  };
105  Promise<int> p;
106  auto f = p.getSemiFuture().within(one_ms);
108 }
auto f
#define EXPECT_THROW(statement, expected_exception)
Definition: gtest.h:1843
constexpr detail::Map< Move > move
Definition: Base-inl.h:2567
#define SCOPE_EXIT
Definition: ScopeGuard.h:274
std::chrono::milliseconds const one_ms(1)
SemiFuture< T > getSemiFuture()
Definition: Promise-inl.h:88
TEST ( Timekeeper  ,
futureDelayed   
)

Definition at line 110 of file TimekeeperTest.cpp.

References EXPECT_GE, folly::makeFuture(), now(), and one_ms().

110  {
111  auto t1 = now();
112  auto dur = makeFuture()
113  .delayed(one_ms)
114  .thenValue([=](auto&&) { return now() - t1; })
115  .get();
116 
117  EXPECT_GE(dur, one_ms);
118 }
std::chrono::steady_clock::time_point now()
#define EXPECT_GE(val1, val2)
Definition: gtest.h:1932
std::chrono::milliseconds const one_ms(1)
Future< typename std::decay< T >::type > makeFuture(T &&t)
Definition: Future-inl.h:1310
TEST ( Timekeeper  ,
semiFutureDelayed   
)

Definition at line 120 of file TimekeeperTest.cpp.

References EXPECT_GE, folly::makeSemiFuture(), now(), and one_ms().

120  {
121  auto t1 = now();
122  auto dur = makeSemiFuture()
123  .delayed(one_ms)
124  .toUnsafeFuture()
125  .thenValue([=](auto&&) { return now() - t1; })
126  .get();
127 
128  EXPECT_GE(dur, one_ms);
129 }
std::chrono::steady_clock::time_point now()
#define EXPECT_GE(val1, val2)
Definition: gtest.h:1932
std::chrono::milliseconds const one_ms(1)
SemiFuture< typename std::decay< T >::type > makeSemiFuture(T &&t)
Definition: Future-inl.h:712
TEST ( Timekeeper  ,
futureDelayedUnsafe   
)

Definition at line 131 of file TimekeeperTest.cpp.

References EXPECT_GE, folly::makeFuture(), now(), and one_ms().

131  {
132  auto t1 = now();
133  auto dur = makeFuture()
134  .delayedUnsafe(one_ms)
135  .thenValue([=](auto&&) { return now() - t1; })
136  .get();
137 
138  EXPECT_GE(dur, one_ms);
139 }
std::chrono::steady_clock::time_point now()
#define EXPECT_GE(val1, val2)
Definition: gtest.h:1932
std::chrono::milliseconds const one_ms(1)
Future< typename std::decay< T >::type > makeFuture(T &&t)
Definition: Future-inl.h:1310
TEST ( Timekeeper  ,
futureDelayedStickyExecutor   
)

Definition at line 141 of file TimekeeperTest.cpp.

References EXPECT_EQ, EXPECT_GE, folly::makeFuture(), folly::makeSemiFuture(), now(), one_ms(), and folly::ManualExecutor::run().

141  {
142  // Check that delayed without an executor binds the inline executor.
143  {
144  auto t1 = now();
145  class TimekeeperHelper : public ThreadWheelTimekeeper {
146  public:
147  std::thread::id get_thread_id() {
148  return thread_.get_id();
149  }
150  };
151  TimekeeperHelper tk;
152  std::thread::id timekeeper_thread_id = tk.get_thread_id();
153  std::thread::id task_thread_id{};
154  auto dur = makeFuture()
155  .delayed(one_ms, &tk)
156  .thenValue([=, &task_thread_id](auto&&) {
157  task_thread_id = std::this_thread::get_id();
158  return now() - t1;
159  })
160  .get();
161 
162  EXPECT_GE(dur, one_ms);
163  EXPECT_EQ(timekeeper_thread_id, task_thread_id);
164  }
165 
166  // Check that delayed applied to an executor returns a future that binds
167  // to the same executor as was input.
168  {
169  auto t1 = now();
170  std::thread::id driver_thread_id{};
171  std::thread::id first_task_thread_id{};
172  std::thread::id second_task_thread_id{};
174  std::atomic<bool> stop_signal{false};
175  std::thread me_driver{[&me, &driver_thread_id, &stop_signal] {
176  driver_thread_id = std::this_thread::get_id();
177  while (!stop_signal) {
178  me.run();
179  }
180  }};
181  auto dur = makeSemiFuture()
182  .via(&me)
183  .thenValue([&first_task_thread_id](auto&&) {
184  first_task_thread_id = std::this_thread::get_id();
185  })
186  .delayed(one_ms)
187  .thenValue([=, &second_task_thread_id](auto&&) {
188  second_task_thread_id = std::this_thread::get_id();
189  return now() - t1;
190  })
191  .get();
192  stop_signal = true;
193  me_driver.join();
194  EXPECT_GE(dur, one_ms);
195  EXPECT_EQ(driver_thread_id, first_task_thread_id);
196  EXPECT_EQ(driver_thread_id, second_task_thread_id);
197  }
198 }
#define EXPECT_EQ(val1, val2)
Definition: gtest.h:1922
std::chrono::steady_clock::time_point now()
#define EXPECT_GE(val1, val2)
Definition: gtest.h:1932
std::chrono::milliseconds const one_ms(1)
Future< typename std::decay< T >::type > makeFuture(T &&t)
Definition: Future-inl.h:1310
SemiFuture< typename std::decay< T >::type > makeSemiFuture(T &&t)
Definition: Future-inl.h:712
TEST ( Timekeeper  ,
futureWithinThrows   
)

Definition at line 200 of file TimekeeperTest.cpp.

References EXPECT_EQ, f, folly::Promise< T >::getFuture(), folly::gen::move, and one_ms().

200  {
201  Promise<int> p;
202  auto f =
203  p.getFuture().within(one_ms).onError([](FutureTimeout&) { return -1; });
204 
205  EXPECT_EQ(-1, std::move(f).get());
206 }
auto f
#define EXPECT_EQ(val1, val2)
Definition: gtest.h:1922
constexpr detail::Map< Move > move
Definition: Base-inl.h:2567
std::chrono::milliseconds const one_ms(1)
Future< T > getFuture()
Definition: Promise-inl.h:97
TEST ( Timekeeper  ,
semiFutureWithinThrows   
)

Definition at line 208 of file TimekeeperTest.cpp.

References EXPECT_EQ, f, folly::Promise< T >::getSemiFuture(), folly::gen::move, and one_ms().

208  {
209  Promise<int> p;
210  auto f = p.getSemiFuture().within(one_ms).toUnsafeFuture().onError(
211  [](FutureTimeout&) { return -1; });
212 
213  EXPECT_EQ(-1, std::move(f).get());
214 }
auto f
#define EXPECT_EQ(val1, val2)
Definition: gtest.h:1922
constexpr detail::Map< Move > move
Definition: Base-inl.h:2567
std::chrono::milliseconds const one_ms(1)
SemiFuture< T > getSemiFuture()
Definition: Promise-inl.h:88
TEST ( Timekeeper  ,
futureWithinAlreadyComplete   
)

Definition at line 216 of file TimekeeperTest.cpp.

References EXPECT_EQ, f, folly::makeFuture(), folly::gen::move, and one_ms().

216  {
217  auto f =
218  makeFuture(42).within(one_ms).onError([&](FutureTimeout&) { return -1; });
219 
220  EXPECT_EQ(42, std::move(f).get());
221 }
auto f
#define EXPECT_EQ(val1, val2)
Definition: gtest.h:1922
constexpr detail::Map< Move > move
Definition: Base-inl.h:2567
std::chrono::milliseconds const one_ms(1)
Future< typename std::decay< T >::type > makeFuture(T &&t)
Definition: Future-inl.h:1310
TEST ( Timekeeper  ,
semiFutureWithinAlreadyComplete   
)

Definition at line 223 of file TimekeeperTest.cpp.

References EXPECT_EQ, f, folly::makeSemiFuture(), folly::gen::move, and one_ms().

223  {
224  auto f = makeSemiFuture(42).within(one_ms).toUnsafeFuture().onError(
225  [&](FutureTimeout&) { return -1; });
226 
227  EXPECT_EQ(42, std::move(f).get());
228 }
auto f
#define EXPECT_EQ(val1, val2)
Definition: gtest.h:1922
constexpr detail::Map< Move > move
Definition: Base-inl.h:2567
std::chrono::milliseconds const one_ms(1)
SemiFuture< typename std::decay< T >::type > makeSemiFuture(T &&t)
Definition: Future-inl.h:712
TEST ( Timekeeper  ,
futureWithinFinishesInTime   
)

Definition at line 230 of file TimekeeperTest.cpp.

References EXPECT_EQ, f, folly::Promise< T >::getFuture(), folly::gen::move, and folly::Promise< T >::setValue().

230  {
231  Promise<int> p;
232  auto f = p.getFuture()
233  .within(std::chrono::minutes(1))
234  .onError([&](FutureTimeout&) { return -1; });
235  p.setValue(42);
236 
237  EXPECT_EQ(42, std::move(f).get());
238 }
auto f
#define EXPECT_EQ(val1, val2)
Definition: gtest.h:1922
constexpr detail::Map< Move > move
Definition: Base-inl.h:2567
Future< T > getFuture()
Definition: Promise-inl.h:97
std::enable_if< std::is_same< Unit, B >::value, void >::type setValue()
Definition: Promise.h:326
TEST ( Timekeeper  ,
semiFutureWithinFinishesInTime   
)

Definition at line 240 of file TimekeeperTest.cpp.

References EXPECT_EQ, f, folly::Promise< T >::getSemiFuture(), folly::gen::move, and folly::Promise< T >::setValue().

240  {
241  Promise<int> p;
242  auto f = p.getSemiFuture()
243  .within(std::chrono::minutes(1))
244  .toUnsafeFuture()
245  .onError([&](FutureTimeout&) { return -1; });
246  p.setValue(42);
247 
248  EXPECT_EQ(42, std::move(f).get());
249 }
auto f
#define EXPECT_EQ(val1, val2)
Definition: gtest.h:1922
constexpr detail::Map< Move > move
Definition: Base-inl.h:2567
std::enable_if< std::is_same< Unit, B >::value, void >::type setValue()
Definition: Promise.h:326
SemiFuture< T > getSemiFuture()
Definition: Promise-inl.h:88
TEST ( Timekeeper  ,
futureWithinVoidSpecialization   
)

Definition at line 251 of file TimekeeperTest.cpp.

References folly::makeFuture(), and one_ms().

251  {
252  makeFuture().within(one_ms);
253 }
std::chrono::milliseconds const one_ms(1)
Future< typename std::decay< T >::type > makeFuture(T &&t)
Definition: Future-inl.h:1310
TEST ( Timekeeper  ,
semiFutureWithinVoidSpecialization   
)

Definition at line 255 of file TimekeeperTest.cpp.

References folly::makeSemiFuture(), and one_ms().

255  {
256  makeSemiFuture().within(one_ms);
257 }
std::chrono::milliseconds const one_ms(1)
SemiFuture< typename std::decay< T >::type > makeSemiFuture(T &&t)
Definition: Future-inl.h:712
TEST ( Timekeeper  ,
futureWithinException   
)

Definition at line 259 of file TimekeeperTest.cpp.

References awhile(), EXPECT_THROW, f, folly::Promise< T >::getFuture(), and folly::gen::move.

259  {
260  Promise<Unit> p;
261  auto f = p.getFuture().within(awhile, std::runtime_error("expected"));
262  EXPECT_THROW(std::move(f).get(), std::runtime_error);
263 }
auto f
#define EXPECT_THROW(statement, expected_exception)
Definition: gtest.h:1843
constexpr detail::Map< Move > move
Definition: Base-inl.h:2567
Future< T > getFuture()
Definition: Promise-inl.h:97
std::chrono::milliseconds const awhile(10)
TEST ( Timekeeper  ,
semiFutureWithinException   
)

Definition at line 265 of file TimekeeperTest.cpp.

References awhile(), EXPECT_THROW, f, folly::Promise< T >::getSemiFuture(), and folly::gen::move.

265  {
266  Promise<Unit> p;
267  auto f = p.getSemiFuture().within(awhile, std::runtime_error("expected"));
268  EXPECT_THROW(std::move(f).get(), std::runtime_error);
269 }
auto f
#define EXPECT_THROW(statement, expected_exception)
Definition: gtest.h:1843
constexpr detail::Map< Move > move
Definition: Base-inl.h:2567
SemiFuture< T > getSemiFuture()
Definition: Promise-inl.h:88
std::chrono::milliseconds const awhile(10)
TEST ( Timekeeper  ,
onTimeout   
)

Definition at line 271 of file TimekeeperTest.cpp.

References EXPECT_TRUE, flag, folly::makeFuture(), one_ms(), and zero_ms().

271  {
272  bool flag = false;
273  makeFuture(42)
274  .delayed(10 * one_ms)
275  .onTimeout(
276  zero_ms,
277  [&] {
278  flag = true;
279  return -1;
280  })
281  .get();
282  EXPECT_TRUE(flag);
283 }
static once_flag flag
Definition: Random.cpp:75
std::chrono::milliseconds const one_ms(1)
std::chrono::milliseconds const zero_ms(0)
#define EXPECT_TRUE(condition)
Definition: gtest.h:1859
Future< typename std::decay< T >::type > makeFuture(T &&t)
Definition: Future-inl.h:1310
TEST ( Timekeeper  ,
onTimeoutComplete   
)

Definition at line 285 of file TimekeeperTest.cpp.

References EXPECT_FALSE, flag, folly::makeFuture(), and zero_ms().

285  {
286  bool flag = false;
287  makeFuture(42)
288  .onTimeout(
289  zero_ms,
290  [&] {
291  flag = true;
292  return -1;
293  })
294  .get();
295  EXPECT_FALSE(flag);
296 }
static once_flag flag
Definition: Random.cpp:75
std::chrono::milliseconds const zero_ms(0)
#define EXPECT_FALSE(condition)
Definition: gtest.h:1862
Future< typename std::decay< T >::type > makeFuture(T &&t)
Definition: Future-inl.h:1310
TEST ( Timekeeper  ,
onTimeoutReturnsFuture   
)

Definition at line 298 of file TimekeeperTest.cpp.

References EXPECT_TRUE, flag, folly::makeFuture(), one_ms(), and zero_ms().

298  {
299  bool flag = false;
300  makeFuture(42)
301  .delayed(10 * one_ms)
302  .onTimeout(
303  zero_ms,
304  [&] {
305  flag = true;
306  return makeFuture(-1);
307  })
308  .get();
309  EXPECT_TRUE(flag);
310 }
static once_flag flag
Definition: Random.cpp:75
std::chrono::milliseconds const one_ms(1)
std::chrono::milliseconds const zero_ms(0)
#define EXPECT_TRUE(condition)
Definition: gtest.h:1859
Future< typename std::decay< T >::type > makeFuture(T &&t)
Definition: Future-inl.h:1310
TEST ( Timekeeper  ,
onTimeoutVoid   
)

Definition at line 312 of file TimekeeperTest.cpp.

References folly::makeFuture(), one_ms(), and zero_ms().

312  {
313  makeFuture().delayed(one_ms).onTimeout(zero_ms, [&] {});
314  makeFuture().delayed(one_ms).onTimeout(zero_ms, [&] {
315  return makeFuture<Unit>(std::runtime_error("expected"));
316  });
317  // just testing compilation here
318 }
std::chrono::milliseconds const one_ms(1)
std::chrono::milliseconds const zero_ms(0)
Future< typename std::decay< T >::type > makeFuture(T &&t)
Definition: Future-inl.h:1310
TEST ( Timekeeper  ,
interruptDoesntCrash   
)

Definition at line 320 of file TimekeeperTest.cpp.

References f, folly::futures::sleep(), and too_long().

320  {
321  auto f = futures::sleep(too_long);
322  f.cancel();
323 }
auto f
Future< Unit > sleep(Duration dur, Timekeeper *tk)
Definition: Future.cpp:42
std::chrono::seconds const too_long(10)
TEST ( Timekeeper  ,
chainedInterruptTest   
)

Definition at line 325 of file TimekeeperTest.cpp.

References EXPECT_FALSE, f, and folly::futures::sleep().

325  {
326  bool test = false;
327  auto f =
328  futures::sleep(milliseconds(100)).thenValue([&](auto&&) { test = true; });
329  f.cancel();
330  f.wait();
331  EXPECT_FALSE(test);
332 }
auto f
Future< Unit > sleep(Duration dur, Timekeeper *tk)
Definition: Future.cpp:42
#define EXPECT_FALSE(condition)
Definition: gtest.h:1862
TEST ( Timekeeper  ,
futureWithinChainedInterruptTest   
)

Definition at line 334 of file TimekeeperTest.cpp.

References EXPECT_FALSE, EXPECT_TRUE, f, folly::Promise< T >::getFuture(), folly::exception_wrapper::handle(), folly::Promise< T >::setException(), and folly::Promise< T >::setInterruptHandler().

334  {
335  bool test = false;
336  Promise<Unit> p;
337  p.setInterruptHandler([&test, &p](const exception_wrapper& ex) {
338  ex.handle(
339  [&test](const FutureCancellation& /* cancellation */) { test = true; });
340  p.setException(ex);
341  });
342  auto f = p.getFuture().within(milliseconds(100));
343  EXPECT_FALSE(test) << "Sanity check";
344  f.cancel();
345  f.wait();
346  EXPECT_TRUE(test);
347 }
void setInterruptHandler(F &&fn)
Definition: Promise-inl.h:117
auto f
void handle(CatchFns...fns)
void setException(exception_wrapper ew)
Definition: Promise-inl.h:111
Future< T > getFuture()
Definition: Promise-inl.h:97
#define EXPECT_TRUE(condition)
Definition: gtest.h:1859
#define EXPECT_FALSE(condition)
Definition: gtest.h:1862
TEST ( Timekeeper  ,
semiFutureWithinChainedInterruptTest   
)

Definition at line 349 of file TimekeeperTest.cpp.

References EXPECT_FALSE, EXPECT_TRUE, f, folly::Promise< T >::getSemiFuture(), folly::exception_wrapper::handle(), folly::Promise< T >::setException(), and folly::Promise< T >::setInterruptHandler().

349  {
350  bool test = false;
351  Promise<Unit> p;
352  p.setInterruptHandler([&test, &p](const exception_wrapper& ex) {
353  ex.handle(
354  [&test](const FutureCancellation& /* cancellation */) { test = true; });
355  p.setException(ex);
356  });
357  auto f = p.getSemiFuture().within(milliseconds(100));
358  EXPECT_FALSE(test) << "Sanity check";
359  f.cancel();
360  f.wait();
361  EXPECT_TRUE(test);
362 }
void setInterruptHandler(F &&fn)
Definition: Promise-inl.h:117
auto f
void handle(CatchFns...fns)
void setException(exception_wrapper ew)
Definition: Promise-inl.h:111
#define EXPECT_TRUE(condition)
Definition: gtest.h:1859
SemiFuture< T > getSemiFuture()
Definition: Promise-inl.h:88
#define EXPECT_FALSE(condition)
Definition: gtest.h:1862
TEST ( Timekeeper  ,
executor   
)

Definition at line 364 of file TimekeeperTest.cpp.

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

364  {
365  class ExecutorTester : public Executor {
366  public:
367  void add(Func f) override {
368  count++;
369  f();
370  }
371  std::atomic<int> count{0};
372  };
373 
374  Promise<Unit> p;
375  ExecutorTester tester;
376  auto f = p.getFuture().via(&tester).within(one_ms).thenValue([&](auto&&) {});
377  p.setValue();
378  f.wait();
379  EXPECT_EQ(2, tester.count);
380 }
auto f
#define EXPECT_EQ(val1, val2)
Definition: gtest.h:1922
virtual void add(Func)=0
Function< void()> Func
Definition: Executor.h:27
std::chrono::milliseconds const one_ms(1)
Future< T > getFuture()
Definition: Promise-inl.h:97
int * count
std::enable_if< std::is_same< Unit, B >::value, void >::type setValue()
Definition: Promise.h:326
TEST_F ( TimekeeperFixture  ,
after   
)

Definition at line 41 of file TimekeeperTest.cpp.

References awhile(), EXPECT_FALSE, EXPECT_GE, f, folly::gen::move, and now().

41  {
42  auto t1 = now();
43  auto f = timeLord_->after(awhile);
44  EXPECT_FALSE(f.isReady());
45  std::move(f).get();
46  auto t2 = now();
47 
48  EXPECT_GE(t2 - t1, awhile);
49 }
auto f
constexpr detail::Map< Move > move
Definition: Base-inl.h:2567
std::chrono::steady_clock::time_point now()
#define EXPECT_GE(val1, val2)
Definition: gtest.h:1932
#define EXPECT_FALSE(condition)
Definition: gtest.h:1862
std::chrono::milliseconds const awhile(10)
TEST_F ( TimekeeperFixture  ,
atBeforeNow   
)

Definition at line 395 of file TimekeeperTest.cpp.

References EXPECT_FALSE, EXPECT_TRUE, f, now(), and too_long().

395  {
396  auto f = timeLord_->at(now() - too_long);
397  EXPECT_TRUE(f.isReady());
398  EXPECT_FALSE(f.hasException());
399 }
auto f
std::chrono::steady_clock::time_point now()
std::chrono::seconds const too_long(10)
#define EXPECT_TRUE(condition)
Definition: gtest.h:1859
#define EXPECT_FALSE(condition)
Definition: gtest.h:1862
TEST_F ( TimekeeperFixture  ,
howToCastDuration   
)

Definition at line 401 of file TimekeeperTest.cpp.

References f.

401  {
402  // I'm not sure whether this rounds up or down but it's irrelevant for the
403  // purpose of this example.
404  auto f = timeLord_->after(
405  std::chrono::duration_cast<Duration>(std::chrono::nanoseconds(1)));
406 }
auto f
TEST_F ( TimekeeperFixture  ,
destruction   
)

Definition at line 408 of file TimekeeperTest.cpp.

References folly::ThreadWheelTimekeeper::after(), folly::Optional< Value >::clear(), folly::Optional< Value >::emplace(), EXPECT_FALSE, EXPECT_TRUE, and f.

408  {
410  tk.emplace();
411  auto f = tk->after(std::chrono::seconds(10));
412  EXPECT_FALSE(f.isReady());
413  tk.clear();
414  EXPECT_TRUE(f.isReady());
415  EXPECT_TRUE(f.hasException());
416 }
auto f
Value & emplace(Args &&...args)
Definition: Optional.h:231
#define EXPECT_TRUE(condition)
Definition: gtest.h:1859
#define EXPECT_FALSE(condition)
Definition: gtest.h:1862
Future< Unit > after(Duration) override
void clear() noexcept
Definition: Optional.h:251
std::chrono::seconds const too_long ( 10  )

Referenced by TEST(), and TEST_F().

std::chrono::milliseconds const zero_ms ( )

Referenced by TEST().