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 2013/03/29 23:41:08 UTC

svn commit: r1462666 - in /incubator/mesos/trunk/third_party/libprocess: include/process/delay.hpp include/process/dispatch.hpp include/process/event.hpp include/process/gmock.hpp src/process.cpp src/tests/process_tests.cpp

Author: benh
Date: Fri Mar 29 22:41:08 2013
New Revision: 1462666

URL: http://svn.apache.org/r1462666
Log:
Added EXPECT_DISPATCH.

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

Modified:
    incubator/mesos/trunk/third_party/libprocess/include/process/delay.hpp
    incubator/mesos/trunk/third_party/libprocess/include/process/dispatch.hpp
    incubator/mesos/trunk/third_party/libprocess/include/process/event.hpp
    incubator/mesos/trunk/third_party/libprocess/include/process/gmock.hpp
    incubator/mesos/trunk/third_party/libprocess/src/process.cpp
    incubator/mesos/trunk/third_party/libprocess/src/tests/process_tests.cpp

Modified: incubator/mesos/trunk/third_party/libprocess/include/process/delay.hpp
URL: http://svn.apache.org/viewvc/incubator/mesos/trunk/third_party/libprocess/include/process/delay.hpp?rev=1462666&r1=1462665&r2=1462666&view=diff
==============================================================================
--- incubator/mesos/trunk/third_party/libprocess/include/process/delay.hpp (original)
+++ incubator/mesos/trunk/third_party/libprocess/include/process/delay.hpp Fri Mar 29 22:41:08 2013
@@ -32,7 +32,10 @@ Timer delay(const Duration& duration,
                          thunk)));
 
   std::tr1::function<void(void)> dispatch =
-    std::tr1::bind(internal::dispatch, pid, dispatcher);
+    std::tr1::bind(internal::dispatch,
+                   pid,
+                   dispatcher,
+                   internal::canonicalize(method));
 
   return Timer::create(duration, dispatch);
 }
@@ -78,7 +81,10 @@ Timer delay(const Duration& duration,
                            thunk)));                                    \
                                                                         \
     std::tr1::function<void(void)> dispatch =                           \
-      std::tr1::bind(internal::dispatch, pid, dispatcher);              \
+      std::tr1::bind(internal::dispatch,                                \
+                     pid,                                               \
+                     dispatcher,                                        \
+                     internal::canonicalize(method));                   \
                                                                         \
     return Timer::create(duration, dispatch);                           \
   }                                                                     \

Modified: incubator/mesos/trunk/third_party/libprocess/include/process/dispatch.hpp
URL: http://svn.apache.org/viewvc/incubator/mesos/trunk/third_party/libprocess/include/process/dispatch.hpp?rev=1462666&r1=1462665&r2=1462666&view=diff
==============================================================================
--- incubator/mesos/trunk/third_party/libprocess/include/process/dispatch.hpp (original)
+++ incubator/mesos/trunk/third_party/libprocess/include/process/dispatch.hpp Fri Mar 29 22:41:08 2013
@@ -1,6 +1,8 @@
 #ifndef __PROCESS_DISPATCH_HPP__
 #define __PROCESS_DISPATCH_HPP__
 
+#include <string>
+
 #include <tr1/functional>
 #include <tr1/memory> // TODO(benh): Replace all shared_ptr with unique_ptr.
 
@@ -49,7 +51,8 @@ namespace internal {
 // will probably change in the future to unique_ptr (or a variant).
 void dispatch(
     const UPID& pid,
-    const std::tr1::shared_ptr<std::tr1::function<void(ProcessBase*)> >& f);
+    const std::tr1::shared_ptr<std::tr1::function<void(ProcessBase*)> >& f,
+    const std::string& method = std::string());
 
 // For each return type (void, future, value) there is a dispatcher
 // function which should complete the picture. Given the process
@@ -96,6 +99,15 @@ void rdispatcher(
   promise->set((*thunk)(t));
 }
 
+
+// Canonicalizes a pointer to a member function (i.e., method) into a
+// bytes representation for comparison (e.g., in tests).
+template <typename Method>
+std::string canonicalize(Method method)
+{
+  return std::string(reinterpret_cast<const char*>(&method), sizeof(method));
+}
+
 } // namespace internal {
 
 
@@ -125,7 +137,7 @@ void rdispatcher(
 //                          std::tr1::placeholders::_1,
 //                          thunk)));
 //
-//   internal::dispatch(pid, dispatcher);
+//   internal::dispatch(pid, dispatcher, internal::canonicalize(method));
 // }
 
 template <typename T>
@@ -143,7 +155,7 @@ void dispatch(
                          std::tr1::placeholders::_1,
                          thunk)));
 
