Skip to content

Commit

Permalink
dev: Use targetted messaging towards netsim (#151)
Browse files Browse the repository at this point in the history
* dev: Use targetted messaging towards netsim

* tests: Adapt tests to netsim targetted messaging

* Apply suggestions from code review
  • Loading branch information
KonradBkd authored Nov 28, 2024
1 parent 8eb649d commit 2eda6ec
Show file tree
Hide file tree
Showing 10 changed files with 118 additions and 81 deletions.
2 changes: 1 addition & 1 deletion SilKit/source/services/can/SimBehaviorDetailed.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ SimBehaviorDetailed::SimBehaviorDetailed(Core::IParticipantInternal* participant
template <typename MsgT>
void SimBehaviorDetailed::SendMsgImpl(MsgT&& msg)
{
_participant->SendMsg(_parentServiceEndpoint, std::forward<MsgT>(msg));
_participant->SendMsg(_parentServiceEndpoint, _simulatedLink.GetParticipantName(), std::forward<MsgT>(msg));
}
void SimBehaviorDetailed::SendMsg(CanConfigureBaudrate&& msg)
{
Expand Down
41 changes: 23 additions & 18 deletions SilKit/source/services/can/Test_CanControllerDetailedSim.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -50,11 +50,12 @@ using ::SilKit::Core::Tests::DummyParticipant;

class MockParticipant : public DummyParticipant
{

public:
MOCK_METHOD2(SendMsg, void(const IServiceEndpoint *, const WireCanFrameEvent &));
MOCK_METHOD2(SendMsg, void(const IServiceEndpoint *, const CanFrameTransmitEvent &));
MOCK_METHOD2(SendMsg, void(const IServiceEndpoint *, const CanConfigureBaudrate &));
MOCK_METHOD2(SendMsg, void(const IServiceEndpoint *, const CanSetControllerMode &));
MOCK_METHOD(void, SendMsg, (const IServiceEndpoint *, const std::string &, const WireCanFrameEvent &), (override));
MOCK_METHOD(void, SendMsg, (const IServiceEndpoint *, const std::string &, const CanFrameTransmitEvent &), (override));
MOCK_METHOD(void, SendMsg, (const IServiceEndpoint *, const std::string &, const CanConfigureBaudrate &), (override));
MOCK_METHOD(void, SendMsg, (const IServiceEndpoint *, const std::string &, const CanSetControllerMode &), (override));
};

class CanControllerCallbacks
Expand All @@ -66,16 +67,18 @@ class CanControllerCallbacks
MOCK_METHOD2(FrameTransmitHandler, void(ICanController *, CanFrameTransmitEvent));
};

const std::string netsimName = "bussim";

TEST(Test_CanControllerDetailedSim, send_can_message)
{
MockParticipant mockParticipant;
CanController canController(&mockParticipant, {}, mockParticipant.GetTimeProvider());
canController.SetDetailedBehavior({"bussim", "n1", "c1", 8});
canController.SetDetailedBehavior({netsimName, "n1", "c1", 8});
canController.SetServiceDescriptor({"p1", "n1", "c1", 8});

WireCanFrameEvent testFrameEvent{};

EXPECT_CALL(mockParticipant, SendMsg(&canController, testFrameEvent)).Times(1);
EXPECT_CALL(mockParticipant, SendMsg(&canController, netsimName, testFrameEvent)).Times(1);

canController.SendFrame(ToCanFrame(testFrameEvent.frame));
}
Expand All @@ -84,7 +87,7 @@ TEST(Test_CanControllerDetailedSim, receive_can_message)
{
using namespace std::placeholders;

ServiceDescriptor busSimAddress{"bussim", "n1", "c1", 8};
ServiceDescriptor busSimAddress{netsimName, "n1", "c1", 8};

MockParticipant mockParticipant;
CanControllerCallbacks callbackProvider;
Expand All @@ -110,18 +113,18 @@ TEST(Test_CanControllerDetailedSim, start_stop_sleep_reset)
MockParticipant mockParticipant;

CanController canController(&mockParticipant, {}, mockParticipant.GetTimeProvider());
canController.SetDetailedBehavior({"bussim", "n1", "c1", 8});
canController.SetDetailedBehavior({netsimName, "n1", "c1", 8});
canController.SetServiceDescriptor({"p1", "n1", "c1", 8});

CanSetControllerMode startCommand = {{0, 0}, CanControllerState::Started};
CanSetControllerMode stopCommand = {{0, 0}, CanControllerState::Stopped};
CanSetControllerMode sleepCommand = {{0, 0}, CanControllerState::Sleep};
CanSetControllerMode resetCommand = {{1, 1}, CanControllerState::Uninit};

EXPECT_CALL(mockParticipant, SendMsg(&canController, startCommand)).Times(1);
EXPECT_CALL(mockParticipant, SendMsg(&canController, stopCommand)).Times(1);
EXPECT_CALL(mockParticipant, SendMsg(&canController, sleepCommand)).Times(1);
EXPECT_CALL(mockParticipant, SendMsg(&canController, resetCommand)).Times(1);
EXPECT_CALL(mockParticipant, SendMsg(&canController, netsimName, startCommand)).Times(1);
EXPECT_CALL(mockParticipant, SendMsg(&canController, netsimName, stopCommand)).Times(1);
EXPECT_CALL(mockParticipant, SendMsg(&canController, netsimName, sleepCommand)).Times(1);
EXPECT_CALL(mockParticipant, SendMsg(&canController, netsimName, resetCommand)).Times(1);

canController.Start();
canController.Stop();
Expand All @@ -134,14 +137,14 @@ TEST(Test_CanControllerDetailedSim, set_baudrate)
MockParticipant mockParticipant;

CanController canController(&mockParticipant, {}, mockParticipant.GetTimeProvider());
canController.SetDetailedBehavior({"bussim", "n1", "c1", 8});
canController.SetDetailedBehavior({netsimName, "n1", "c1", 8});
canController.SetServiceDescriptor({"p1", "n1", "c1", 8});

CanConfigureBaudrate baudrate1 = {3000, 0, 0};
CanConfigureBaudrate baudrate2 = {3000, 500000, 0};

EXPECT_CALL(mockParticipant, SendMsg(&canController, baudrate1)).Times(1);
EXPECT_CALL(mockParticipant, SendMsg(&canController, baudrate2)).Times(1);
EXPECT_CALL(mockParticipant, SendMsg(&canController, netsimName, baudrate1)).Times(1);
EXPECT_CALL(mockParticipant, SendMsg(&canController, netsimName, baudrate2)).Times(1);

canController.SetBaudRate(baudrate1.baudRate, baudrate1.fdBaudRate, baudrate1.xlBaudRate);
canController.SetBaudRate(baudrate2.baudRate, baudrate2.fdBaudRate, baudrate2.xlBaudRate);
Expand All @@ -151,7 +154,7 @@ TEST(Test_CanControllerDetailedSim, receive_new_controller_state)
{
using namespace std::placeholders;

ServiceDescriptor busSimAddress{"bussim", "n1", "c1", 8};
ServiceDescriptor busSimAddress{netsimName, "n1", "c1", 8};

MockParticipant mockParticipant;

Expand Down Expand Up @@ -197,7 +200,7 @@ TEST(Test_CanControllerDetailedSim, receive_new_controller_state)
TEST(Test_CanControllerDetailedSim, receive_ack)
{
using namespace std::placeholders;
ServiceDescriptor busSimAddress{"bussim", "n1", "c1", 8};
ServiceDescriptor busSimAddress{netsimName, "n1", "c1", 8};

MockParticipant mockParticipant;

Expand Down Expand Up @@ -245,7 +248,9 @@ TEST(Test_CanControllerDetailedSim, must_not_generate_ack)
canController.SetServiceDescriptor(controllerAddress);

WireCanFrameEvent msg{};
EXPECT_CALL(mockParticipant, SendMsg(An<const IServiceEndpoint *>(), A<const CanFrameTransmitEvent &>())).Times(0);
EXPECT_CALL(mockParticipant,
SendMsg(An<const IServiceEndpoint *>(), netsimName, A<const CanFrameTransmitEvent &>()))
.Times(0);

CanController canControllerFrom(&mockParticipant, {}, mockParticipant.GetTimeProvider());
canControllerFrom.SetServiceDescriptor(busSimAddress);
Expand Down
2 changes: 1 addition & 1 deletion SilKit/source/services/ethernet/SimBehaviorDetailed.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ SimBehaviorDetailed::SimBehaviorDetailed(Core::IParticipantInternal* participant
template <typename MsgT>
void SimBehaviorDetailed::SendMsgImpl(MsgT&& msg)
{
_participant->SendMsg(_parentServiceEndpoint, std::forward<MsgT>(msg));
_participant->SendMsg(_parentServiceEndpoint, _simulatedLink.GetParticipantName(), std::forward<MsgT>(msg));
}

void SimBehaviorDetailed::SendMsg(WireEthernetFrameEvent&& msg)
Expand Down
24 changes: 14 additions & 10 deletions SilKit/source/services/ethernet/Test_EthControllerDetailedSim.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -61,10 +61,10 @@ auto AnEthMessageWith(std::chrono::nanoseconds timestamp) -> testing::Matcher<co
class MockParticipant : public DummyParticipant
{
public:
MOCK_METHOD2(SendMsg, void(const IServiceEndpoint *, const WireEthernetFrameEvent &));
MOCK_METHOD2(SendMsg, void(const IServiceEndpoint *, const EthernetFrameTransmitEvent &));
MOCK_METHOD2(SendMsg, void(const IServiceEndpoint *, const EthernetStatus &));
MOCK_METHOD2(SendMsg, void(const IServiceEndpoint *, const EthernetSetMode &));
MOCK_METHOD(void, SendMsg, (const IServiceEndpoint *,const std::string&, const WireEthernetFrameEvent &), (override));
MOCK_METHOD(void, SendMsg, (const IServiceEndpoint *,const std::string&, const EthernetFrameTransmitEvent &), (override));
MOCK_METHOD(void, SendMsg, (const IServiceEndpoint *,const std::string&, const EthernetStatus &), (override));
MOCK_METHOD(void, SendMsg, (const IServiceEndpoint *,const std::string&, const EthernetSetMode &), (override));
};

class Test_EthControllerDetailedSim : public testing::Test
Expand Down Expand Up @@ -96,8 +96,10 @@ class Test_EthControllerDetailedSim : public testing::Test
}

protected:
const std::string netsimName = "bussim";

ServiceDescriptor controllerAddress{"controller", "n1", "c1", 8};
ServiceDescriptor busSimAddress{"bussim", "n1", "c1", 8};
ServiceDescriptor busSimAddress{netsimName, "n1", "c1", 8};

MockParticipant participant;
Callbacks callbacks;
Expand Down Expand Up @@ -127,9 +129,9 @@ TEST_F(Test_EthControllerDetailedSim, keep_track_of_state)
EthernetSetMode Activate{EthernetMode::Active};
EthernetSetMode Deactivate{EthernetMode::Inactive};

EXPECT_CALL(participant, SendMsg(&controller, Activate)).Times(1);
EXPECT_CALL(participant, SendMsg(&controller, netsimName, Activate)).Times(1);

EXPECT_CALL(participant, SendMsg(&controller, Deactivate)).Times(1);
EXPECT_CALL(participant, SendMsg(&controller, netsimName, Deactivate)).Times(1);

controller.Deactivate();
controller.Activate();
Expand All @@ -143,7 +145,7 @@ TEST_F(Test_EthControllerDetailedSim, keep_track_of_state)

TEST_F(Test_EthControllerDetailedSim, send_eth_message)
{
EXPECT_CALL(participant, SendMsg(&controller, AnEthMessageWith(0ns))).Times(1);
EXPECT_CALL(participant, SendMsg(&controller, netsimName, AnEthMessageWith(0ns))).Times(1);

EXPECT_CALL(participant.mockTimeProvider, Now()).Times(1);

Expand All @@ -155,7 +157,7 @@ TEST_F(Test_EthControllerDetailedSim, send_eth_message)
*/
TEST_F(Test_EthControllerDetailedSim, send_eth_frame)
{
EXPECT_CALL(participant, SendMsg(&controller, AnEthMessageWith(0ns))).Times(1);
EXPECT_CALL(participant, SendMsg(&controller, netsimName, AnEthMessageWith(0ns))).Times(1);

EXPECT_CALL(participant.mockTimeProvider, Now()).Times(1);

Expand Down Expand Up @@ -199,7 +201,9 @@ TEST_F(Test_EthControllerDetailedSim, must_not_generate_ack)
WireEthernetFrameEvent msg{};
msg.userContext = reinterpret_cast<void *>(17);

EXPECT_CALL(participant, SendMsg(An<const IServiceEndpoint *>(), A<const EthernetFrameTransmitEvent &>())).Times(0);
EXPECT_CALL(participant,
SendMsg(An<const IServiceEndpoint *>(), netsimName, A<const EthernetFrameTransmitEvent &>()))
.Times(0);

controller.ReceiveMsg(&controllerBusSim, msg);
}
Expand Down
2 changes: 1 addition & 1 deletion SilKit/source/services/flexray/FlexrayController.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -340,7 +340,7 @@ void FlexrayController::ReceiveMsg(const IServiceEndpoint* from, const FlexrayPo
template <typename MsgT>
void FlexrayController::SendMsg(MsgT&& msg)
{
_participant->SendMsg(this, std::forward<MsgT>(msg));
_participant->SendMsg(this, _simulatedLink.GetParticipantName(), std::forward<MsgT>(msg));
}

//------------------------
Expand Down
Loading

0 comments on commit 2eda6ec

Please sign in to comment.