1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422
|
From 92e8c8ec490628d75052c6d8029e7107148de6d4 Mon Sep 17 00:00:00 2001
From: Thomas Ives <tri@observatorysciences.co.uk>
Date: Thu, 28 Aug 2025 15:06:03 +0100
Subject: [PATCH 1/5] catch2_event_old_client: Add test to catch duplicate
event issue
This test reproduces the issue described in #1521.
There are three test cases added here which cover different places where
we have the duplicate events issue.
The first test case covers the case where events are being pushed
manually both with and without change detection. This finds the
duplicate event issue in the
`ZmqEventSupplier::detect_and_push_<x>_event` methods along with the
`ZmqEventSupplier::push_event_loop` method.
The second test case covers events sent by the polling loop, this also
triggers the issue with the
`ZmqEventSupplier::detect_and_push_<x>_event` and is included mostly so
that we can check periodic events work.
The final test case covers attribute configuration events, which
triggers the duplicate event issue with the
`EventSupplier::push_att_config_event` method.
---
tests/CMakeLists.txt | 1 +
tests/catch2_event_old_client.cpp | 374 ++++++++++++++++++++++++++++++
2 files changed, 375 insertions(+)
create mode 100644 tests/catch2_event_old_client.cpp
Index: tango/lib/cpp/tests/catch2_event_old_client.cpp
===================================================================
--- /dev/null
+++ tango/lib/cpp/tests/catch2_event_old_client.cpp
@@ -0,0 +1,374 @@
+#include "catch2_common.h"
+
+#include "tango/internal/utils.h"
+
+#include <memory>
+
+using CallbackMockType = TangoTest::CallbackMock<Tango::EventData>;
+
+constexpr const char *k_attr_name = "basicAttr";
+constexpr const char *k_no_detect_attr_name = "noDetectAttr";
+constexpr static Tango::DevLong k_polling_period = TANGO_TEST_CATCH2_DEFAULT_POLL_PERIOD;
+
+void simulate_old_clients_subscription(Tango::DeviceProxy *admin,
+ Tango::EventType ev_type,
+ int idlver,
+ std::string_view device_name,
+ std::string_view attr_name)
+{
+ // Here we trick the device server into thinking that an old client has
+ // subscribed by calling ZmqEventSubscriptionChange. The order here
+ // matters, we want to subscribe to idl5 first, otherwise the duplicates
+ // will get discarded on the client side.
+ for(int oldver = idlver - 1; oldver >= 4; --oldver)
+ {
+ std::vector<std::string> request;
+ request.reserve(5);
+ request.emplace_back(device_name);
+ request.emplace_back(attr_name);
+ request.emplace_back("subscribe");
+ {
+ std::stringstream ss;
+ if(oldver >= 5)
+ {
+ ss << "idl5_";
+ }
+ ss << Tango::EventName[ev_type];
+ request.emplace_back(ss.str());
+ }
+ {
+ std::stringstream ss;
+ ss << oldver;
+ request.emplace_back(ss.str());
+ }
+ Tango::DeviceData din;
+ din << request;
+
+ REQUIRE_NOTHROW(admin->command_inout("ZmqEventSubscriptionChange", din));
+ }
+}
+
+template <typename Base>
+class DuplicatePushedEvent : public Base
+{
+ public:
+ using Base::Base;
+
+ ~DuplicatePushedEvent() override { }
+
+ void init_device() override
+ {
+ value = 0;
+ }
+
+ void read_attr(Tango::Attribute &att) override
+ {
+ att.set_value(&value);
+ }
+
+ void push_change()
+ {
+ value += 1;
+ this->push_change_event(k_attr_name, &value);
+ this->push_change_event(k_no_detect_attr_name, &value);
+ }
+
+ void push_archive()
+ {
+ value += 1;
+ this->push_archive_event(k_attr_name, &value);
+ this->push_archive_event(k_no_detect_attr_name, &value);
+ }
+
+ void push_user()
+ {
+ value += 1;
+ std::vector<std::string> filter_names;
+ std::vector<double> filter_values;
+ this->push_event(k_attr_name, filter_names, filter_values, &value);
+ this->push_event(k_no_detect_attr_name, filter_names, filter_values, &value);
+ }
+
+ static void attribute_factory(std::vector<Tango::Attr *> &attrs)
+ {
+ using Attr = TangoTest::AutoAttr<&DuplicatePushedEvent::read_attr>;
+ Tango::UserDefaultAttrProp props;
+ props.set_abs_change("1");
+ props.set_archive_abs_change("1");
+
+ auto attr = new Attr(k_attr_name, Tango::DEV_LONG);
+ attr->set_default_properties(props);
+ attr->set_change_event(true, true);
+ attr->set_archive_event(true, true);
+ attrs.push_back(attr);
+
+ auto no_detect_attr = new Attr(k_no_detect_attr_name, Tango::DEV_LONG);
+ no_detect_attr->set_change_event(true, false);
+ no_detect_attr->set_archive_event(true, false);
+ attrs.push_back(no_detect_attr);
+ }
+
+ static void command_factory(std::vector<Tango::Command *> &cmds)
+ {
+ cmds.push_back(new TangoTest::AutoCommand<&DuplicatePushedEvent::push_change>("push_change"));
+ cmds.push_back(new TangoTest::AutoCommand<&DuplicatePushedEvent::push_archive>("push_archive"));
+ cmds.push_back(new TangoTest::AutoCommand<&DuplicatePushedEvent::push_user>("push_user_event"));
+ }
+
+ private:
+ Tango::DevLong value{0};
+};
+
+TANGO_TEST_AUTO_DEV_TMPL_INSTANTIATE(DuplicatePushedEvent, 6)
+
+SCENARIO("Manually pushed events are not duplicated when old clients are connected")
+{
+ int idlver = GENERATE(TangoTest::idlversion(6));
+ GIVEN("a device proxy to a simple IDLv" << idlver << " device")
+ {
+ TangoTest::Context ctx{"duplicate_event", "DuplicatePushedEvent", idlver};
+ std::shared_ptr<Tango::DeviceProxy> device = ctx.get_proxy();
+
+ REQUIRE(idlver == device->get_idl_version());
+
+ std::string attr_name = GENERATE(k_attr_name, k_no_detect_attr_name);
+ Tango::EventType ev_type = GENERATE(Tango::CHANGE_EVENT, Tango::ARCHIVE_EVENT, Tango::USER_EVENT);
+
+ WHEN("some old clients subscribe to " << Tango::EventName[ev_type] << " events for " << attr_name)
+ {
+ std::unique_ptr<Tango::DeviceProxy> admin = ctx.get_admin_proxy();
+
+ simulate_old_clients_subscription(admin.get(), ev_type, idlver, device->dev_name(), attr_name);
+
+ AND_WHEN("we subscribe to " << Tango::EventName[ev_type] << " events for " << attr_name)
+ {
+ CallbackMockType callback;
+
+ device->subscribe_event(attr_name, ev_type, &callback);
+
+ auto maybe_initial_event = callback.pop_next_event();
+ REQUIRE(maybe_initial_event != std::nullopt);
+ maybe_initial_event = callback.pop_next_event();
+
+ AND_WHEN("the server sends an single event")
+ {
+ {
+ std::stringstream ss;
+ ss << "push_" << Tango::EventName[ev_type];
+ REQUIRE_NOTHROW(device->command_inout(ss.str()));
+ }
+
+ THEN("we only receive one event")
+ {
+ using TangoTest::AnyLikeContains;
+
+ auto maybe_event = callback.pop_next_event();
+ REQUIRE(maybe_event != std::nullopt);
+ REQUIRE(maybe_event->event == Tango::EventName[ev_type]);
+ REQUIRE_THAT(*maybe_event->attr_value, AnyLikeContains(static_cast<Tango::DevLong>(1)));
+
+ maybe_event = callback.pop_next_event();
+ REQUIRE(maybe_event == std::nullopt);
+ }
+ }
+ }
+ }
+ }
+}
+
+template <typename Base>
+class DuplicatePollingEvent : public Base
+{
+ public:
+ using Base::Base;
+
+ ~DuplicatePollingEvent() override { }
+
+ void init_device() override
+ {
+ value = 0;
+ }
+
+ void read_attr(Tango::Attribute &att) override
+ {
+ value += 1;
+ att.set_value(&value);
+ }
+
+ static void attribute_factory(std::vector<Tango::Attr *> &attrs)
+ {
+ using Attr = TangoTest::AutoAttr<&DuplicatePollingEvent::read_attr>;
+ Tango::UserDefaultAttrProp props;
+ {
+ std::stringstream ss;
+ ss << k_polling_period;
+ props.set_period(ss.str().c_str());
+ }
+ props.set_abs_change("1");
+ props.set_archive_abs_change("1");
+
+ auto attr = new Attr(k_attr_name, Tango::DEV_LONG);
+ attr->set_default_properties(props);
+ attr->set_polling_period(k_polling_period);
+ attrs.push_back(attr);
+ }
+
+ private:
+ Tango::DevLong value{0};
+};
+
+TANGO_TEST_AUTO_DEV_TMPL_INSTANTIATE(DuplicatePollingEvent, 6)
+
+SCENARIO("Periodic events are not duplicated when old clients are connected")
+{
+ int idlver = GENERATE(TangoTest::idlversion(6));
+ GIVEN("a device proxy to a simple IDLv" << idlver << " device")
+ {
+ TangoTest::Context ctx{"duplicate_event", "DuplicatePollingEvent", idlver};
+ std::shared_ptr<Tango::DeviceProxy> device = ctx.get_proxy();
+
+ REQUIRE(idlver == device->get_idl_version());
+
+ Tango::EventType ev_type = GENERATE(Tango::CHANGE_EVENT, Tango::ARCHIVE_EVENT, Tango::PERIODIC_EVENT);
+
+ WHEN("some old clients subscribe to " << Tango::EventName[ev_type] << " events")
+ {
+ std::unique_ptr<Tango::DeviceProxy> admin = ctx.get_admin_proxy();
+
+ simulate_old_clients_subscription(admin.get(), ev_type, idlver, device->dev_name(), k_attr_name);
+
+ AND_WHEN("we subscribe to " << Tango::EventName[ev_type] << " events")
+ {
+ CallbackMockType callback;
+
+ device->subscribe_event(k_attr_name, ev_type, &callback);
+
+ // We only require one event for the initial subscription, we
+ // don't want to consume any events from the Zmq interface.
+ // Not quite sure why this matters for polling and not when
+ // pushing. I think the de-duplicate on the client side means
+ // we only see the duplicate event for the first events that are
+ // pushed.
+ auto maybe_initial_event = callback.pop_next_event();
+ REQUIRE(maybe_initial_event != std::nullopt);
+
+ AND_WHEN("we wait for three events")
+ {
+ // We wait for three events here as if there are duplicates
+ // two of these must have the same value, but we don't
+ // know which two.
+ auto maybe_event1 = callback.pop_next_event();
+ auto maybe_event2 = callback.pop_next_event();
+ auto maybe_event3 = callback.pop_next_event();
+
+ REQUIRE(maybe_event1 != std::nullopt);
+ REQUIRE(maybe_event2 != std::nullopt);
+ REQUIRE(maybe_event3 != std::nullopt);
+
+ THEN("the events have different values")
+ {
+ Tango::DevLong value1;
+ *maybe_event1->attr_value >> value1;
+ Tango::DevLong value2;
+ *maybe_event2->attr_value >> value2;
+ Tango::DevLong value3;
+ *maybe_event3->attr_value >> value3;
+
+ REQUIRE(value1 != value2);
+ REQUIRE(value2 != value3);
+ }
+ }
+ }
+ }
+ }
+}
+
+template <typename Base>
+class DuplicateAttrConfEvent : public Base
+{
+ public:
+ using Base::Base;
+
+ ~DuplicateAttrConfEvent() override { }
+
+ void init_device() override
+ {
+ value = 0;
+ }
+
+ void read_attr(Tango::Attribute &att) override
+ {
+ value += 1;
+ att.set_value(&value);
+ }
+
+ static void attribute_factory(std::vector<Tango::Attr *> &attrs)
+ {
+ using Attr = TangoTest::AutoAttr<&DuplicateAttrConfEvent::read_attr>;
+ Tango::UserDefaultAttrProp props;
+ props.set_abs_change("1");
+
+ auto attr = new Attr(k_attr_name, Tango::DEV_LONG);
+ attr->set_default_properties(props);
+ attrs.push_back(attr);
+ }
+
+ private:
+ Tango::DevLong value{0};
+};
+
+TANGO_TEST_AUTO_DEV_TMPL_INSTANTIATE(DuplicateAttrConfEvent, 6)
+
+SCENARIO("Attribute config events are not duplicated when old clients are connected")
+{
+ int idlver = GENERATE(TangoTest::idlversion(6));
+ GIVEN("a device proxy to a simple IDLv" << idlver << " device")
+ {
+ TangoTest::Context ctx{"duplicate_event", "DuplicateAttrConfEvent", idlver};
+ std::shared_ptr<Tango::DeviceProxy> device = ctx.get_proxy();
+
+ REQUIRE(idlver == device->get_idl_version());
+
+ Tango::EventType ev_type = Tango::ATTR_CONF_EVENT;
+
+ WHEN("some old clients subscribe to attr config")
+ {
+ std::unique_ptr<Tango::DeviceProxy> admin = ctx.get_admin_proxy();
+
+ simulate_old_clients_subscription(admin.get(), ev_type, idlver, device->dev_name(), k_attr_name);
+
+ AND_WHEN("we subscribe to attr config events")
+ {
+ TangoTest::CallbackMock<Tango::AttrConfEventData> callback;
+
+ device->subscribe_event(k_attr_name, ev_type, &callback);
+
+ auto maybe_initial_event = callback.pop_next_event();
+ REQUIRE(maybe_initial_event != std::nullopt);
+
+ maybe_initial_event = callback.pop_next_event();
+
+ AND_WHEN("we configure the attribute")
+ {
+ auto ai = device->attribute_query(k_attr_name);
+ ai.events.ch_event.abs_change = "Not specified";
+
+ Tango::AttributeInfoListEx ail;
+ ail.push_back(ai);
+ REQUIRE_NOTHROW(device->set_attribute_config(ail));
+
+ THEN("we only get a single attr conf event")
+ {
+ auto maybe_event = callback.pop_next_event();
+ REQUIRE(maybe_event != std::nullopt);
+ REQUIRE(maybe_event->event == Tango::EventName[ev_type]);
+ REQUIRE(maybe_event->attr_conf != nullptr);
+
+ maybe_event = callback.pop_next_event();
+ REQUIRE(maybe_event == std::nullopt);
+ }
+ }
+ }
+ }
+ }
+}
Index: tango/lib/cpp/tests/CMakeLists.txt
===================================================================
--- tango.orig/lib/cpp/tests/CMakeLists.txt
+++ tango/lib/cpp/tests/CMakeLists.txt
@@ -312,6 +312,7 @@ tango_catch2_tests_create(
catch2_attr_polling.cpp
catch2_cmd_polling.cpp
catch2_connection.cpp
+ catch2_event_old_client.cpp
catch2_event_reconnection.cpp
catch2_test_dtypes.cpp
catch2_dev_state.cpp
|