-  internal::dispatch(pid, dispatcher);
+  internal::dispatch(pid, dispatcher, internal::canonicalize(method));
 }
 
 template <typename T>
@@ -183,7 +195,7 @@ void dispatch(
                            std::tr1::placeholders::_1,                  \
                            thunk)));                                    \
                                                                         \
-    internal::dispatch(pid, dispatcher);                                \
+    internal::dispatch(pid, dispatcher, internal::canonicalize(method)); \
   }                                                                     \
                                                                         \
   template <typename T,                                                 \
@@ -235,7 +247,7 @@ void dispatch(
 //                          std::tr1::placeholders::_1,
 //                          thunk, promise)));
 //
-//   internal::dispatch(pid, dispatcher);
+//   internal::dispatch(pid, dispatcher, internal::canonicalize(method));
 //
 //   return future;
 // }
@@ -258,7 +270,7 @@ Future<R> dispatch(
                          std::tr1::placeholders::_1,
                          thunk, promise)));
 
-  internal::dispatch(pid, dispatcher);
+  internal::dispatch(pid, dispatcher, internal::canonicalize(method));
 
   return future;
 }
@@ -304,7 +316,7 @@ Future<R> dispatch(
                            std::tr1::placeholders::_1,                  \
                            thunk, promise)));                           \
                                                                         \
-    internal::dispatch(pid, dispatcher);                                \
+    internal::dispatch(pid, dispatcher, internal::canonicalize(method)); \
                                                                         \
     return future;                                                      \
   }                                                                     \
@@ -360,7 +372,7 @@ Future<R> dispatch(
 //                          std::tr1::placeholders::_1,
 //                          thunk, promise)));
 //
-//   internal::dispatch(pid, dispatcher);
+//   internal::dispatch(pid, dispatcher, internal::canonicalize(method));
 //
 //   return future;
 // }
@@ -383,7 +395,7 @@ Future<R> dispatch(
                          std::tr1::placeholders::_1,
                          thunk, promise)));
 
-  internal::dispatch(pid, dispatcher);
+  internal::dispatch(pid, dispatcher, internal::canonicalize(method));
 
   return future;
 }
@@ -429,7 +441,7 @@ Future<R> dispatch(
                            std::tr1::placeholders::_1,                  \
                            thunk, promise)));                           \
                                                                         \
-    internal::dispatch(pid, dispatcher);                                \
+    internal::dispatch(pid, dispatcher, internal::canonicalize(method)); \
                                                                         \
     return future;                                                      \
   }                                                                     \

Modified: incubator/mesos/trunk/third_party/libprocess/include/process/event.hpp
URL: http://svn.apache.org/viewvc/incubator/mesos/trunk/third_party/libprocess/include/process/event.hpp?rev=1462666&r1=1462665&r2=1462666&view=diff
==============================================================================
--- incubator/mesos/trunk/third_party/libprocess/include/process/event.hpp (original)
+++ incubator/mesos/trunk/third_party/libprocess/include/process/event.hpp Fri Mar 29 22:41:08 2013
@@ -123,16 +123,33 @@ private:
 struct DispatchEvent : Event
 {
   DispatchEvent(
-      const std::tr1::shared_ptr<std::tr1::function<void(ProcessBase*)> >& _f)
-    : f(_f) {}
+      const UPID& _pid,
+      const std::tr1::shared_ptr<std::tr1::function<void(ProcessBase*)> >& _f,
+      const std::string& _method)
+    : pid(_pid),
+      f(_f),
+      method(_method)
+  {}
 
   virtual void visit(EventVisitor* visitor) const
   {
     visitor->visit(*this);
   }
 
+  // PID receiving the dispatch.
+  const UPID pid;
+
+  // Function to get invoked as a result of this dispatch event.
   const std::tr1::shared_ptr<std::tr1::function<void(ProcessBase*)> > f;
 
+  // Canonical "byte" representation of a pointer to a member function
+  // (i.e., method) encapsulated in the above function (or empty if
+  // not applicable). Note that we use a byte representation because a
+  // pointer to a member function is not actually a pointer, but
+  // instead a POD.
+  // TODO(benh): Perform canonicalization lazily.
+  const std::string method;
+
 private:
   // Not copyable, not assignable.
   DispatchEvent(const DispatchEvent&);

