Revision: 7456
http://playerstage.svn.sourceforge.net/playerstage/?rev=7456&view=rev
Author: gerkey
Date: 2009-03-10 23:39:31 +0000 (Tue, 10 Mar 2009)
Log Message:
-----------
Reorg test to use test fixture
Modified Paths:
--------------
code/websim/test/uritest.cc
Modified: code/websim/test/uritest.cc
===================================================================
--- code/websim/test/uritest.cc 2009-03-10 19:02:49 UTC (rev 7455)
+++ code/websim/test/uritest.cc 2009-03-10 23:39:31 UTC (rev 7456)
@@ -7,90 +7,104 @@
#include <sys/queue.h>
#include <event.h>
+#include <boost/bind.hpp>
+
#include <sys/time.h>
#include <time.h>
-const char* g_host = "localhost";
-const unsigned short g_port = 8000;
-bool g_success;
-bool g_failure;
-struct timeval t0;
-std::string g_response;
-evhttp_connection* g_http_con;
-
-void Mark() { gettimeofday(&t0, NULL); }
-double
-ElapsedTime()
+class URITester : public testing::Test
{
- struct timeval t1;
- gettimeofday(&t1, NULL);
- return (t1.tv_sec + t1.tv_usec / 1e6) - (t0.tv_sec + t0.tv_usec / 1e6);
-}
+ public:
+ bool success;
+ bool failure;
+ std::string response;
+ std::string host;
+ unsigned short port;
+ struct timeval t0;
+ evhttp_connection* http_con;
-void
-Loop(double dt)
-{
- struct timespec sleeptime = {0, 1000000};
- g_success = false;
- g_failure = false;
- Mark();
- while(!g_success && !g_failure && (ElapsedTime() < dt))
- {
- event_loop(EVLOOP_NONBLOCK);
- nanosleep(&sleeptime, NULL);
- }
-}
+ void Mark() { gettimeofday(&t0, NULL); }
+ double ElapsedTime()
+ {
+ struct timeval t1;
+ gettimeofday(&t1, NULL);
+ return (t1.tv_sec + t1.tv_usec / 1e6) - (t0.tv_sec + t0.tv_usec / 1e6);
+ }
-void
-RequestCallback(evhttp_request* req, void* arg)
-{
- if(req->input_buffer->buffer)
- g_response = std::string((const char*)req->input_buffer->buffer);
- else
- g_response = "";
- if(req->response_code == 200)
- g_success = true;
- else
- g_failure = true;
-}
+ void
+ Loop(double dt)
+ {
+ struct timespec sleeptime = {0, 1000000};
+ success = false;
+ failure = false;
+ Mark();
+ while(!success && !failure && (ElapsedTime() < dt))
+ {
+ event_loop(EVLOOP_NONBLOCK);
+ nanosleep(&sleeptime, NULL);
+ }
+ }
-void
-MakeRequest(std::string uri, double dt)
-{
- struct evhttp_request* er;
- printf("Requesting: %s\n", uri.c_str());
- ASSERT_TRUE((er = evhttp_request_new(RequestCallback, NULL)));
- ASSERT_FALSE(evhttp_make_request(g_http_con, er, EVHTTP_REQ_GET,
- uri.c_str()));
- Loop(dt);
- if(g_success)
- printf("Success; response: %s\n", g_response.c_str());
- else if(g_failure)
- printf("Failure; response: %s\n", g_response.c_str());
- else
- printf("Timeout\n");
-}
+ static void
+ RequestCallback(evhttp_request* req, void* arg)
+ {
+ URITester* obj = (URITester*)arg;
+ if(req->input_buffer->buffer)
+ obj->response = std::string((const char*)req->input_buffer->buffer);
+ else
+ obj->response = "";
+ if(req->response_code == 200)
+ obj->success = true;
+ else
+ obj->failure = true;
+ }
-TEST(URITest, createGetSetGet)
+ void
+ MakeRequest(std::string uri, double dt)
+ {
+ struct evhttp_request* er;
+ printf("Requesting: %s\n", uri.c_str());
+ ASSERT_TRUE((er = evhttp_request_new(&URITester::RequestCallback,
this)));
+ ASSERT_FALSE(evhttp_make_request(http_con, er, EVHTTP_REQ_GET,
+ uri.c_str()));
+ Loop(dt);
+ if(success)
+ printf("Success; response: %s\n", response.c_str());
+ else if(failure)
+ printf("Failure; response: %s\n", response.c_str());
+ else
+ printf("Timeout\n");
+ }
+
+ protected:
+ virtual void SetUp()
+ {
+ event_init();
+ host = "localhost";
+ port = 8000;
+ }
+
+};
+
+TEST_F(URITester, createGetSetGet)
{
- ASSERT_TRUE((g_http_con = evhttp_connection_new(g_host, g_port)));
+ ASSERT_TRUE((http_con = evhttp_connection_new(host.c_str(), port)));
MakeRequest("/sim/factory/create?name=foo&type=pioneer2dx", 5.0);
- if(!g_success)
+ if(!success)
FAIL();
MakeRequest("/foo/pva/get", 5.0);
- if(!g_success)
+ if(!success)
ADD_FAILURE();
MakeRequest("/foo/pva/set?pz=2.0", 5.0);
- if(!g_success)
+ if(!success)
ADD_FAILURE();
}
int main(int argc, char **argv)
{
testing::InitGoogleTest(&argc, argv);
- event_init();
return RUN_ALL_TESTS();
}
This was sent by the SourceForge.net collaborative development platform, the
world's largest Open Source development site.
------------------------------------------------------------------------------
Apps built with the Adobe(R) Flex(R) framework and Flex Builder(TM) are
powering Web 2.0 with engaging, cross-platform capabilities. Quickly and
easily build your RIAs with Flex Builder, the Eclipse(TM)based development
software that enables intelligent coding and step-through debugging.
Download the free 60 day trial. http://p.sf.net/sfu/www-adobe-com
_______________________________________________
Playerstage-commit mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/playerstage-commit