87 lines
2.2 KiB
C++
87 lines
2.2 KiB
C++
#include "networked_kv/application.hpp"
|
|
|
|
#include <cassert>
|
|
#include <cstdio>
|
|
|
|
namespace
|
|
{
|
|
using namespace NetworkedKv;
|
|
|
|
void config_validation()
|
|
{
|
|
NetworkedConfig bad;
|
|
bad.listen_address.clear();
|
|
assert(!bad.validate().has_value());
|
|
|
|
NetworkedConfig no_connections;
|
|
no_connections.max_connections = 0;
|
|
assert(!no_connections.validate().has_value());
|
|
|
|
NetworkedConfig ok;
|
|
assert(ok.validate().has_value());
|
|
}
|
|
|
|
void managed_mode_owns_the_host()
|
|
{
|
|
ApplicationResources resources;
|
|
resources.ownership = ResourceOwnership::Managed;
|
|
auto app = NetworkedApplication::create(resources, NetworkedConfig{});
|
|
assert(app.has_value());
|
|
|
|
// Construction opens no listener or worker.
|
|
const auto created = app->health();
|
|
assert(created.state == ApplicationState::Created);
|
|
assert(!created.ready);
|
|
|
|
assert(app->start().has_value());
|
|
const auto running = app->health();
|
|
assert(running.state == ApplicationState::Running);
|
|
assert(running.managed);
|
|
|
|
auto report = app->stop();
|
|
assert(report.has_value());
|
|
assert(app->health().state == ApplicationState::Stopped);
|
|
|
|
// Idempotent.
|
|
assert(app->stop().has_value());
|
|
}
|
|
|
|
void embedded_mode_borrows_the_host()
|
|
{
|
|
auto host = Void::Runtime::Host::create();
|
|
assert(host.has_value());
|
|
assert(host->start().has_value());
|
|
|
|
ApplicationResources resources;
|
|
resources.ownership = ResourceOwnership::Embedded;
|
|
resources.borrowed_host = &*host;
|
|
auto app = NetworkedApplication::create(resources, NetworkedConfig{});
|
|
assert(app.has_value());
|
|
assert(app->start().has_value());
|
|
assert(app->health().state == ApplicationState::Running);
|
|
|
|
assert(app->stop().has_value());
|
|
// The borrowed host is not shut down by the composition.
|
|
assert(host->is_running());
|
|
(void)host->shutdown_blocking();
|
|
}
|
|
|
|
void embedded_requires_a_host()
|
|
{
|
|
ApplicationResources resources;
|
|
resources.ownership = ResourceOwnership::Embedded;
|
|
auto app = NetworkedApplication::create(resources, NetworkedConfig{});
|
|
assert(!app.has_value());
|
|
}
|
|
} // namespace
|
|
|
|
int main()
|
|
{
|
|
config_validation();
|
|
managed_mode_owns_the_host();
|
|
embedded_mode_borrows_the_host();
|
|
embedded_requires_a_host();
|
|
std::puts("networked-kv lifecycle ok");
|
|
return 0;
|
|
}
|