Skip to content

Commit

Permalink
Additional unit-test for #21.
Browse files Browse the repository at this point in the history
  • Loading branch information
eao197 committed Dec 17, 2020
1 parent d448058 commit 89706ac
Show file tree
Hide file tree
Showing 6 changed files with 166 additions and 0 deletions.
1 change: 1 addition & 0 deletions dev/test/so_5/internal_stats/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,6 @@ add_subdirectory(simple_coop_count)
add_subdirectory(simple_named_mbox_count)
add_subdirectory(simple_timer_thread)
add_subdirectory(simple_work_thread_activity)
add_subdirectory(simple_work_thread_activity_wrapped_env)

add_subdirectory(all_dispatchers)
1 change: 1 addition & 0 deletions dev/test/so_5/internal_stats/build_tests.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
required_prj "#{path}/simple_named_mbox_count/prj.ut.rb"
required_prj "#{path}/simple_timer_thread/prj.ut.rb"
required_prj "#{path}/simple_work_thread_activity/prj.ut.rb"
required_prj "#{path}/simple_work_thread_activity_wrapped_env/prj.ut.rb"

required_prj "#{path}/all_dispatchers/prj.rb"
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
set(UNITTEST _unit.test.internal_stats.simple_work_thread_activity_wrapped_env)
include(${CMAKE_SOURCE_DIR}/cmake/unittest.cmake)
Original file line number Diff line number Diff line change
@@ -0,0 +1,142 @@
/*
* A simple test for getting stats about work thread activity.
*/

#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/ensure.hpp>
#include <test/3rd_party/various_helpers/time_limited_execution.hpp>

class a_test_t : public so_5::agent_t
{
public :
a_test_t( context_t ctx )
: so_5::agent_t( ctx )
{}

virtual void
so_define_agent() override
{
so_default_state().event(
so_environment().stats_controller().mbox(),
&a_test_t::evt_monitor_activity );
}

virtual void
so_evt_start() override
{
create_child_coops();

so_environment().stats_controller().turn_on();
}

private :
unsigned int m_actual_values = { 0 };

void
evt_monitor_activity(
const so_5::stats::messages::work_thread_activity & evt )
{
namespace stats = so_5::stats;

std::cout << evt.m_prefix << evt.m_suffix
<< " [" << evt.m_thread_id << "] ->\n"
<< " working: " << evt.m_stats.m_working_stats << "\n"
<< " waiting: " << evt.m_stats.m_waiting_stats << std::endl;

++m_actual_values;

if( 4 == m_actual_values )
so_environment().stop();
}

void
create_child_coops()
{
class empty_actor_t final : public so_5::agent_t
{
public :
using so_5::agent_t::agent_t;
};

for( int i = 0; i != 10; ++i )
{
auto coop = so_5::create_child_coop( *this );
coop->make_agent< empty_actor_t >();

so_environment().register_coop( std::move( coop ) );
}
}
};

void
init( so_5::environment_t & env )
{
ensure_or_die(
so_5::work_thread_activity_tracking_t::on ==
env.work_thread_activity_tracking(),
"work_thread_activity_tracking should be 'on' at this point" );

env.introduce_coop( []( so_5::coop_t & coop ) {
coop.make_agent< a_test_t >();

class actor_t final : public so_5::agent_t
{
struct next final : public so_5::signal_t {};
public :
using so_5::agent_t::agent_t;

void so_evt_start() override
{
so_subscribe_self().event( [this](mhood_t<next>) {
so_5::send< next >( *this );
std::this_thread::sleep_for( std::chrono::seconds(1) );
} );

so_5::send< next >( *this );
}
};

coop.make_agent_with_binder< actor_t >(
so_5::disp::one_thread::make_dispatcher(
coop.environment(), "busy" ).binder() );
} );
}

int
main()
{
try
{
run_with_time_limit(
[]()
{
so_5::wrapped_env_t env{
&init,
[]( so_5::environment_params_t & params ) {
params.turn_work_thread_activity_tracking_on();
}
};

std::cout << "Waiting for the completion..." << std::endl;
env.join();
},
20 );
}
catch( const std::exception & ex )
{
std::cerr << "Error: " << ex.what() << std::endl;
return 1;
}

return 0;
}

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.internal_stats.simple_work_thread_activity_wrapped_env'

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/internal_stats/simple_work_thread_activity_wrapped_env'

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

0 comments on commit 89706ac

Please sign in to comment.