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 2011/06/27 08:08:39 UTC
svn commit: r1140024 [15/15] - in /incubator/mesos/trunk: ./ ec2/
ec2/deploy.karmic64/ ec2/deploy.solaris/ frameworks/torque/nexus-hpl/
include/mesos/ src/ src/common/ src/configurator/ src/detector/
src/examples/ src/examples/java/ src/examples/python...
Modified: incubator/mesos/trunk/third_party/libprocess/src/tests.cpp
URL: http://svn.apache.org/viewvc/incubator/mesos/trunk/third_party/libprocess/src/tests.cpp?rev=1140024&r1=1140023&r2=1140024&view=diff
==============================================================================
--- incubator/mesos/trunk/third_party/libprocess/src/tests.cpp (original)
+++ incubator/mesos/trunk/third_party/libprocess/src/tests.cpp Mon Jun 27 06:08:33 2011
@@ -1,13 +1,12 @@
#include <gmock/gmock.h>
+#include <process/dispatch.hpp>
+#include <process/latch.hpp>
#include <process/process.hpp>
#include <process/run.hpp>
+#include <process/timer.hpp>
-using process::Future;
-using process::PID;
-using process::Process;
-using process::Promise;
-using process::UPID;
+using namespace process;
using testing::_;
using testing::ReturnArg;
@@ -30,11 +29,11 @@ TEST(libprocess, spawn)
EXPECT_CALL(process, __operator_call__())
.Times(1);
- PID<SpawnMockProcess> pid = process::spawn(&process);
+ PID<SpawnMockProcess> pid = spawn(&process);
ASSERT_FALSE(!pid);
- process::wait(pid);
+ wait(pid);
}
@@ -64,24 +63,24 @@ TEST(libprocess, dispatch)
EXPECT_CALL(process, func2(_))
.WillOnce(ReturnArg<0>());
- PID<DispatchMockProcess> pid = process::spawn(&process);
+ PID<DispatchMockProcess> pid = spawn(&process);
ASSERT_FALSE(!pid);
- process::dispatch(pid, &DispatchMockProcess::func0);
+ dispatch(pid, &DispatchMockProcess::func0);
Future<bool> future;
- future = process::dispatch(pid, &DispatchMockProcess::func1, true);
+ future = dispatch(pid, &DispatchMockProcess::func1, true);
EXPECT_TRUE(future.get());
- future = process::dispatch(pid, &DispatchMockProcess::func2, true);
+ future = dispatch(pid, &DispatchMockProcess::func2, true);
EXPECT_TRUE(future.get());
- process::post(pid, process::TERMINATE);
- process::wait(pid);
+ post(pid, TERMINATE);
+ wait(pid);
}
@@ -97,22 +96,22 @@ TEST(libprocess, call)
EXPECT_CALL(process, func4(_))
.WillOnce(ReturnArg<0>());
- PID<DispatchMockProcess> pid = process::spawn(&process);
+ PID<DispatchMockProcess> pid = spawn(&process);
ASSERT_FALSE(!pid);
int result;
- result = process::call(pid, &DispatchMockProcess::func3, 42);
+ result = call(pid, &DispatchMockProcess::func3, 42);
EXPECT_EQ(42, result);
- result = process::call(pid, &DispatchMockProcess::func4, 43);
+ result = call(pid, &DispatchMockProcess::func4, 43);
EXPECT_EQ(43, result);
- process::post(pid, process::TERMINATE);
- process::wait(pid);
+ post(pid, TERMINATE);
+ wait(pid);
}
@@ -137,14 +136,14 @@ TEST(libprocess, handlers)
EXPECT_CALL(process, func())
.Times(1);
- PID<HandlersMockProcess> pid = process::spawn(&process);
+ PID<HandlersMockProcess> pid = spawn(&process);
ASSERT_FALSE(!pid);
- process::post(pid, "func");
+ post(pid, "func");
- process::post(pid, process::TERMINATE);
- process::wait(pid);
+ post(pid, TERMINATE);
+ wait(pid);
}
@@ -176,22 +175,22 @@ TEST(libprocess, inheritance)
EXPECT_CALL(process, foo())
.Times(1);
- PID<DerivedMockProcess> pid1 = process::spawn(&process);
+ PID<DerivedMockProcess> pid1 = spawn(&process);
ASSERT_FALSE(!pid1);
- process::dispatch(pid1, &DerivedMockProcess::func);
+ dispatch(pid1, &DerivedMockProcess::func);
PID<BaseMockProcess> pid2(process);
PID<BaseMockProcess> pid3 = pid1;
ASSERT_EQ(pid2, pid3);
- process::dispatch(pid3, &BaseMockProcess::func);
- process::dispatch(pid3, &BaseMockProcess::foo);
+ dispatch(pid3, &BaseMockProcess::func);
+ dispatch(pid3, &BaseMockProcess::foo);
- process::post(pid1, process::TERMINATE);
- process::wait(pid1);
+ post(pid1, TERMINATE);
+ wait(pid1);
}
@@ -212,12 +211,248 @@ TEST(libprocess, thunk)
}
};
- int result = process::run(&Thunk::run, 21, 21);
+ int result = run(&Thunk::run, 21, 21).get();
EXPECT_EQ(42, result);
}
+class DelegatorProcess : public Process<DelegatorProcess>
+{
+public:
+ DelegatorProcess(const UPID& delegatee)
+ {
+ delegate("func", delegatee);
+ }
+};
+
+
+class DelegateeProcess : public Process<DelegateeProcess>
+{
+public:
+ DelegateeProcess()
+ {
+ installMessageHandler("func", &DelegateeProcess::func);
+ }
+
+ MOCK_METHOD0(func, void());
+};
+
+
+TEST(libprocess, delegate)
+{
+ ASSERT_TRUE(GTEST_IS_THREADSAFE);
+
+ DelegateeProcess delegatee;
+ DelegatorProcess delegator(delegatee.self());
+
+ EXPECT_CALL(delegatee, func())
+ .Times(1);
+
+ spawn(&delegator);
+ spawn(&delegatee);
+
+ post(delegator.self(), "func");
+
+ post(delegator.self(), TERMINATE);
+ post(delegatee.self(), TERMINATE);
+
+ wait(delegator.self());
+ wait(delegatee.self());
+}
+
+
+class TerminateProcess : public Process<TerminateProcess>
+{
+public:
+ TerminateProcess(Latch* _latch) : latch(_latch) {}
+
+protected:
+ virtual void operator () ()
+ {
+ latch->await();
+ receive();
+ EXPECT_EQ(TERMINATE, name());
+ }
+
+private:
+ Latch* latch;
+};
+
+
+TEST(libprocess, terminate)
+{
+ ASSERT_TRUE(GTEST_IS_THREADSAFE);
+
+ Latch latch;
+
+ TerminateProcess process(&latch);
+
+ spawn(&process);
+
+ post(process.self(), "one");
+ post(process.self(), "two");
+ post(process.self(), "three");
+
+ terminate(process.self());
+
+ latch.trigger();
+
+ wait(process.self());
+}
+
+
+class TimeoutProcess : public Process<TimeoutProcess>
+{
+public:
+ TimeoutProcess() {}
+ MOCK_METHOD0(timeout, void());
+};
+
+
+TEST(libprocess, DISABLED_timer)
+{
+ ASSERT_TRUE(GTEST_IS_THREADSAFE);
+
+ Clock::pause();
+
+ TimeoutProcess process;
+
+ EXPECT_CALL(process, timeout())
+ .Times(1);
+
+ spawn(&process);
+
+ double timeout = 5.0;
+
+ Timer timer =
+ delay(timeout, process.self(), &TimeoutProcess::timeout);
+
+ Clock::advance(timeout);
+
+ post(process.self(), TERMINATE);
+ wait(process.self());
+
+ Clock::resume();
+}
+
+
+TEST(libprocess, select)
+{
+ ASSERT_TRUE(GTEST_IS_THREADSAFE);
+
+ Promise<int> promise1;
+ Promise<int> promise2;
+ Promise<int> promise3;
+ Promise<int> promise4;
+
+ std::set<Future<int> > futures;
+ futures.insert(promise1.future());
+ futures.insert(promise2.future());
+ futures.insert(promise3.future());
+ futures.insert(promise4.future());
+
+ promise1.set(42);
+
+ Option<Future<int> > option = select(futures, 0);
+
+ EXPECT_TRUE(option.isSome());
+ EXPECT_TRUE(option.get().ready());
+ EXPECT_EQ(42, option.get().get());
+}
+
+
+// #define ENUMERATE1(item) item##1
+// #define ENUMERATE2(item) ENUMERATE1(item), item##2
+// #define ENUMERATE3(item) ENUMERATE2(item), item##3
+// #define ENUMERATE4(item) ENUMERATE3(item), item##4
+// #define ENUMERATE5(item) ENUMERATE4(item), item##5
+// #define ENUMERATE6(item) ENUMERATE5(item), item##6
+// #define ENUMERATE(item, n) ENUMERATE##n(item)
+
+// #define GenerateVoidDispatch(n) \
+// template <typename T, \
+// ENUM(typename P, n), \
+// ENUM(typename A, n)> \
+// void dispatch(const PID<T>& pid, \
+// void (T::*method)(ENUM(P, n)), \
+// ENUM(A, a, n)) \
+// { \
+// std::tr1::function<void(T*)> thunk = \
+// std::tr1::bind(method, std::tr1::placeholders::_1, ENUM(a, 5)); \
+// \
+// std::tr1::function<void(ProcessBase*)>* dispatcher = \
+// new std::tr1::function<void(ProcessBase*)>( \
+// std::tr1::bind(&internal::vdispatcher<T>, \
+// std::tr1::placeholders::_1, \
+// thunk)); \
+// \
+// internal::dispatch(pid, dispatcher); \
+// }
+
+// }
+
+
+TEST(libprocess, pid)
+{
+ ASSERT_TRUE(GTEST_IS_THREADSAFE);
+
+ TimeoutProcess process;
+
+ PID<TimeoutProcess> pid = process;
+
+// foo(process, &TimeoutProcess::timeout);
+ // dispatch(process, &TimeoutProcess::timeout);
+}
+
+
+class Listener1 : public Process<Listener1>
+{
+public:
+ virtual void event1() = 0;
+};
+
+
+class Listener2 : public Process<Listener2>
+{
+public:
+ virtual void event2() = 0;
+};
+
+
+class MultipleListenerProcess
+ : public Process<MultipleListenerProcess>,
+ public Listener1,
+ public Listener2
+{
+public:
+ MOCK_METHOD0(event1, void());
+ MOCK_METHOD0(event2, void());
+};
+
+
+TEST(libprocess, listener)
+{
+ ASSERT_TRUE(GTEST_IS_THREADSAFE);
+
+ MultipleListenerProcess process;
+
+ EXPECT_CALL(process, event1())
+ .Times(1);
+
+ EXPECT_CALL(process, event2())
+ .Times(1);
+
+ spawn(process);
+
+ dispatch(PID<Listener1>(process), &Listener1::event1);
+ dispatch(PID<Listener2>(process), &Listener2::event2);
+
+ terminate(process, false);
+ wait(process);
+}
+
+
int main(int argc, char** argv)
{
// Initialize Google Mock/Test.
Added: incubator/mesos/trunk/third_party/libprocess/src/timer.cpp
URL: http://svn.apache.org/viewvc/incubator/mesos/trunk/third_party/libprocess/src/timer.cpp?rev=1140024&view=auto
==============================================================================
--- incubator/mesos/trunk/third_party/libprocess/src/timer.cpp (added)
+++ incubator/mesos/trunk/third_party/libprocess/src/timer.cpp Mon Jun 27 06:08:33 2011
@@ -0,0 +1,51 @@
+#include <process/timer.hpp>
+
+namespace process {
+
+class TimerProcess : public Process<TimerProcess>
+{
+public:
+ TimerProcess(double _secs,
+ const UPID& _pid,
+ std::tr1::function<void(ProcessBase*)>* _dispatcher)
+ : secs(_secs), pid(_pid), dispatcher(_dispatcher) {}
+
+protected:
+ virtual void operator () ()
+ {
+ if (receive(secs) == TIMEOUT) {
+ internal::dispatch(pid, dispatcher);
+ } else {
+ delete dispatcher;
+ }
+ }
+
+private:
+ const double secs;
+ const UPID pid;
+ std::tr1::function<void(ProcessBase*)>* dispatcher;
+};
+
+
+Timer::Timer(double secs,
+ const UPID& pid,
+ std::tr1::function<void(ProcessBase*)>* dispatcher)
+{
+ timer = spawn(new TimerProcess(secs, pid, dispatcher), true);
+}
+
+
+Timer::~Timer()
+{
+ // NOTE: Do not terminate the timer! Some users will simply ignore
+ // saving the timer because they never want to cancel, thus
+ // we can not terminate it here!
+}
+
+
+void Timer::cancel()
+{
+ terminate(timer);
+}
+
+} // namespace process {