-
-
Notifications
You must be signed in to change notification settings - Fork 12
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
250 additions
and
23 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
#ifdef ICHOR_USE_BOOST_BEAST | ||
|
||
#include <ichor/event_queues/MultimapQueue.h> | ||
#include <ichor/events/RunFunctionEvent.h> | ||
#include <ichor/coroutines/AsyncManualResetEvent.h> | ||
#include <ichor/services/logging/LoggerAdmin.h> | ||
#include <ichor/services/network/http/HttpHostService.h> | ||
#include <ichor/services/network/http/HttpConnectionService.h> | ||
#include <ichor/services/network/http/HttpContextService.h> | ||
#include <ichor/services/network/ClientAdmin.h> | ||
#include <ichor/services/serialization/SerializationAdmin.h> | ||
#include <ichor/services/logging/CoutFrameworkLogger.h> | ||
#include <ichor/services/logging/CoutLogger.h> | ||
#include "Common.h" | ||
#include "TestServices/HttpThreadService.h" | ||
#include "../examples/common/TestMsgJsonSerializer.h" | ||
|
||
using namespace Ichor; | ||
|
||
std::unique_ptr<Ichor::AsyncManualResetEvent> _evt; | ||
std::thread::id testThreadId; | ||
std::thread::id dmThreadId; | ||
bool evtGate; | ||
|
||
TEST_CASE("HttpTests") { | ||
SECTION("Http events on same thread") { | ||
testThreadId = std::this_thread::get_id(); | ||
_evt = std::make_unique<Ichor::AsyncManualResetEvent>(); | ||
auto queue = std::make_unique<MultimapQueue>(); | ||
auto &dm = queue->createManager(); | ||
evtGate = false; | ||
|
||
std::thread t([&]() { | ||
dmThreadId = std::this_thread::get_id(); | ||
|
||
dm.createServiceManager<CoutFrameworkLogger, IFrameworkLogger>({}, 10); | ||
dm.createServiceManager<LoggerAdmin<CoutLogger>, ILoggerAdmin>(); | ||
dm.createServiceManager<SerializationAdmin, ISerializationAdmin>(); | ||
dm.createServiceManager<TestMsgJsonSerializer, ISerializer>(); | ||
dm.createServiceManager<HttpContextService, IHttpContextService>(); | ||
dm.createServiceManager<HttpHostService, IHttpService>(Properties{{"Address", Ichor::make_any<std::string>("127.0.0.1")}, {"Port", Ichor::make_any<uint16_t>(8001)}}); | ||
dm.createServiceManager<ClientAdmin<HttpConnectionService, IHttpConnectionService>, IClientAdmin>(); | ||
dm.createServiceManager<HttpThreadService>(Properties{{"Address", Ichor::make_any<std::string>("127.0.0.1")}, {"Port", Ichor::make_any<uint16_t>(8001)}}); | ||
|
||
queue->start(CaptureSigInt); | ||
}); | ||
|
||
while(!evtGate) { | ||
std::this_thread::sleep_for(1ms); | ||
} | ||
|
||
dm.pushEvent<RunFunctionEvent>(0, [&](DependencyManager &_dm) -> AsyncGenerator<void> { | ||
REQUIRE(Ichor::Detail::_local_dm == &_dm); | ||
REQUIRE(Ichor::Detail::_local_dm == &dm); | ||
REQUIRE(testThreadId != std::this_thread::get_id()); | ||
REQUIRE(dmThreadId == std::this_thread::get_id()); | ||
_evt->set(); | ||
co_return; | ||
}); | ||
|
||
t.join(); | ||
} | ||
} | ||
|
||
#endif |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,126 @@ | ||
#pragma once | ||
|
||
#include <ichor/DependencyManager.h> | ||
#include <ichor/services/logging/Logger.h> | ||
#include <ichor/services/timer/TimerService.h> | ||
#include <ichor/services/network/NetworkEvents.h> | ||
#include <ichor/services/network/http/IHttpConnectionService.h> | ||
#include <ichor/services/network/http/IHttpService.h> | ||
#include <ichor/events/RunFunctionEvent.h> | ||
#include <ichor/Service.h> | ||
#include <ichor/LifecycleManager.h> | ||
#include <ichor/services/serialization/ISerializationAdmin.h> | ||
#include "../examples/common/TestMsg.h" | ||
|
||
using namespace Ichor; | ||
|
||
extern std::unique_ptr<Ichor::AsyncManualResetEvent> _evt; | ||
extern bool evtGate; | ||
extern std::thread::id testThreadId; | ||
extern std::thread::id dmThreadId; | ||
|
||
class HttpThreadService final : public Service<HttpThreadService> { | ||
public: | ||
HttpThreadService(DependencyRegister ®, Properties props, DependencyManager *mng) : Service(std::move(props), mng) { | ||
reg.registerDependency<ISerializationAdmin>(this, true); | ||
reg.registerDependency<IHttpConnectionService>(this, true, getProperties()); | ||
reg.registerDependency<IHttpService>(this, true); | ||
} | ||
~HttpThreadService() final = default; | ||
|
||
private: | ||
StartBehaviour start() final { | ||
getManager().pushEvent<RunFunctionEvent>(getServiceId(), [this](DependencyManager &dm) -> AsyncGenerator<void> { | ||
auto toSendMsg = _serializationAdmin->serialize(TestMsg{11, "hello"}); | ||
|
||
if(dmThreadId != std::this_thread::get_id()) { | ||
throw std::runtime_error("dmThreadId id incorrect"); | ||
} | ||
if(testThreadId == std::this_thread::get_id()) { | ||
throw std::runtime_error("testThreadId id incorrect"); | ||
} | ||
|
||
co_await sendTestRequest(std::move(toSendMsg)).begin(); | ||
|
||
if(dmThreadId != std::this_thread::get_id()) { | ||
throw std::runtime_error("dmThreadId id incorrect"); | ||
} | ||
if(testThreadId == std::this_thread::get_id()) { | ||
throw std::runtime_error("testThreadId id incorrect"); | ||
} | ||
|
||
co_return; | ||
}); | ||
|
||
return StartBehaviour::SUCCEEDED; | ||
} | ||
|
||
StartBehaviour stop() final { | ||
_routeRegistration.reset(); | ||
return StartBehaviour::SUCCEEDED; | ||
} | ||
|
||
void addDependencyInstance(ISerializationAdmin *serializationAdmin, IService *) { | ||
_serializationAdmin = serializationAdmin; | ||
} | ||
|
||
void removeDependencyInstance(ISerializationAdmin *serializationAdmin, IService *) { | ||
_serializationAdmin = nullptr; | ||
} | ||
|
||
void addDependencyInstance(IHttpConnectionService *connectionService, IService *) { | ||
_connectionService = connectionService; | ||
} | ||
|
||
void addDependencyInstance(IHttpService *svc, IService *) { | ||
_routeRegistration = svc->addRoute(HttpMethod::post, "/test", [this](HttpRequest &req) -> AsyncGenerator<HttpResponse> { | ||
if(dmThreadId != std::this_thread::get_id()) { | ||
throw std::runtime_error("dmThreadId id incorrect"); | ||
} | ||
if(testThreadId == std::this_thread::get_id()) { | ||
throw std::runtime_error("testThreadId id incorrect"); | ||
} | ||
|
||
auto msg = _serializationAdmin->deserialize<TestMsg>(std::move(req.body)); | ||
evtGate = true; | ||
|
||
co_await *_evt; | ||
|
||
if(dmThreadId != std::this_thread::get_id()) { | ||
throw std::runtime_error("dmThreadId id incorrect"); | ||
} | ||
if(testThreadId == std::this_thread::get_id()) { | ||
throw std::runtime_error("testThreadId id incorrect"); | ||
} | ||
|
||
co_return HttpResponse{false, HttpStatus::ok, _serializationAdmin->serialize(TestMsg{11, "hello"}), {}}; | ||
}); | ||
} | ||
|
||
void removeDependencyInstance(IHttpService *, IService *) { | ||
_routeRegistration.reset(); | ||
} | ||
|
||
void removeDependencyInstance(IHttpConnectionService *connectionService, IService *) { | ||
} | ||
|
||
friend DependencyRegister; | ||
friend DependencyManager; | ||
|
||
AsyncGenerator<void> sendTestRequest(std::vector<uint8_t> &&toSendMsg) { | ||
auto &response = *co_await _connectionService->sendAsync(HttpMethod::post, "/test", {}, std::move(toSendMsg)).begin(); | ||
|
||
if(response.status == HttpStatus::ok) { | ||
auto msg = _serializationAdmin->deserialize<TestMsg>(response.body); | ||
} else { | ||
throw std::runtime_error("Status not ok"); | ||
} | ||
getManager().pushEvent<QuitEvent>(getServiceId()); | ||
|
||
co_return; | ||
} | ||
|
||
ISerializationAdmin *_serializationAdmin{nullptr}; | ||
IHttpConnectionService *_connectionService{nullptr}; | ||
std::unique_ptr<HttpRouteRegistration> _routeRegistration{nullptr}; | ||
}; |