Beispiel #1
0
static void runFiberYieldTest(TestRunner& tr)
{
   tr.group("Fiber Yield");

   tr.test("10 yielding fibers/10 iterations");
   {
      Kernel k;
      k.getEngine()->start();

      FiberScheduler fs;

      // queue up some fibers prior to starting
      for(int i = 0; i < 10; ++i)
      {
         fs.addFiber(new TestFiber(10));
      }

      uint64_t startTime = Timer::startTiming();
      fs.start(&k, 1);

      fs.waitForLastFiberExit(true);
      printf("time=%g secs... ", Timer::getSeconds(startTime));

      k.getEngine()->stop();
   }
   tr.passIfNoException();

   tr.ungroup();
}
Beispiel #2
0
static void runHttpServerTest(TestRunner& tr)
{
   tr.test("Http Server");

   // create kernel
   Kernel k;

   // set thread stack size in engine (128k)
   k.getEngine()->getThreadPool()->setThreadStackSize(131072);

   // optional for testing --
   // limit threads to 2: one for accepting, 1 for handling
   //k.getEngine()->getThreadPool()->setPoolSize(2);

   // start engine
   k.getEngine()->start();

   // create server
   Server server;
   InternetAddress address("0.0.0.0", 19100);

   // create SSL/generic http connection servicer
   HttpConnectionServicer hcs;
//   SslContext context;
//   SslSocketDataPresenter presenter1(&context);
//   NullSocketDataPresenter presenter2;
//   SocketDataPresenterList list(false);
//   list.add(&presenter1);
//   list.add(&presenter2);
   server.addConnectionService(&address, &hcs);//, &list);

   // create test http request servicer
   TestHttpRequestServicer test1("/test");
   hcs.addRequestServicer(&test1, false);

   uint64_t seconds = 30;

   if(server.start(&k))
   {
      printf("\nServer started on %s and will run for %" PRIu64 " seconds.\n",
         address.toString(false).c_str(), seconds);
   }
   else if(Exception::get() != NULL)
   {
      printf("\nServer start failed with errors=%s\n",
         Exception::get()->getMessage());
   }

   // sleep
   Thread::sleep(seconds * 1000);

   server.stop();
   printf("Server stopped.\n");

   // stop kernel engine
   k.getEngine()->stop();

   tr.passIfNoException();
}
Beispiel #3
0
static void runHttpClientRedirectLoopTest(TestRunner& tr)
{
   tr.test("Http Client Redirect Loop");

   // start a kernel
   Kernel k;
   k.getEngine()->start();

   // create server
   Server server;
   InternetAddress address("0.0.0.0", 19123);

   // create SSL/generic http connection servicer
   HttpConnectionServicer hcs;
   server.addConnectionService(&address, &hcs);

   // create redirect loop http request servicer
   RedirectLoopHttpRequestServicer loop("/loop");
   hcs.addRequestServicer(&loop, false);

   if(server.start(&k))
   {
      printf("\nServer started on %s\n",
         address.toString(false).c_str());

      // create client
      HttpClient client;

      printf("Connecting and expecting a redirect loop...\n");

      // do get
      Url url("http://localhost:19123/loop");
      HttpResponse* response = client.get(&url, NULL, 2);
      if(response != NULL)
      {
         printf("Response=\n%s\n", response->getHeader()->toString().c_str());
      }
      else
      {
         printf("Correctly detected an exception:\n");
         printf("'%s'\n", Exception::get()->getMessage());
      }
      client.disconnect();
   }
   else if(Exception::get() != NULL)
   {
      printf("\nServer start failed with errors=%s\n",
         Exception::get()->getMessage());
   }

   tr.passIfException();

   // stop server and kernel
   server.stop();
   k.getEngine()->stop();
   Exception::clear();
}
Beispiel #4
0
static void runPingTest(TestRunner& tr)
{
   tr.test("Ping");

   Config cfg = tr.getApp()->getConfig()["pong"];

   // Stats and control
   PingPong pingPong(cfg);

   // create kernel
   Kernel k;

   // set thread stack size in engine (128k)
   k.getEngine()->getThreadPool()->setThreadStackSize(
      cfg["threadStackSize"]->getUInt32());

   // optional for testing --
   // limit threads to 2: one for accepting, 1 for handling
   //k.getEngine()->getThreadPool()->setPoolSize(2);
   k.getEngine()->getThreadPool()->setPoolSize(cfg["threads"]->getUInt32());

   // start engine
   k.getEngine()->start();

   // create server
   Server server;
   server.setMaxConnectionCount(cfg["maxConnections"]->getInt32());
   InternetAddress address("0.0.0.0", cfg["port"]->getUInt32());

   // create SSL/generic http connection servicer
   HttpConnectionServicer hcs;
//   SslContext context;
//   SslSocketDataPresenter presenter1(&context);
//   NullSocketDataPresenter presenter2;
//   SocketDataPresenterList list(false);
//   list.add(&presenter1);
//   list.add(&presenter2);
   //server.addConnectionService(&address, &hcs);//, &list);
   server.addConnectionService(
      &address, &hcs, NULL, "pong",
      cfg["maxConnections"]->getInt32(),
      cfg["backlog"]->getInt32());

   // create test http request servicer
   NoContentServicer noContentSrv(&pingPong, "/");
   hcs.addRequestServicer(&noContentSrv, false);

   PingServicer ping(&pingPong, "/pong");
   hcs.addRequestServicer(&ping, false);

   const int bufsize = 4096;
   char buf[bufsize];
   memset(buf, '.', bufsize);
   DataServicer data(&pingPong, "/data", buf, bufsize);
   hcs.addRequestServicer(&data, false);

   StatsServicer stats(&pingPong, "/stats");
   hcs.addRequestServicer(&stats, false);

   ResetServicer reset(&pingPong, "/reset");
   hcs.addRequestServicer(&reset, false);

   QuitServicer quit(&pingPong, "/quit");
   hcs.addRequestServicer(&quit, false);

   if(server.start(&k))
   {
      uint64_t num = cfg["num"]->getUInt64();
      MO_INFO("Server started.");
      if(num == 0)
      {
         MO_INFO("Servicing forever. CTRL-C to quit.");
      }
      {
         MO_INFO("Servicing approximately %" PRIu64 " connections.", num);
      }
   }
   else if(Exception::get() != NULL)
   {
      MO_ERROR("Server started with errors=%s\n",
         Exception::get()->getMessage());
   }

   // start timing
   pingPong.reset();

   // either serve for limited time, or wait for lock
   uint32_t time = cfg["time"]->getUInt32();
   if(time != 0)
   {
      Thread::sleep(time);
   }
   else
   {
      pingPong.getLock().wait();
   }

   server.stop();
   MO_INFO("Server stopped.");

   // stop kernel engine
   k.getEngine()->stop();

   tr.passIfNoException();
}
Beispiel #5
0
static void runPingTest(TestRunner& tr)
{
   tr.test("Ping");

   // create kernel
   Kernel k;
   k.getEngine()->start();

   // create server
   Server server;
   InternetAddress address("localhost", 19100);

//   // create SSL/generic ping connection servicer
//   PingConnectionServicer pcs;
////   SslContext context;
////   SslSocketDataPresenter presenter1(&context);
////   NullSocketDataPresenter presenter2;
////   SocketDataPresenterList list(false);
////   list.add(&presenter1);
////   list.add(&presenter2);
//   server.addConnectionService(&address, &pcs);//, &list);

   // create SSL/generic http connection servicer
   HttpConnectionServicer hcs;
//   SslContext context;
//   SslSocketDataPresenter presenter1(&context);
//   NullSocketDataPresenter presenter2;
//   SocketDataPresenterList list(false);
//   list.add(&presenter1);
//   list.add(&presenter2);
   server.addConnectionService(&address, &hcs);//, &list);

   // create test http request servicer
   PingHttpRequestServicer test1("/test");
   hcs.addRequestServicer(&test1, false);

   if(server.start(&k))
   {
      printf("Server started.\n");
   }
   else if(Exception::get() != NULL)
   {
      printf("Server started with errors=%s\n",
         Exception::get()->getMessage());
   }

   // connect
   Url url("http://localhost:19100");
   HttpTrailer trailer;
   File file("/tmp/index.html");
   FileOutputStream fos(file);
   HttpClient client;

   uint64_t start = System::getCurrentMilliseconds();

   client.connect(&url);
   client.get(&url, NULL);
   client.receiveContent(&fos, &trailer);

   uint64_t end = System::getCurrentMilliseconds();

   client.disconnect();

   // sleep
   //Thread::sleep(10000);

   server.stop();
   printf("Server stopped.\n");

   // stop kernel engine
   k.getEngine()->stop();

   uint64_t millis = end - start;
   printf("Connection Time: %" PRIu64 "\n", millis);

//   uint64_t millis = test1.end - test1.start;
//   double cps = ((double)pcs.serviced) / millis * 1000.0;
//   printf("Connections serviced: %d\n", pcs.serviced);
//   printf("Time: %" PRIu64 "\n", millis);
//   printf("Connections/Second: %f\n", cps);

   tr.passIfNoException();
}
Beispiel #6
0
static void runWebServerTest(TestRunner& tr)
{
   const char* path = "/test";
   const char* content = "web server test";
   const char* regexPath = "/test/dumplings/regextest/turkey";
   const char* regexPath2 = "/test/dumplings/regextest2/turkey";
   const char* regexPath3 = "/test/dumplings/regextest3/turkey";
   const char* regexContent = "web server test (regex)";

   // create kernel
   Kernel k;

   // set thread stack size in engine (128k)
   k.getEngine()->getThreadPool()->setThreadStackSize(131072);

   // optional for testing --
   // limit threads to 2: one for accepting, 1 for handling
   //k.getEngine()->getThreadPool()->setPoolSize(2);

   // start engine
   k.getEngine()->start();

   // create server
   Server server;

   WebServer ws;
   Config cfg;
   cfg["host"] = "localhost";
   cfg["port"] = 0;
   cfg["security"] = "off";
   WebServiceContainerRef wsc = new WebServiceContainer();
   ws.setContainer(wsc);
   ws.initialize(cfg);
   WebServiceRef tws = new TestWebService(path, content, regexContent);
   wsc->addService(tws, WebService::Both);
   ws.enable(&server);

   // start server
   assertNoException(server.start(&k));

   // get server port
   int port = ws.getHostAddress()->getPort();

   // check the regular path and data
   tr.test("WebServer - regular path handler");
   {
      Url url;
      url.format("http://%s:%d%s", cfg["host"]->getString(), port, path);
      _checkUrlText(tr, &url, 200, content, strlen(content));
   }
   tr.passIfNoException();

   // check the regex path and data
   tr.test("WebServer - regex path handler");
   {
      Url url;
      url.format("http://%s:%d%s", cfg["host"]->getString(), port, regexPath);
      _checkUrlText(tr, &url, 200, regexContent, strlen(regexContent));
   }
   tr.passIfNoException();

   // check the regex path and data
   tr.test("WebServer - regex path handler matches");
   {
      DynamicObject info;
      info["type"] = "monarch.ws.RestfulHandler";
      DynamicObject& matches = info["matches"];
      matches[0] = "dumplings";
      matches[1] = "turkey";
      string expect = JsonWriter::writeToString(info);

      Url url;
      url.format("http://%s:%d%s", cfg["host"]->getString(), port, regexPath2);
      _checkUrlText(tr, &url, 200, expect.c_str(), expect.length());
   }
   tr.passIfNoException();

   // check the web service authentication exception
   tr.test("WebServer - authentication exception");
   {
      DynamicObject ex;
      ex["message"] = "WebService authentication failed. Access denied.";
      ex["type"] = "monarch.ws.AccessDenied";
      ex["details"]["httpStatusCode"] = 403;
      ex["details"]["path"] = "/test/dumplings/regextest3/turkey";
      ex["details"]["public"] = true;
      DynamicObject& cause = ex["cause"];
      cause["message"] = "Tried to authenticate but failed.";
      cause["type"] = "tests.ws.Exception";
      string expect = JsonWriter::writeToString(ex, true);

      Url url;
      url.format("http://%s:%d%s", cfg["host"]->getString(), port, regexPath3);
      _checkUrlText(tr, &url, 400, expect.c_str(), expect.length());
   }
   tr.passIfNoException();

   server.stop();

   // stop kernel engine
   k.getEngine()->stop();
}