Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Can't test an agent that creates a child coop #36

Closed
JorgenPo opened this issue Sep 21, 2021 · 3 comments
Closed

Can't test an agent that creates a child coop #36

JorgenPo opened this issue Sep 21, 2021 · 3 comments

Comments

@JorgenPo
Copy link

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?

@eao197
Copy link
Member

eao197 commented Sep 21, 2021

Hi!

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

@eao197
Copy link
Member

eao197 commented Sep 21, 2021

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.

@JorgenPo
Copy link
Author

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!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants