You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@mesos.apache.org by be...@apache.org on 2017/02/04 07:46:49 UTC

[5/6] mesos git commit: Introduced process::after.

Introduced process::after.

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


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

Branch: refs/heads/master
Commit: d6fde52eb2f09e156c46b3e094f597c8cf04134a
Parents: 9316268
Author: Benjamin Hindman <be...@gmail.com>
Authored: Sun Dec 18 18:49:08 2016 -0800
Committer: Benjamin Hindman <be...@gmail.com>
Committed: Fri Feb 3 23:44:26 2017 -0800

----------------------------------------------------------------------
 3rdparty/libprocess/Makefile.am               |  1 +
 3rdparty/libprocess/include/Makefile.am       |  1 +
 3rdparty/libprocess/include/process/after.hpp | 72 ++++++++++++++++
 3rdparty/libprocess/src/tests/CMakeLists.txt  |  1 +
 3rdparty/libprocess/src/tests/after_tests.cpp | 99 ++++++++++++++++++++++
 5 files changed, 174 insertions(+)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/mesos/blob/d6fde52e/3rdparty/libprocess/Makefile.am
----------------------------------------------------------------------
diff --git a/3rdparty/libprocess/Makefile.am b/3rdparty/libprocess/Makefile.am
index 5e7fdd0..7538618 100644
--- a/3rdparty/libprocess/Makefile.am
+++ b/3rdparty/libprocess/Makefile.am
@@ -234,6 +234,7 @@ libprocess_la_LIBADD =		\
 check_PROGRAMS = libprocess-tests benchmarks
 
 libprocess_tests_SOURCES =					\
+  src/tests/after_tests.cpp					\
   src/tests/collect_tests.cpp					\
   src/tests/decoder_tests.cpp					\
   src/tests/encoder_tests.cpp					\

http://git-wip-us.apache.org/repos/asf/mesos/blob/d6fde52e/3rdparty/libprocess/include/Makefile.am
----------------------------------------------------------------------
diff --git a/3rdparty/libprocess/include/Makefile.am b/3rdparty/libprocess/include/Makefile.am
index 662c04e..ce3106a 100644
--- a/3rdparty/libprocess/include/Makefile.am
+++ b/3rdparty/libprocess/include/Makefile.am
@@ -13,6 +13,7 @@
 # Headers.
 nobase_include_HEADERS =		\
   process/address.hpp			\
+  process/after.hpp			\
   process/authenticator.hpp		\
   process/async.hpp			\
   process/check.hpp			\

http://git-wip-us.apache.org/repos/asf/mesos/blob/d6fde52e/3rdparty/libprocess/include/process/after.hpp
----------------------------------------------------------------------
diff --git a/3rdparty/libprocess/include/process/after.hpp b/3rdparty/libprocess/include/process/after.hpp
new file mode 100644
index 0000000..fa8827d
--- /dev/null
+++ b/3rdparty/libprocess/include/process/after.hpp
@@ -0,0 +1,72 @@
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+//
+//     http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License
+
+#ifndef __PROCESS_AFTER_HPP__
+#define __PROCESS_AFTER_HPP__
+
+#include <memory>
+
+#include <process/clock.hpp>
+#include <process/future.hpp>
+#include <process/timer.hpp>
+
+#include <stout/duration.hpp>
+#include <stout/nothing.hpp>
+
+namespace process {
+
+// Provides an abstraction over `Timer` and `Clock::timer` that
+// completes a future after some duration and lets you attempt to
+// discard that future.
+//
+// This can be used along with `loop` to create "waiting loops", for
+// example:
+//
+//   // Wait until a file exists, check for it ever every second.
+//   loop(None(),
+//        []() {
+//          return after(Seconds(1));
+//        },
+//        [=]() {
+//          if (os::exists(file)) -> ControlFlow<Nothing> {
+//            return Break();
+//          }
+//          return Continue();
+//        });
+inline Future<Nothing> after(const Duration& duration)
+{
+  std::shared_ptr<Promise<Nothing>> promise(new Promise<Nothing>());
+
+  Timer timer = Clock::timer(duration, [=]() {
+    promise->set(Nothing());
+  });
+
+  // Attempt to discard the promise if the future is discarded.
+  //
+  // NOTE: while the future holds a reference to the promise there is
+  // no cicular reference here because even if there are no references
+  // to the Future the timer will eventually fire and we'll set the
+  // promise which will clear the `onDiscard` callback and delete the
+  // reference to Promise.
+  promise->future()
+    .onDiscard([=]() {
+      if (Clock::cancel(timer)) {
+        promise->discard();
+      }
+    });
+
+  return promise->future();
+}
+
+} // namespace process {
+
+#endif // __PROCESS_AFTER_HPP__

http://git-wip-us.apache.org/repos/asf/mesos/blob/d6fde52e/3rdparty/libprocess/src/tests/CMakeLists.txt
----------------------------------------------------------------------
diff --git a/3rdparty/libprocess/src/tests/CMakeLists.txt b/3rdparty/libprocess/src/tests/CMakeLists.txt
index b298bbe..4dccf24 100644
--- a/3rdparty/libprocess/src/tests/CMakeLists.txt
+++ b/3rdparty/libprocess/src/tests/CMakeLists.txt
@@ -19,6 +19,7 @@
 set(PROCESS_TESTS_SRC
   ${PROCESS_TESTS_SRC}
   main.cpp
+  address_tests.cpp
   collect_tests.cpp
   decoder_tests.cpp
   encoder_tests.cpp

http://git-wip-us.apache.org/repos/asf/mesos/blob/d6fde52e/3rdparty/libprocess/src/tests/after_tests.cpp
----------------------------------------------------------------------
diff --git a/3rdparty/libprocess/src/tests/after_tests.cpp b/3rdparty/libprocess/src/tests/after_tests.cpp
new file mode 100644
index 0000000..4fd9d51
--- /dev/null
+++ b/3rdparty/libprocess/src/tests/after_tests.cpp
@@ -0,0 +1,99 @@
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+//
+//     http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License
+
+#include <gmock/gmock.h>
+
+#include <process/after.hpp>
+#include <process/future.hpp>
+#include <process/gtest.hpp>
+#include <process/loop.hpp>
+
+#include <stout/duration.hpp>
+
+using process::after;
+using process::Break;
+using process::Clock;
+using process::Continue;
+using process::ControlFlow;
+using process::Future;
+using process::loop;
+using process::Promise;
+
+
+TEST(AfterTest, After)
+{
+  AWAIT_READY(after(Milliseconds(1)));
+
+  Clock::pause();
+
+  Future<Nothing> future = after(Days(1));
+
+  EXPECT_TRUE(future.isPending());
+
+  Clock::advance(Days(1));
+
+  AWAIT_READY(future);
+
+  future = after(Days(1));
+
+  EXPECT_TRUE(future.isPending());
+
+  future.discard();
+
+  AWAIT_DISCARDED(future);
+
+  Clock::resume();
+}
+
+
+TEST(AfterTest, Loop)
+{
+  Future<Nothing> future =
+    loop(None(),
+         []() {
+           return after(Days(1));
+         },
+         [](const Nothing&) -> ControlFlow<Nothing> {
+           return Continue();
+         });
+
+  EXPECT_TRUE(future.isPending());
+
+  future.discard();
+
+  AWAIT_DISCARDED(future);
+
+  Promise<Nothing> promise1;
+  Promise<ControlFlow<Nothing>> promise2;
+
+  future =
+    loop(None(),
+         []() {
+           return after(Milliseconds(1));
+         },
+         [&](const Nothing&) {
+           promise1.set(Nothing());
+           return promise2.future();
+         });
+
+  EXPECT_TRUE(future.isPending());
+
+  AWAIT_READY(promise1.future());
+
+  promise2.future().onDiscard([&]() {
+    promise2.set(ControlFlow<Nothing>(Break()));
+  });
+
+  future.discard();
+
+  AWAIT_READY(future);
+}