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

Send state message when components are removed #1235

Merged
merged 5 commits into from
Dec 8, 2021
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions src/EntityComponentManager_TEST.cc
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,7 @@ TEST_P(EntityComponentManagerFixture, InvalidComponentType)
EXPECT_EQ(2u, manager.CreateEntity());
EXPECT_TRUE(manager.HasEntity(2));
EXPECT_FALSE(manager.RemoveComponent(2, IntComponent::typeId));
EXPECT_FALSE(manager.HasRemovedComponents());

// We should get a nullptr if the component type doesn't exist.
EXPECT_TRUE(manager.HasEntity(1u));
Expand Down Expand Up @@ -182,6 +183,7 @@ TEST_P(EntityComponentManagerFixture, RemoveComponent)
EXPECT_FALSE(manager.EntityHasComponentType(eInt, IntComponent::typeId));
EXPECT_TRUE(manager.ComponentTypes(eInt).empty());
EXPECT_EQ(nullptr, manager.Component<IntComponent>(eInt));
EXPECT_TRUE(manager.HasRemovedComponents());

EXPECT_TRUE(manager.RemoveComponent(eDouble, DoubleComponent::typeId));
EXPECT_FALSE(manager.EntityHasComponentType(eDouble,
Expand Down
93 changes: 93 additions & 0 deletions test/integration/scene_broadcaster_system.cc
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,16 @@

#include <ignition/common/Console.hh>
#include <ignition/common/Util.hh>
#include "ignition/gazebo/components/Model.hh"
#include "ignition/gazebo/components/Name.hh"
#include "ignition/gazebo/components/Pose.hh"
#include <ignition/transport/Node.hh>

#include "ignition/gazebo/Server.hh"
#include "ignition/gazebo/test_config.hh"

#include "../helpers/EnvTestFixture.hh"
#include "../helpers/Relay.hh"

using namespace ignition;

Expand Down Expand Up @@ -612,6 +616,95 @@ TEST_P(SceneBroadcasterTest, StateStatic)
EXPECT_TRUE(received);
}

/////////////////////////////////////////////////
/// Test whether the scene topic is published when a component is removed.
TEST_P(SceneBroadcasterTest, RemovedComponent)
{
// Start server
ignition::gazebo::ServerConfig serverConfig;
serverConfig.SetSdfFile(std::string(PROJECT_SOURCE_PATH) +
"/test/worlds/shapes.sdf");

gazebo::Server server(serverConfig);
EXPECT_FALSE(server.Running());
EXPECT_FALSE(*server.Running(0));

// Create a system that removes a component
ignition::gazebo::test::Relay testSystem;

testSystem.OnUpdate([](const gazebo::UpdateInfo &_info,
gazebo::EntityComponentManager &_ecm)
{
if (_info.iterations > 1)
{
_ecm.Each<ignition::gazebo::components::Model,
ignition::gazebo::components::Name,
ignition::gazebo::components::Pose>(
[&](const ignition::gazebo::Entity &_entity,
const ignition::gazebo::components::Model *,
const ignition::gazebo::components::Name *_name,
const ignition::gazebo::components::Pose *)->bool
{
if (_name->Data() == "box")
{
_ecm.RemoveComponent<ignition::gazebo::components::Pose>(_entity);
}
nkoenig marked this conversation as resolved.
Show resolved Hide resolved
return true;
});
}
});
server.AddSystem(testSystem.systemPtr);

bool received = false;
bool hasState = false;
std::function<void(const msgs::SerializedStepMap &)> cb =
[&](const msgs::SerializedStepMap &_res)
{
received = true;
hasState = _res.has_state();
};

transport::Node node;
EXPECT_TRUE(node.Subscribe("/world/default/state", cb));

unsigned int sleep = 0u;
unsigned int maxSleep = 30u;

// Run server once. The first time should send the state message
server.RunOnce(true);
// cppcheck-suppress unmatchedSuppression
// cppcheck-suppress knownConditionTrueFalse
while (!received && sleep++ < maxSleep)
IGN_SLEEP_MS(100);
EXPECT_TRUE(received);
EXPECT_TRUE(hasState);

// Run server again. The second time shouldn't send the state message.
sleep = 0u;
received = false;
hasState = false;
server.RunOnce(true);
// cppcheck-suppress unmatchedSuppression
// cppcheck-suppress knownConditionTrueFalse
while (!received && sleep++ < maxSleep)
IGN_SLEEP_MS(100);
EXPECT_FALSE(received);
EXPECT_FALSE(hasState);

// Run server again. The third time should send the state message because
// the test system removed a component.
sleep = 0u;
received = false;
hasState = false;
server.RunOnce(true);
// cppcheck-suppress unmatchedSuppression
// cppcheck-suppress knownConditionTrueFalse
while (!received && sleep++ < maxSleep)
IGN_SLEEP_MS(100);
EXPECT_TRUE(received);
EXPECT_TRUE(hasState);
chapulina marked this conversation as resolved.
Show resolved Hide resolved
}

// Run multiple times
INSTANTIATE_TEST_SUITE_P(ServerRepeat, SceneBroadcasterTest,
::testing::Range(1, 2));