Commit de72dfdb authored by Dominik Charousset's avatar Dominik Charousset

Make authentication test deterministic

parent 195eba3a
...@@ -48,9 +48,10 @@ public: ...@@ -48,9 +48,10 @@ public:
load<io::middleman>(); load<io::middleman>();
load<openssl::manager>(); load<openssl::manager>();
add_message_type<std::vector<int>>("std::vector<int>"); add_message_type<std::vector<int>>("std::vector<int>");
actor_system_config::parse(test::engine::argc(), actor_system_config::parse(test::engine::argc(), test::engine::argv());
test::engine::argv()); middleman_detach_multiplexer = false;
scheduler_max_threads = 1; middleman_detach_utility_actors = false;
scheduler_policy = atom("testing");
} }
static std::string data_dir() { static std::string data_dir() {
...@@ -70,36 +71,38 @@ public: ...@@ -70,36 +71,38 @@ public:
behavior make_pong_behavior() { behavior make_pong_behavior() {
return { return {
[](int val) -> int { [](int val) -> int {
CAF_MESSAGE("pong with " << ++val); ++val;
CAF_MESSAGE("pong " << val);
return val; return val;
} }
}; };
} }
behavior make_ping_behavior(event_based_actor* self, const actor& pong) { behavior make_ping_behavior(event_based_actor* self, const actor& pong) {
CAF_MESSAGE("ping with " << 0); CAF_MESSAGE("ping " << 0);
self->send(pong, 0); self->send(pong, 0);
return { return {
[=](int val) -> int { [=](int val) -> int {
if (val == 3) { CAF_MESSAGE("ping " << val);
CAF_MESSAGE("ping with exit"); if (val >= 3) {
self->send_exit(self->current_sender(), CAF_MESSAGE("terminate ping");
exit_reason::user_shutdown);
CAF_MESSAGE("ping quits");
self->quit(); self->quit();
} }
CAF_MESSAGE("ping with " << val);
return val; return val;
} }
}; };
} }
struct fixture { struct fixture {
using sched_t = scheduler::test_coordinator;
config server_side_config; config server_side_config;
config client_side_config; config client_side_config;
bool initialized; bool initialized;
union { actor_system server_side; }; union { actor_system server_side; };
union { actor_system client_side; }; union { actor_system client_side; };
sched_t* ssched;
sched_t* csched;
fixture() : initialized(false) { fixture() : initialized(false) {
// nop // nop
...@@ -136,11 +139,41 @@ struct fixture { ...@@ -136,11 +139,41 @@ struct fixture {
} }
*x.second = std::move(path); *x.second = std::move(path);
} }
CAF_MESSAGE("initialize server side");
new (&server_side) actor_system(server_side_config); new (&server_side) actor_system(server_side_config);
CAF_MESSAGE("initialize client side");
new (&client_side) actor_system(client_side_config); new (&client_side) actor_system(client_side_config);
ssched = &dynamic_cast<sched_t&>(server_side.scheduler());
csched = &dynamic_cast<sched_t&>(client_side.scheduler());
initialized = true; initialized = true;
return true; return true;
} }
sched_t& sched_by_sys(actor_system& sys) {
return &sys == &server_side ? *ssched : *csched;
}
bool exec_one(actor_system& sys) {
CAF_ASSERT(initialized);
CAF_PUSH_AID(0);
CAF_SET_LOGGER_SYS(&sys);
return sched_by_sys(sys).try_run_once()
|| sys.middleman().backend().try_run_once();
}
void exec_loop(actor_system& sys) {
while (exec_one(sys))
; // nop
}
void exec_loop() {
while (exec_one(client_side) | exec_one(server_side))
; // nop
}
void loop_after_next_enqueue(actor_system& sys) {
auto s = &sys == &server_side ? ssched : csched;
s->after_next_enqueue([=] { exec_loop(); });
}
}; };
} // namespace <anonymous> } // namespace <anonymous>
...@@ -154,23 +187,44 @@ CAF_TEST(authentication_succes) { ...@@ -154,23 +187,44 @@ CAF_TEST(authentication_succes) {
if (!init(false)) if (!init(false))
return; return;
// server side // server side
CAF_EXP_THROW(port, CAF_MESSAGE("spawn pong on server");
publish(server_side.spawn(make_pong_behavior), 0, local_host)); auto spong = server_side.spawn(make_pong_behavior);
exec_loop();
loop_after_next_enqueue(server_side);
CAF_MESSAGE("publish pong");
CAF_EXP_THROW(port, publish(spong, 0, local_host));
exec_loop();
// client side // client side
CAF_MESSAGE("connect to pong via port " << port);
loop_after_next_enqueue(client_side);
CAF_EXP_THROW(pong, remote_actor(client_side, local_host, port)); CAF_EXP_THROW(pong, remote_actor(client_side, local_host, port));
CAF_MESSAGE("spawn ping and exchange messages");
client_side.spawn(make_ping_behavior, pong); client_side.spawn(make_ping_behavior, pong);
exec_loop();
CAF_MESSAGE("terminate pong");
anon_send_exit(spong, exit_reason::user_shutdown);
exec_loop();
} }
CAF_TEST(authentication_failure) { CAF_TEST(authentication_failure) {
if (!init(true)) if (!init(true))
return; return;
// server side // server side
auto pong = server_side.spawn(make_pong_behavior); CAF_MESSAGE("spawn pong on server");
CAF_EXP_THROW(port, publish(pong, 0, local_host)); auto spong = server_side.spawn(make_pong_behavior);
exec_loop();
loop_after_next_enqueue(server_side);
CAF_MESSAGE("publish pong");
CAF_EXP_THROW(port, publish(spong, 0, local_host));
exec_loop();
// client side // client side
CAF_MESSAGE("connect to pong via port " << port);
loop_after_next_enqueue(client_side);
auto remote_pong = remote_actor(client_side, local_host, port); auto remote_pong = remote_actor(client_side, local_host, port);
CAF_REQUIRE(!remote_pong); CAF_CHECK(!remote_pong);
anon_send_exit(pong, exit_reason::user_shutdown); CAF_MESSAGE("terminate pong");
anon_send_exit(spong, exit_reason::user_shutdown);
exec_loop();
} }
CAF_TEST_FIXTURE_SCOPE_END() CAF_TEST_FIXTURE_SCOPE_END()
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment