Skip to content

Commit

Permalink
A couple of new unit-tests.
Browse files Browse the repository at this point in the history
  • Loading branch information
eao197 committed Sep 18, 2023
1 parent 41c8ad5 commit f8e0319
Show file tree
Hide file tree
Showing 10 changed files with 279 additions and 0 deletions.
2 changes: 2 additions & 0 deletions dev/test/so_5/message_limits/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ add_subdirectory(redirect_msg_too_deep)
add_subdirectory(subscr_without_limit)
add_subdirectory(transform_msg)
add_subdirectory(transform_msg_too_deep)
add_subdirectory(transform_to_mutable_msg)
add_subdirectory(transform_to_mutable_msg_2)
add_subdirectory(another_direct_mbox)
add_subdirectory(redirect_from_timer_to_full_mchain)
add_subdirectory(transform_from_timer_to_full_mchain)
Expand Down
2 changes: 2 additions & 0 deletions dev/test/so_5/message_limits/build_tests.rb
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@
required_prj "#{path}/transform_msg/sc_mbox/prj.ut.rb"
required_prj "#{path}/transform_msg_too_deep/mc_mbox/prj.ut.rb"
required_prj "#{path}/transform_msg_too_deep/sc_mbox/prj.ut.rb"
required_prj "#{path}/transform_to_mutable_msg/prj.ut.rb"
required_prj "#{path}/transform_to_mutable_msg_2/prj.ut.rb"

required_prj "#{path}/another_direct_mbox/prj.ut.rb"

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
set(UNITTEST _unit.test.message_limits.transform_to_mutable_msg)
include(${CMAKE_SOURCE_DIR}/cmake/unittest.cmake)
115 changes: 115 additions & 0 deletions dev/test/so_5/message_limits/transform_to_mutable_msg/main.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,115 @@
/*
* A test for trasformation of an immutable message into a mutable message.
*/

#include <iostream>
#include <map>
#include <exception>
#include <stdexcept>
#include <cstdlib>
#include <thread>
#include <chrono>

#include <so_5/all.hpp>

#include <test/3rd_party/various_helpers/time_limited_execution.hpp>

struct msg_initial_message final : public so_5::message_t
{
const std::string m_msg;

msg_initial_message( std::string msg ) : m_msg{ std::move(msg) }
{}
};

struct msg_transformed_message final : public so_5::message_t
{
std::string m_msg;

msg_transformed_message( std::string msg ) : m_msg{ std::move(msg) }
{}
};

class agent_with_limit_t final : public so_5::agent_t
{
public :
agent_with_limit_t(
context_t ctx )
: so_5::agent_t{ ctx
+ limit_then_transform( 1u,
[this]( const msg_initial_message & msg ) {
return make_transformed<
so_5::mutable_msg< msg_transformed_message > >(
so_direct_mbox(),
"<" + msg.m_msg + ">" );
} )
+ limit_then_abort< so_5::mutable_msg< msg_transformed_message > >( 1u )
}
{}

void
so_define_agent() override
{
so_subscribe_self()
.event( [this]( mhood_t<msg_initial_message> cmd ) {
m_received += "[initial:" + cmd->m_msg + "]";
} )
.event( [this]( mutable_mhood_t<msg_transformed_message> cmd ) {
m_received += "[transformed:" + cmd->m_msg + "]";

const std::string expected =
"[initial:hello][transformed:<bye>]";

if( expected != m_received )
throw std::runtime_error( expected + " != " + m_received );
else
so_deregister_agent_coop_normally();
} )
;
}

virtual void
so_evt_start() override
{
so_5::send< msg_initial_message >( *this, "hello" );
so_5::send< msg_initial_message >( *this, "bye" );
}

private :
std::string m_received;
};

void
init( so_5::environment_t & env )
{
env.introduce_coop(
[]( so_5::coop_t & coop ) {
coop.make_agent< agent_with_limit_t >();
} );
}

int
main()
{
try
{
run_with_time_limit(
[]()
{
so_5::launch( &init,
[](so_5::environment_params_t & params) {
params.message_delivery_tracer(
so_5::msg_tracing::std_cout_tracer() );
} );
},
5 );
}
catch( const std::exception & ex )
{
std::cerr << "Error: " << ex.what() << std::endl;
return 1;
}

return 0;
}

