Stiffstream / sobjectizer

An implementation of Actor, Publish-Subscribe, and CSP models in one rather small C++ framework. With performance, quality, and stability proved by years in the production.

Home Page:https://stiffstream.com/en/products/sobjectizer.html

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Can't test an agent that creates a child coop

JorgenPo opened this issue · comments

Hello, I am using sobjectizer in my work project and trying to write some tests for a simple agent using testing_env_t testing environment. I already tested another agent without issues but when I try to test an agent that creates child cooperations I got an error.

Some environment info:
sobjectizer 5.7.2 installed from conan
gcc 11

Simple test agent:

class ChildAgent : public so_5::agent_t {
public:
  explicit ChildAgent(context_t ctx) : so_5::agent_t(std::move(ctx)) {}
};

class TestAgent : public so_5::agent_t {
public:
  explicit TestAgent(context_t ctx) : so_5::agent_t(std::move(ctx)) {}

  void so_evt_start() override {
    so_5::introduce_child_coop(*this, [](so_5::coop_t &coop) {
      coop.make_agent<ChildAgent>();
    });
  }
};

Test code looks like this:

TEST_CASE("Simple agent test") {
  so_5::experimental::testing::testing_env_t testing;
  testing.environment().introduce_coop([](so_5::coop_t &coop) {
    coop.make_agent<TestAgent>();
  });

  // After executing the scenario an exception is thrown
  testing.scenario().run_for(std::chrono::milliseconds(100));
}

When I run this test I get an exception from sobjectizer:

SObjectizer event exception caught: (.../sobjectizer/dev/so_5/impl/coop_repository_basis.cpp:108): error(28) a new coop can't be registered when shutdown is in progress; cooperation: {coop:id=2}

When I remove so_5::introduce_child_coop from TestAgent::so_evt_start then all is ok.

Am I doing something wrong? How could I test an agent with a child cooperation created inside it?

Hi!

I'll take some time to think about your case.

It looks like the problem is the absence of any test scenario steps in your test case. You just create a parent coop and then run an empty test scenario by calling run_for method. Because the test scenario is empty the work of the test environment will be finished. It's an asynchronous process that is executed in the background and takes some time. But when you call run_for there is another activity on some worker thread: execution of TestAgent's so_evt_start. And that execution is going in parallel with the shutdown operation. That is why you can't register new cooperation and got the exception.

Please note that agents created in testing.environment().introduce_coop() are frozen until run_for is called. There is an explanation: https://github.com/Stiffstream/sobjectizer/wiki/SO-5.7-Experimental-Testing#a-test-case-for-pingerponger

When testing_env_t is used all agents are frozen after the registration if the testing scenario is not started yet. This means that agents are present in the SObjectizer Environment but they can't handle any events (even so_evt_start is not called). It is possible to send a message to a frozen agent, but this message will wait in some event queue while the agent will be unfrozen.

All agents those are registered before a call to run_for will be automatically unfrozen when run_for is called. It means that so_evt_start for Pinger agent will be called only when we call run_for in our scenario.

Thanks for your quick reply.
I've added some test scenario steps and the problem is gone in this simplified test case. In my original test code I had several test steps but it seems like that was a problem with some of these steps and as a result the mentioned exception occurred.
Thanks for the solution and explanations!