summaryrefslogtreecommitdiff
path: root/tests/test-Http.cc
diff options
context:
space:
mode:
authorPixel <pixel@nobis-crew.org>2012-04-06 11:00:12 -0700
committerPixel <pixel@nobis-crew.org>2012-04-06 11:00:12 -0700
commite4931a41aabe7931fe4f57835266d2a121008ce3 (patch)
treed344329f25b4a7cfad88110fe76c2e013023de8c /tests/test-Http.cc
parent692b6c7227fbcd916e5e3dbfa53d01db0319244e (diff)
Improving a bit the http unit test.
Diffstat (limited to 'tests/test-Http.cc')
-rw-r--r--tests/test-Http.cc98
1 files 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 <Main.h>
#include <HttpServer.h>
#include <TaskMan.h>
+#include <Socket.h>
#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<Handle> out) throw (GeneralException);
+ Events::Async & m_event;
+ bool & m_stop;
+};
+
+bool StopAction::Do(HttpServer * server, Http::Request & req, HttpServer::Action::ActionMatch & match, IO<Handle> out) throw (GeneralException) {
+ m_stop = true;
+ m_event.trigger();
+
+ HttpServer::Response response(server, req, out);
+ response->writeString(
+"<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Transitional//EN\"\n"
+"\"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd\">\n"
+"<html xmlns=\"http://www.w3.org/1999/xhtml\">\n"
+" <head>\n"
+" <title>Stop</title>\n"
+" </head>\n"
+"\n"
+" <body>\n"
+" Server stopping.\n"
+" </body>\n"
+"</html>\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<Handle> 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<Handle> 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<Socket> 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.");
}