/*
- * Copyright 2017 Facebook, Inc.
+ * Copyright 2014-present Facebook, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* limitations under the License.
*/
-#include <folly/Baton.h>
+#include <folly/futures/Future.h>
#include <folly/Executor.h>
#include <folly/Memory.h>
#include <folly/Unit.h>
#include <folly/dynamic.h>
-#include <folly/futures/Future.h>
#include <folly/portability/GTest.h>
+#include <folly/synchronization/Baton.h>
#include <algorithm>
#include <atomic>
Future<int> onErrorHelperGeneric(const std::exception&) {
return makeFuture(20);
}
-}
+} // namespace
TEST(Future, onError) {
bool theFlag = false;
}
TEST(Future, value) {
- auto f = makeFuture(std::unique_ptr<int>(new int(42)));
+ auto f = makeFuture(std::make_unique<int>(42));
auto up = std::move(f.value());
EXPECT_EQ(42, *up);
// bulk_data, to be captured in the lambda passed to Future::then.
// This is meant to force that the lambda can't be stored inside
// the Future object.
- std::array<char, sizeof(detail::Core<int>)> bulk_data = {{0}};
+ std::array<char, sizeof(futures::detail::Core<int>)> bulk_data = {{0}};
// suppress gcc warning about bulk_data not being used
EXPECT_EQ(bulk_data[0], 0);
struct MyRequestData : RequestData {
MyRequestData(bool value = false) : value(value) {}
+
+ bool hasCallback() override {
+ return false;
+ }
+
bool value;
};
EXPECT_EQ(203, makeFuture<int>(200).then(cfoo).value());
EXPECT_EQ(303, makeFuture<int>(300).then(Foo()).value());
}
+
+TEST(Future, futureWithinCtxCleanedUpWhenTaskFinishedInTime) {
+ // Used to track the use_count of callbackInput even outside of its scope
+ std::weak_ptr<int> target;
+ {
+ Promise<std::shared_ptr<int>> promise;
+ auto input = std::make_shared<int>(1);
+ auto longEnough = std::chrono::milliseconds(1000);
+
+ promise.getFuture()
+ .within(longEnough)
+ .then([&target](
+ folly::Try<std::shared_ptr<int>>&& callbackInput) mutable {
+ target = callbackInput.value();
+ });
+ promise.setValue(input);
+ }
+ // After promise's life cycle is finished, make sure no one is holding the
+ // input anymore, in other words, ctx should have been cleaned up.
+ EXPECT_EQ(0, target.use_count());
+}
+
+TEST(Future, futureWithinNoValueReferenceWhenTimeOut) {
+ Promise<std::shared_ptr<int>> promise;
+ auto veryShort = std::chrono::milliseconds(1);
+
+ promise.getFuture().within(veryShort).then(
+ [](folly::Try<std::shared_ptr<int>>&& callbackInput) {
+ // Timeout is fired. Verify callbackInput is not referenced
+ EXPECT_EQ(0, callbackInput.value().use_count());
+ });
+}