11 changes: 11 additions & 0 deletions dev/test/so_5/message_limits/transform_to_mutable_msg/prj.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
require 'mxx_ru/cpp'

MxxRu::Cpp::exe_target {

required_prj 'so_5/prj.rb'

target '_unit.test.message_limits.transform_to_mutable_msg'

cpp_source 'main.cpp'
}

Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
require 'mxx_ru/binary_unittest'

path = 'test/so_5/message_limits/transform_to_mutable_msg'

MxxRu::setup_target(
MxxRu::BinaryUnittestTarget.new(
"#{path}/prj.ut.rb",
"#{path}/prj.rb" )
)
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
set(UNITTEST _unit.test.message_limits.transform_to_mutable_msg_2)
include(${CMAKE_SOURCE_DIR}/cmake/unittest.cmake)
116 changes: 116 additions & 0 deletions dev/test/so_5/message_limits/transform_to_mutable_msg_2/main.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,116 @@
/*
* A test for trasformation of an immutable message into a mutable message.
* Messages that not derived from so_5::message_t are used.
*/

#include <iostream>
#include <map>
#include <exception>
#include <stdexcept>
#include <cstdlib>
#include <thread>
#include <chrono>

#include <so_5/all.hpp>

#include <test/3rd_party/various_helpers/time_limited_execution.hpp>

struct msg_initial_message
{
const std::string m_msg;

msg_initial_message( std::string msg ) : m_msg{ std::move(msg) }
{}
};

struct msg_transformed_message
{
std::string m_msg;

msg_transformed_message( std::string msg ) : m_msg{ std::move(msg) }
{}
};

class agent_with_limit_t final : public so_5::agent_t
{
public :
agent_with_limit_t(
context_t ctx )
: so_5::agent_t{ ctx
+ limit_then_transform( 1u,
[this]( const msg_initial_message & msg ) {
return make_transformed<
so_5::mutable_msg< msg_transformed_message > >(
so_direct_mbox(),
"<" + msg.m_msg + ">" );
} )
+ limit_then_abort< so_5::mutable_msg< msg_transformed_message > >( 1u )
}
{}

void
so_define_agent() override
{
so_subscribe_self()
.event( [this]( mhood_t<msg_initial_message> cmd ) {
m_received += "[initial:" + cmd->m_msg + "]";
} )
.event( [this]( mutable_mhood_t<msg_transformed_message> cmd ) {
m_received += "[transformed:" + cmd->m_msg + "]";

const std::string expected =
"[initial:hello][transformed:<bye>]";

if( expected != m_received )
throw std::runtime_error( expected + " != " + m_received );
else
so_deregister_agent_coop_normally();
} )
;
}

virtual void
so_evt_start() override
{
so_5::send< msg_initial_message >( *this, "hello" );
so_5::send< msg_initial_message >( *this, "bye" );
}

private :
std::string m_received;
};

void
init( so_5::environment_t & env )
{
env.introduce_coop(
[]( so_5::coop_t & coop ) {
coop.make_agent< agent_with_limit_t >();
} );
}

int
main()
{
try
{
run_with_time_limit(
[]()
{
so_5::launch( &init,
[](so_5::environment_params_t & params) {
params.message_delivery_tracer(
so_5::msg_tracing::std_cout_tracer() );
} );
},
5 );
}
catch( const std::exception & ex )
{
std::cerr << "Error: " << ex.what() << std::endl;
return 1;
}

return 0;
}

11 changes: 11 additions & 0 deletions dev/test/so_5/message_limits/transform_to_mutable_msg_2/prj.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
require 'mxx_ru/cpp'

MxxRu::Cpp::exe_target {

required_prj 'so_5/prj.rb'

target '_unit.test.message_limits.transform_to_mutable_msg_2'

cpp_source 'main.cpp'
}

Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
require 'mxx_ru/binary_unittest'

path = 'test/so_5/message_limits/transform_to_mutable_msg_2'

MxxRu::setup_target(
MxxRu::BinaryUnittestTarget.new(
"#{path}/prj.ut.rb",
"#{path}/prj.rb" )
)

0 comments on commit f8e0319

Please sign in to comment.