Skip to content

Commit

Permalink
Correct receive method and tests.
Browse files Browse the repository at this point in the history
  • Loading branch information
KerstinKeller committed May 14, 2024
1 parent 552edca commit c050e9e
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 13 deletions.
16 changes: 12 additions & 4 deletions ecal/core/include/ecal/msg/dynamic.h
Original file line number Diff line number Diff line change
Expand Up @@ -138,8 +138,9 @@ namespace eCAL
* @param [out] time_ Optional receive time stamp.
* @param rcv_timeout_ Receive timeout in ms.
*
* @return True if a message could received, false otherwise.
* @return std::optional which holds the value if a value could be received, and std::nullopt if it couldn't.
**/
// Do we want to call error callbacks on receive? Probably not! std::expected wouuld be a good thing to return the reason why things went wrong.
std::optional<T> Receive(long long* time_ = nullptr, int rcv_timeout_ = 0)
{
std::string rec_buf;
Expand All @@ -157,7 +158,14 @@ namespace eCAL
return std::nullopt;
}

return(m_deserializer.Deserialize(msg_, rec_buf.c_str(), rec_buf.size(), m_datatype_info_received.value()));
try
{
return(m_deserializer.Deserialize(rec_buf.c_str(), rec_buf.size(), m_datatype_info_received.value()));
}
catch (const DynamicReflectionException& e)
{
return std::nullopt;
}
}

/**
Expand Down Expand Up @@ -265,9 +273,9 @@ namespace eCAL
auto msg = m_deserializer.Deserialize(data_->buf, data_->size, m_datatype_info_received.value());
fn_callback(topic_name_, msg, data_->time, data_->clock, data_->id);
}
catch (...)
catch (const DynamicReflectionException& e)
{
CallErrorCallback("Dynamic Deserialization: Error deserializing data.");
CallErrorCallback(std::string("Dynamic Deserialization: Error deserializing data: ") + e.what() );
}
}

Expand Down
2 changes: 1 addition & 1 deletion ecal/tests/cpp/pubsub_proto_test/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ target_link_libraries(${PROJECT_NAME}
)


target_compile_features(${PROJECT_NAME} PRIVATE cxx_std_14)
target_compile_features(${PROJECT_NAME} PRIVATE cxx_std_17)

ecal_install_gtest(${PROJECT_NAME})

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ class ProtoDynSubscriberTest : public ::testing::Test {
pub.Send(p);
}

void OnPerson(const char*, const google::protobuf::Message&, long long)
void OnPerson(const char*, const std::shared_ptr<google::protobuf::Message>&, long long)
{
received_callbacks++;
}
Expand Down Expand Up @@ -122,12 +122,9 @@ TEST_F(core_cpp_pubsub_proto_dyn, ProtoDynSubscriberTest_SendReceive)
SendPerson(person_pub);
std::this_thread::sleep_for(std::chrono::milliseconds(100));

google::protobuf::Message* message = person_dyn_rec.getMessagePointer();
ASSERT_NE(message, nullptr) << "pointer returned by dynamic subscriber may not be null";

bool received = person_dyn_rec.Receive(*message, nullptr, 500);
auto received_message = person_dyn_rec.Receive(nullptr, 500);
// assert that the OnPerson callback has been called once.
ASSERT_TRUE(received) << "we should have received data that was sent";
auto id = extract_id(*message);
ASSERT_EQ(id, 1);
ASSERT_TRUE(received_message.has_value()) << "we should have received data that was sent";
auto id = extract_id(*received_message.value());
ASSERT_EQ(id, 1) << "Extracted ID needs to be 1";
}

0 comments on commit c050e9e

Please sign in to comment.