From e4931a41aabe7931fe4f57835266d2a121008ce3 Mon Sep 17 00:00:00 2001 From: Pixel Date: Fri, 6 Apr 2012 11:00:12 -0700 Subject: Improving a bit the http unit test. --- tests/test-Http.cc | 98 +++++++++++++++++++++++++++++++++++++++++++++++++----- 1 file changed, 90 insertions(+), 8 deletions(-) diff --git a/tests/test-Http.cc b/tests/test-Http.cc index af78e43..9d4d0e3 100644 --- a/tests/test-Http.cc +++ b/tests/test-Http.cc @@ -1,14 +1,49 @@ #include #include #include +#include #define DAEMON_NAME "Balau/1.0" using namespace Balau; +static Regex stopURL("/stop$"); + +class StopAction : public HttpServer::Action { + public: + StopAction(Events::Async & event, bool & stop) : Action(stopURL), m_event(event), m_stop(stop) { } + private: + virtual bool Do(HttpServer * server, Http::Request & req, HttpServer::Action::ActionMatch & match, IO out) throw (GeneralException); + Events::Async & m_event; + bool & m_stop; +}; + +bool StopAction::Do(HttpServer * server, Http::Request & req, HttpServer::Action::ActionMatch & match, IO out) throw (GeneralException) { + m_stop = true; + m_event.trigger(); + + HttpServer::Response response(server, req, out); + response->writeString( +"\n" +"\n" +" \n" +" Stop\n" +" \n" +"\n" +" \n" +" Server stopping.\n" +" \n" +"\n"); + + response.Flush(); + return true; +} + class TestAction : public HttpServer::Action { public: TestAction() : Action(Regexes::any) { } + private: virtual bool Do(HttpServer * server, Http::Request & req, HttpServer::Action::ActionMatch & match, IO out) throw (GeneralException); }; @@ -31,11 +66,12 @@ bool TestAction::Do(HttpServer * server, Http::Request & req, HttpServer::Action return true; } -Balau::Regex testFailureURL("^/failure.html$"); +static Regex testFailureURL("^/failure.html$"); class TestFailure : public HttpServer::Action { public: TestFailure() : Action(testFailureURL) { } + private: virtual bool Do(HttpServer * server, Http::Request & req, HttpServer::Action::ActionMatch & match, IO out) throw (GeneralException); }; @@ -43,32 +79,78 @@ bool TestFailure::Do(HttpServer * server, Http::Request & req, HttpServer::Actio throw GeneralException("Test..."); } -#define NTHREADS 4 +class Stopper : public Task { + virtual const char * getName() const { return "ServerStopper"; } + virtual void Do(); +}; + +void Stopper::Do() { + IO s(new Socket()); + bool c = s->connect("localhost", 8080); + TAssert(c); + s->writeString("GET /stop HTTP/1.0\r\n\r\n"); +} + +static const int NTHREADS = 4; void MainTask::Do() { Printer::enable(M_DEBUG); Printer::log(M_STATUS, "Test::Http running."); - Thread * tms[NTHREADS]; + TaskMan::TaskManThread * tms[NTHREADS]; for (int i = 0; i < NTHREADS; i++) tms[i] = TaskMan::createThreadedTaskMan(); + Events::Async event; + bool stop = false; + HttpServer * s = new HttpServer(); - HttpServer::Action * a = new TestAction(); - HttpServer::Action * f = new TestFailure(); - a->registerMe(s); - f->registerMe(s); + (new TestAction())->registerMe(s); + (new TestFailure())->registerMe(s); + (new StopAction(event, stop))->registerMe(s); s->setPort(8080); s->setLocal("localhost"); s->start(); + Events::Timeout timeout(1); + waitFor(&timeout); yield(); + waitFor(&event); + Task * stopper = new Stopper; + Events::TaskEvent stopperEvent(stopper); + waitFor(&stopperEvent); + TaskMan::createTask(stopper); + + bool gotEvent = false, gotStopperEvent = false; + int count = 0; + + while (!gotEvent || !gotStopperEvent) { + count++; + yield(); + if (event.gotSignal()) { + TAssert(!gotEvent); + gotEvent = true; + } + if (stopperEvent.gotSignal()) { + TAssert(!gotStopperEvent); + gotStopperEvent = true; + stopperEvent.ack(); + } + } + TAssert(count <= 2); + + TAssert(stop); + Printer::log(M_STATUS, "Test::Http is stopping."); + s->stop(); - for (int i = 0; i < NTHREADS; i++) + for (int i = 0; i < NTHREADS; i++) { + tms[i]->stopMe(); tms[i]->join(); + delete tms[i]; + } Printer::log(M_STATUS, "Test::Http passed."); } -- cgit v1.2.3