1 #define DOCTEST_CONFIG_IMPLEMENT_WITH_MAIN
2 #include <doctest/doctest.h>
3 
4 #include <so_5_extra/mboxes/proxy.hpp>
5 
6 #include <so_5/all.hpp>
7 
8 #include <test/3rd_party/various_helpers/time_limited_execution.hpp>
9 #include <test/3rd_party/various_helpers/ensure.hpp>
10 
11 namespace proxy_mbox_ns = so_5::extra::mboxes::proxy;
12 
13 struct ping final : public so_5::message_t
14 	{
15 		const so_5::mbox_t m_reply_to;
16 
pingping17 		ping( so_5::mbox_t reply_to ) : m_reply_to{ std::move(reply_to) } {}
18 	};
19 
20 struct pong final : public so_5::signal_t {};
21 
22 struct shutdown final : public so_5::signal_t {};
23 
24 class master_t final : public so_5::agent_t
25 	{
26 		const so_5::mbox_t m_mbox;
27 
28 	public :
master_t(context_t ctx,so_5::mbox_t mbox)29 		master_t( context_t ctx, so_5::mbox_t mbox )
30 			:	so_5::agent_t{ std::move(ctx) }
31 			,	m_mbox{ std::move(mbox) }
32 			{
33 				so_subscribe_self().event( &master_t::on_pong );
34 			}
35 
36 		void
so_evt_start()37 		so_evt_start() override
38 			{
39 				so_5::send<ping>( m_mbox, so_direct_mbox() );
40 			}
41 
42 	private :
43 		void
on_pong(mhood_t<pong>)44 		on_pong( mhood_t<pong> )
45 			{
46 				so_5::send_delayed< shutdown >(
47 						m_mbox,
48 						std::chrono::milliseconds(50) );
49 			}
50 	};
51 
52 class slave_t final : public so_5::agent_t
53 	{
54 	public :
slave_t(context_t ctx)55 		slave_t( context_t ctx )
56 			: so_5::agent_t{ std::move(ctx) }
57 			{
58 				so_subscribe_self()
__anon5f30d7af0102( mhood_t<ping> cmd ) 59 					.event( []( mhood_t<ping> cmd ) {
60 							so_5::send< pong >( cmd->m_reply_to );
61 						} )
__anon5f30d7af0202( mhood_t<shutdown> ) 62 					.event( [this]( mhood_t<shutdown> ) {
63 							so_deregister_agent_coop_normally();
64 						} );
65 			}
66 	};
67 
68 TEST_CASE( "simple proxy" )
69 {
__anon5f30d7af0302null70 	run_with_time_limit( [] {
71 			so_5::launch( [&](so_5::environment_t & env) {
72 						env.introduce_coop(
73 							so_5::disp::active_obj::make_dispatcher( env ).binder(),
74 							[]( so_5::coop_t & coop ) {
75 								auto slave = coop.make_agent< slave_t >();
76 								coop.make_agent< master_t >(
77 										so_5::mbox_t{
78 											std::make_unique< proxy_mbox_ns::simple_t >(
79 												slave->so_direct_mbox() )
80 										} );
81 							} );
82 					},
83 					[](so_5::environment_params_t & params) {
84 						params.message_delivery_tracer(
85 								so_5::msg_tracing::std_cout_tracer() );
86 					} );
87 		},
88 		5 );
89 }
90 
91