Sha256: d303e7dd27b05642d87162109ea95babbdce99fac0626894397f94b08508e0fb

Contents?: true

Size: 1.99 KB

Versions: 37

Compression:

Stored size: 1.99 KB

Contents

#include <TestSupport.h>
#include <boost/bind.hpp>
#include <Utils/MessagePassing.h>
#include <Utils/Timer.h>

using namespace Passenger;
using namespace std;

namespace tut {
	struct MessagePassingTest {
		MessageBoxPtr box;

		MessagePassingTest() {
			box = boost::make_shared<MessageBox>();
		}

		void sendMessagesLater() {
			syscalls::usleep(20000);
			box->send("hi");
			syscalls::usleep(20000);
			box->send("ho");
		}
	};
	
	DEFINE_TEST_GROUP(MessagePassingTest);

	TEST_METHOD(1) {
		// Sending and receiving 1 message.
		box->send("hi");
		ensure_equals(box->size(), 1u);
		ensure_equals(box->recv("hi")->name, "hi");
		ensure_equals(box->size(), 0u);
	}

	TEST_METHOD(2) {
		// Sending and receiving multiple messages out of order.
		box->send("ho");
		box->send("hi");
		box->send("ha");
		ensure_equals(box->size(), 3u);
		ensure_equals(box->recv("hi")->name, "hi");
		ensure_equals(box->size(), 2u);
		ensure_equals(box->recv("ho")->name, "ho");
		ensure_equals(box->size(), 1u);
		ensure_equals(box->recv("ha")->name, "ha");
		ensure_equals(box->size(), 0u);
	}

	TEST_METHOD(3) {
		// Receive with zero timeout.
		unsigned long long timeout = 0;
		Timer timer;
		ensure_equals(box->recv("hi", &timeout), MessagePtr());
		ensure(timer.elapsed() < 10);
		ensure_equals(timeout, 0ull);
	}

	TEST_METHOD(4) {
		// Receive with non-zero timeout.
		unsigned long long timeout = 20000;
		Timer timer;
		ensure_equals(box->recv("hi", &timeout), MessagePtr());
		ensure("(1)", timer.elapsed() >= 19);
		ensure("(2)", timer.elapsed() < 95);
		ensure("(3)", timeout >= 19000ull);
	}

	TEST_METHOD(5) {
		// Test waiting with timeout.
		TempThread thr(boost::bind(&MessagePassingTest::sendMessagesLater, this));
		unsigned long long timeout = 200000;
		Timer timer;
		ensure_equals(box->recv("ho", &timeout)->name, "ho");
		ensure(timer.elapsed() >= 39);
		ensure(timer.elapsed() < 95);
		ensure_equals(box->size(), 1u);
		ensure_equals(box->recv("hi")->name, "hi");
		ensure_equals(box->size(), 0u);
		ensure(timeout >= 140000);
	}
}

Version data entries

37 entries across 37 versions & 1 rubygems

Version Path
passenger-4.0.60 test/cxx/MessagePassingTest.cpp
passenger-5.0.4 test/cxx/MessagePassingTest.cpp
passenger-5.0.3 test/cxx/MessagePassingTest.cpp
passenger-5.0.2 test/cxx/MessagePassingTest.cpp
passenger-5.0.1 test/cxx/MessagePassingTest.cpp
passenger-5.0.0.rc2 test/cxx/MessagePassingTest.cpp
passenger-5.0.0.rc1 test/cxx/MessagePassingTest.cpp
passenger-4.0.59 test/cxx/MessagePassingTest.cpp
passenger-5.0.0.beta3 test/cxx/MessagePassingTest.cpp
passenger-4.0.58 test/cxx/MessagePassingTest.cpp
passenger-4.0.57 test/cxx/MessagePassingTest.cpp
passenger-5.0.0.beta2 test/cxx/MessagePassingTest.cpp
passenger-4.0.56 test/cxx/MessagePassingTest.cpp
passenger-4.0.55 test/cxx/MessagePassingTest.cpp
passenger-5.0.0.beta1 test/cxx/MessagePassingTest.cpp
passenger-4.0.53 test/cxx/MessagePassingTest.cpp
passenger-4.0.52 test/cxx/MessagePassingTest.cpp
passenger-4.0.51 test/cxx/MessagePassingTest.cpp
passenger-4.0.50 test/cxx/MessagePassingTest.cpp
passenger-4.0.49 test/cxx/MessagePassingTest.cpp