From 3ad9a1b9120d1dd3a23a87f6ff053b01f8601bbb Mon Sep 17 00:00:00 2001 From: Jeremy Nimmer Date: Tue, 5 Sep 2023 20:48:47 -0700 Subject: [PATCH] [lcm] Hotfix for "Eschew LCM C++ API" (#20137) Previously, when multicast was disabled all operations printed a warning to the console and then did nothing. The prior commit changed that to throw an error instead, which was breaking. Instead, let LCM still print the console warning but then instead of throwing provide an inert DrakeLcm instead. --- lcm/drake_lcm.cc | 5 ++++- lcm/test/drake_lcm_test.cc | 6 ++++++ 2 files changed, 10 insertions(+), 1 deletion(-) diff --git a/lcm/drake_lcm.cc b/lcm/drake_lcm.cc index 7eb5de839004..4cb93ef18d29 100644 --- a/lcm/drake_lcm.cc +++ b/lcm/drake_lcm.cc @@ -61,7 +61,10 @@ class DrakeLcm::Impl { // Create the native instance only after all other checks are finished. lcm_ = ::lcm_create(lcm_url_.c_str()); if (lcm_ == nullptr) { - throw std::runtime_error("Failure on lcm_create()"); + // The initialization failed and printed a console warning. Create + // a dummy object instead (to at least have something non-null). + lcm_ = ::lcm_create("memq://"); + DRAKE_THROW_UNLESS(lcm_ != nullptr); } } diff --git a/lcm/test/drake_lcm_test.cc b/lcm/test/drake_lcm_test.cc index 5ada76e2ca40..6d5c7f695ef0 100644 --- a/lcm/test/drake_lcm_test.cc +++ b/lcm/test/drake_lcm_test.cc @@ -77,6 +77,12 @@ TEST_F(DrakeLcmTest, CustomUrlTest) { EXPECT_EQ(dut_->get_lcm_url(), kUdpmUrl); } +TEST_F(DrakeLcmTest, BadUrlTest) { + // At the moment, invalid URLs print to the console but do not throw. + // We probably want to revisit this as some point (to fail-fast). + EXPECT_NO_THROW(DrakeLcm("no-such-scheme://foo")); +} + TEST_F(DrakeLcmTest, DeferThreadTest) { DrakeLcmParams params; params.lcm_url = kUdpmUrl;