Files
SEQUA-Engine/core/tests/server/test_runtime.cpp
T
Ange-Marie MAURIN - A2M Systèmes a185b3414a feat(core): protocole Studio - Core 0.2, session en direct, bail et sécurité
CoreServices / CoreRuntime / LiveSession (conception reprise d'un subagent interrompu, implémentation complétée) : session à 60 Hz avec entrée maintenue, export de l'enregistrement et des empreintes par tick, import par rejeu (sim::rebuild_session) puis poursuite dans le même enregistrement ; bail réversible (--linked, core/attach, core/detach, délai de grâce, code de sortie 3) ; core/shutdown ; jeton en temps constant et contrôle de l'Origin (origines ws:// des clients natifs acceptées, spec mise à jour). Base64 strict corrigé (rejetait des entrées valides). 56 cas de test, ASan vert sur les tests serveur. build.ps1 : -Target accepte a,b.
2026-11-06 23:00:53 +01:00

123 lines
3.8 KiB
C++

// SPDX-License-Identifier: MPL-2.0
#include "sequa/core/server/runtime.hpp"
#include <doctest/doctest.h>
#include <chrono>
#include <thread>
using namespace sequa::core::server;
using namespace std::chrono_literals;
using Reason = CoreRuntime::ExitReason;
namespace {
CoreRuntimeOptions fast(bool linked)
{
CoreRuntimeOptions options;
options.linked = linked;
options.real_time = false;
options.lease_grace = 50ms;
options.attach_deadline = 100ms;
options.shutdown_delay = 20ms;
options.core_version = "test";
return options;
}
void wait_past(std::chrono::milliseconds duration)
{
std::this_thread::sleep_for(duration + 30ms);
}
} // namespace
TEST_CASE("Runtime : instanceId aleatoire de 32 hex, pid renseigne")
{
const CoreRuntime a(fast(false));
const CoreRuntime b(fast(false));
const auto id = a.core_status().instance_id;
CHECK(id.size() == 32);
CHECK(id.find_first_not_of("0123456789abcdef") == std::string::npos);
CHECK(id != b.core_status().instance_id);
CHECK(a.core_status().pid > 0);
}
TEST_CASE("Runtime : sans --linked, pas de bail et pas d'arret")
{
CoreRuntime runtime(fast(false));
CHECK_FALSE(runtime.core_status().linked);
wait_past(100ms);
CHECK(runtime.exit_reason() == Reason::none);
}
TEST_CASE("Runtime : --linked sans controleur -> bail expire")
{
CoreRuntime runtime(fast(true));
CHECK(runtime.core_status().linked);
CHECK(runtime.exit_reason() == Reason::none);
wait_past(100ms);
CHECK(runtime.exit_reason() == Reason::lease_expired);
}
TEST_CASE("Runtime : perte du controleur -> arret apres le delai de grace, sauf rattachement")
{
CoreRuntime runtime(fast(true));
runtime.on_client_connected();
REQUIRE(runtime.attach(1, false) == CoreServices::AttachResult::attached);
wait_past(100ms); // le délai de démarrage ne s'applique plus
CHECK(runtime.exit_reason() == Reason::none);
SUBCASE("personne ne revient")
{
runtime.on_client_disconnected(1);
CHECK(runtime.exit_reason() == Reason::none);
wait_past(50ms);
CHECK(runtime.exit_reason() == Reason::lease_expired);
}
SUBCASE("un nouveau controleur s'attache pendant la grace (rechargement)")
{
runtime.on_client_disconnected(1);
runtime.on_client_connected();
REQUIRE(runtime.attach(2, false) == CoreServices::AttachResult::attached);
wait_past(50ms);
CHECK(runtime.exit_reason() == Reason::none);
}
}
TEST_CASE("Runtime : detach puis deconnexion -> pas d'arret ; attach retablit le bail")
{
CoreRuntime runtime(fast(true));
REQUIRE(runtime.attach(1, false) == CoreServices::AttachResult::attached);
CHECK(runtime.detach(2) == CoreServices::DetachResult::not_controller);
REQUIRE(runtime.detach(1) == CoreServices::DetachResult::detached);
CHECK_FALSE(runtime.core_status().linked);
runtime.on_client_disconnected(1);
wait_past(100ms);
CHECK(runtime.exit_reason() == Reason::none);
// Réversible : un nouveau Studio se rattache, le bail revient.
REQUIRE(runtime.attach(3, false) == CoreServices::AttachResult::attached);
CHECK(runtime.core_status().linked);
runtime.on_client_disconnected(3);
wait_past(50ms);
CHECK(runtime.exit_reason() == Reason::lease_expired);
}
TEST_CASE("Runtime : core/shutdown -> arret apres un court delai")
{
CoreRuntime runtime(fast(false));
runtime.request_shutdown();
CHECK(runtime.exit_reason() == Reason::none); // la réponse doit pouvoir partir
wait_past(20ms);
CHECK(runtime.exit_reason() == Reason::shutdown);
}
TEST_CASE("Runtime : comptage des clients")
{
CoreRuntime runtime(fast(false));
runtime.on_client_connected();
runtime.on_client_connected();
runtime.on_client_disconnected(9);
CHECK(runtime.core_status().clients == 1);
}