Modified: incubator/mesos/trunk/third_party/libprocess/include/process/gmock.hpp
URL: http://svn.apache.org/viewvc/incubator/mesos/trunk/third_party/libprocess/include/process/gmock.hpp?rev=1462666&r1=1462665&r2=1462666&view=diff
==============================================================================
--- incubator/mesos/trunk/third_party/libprocess/include/process/gmock.hpp (original)
+++ incubator/mesos/trunk/third_party/libprocess/include/process/gmock.hpp Fri Mar 29 22:41:08 2013
@@ -5,23 +5,29 @@
 
 #include <tr1/tuple>
 
+#include <process/dispatch.hpp>
 #include <process/event.hpp>
 #include <process/filter.hpp>
 #include <process/pid.hpp>
 
 #include <stout/exit.hpp>
 
-// This macro provides a mechanism for matching libprocess
-// messages. TODO(benh): Also add EXPECT_DISPATCH, EXPECT_HTTP, etc.
+
 #define EXPECT_MESSAGE(name, from, to)                                  \
   EXPECT_CALL(*process::FilterTestEventListener::instance()->install(), \
               filter(testing::A<const process::MessageEvent&>()))       \
     .With(process::MessageMatcher(name, from, to))
 
+
+#define EXPECT_DISPATCH(pid, method)                                    \
+  EXPECT_CALL(*process::FilterTestEventListener::instance()->install(), \
+              filter(testing::A<const process::DispatchEvent&>()))      \
+    .With(process::DispatchMatcher(pid, method))
+
+
 namespace process {
 
-// A gtest matcher used by EXPECT_MESSAGE for matching a libprocess
-// MessageEvent.
+// Used by EXPECT_MESSAGE for matching a MessageEvent.
 MATCHER_P3(MessageMatcher, name, from, to, "")
 {
   const MessageEvent& event = ::std::tr1::get<0>(arg);
@@ -31,6 +37,16 @@ MATCHER_P3(MessageMatcher, name, from, t
 }
 
 
+// Used by EXPECT_DISPATCH for matching a DispatchEvent.
+MATCHER_P2(DispatchMatcher, pid, method, "")
+{
+  const DispatchEvent& event = ::std::tr1::get<0>(arg);
+  return (testing::Matcher<UPID>(pid).Matches(event.pid) &&
+          testing::Matcher<std::string>(internal::canonicalize(method))
+          .Matches(event.method));
+}
+
+
 // A definition of a libprocess filter to enable waiting for events
 // (such as messages or dispatches) via WAIT_UNTIL in tests (i.e.,
 // using triggers). This is not meant to be used directly by tests;

Modified: incubator/mesos/trunk/third_party/libprocess/src/process.cpp
URL: http://svn.apache.org/viewvc/incubator/mesos/trunk/third_party/libprocess/src/process.cpp?rev=1462666&r1=1462665&r2=1462666&view=diff
==============================================================================
--- incubator/mesos/trunk/third_party/libprocess/src/process.cpp (original)
+++ incubator/mesos/trunk/third_party/libprocess/src/process.cpp Fri Mar 29 22:41:08 2013
@@ -3421,11 +3421,13 @@ namespace internal {
 
 void dispatch(
     const UPID& pid,
-    const std::tr1::shared_ptr<std::tr1::function<void(ProcessBase*)> >& f)
+    const std::tr1::shared_ptr<std::tr1::function<void(ProcessBase*)> >& f,
+    const string& method)
 {
   process::initialize();
 
-  process_manager->deliver(pid, new DispatchEvent(f), __process__);
+  DispatchEvent* event = new DispatchEvent(pid, f, method);
+  process_manager->deliver(pid, event, __process__);
 }
 
 } // namespace internal {

Modified: incubator/mesos/trunk/third_party/libprocess/src/tests/process_tests.cpp
URL: http://svn.apache.org/viewvc/incubator/mesos/trunk/third_party/libprocess/src/tests/process_tests.cpp?rev=1462666&r1=1462665&r2=1462666&view=diff
==============================================================================
--- incubator/mesos/trunk/third_party/libprocess/src/tests/process_tests.cpp (original)
+++ incubator/mesos/trunk/third_party/libprocess/src/tests/process_tests.cpp Fri Mar 29 22:41:08 2013
@@ -520,6 +520,16 @@ TEST(Process, expect)
 
   while (!func);
 
+  func = false;
+
+  EXPECT_DISPATCH(pid, &HandlersProcess::func)
+    .WillOnce(DoAll(Assign(&func, true),
+                    Return(true)));
+
+  dispatch(pid, &HandlersProcess::func, pid, "");
+
+  while (!func);
+
   terminate(pid, false);
   wait(pid);
 }