From 6466c35737eff21e9b48c3ce2353d42628f4bb77 Mon Sep 17 00:00:00 2001 From: Konstantin Varlamov Date: Tue, 10 Jul 2018 17:45:16 -0400 Subject: C++ migration: add a C++ implementation of `FSTExponentialBackoff` (#1465) This is a pretty close port of `FSTExponentialBackoff`. The changes are pretty minor: * delay is calculated using duration types, not plain numbers, which should be a little more type-safe; * split a piece of code into a ClampDelay function, because it's reasonably close to std::clamp; * rephrased the class-level comment to make it clearer that the first attempt always has delay = 0; * added simple tests (other platforms don't have tests for this). Also make sure that canceling a DelayedOperation is always valid. --- .../test/firebase/firestore/util/executor_test.cc | 24 ++++++++++++++++++++++ 1 file changed, 24 insertions(+) (limited to 'Firestore/core/test/firebase/firestore/util') diff --git a/Firestore/core/test/firebase/firestore/util/executor_test.cc b/Firestore/core/test/firebase/firestore/util/executor_test.cc index 99bddce..e983bfe 100644 --- a/Firestore/core/test/firebase/firestore/util/executor_test.cc +++ b/Firestore/core/test/firebase/firestore/util/executor_test.cc @@ -115,6 +115,30 @@ TEST_P(ExecutorTest, DelayedOperationIsValidAfterTheOperationHasRun) { EXPECT_NO_THROW(delayed_operation.Cancel()); } +TEST_P(ExecutorTest, CancelingEmptyDelayedOperationIsValid) { + DelayedOperation delayed_operation; + EXPECT_NO_THROW(delayed_operation.Cancel()); +} + +TEST_P(ExecutorTest, DoubleCancelingDelayedOperationIsValid) { + std::string steps; + + executor->Execute([&] { + DelayedOperation delayed_operation = Schedule( + executor.get(), Executor::Milliseconds(1), [&steps] { steps += '1'; }); + Schedule(executor.get(), Executor::Milliseconds(5), [&] { + steps += '2'; + signal_finished(); + }); + + delayed_operation.Cancel(); + delayed_operation.Cancel(); + }); + + EXPECT_TRUE(WaitForTestToFinish()); + EXPECT_EQ(steps, "2"); +} + TEST_P(ExecutorTest, IsCurrentExecutor) { EXPECT_FALSE(executor->IsCurrentExecutor()); EXPECT_NE(executor->Name(), executor->CurrentExecutorName()); -- cgit v1.2.3