You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@mesos.apache.org by bm...@apache.org on 2015/09/02 23:33:47 UTC

mesos git commit: Added a tuple overload for process::collect.

Repository: mesos
Updated Branches:
  refs/heads/master fdc4536c2 -> ca208f283


Added a tuple overload for process::collect.

Review: https://reviews.apache.org/r/38069


Project: http://git-wip-us.apache.org/repos/asf/mesos/repo
Commit: http://git-wip-us.apache.org/repos/asf/mesos/commit/ca208f28
Tree: http://git-wip-us.apache.org/repos/asf/mesos/tree/ca208f28
Diff: http://git-wip-us.apache.org/repos/asf/mesos/diff/ca208f28

Branch: refs/heads/master
Commit: ca208f2832fe193f4d34149ea6a38edb8602f725
Parents: fdc4536
Author: Benjamin Mahler <be...@gmail.com>
Authored: Wed Sep 2 13:59:52 2015 -0700
Committer: Benjamin Mahler <be...@gmail.com>
Committed: Wed Sep 2 14:08:34 2015 -0700

----------------------------------------------------------------------
 3rdparty/libprocess/include/process/collect.hpp | 27 ++++++++++
 3rdparty/libprocess/src/tests/process_tests.cpp | 52 +++++++++++++++++++-
 2 files changed, 77 insertions(+), 2 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/mesos/blob/ca208f28/3rdparty/libprocess/include/process/collect.hpp
----------------------------------------------------------------------
diff --git a/3rdparty/libprocess/include/process/collect.hpp b/3rdparty/libprocess/include/process/collect.hpp
index a1e2051..d07b686 100644
--- a/3rdparty/libprocess/include/process/collect.hpp
+++ b/3rdparty/libprocess/include/process/collect.hpp
@@ -39,6 +39,15 @@ template <typename T>
 Future<std::list<T>> collect(const std::list<Future<T>>& futures);
 
 
+// Waits on each future specified and returns the wrapping future
+// typed of a tuple of values.
+// TODO(jieyu): Investigate the use of variadic templates here.
+template <typename T1, typename T2>
+Future<std::tuple<T1, T2>> collect(
+    const Future<T1>& future1,
+    const Future<T2>& future2);
+
+
 // Waits on each future in the specified set and returns the list of
 // non-pending futures.
 template <typename T>
@@ -194,6 +203,24 @@ inline Future<std::list<T>> collect(
 }
 
 
+template <typename T1, typename T2>
+Future<std::tuple<T1, T2>> collect(
+    const Future<T1>& future1,
+    const Future<T2>& future2)
+{
+  Future<Nothing> wrapper1 = future1
+    .then([]() { return Nothing(); });
+
+  Future<Nothing> wrapper2 = future2
+    .then([]() { return Nothing(); });
+
+  std::list<Future<Nothing>> futures = { wrapper1, wrapper2 };
+
+  return collect(futures)
+    .then([=]() { return std::make_tuple(future1.get(), future2.get()); });
+}
+
+
 template <typename T>
 inline Future<std::list<Future<T>>> await(
     const std::list<Future<T>>& futures)

http://git-wip-us.apache.org/repos/asf/mesos/blob/ca208f28/3rdparty/libprocess/src/tests/process_tests.cpp
----------------------------------------------------------------------
diff --git a/3rdparty/libprocess/src/tests/process_tests.cpp b/3rdparty/libprocess/src/tests/process_tests.cpp
index debc731..435663b 100644
--- a/3rdparty/libprocess/src/tests/process_tests.cpp
+++ b/3rdparty/libprocess/src/tests/process_tests.cpp
@@ -1165,14 +1165,15 @@ TEST(ProcessTest, Select)
 }
 
 
-TEST(ProcessTest, Collect)
+TEST(ProcessTest, Collect1)
 {
   ASSERT_TRUE(GTEST_IS_THREADSAFE);
 
   // First ensure an empty list functions correctly.
   std::list<Future<int>> empty;
   Future<std::list<int>> future = collect(empty);
-  AWAIT_ASSERT_READY(future);
+
+  AWAIT_READY(future);
   EXPECT_TRUE(future.get().empty());
 
   Promise<int> promise1;
@@ -1208,6 +1209,53 @@ TEST(ProcessTest, Collect)
 }
 
 
+TEST(ProcessTest, Collect2)
+{
+  Promise<int> promise1;
+  Promise<bool> promise2;
+
+  Future<std::tuple<int, bool>> future =
+    collect(promise1.future(), promise2.future());
+
+  ASSERT_TRUE(future.isPending());
+
+  promise1.set(42);
+
+  ASSERT_TRUE(future.isPending());
+
+  promise2.set(true);
+
+  AWAIT_READY(future);
+
+  std::tuple<int, bool> values = future.get();
+
+  ASSERT_EQ(42, std::get<0>(values));
+  ASSERT_TRUE(std::get<1>(values));
+
+  // Collect should fail when a future fails.
+  Promise<bool> promise3;
+
+  future = collect(promise1.future(), promise3.future());
+
+  ASSERT_TRUE(future.isPending());
+
+  promise3.fail("failure");
+
+  AWAIT_FAILED(future);
+
+  // Collect should fail when a future is discarded.
+  Promise<bool> promise4;
+
+  future = collect(promise1.future(), promise4.future());
+
+  ASSERT_TRUE(future.isPending());
+
+  promise4.discard();
+
+  AWAIT_FAILED(future);
+}
+
+
 TEST(ProcessTest, Await1)
 {
   ASSERT_TRUE(GTEST_IS_THREADSAFE);