- Adding unit tests for Server

This commit is contained in:
Ian Roddis
2021-08-19 14:23:40 -03:00
parent 0a2a66bc59
commit db47bc1593
5 changed files with 150 additions and 34 deletions

View File

@@ -1,5 +1,7 @@
#pragma once
#include <filesystem>
#include <pistache/description.h>
#include <pistache/endpoint.h>
#include <pistache/http.h>
@@ -7,25 +9,28 @@
#include "loggers/dag_run/DAGRunLogger.hpp"
#include "executors/task/TaskExecutor.hpp"
namespace fs = std::filesystem;
namespace daggy {
class Server {
public:
Server(Pistache::Address addr
, loggers::dag_run::DAGRunLogger & logger
, executors::task::TaskExecutor & executor
, ThreadPool & runnerPool
)
: endpoint_(addr)
, desc_("Daggy API", "0.1")
, logger_(logger)
, executor_(executor)
, runnerPool_(runnerPool)
{}
Server(const Pistache::Address &listenSpec, loggers::dag_run::DAGRunLogger &logger,
executors::task::TaskExecutor &executor,
size_t nDAGRunners
)
: endpoint_(listenSpec), desc_("Daggy API", "0.1"), logger_(logger), executor_(executor),
runnerPool_(nDAGRunners) {}
Server &setWebHandlerThreads(size_t nThreads);
Server &setSSLCertificates(const fs::path &cert, const fs::path &key);
void init(int threads = 1);
void start();
uint16_t getPort() const;
void shutdown();
private:
@@ -39,14 +44,14 @@ namespace daggy {
void handleReady(const Pistache::Rest::Request &request, Pistache::Http::ResponseWriter response);
bool handleAuth(const Pistache::Rest::Request &request, Pistache::Http::ResponseWriter & response);
bool handleAuth(const Pistache::Rest::Request &request, Pistache::Http::ResponseWriter &response);
Pistache::Http::Endpoint endpoint_;
Pistache::Rest::Description desc_;
Pistache::Rest::Router router_;
loggers::dag_run::DAGRunLogger & logger_;
executors::task::TaskExecutor & executor_;
ThreadPool & runnerPool_;
loggers::dag_run::DAGRunLogger &logger_;
executors::task::TaskExecutor &executor_;
ThreadPool runnerPool_;
};
}

View File

@@ -3,7 +3,7 @@
#include <daggy/Serialization.hpp>
#include <daggy/Utilities.hpp>
#define REQ_ERROR(code,msg) response.send(Pistache::Http::Code::code, msg); return;
#define REQ_ERROR(code, msg) response.send(Pistache::Http::Code::code, msg); return;
namespace rj = rapidjson;
using namespace Pistache;
@@ -11,7 +11,8 @@ using namespace Pistache;
namespace daggy {
void Server::init(int threads) {
auto opts = Http::Endpoint::options()
.threads(threads);
.threads(threads)
.flags(Pistache::Tcp::Options::ReuseAddr | Pistache::Tcp::Options::ReusePort);
endpoint_.init(opts);
createDescription();
}
@@ -27,6 +28,10 @@ namespace daggy {
endpoint_.shutdown();
}
uint16_t Server::getPort() const {
return endpoint_.getPort();
}
void Server::createDescription() {
desc_
.info()
@@ -51,7 +56,7 @@ namespace daggy {
auto versionPath = desc_.path("/v1");
auto dagPath = versionPath.path("/dag");
auto dagPath = versionPath.path("/dagrun");
// Run a DAG
dagPath
@@ -82,7 +87,7 @@ namespace daggy {
* "tasks": {...}
*/
void Server::handleRunDAG(const Pistache::Rest::Request &request, Pistache::Http::ResponseWriter response) {
if (! handleAuth(request, response)) return;
if (!handleAuth(request, response)) return;
rj::Document doc;
try {
@@ -91,9 +96,9 @@ namespace daggy {
REQ_ERROR(Bad_Request, std::string{"Invalid JSON payload: "} + e.what());
}
if (! doc.IsObject()) { REQ_ERROR(Bad_Request, "Payload is not a dictionary."); }
if (! doc.HasMember("name")) { REQ_ERROR(Bad_Request, "DAG Run is missing a name."); }
if (! doc.HasMember("tasks")) { REQ_ERROR(Bad_Request, "DAG Run has no tasks."); }
if (!doc.IsObject()) { REQ_ERROR(Bad_Request, "Payload is not a dictionary."); }
if (!doc.HasMember("name")) { REQ_ERROR(Bad_Request, "DAG Run is missing a name."); }
if (!doc.HasMember("tasks")) { REQ_ERROR(Bad_Request, "DAG Run has no tasks."); }
std::string runName = doc["name"].GetString();
std::vector<Task> tasks;
@@ -119,17 +124,18 @@ namespace daggy {
auto runID = logger_.startDAGRun(runName, tasks);
auto dag = buildDAGFromTasks(tasks);
auto fut = runnerPool_.addTask([this, runID, tasks, dag]() { runDAG(runID, tasks, this->executor_, this->logger_, dag); });
auto fut = runnerPool_.addTask(
[this, runID, tasks, dag]() { runDAG(runID, tasks, this->executor_, this->logger_, dag); });
response.send(Pistache::Http::Code::Ok, R"({"runID": )" + std::to_string(runID) + "}");
}
void Server::handleGetDAGRuns(const Pistache::Rest::Request &request, Pistache::Http::ResponseWriter response) {
if (! handleAuth(request, response)) return;
if (!handleAuth(request, response)) return;
}
void Server::handleGetDAGRun(const Pistache::Rest::Request &request, Pistache::Http::ResponseWriter response) {
if (! handleAuth(request, response)) return;
if (!handleAuth(request, response)) return;
}
void Server::handleReady(const Pistache::Rest::Request &request, Pistache::Http::ResponseWriter response) {
@@ -140,8 +146,8 @@ namespace daggy {
* handleAuth will check any auth methods and handle any responses in the case of failed auth. If it returns
* false, callers should cease handling the response
*/
bool Server::handleAuth(const Pistache::Rest::Request &request, Pistache::Http::ResponseWriter & response) {
(void)response;
bool Server::handleAuth(const Pistache::Rest::Request &request, Pistache::Http::ResponseWriter &response) {
(void) response;
return true;
}
}