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
|
#include "catch2_common.h"
constexpr static Tango::DevBoolean k_initial_value = false;
constexpr static Tango::DevBoolean k_new_value = true;
constexpr static Tango::DevLong k_polling_period = TANGO_TEST_CATCH2_DEFAULT_POLL_PERIOD;
constexpr static const char *k_test_reason = "Test_Reason";
constexpr static const char *k_a_helpful_desc = "A helpful description";
template <class Base>
class AttrPollingCfg : public Base
{
public:
using Base::Base;
~AttrPollingCfg() override { }
void init_device() override
{
value = k_initial_value;
}
void read_attribute(Tango::Attribute &att)
{
att.set_value(&value);
}
Tango::DevBoolean is_attr_polled(Tango::DevString attr)
{
return Tango::DeviceImpl::is_attribute_polled(attr);
}
Tango::DevLong get_attr_poll_period(Tango::DevString attr)
{
return Tango::DeviceImpl::get_attribute_poll_period(attr);
}
static void attribute_factory(std::vector<Tango::Attr *> &attrs)
{
using Attr = TangoTest::AutoAttr<&AttrPollingCfg::read_attribute>;
attrs.push_back(new Attr("client_enabled_polling", Tango::DEV_BOOLEAN));
attrs.push_back(new Attr("server_enabled_polling", Tango::DEV_BOOLEAN));
attrs.back()->set_polling_period(k_polling_period);
}
static void command_factory(std::vector<Tango::Command *> &cmds)
{
cmds.push_back(new TangoTest::AutoCommand<&AttrPollingCfg::is_attr_polled>("IsAttrPolled"));
cmds.push_back(new TangoTest::AutoCommand<&AttrPollingCfg::get_attr_poll_period>("AttrPollPeriod"));
}
private:
Tango::DevBoolean value;
};
TANGO_TEST_AUTO_DEV_TMPL_INSTANTIATE(AttrPollingCfg, 4)
// TODO: Add case checking the client can enable polling when we have a database
SCENARIO("Attribute polling can be enabled")
{
int idlver = GENERATE(TangoTest::idlversion(4));
GIVEN("a device proxy to a IDLv" << idlver << " device")
{
TangoTest::Context ctx{"attr_polling", "AttrPollingCfg", idlver};
auto device = ctx.get_proxy();
REQUIRE(idlver == device->get_idl_version());
std::string attr = GENERATE(as<std::string>(), "client_enabled_polling", "server_enabled_polling");
bool setup_polling = attr.find("client") == 0;
AND_GIVEN("an attribute " << (setup_polling ? "that we enable polling for" : "with polling already enabled"))
{
if(setup_polling)
{
REQUIRE_NOTHROW(device->poll_attribute(attr, k_polling_period));
}
THEN("the device proxy reports the attribute is polled")
{
REQUIRE(device->is_attribute_polled(attr));
AND_THEN("the device server reports the attribute is polled")
{
Tango::DeviceData in, out;
in << attr;
REQUIRE_NOTHROW(out = device->command_inout("IsAttrPolled", in));
REQUIRE_THAT(out, TangoTest::AnyLikeContains(true));
}
}
THEN("the device proxy reports the correct polling period")
{
REQUIRE(device->get_attribute_poll_period(attr) == k_polling_period);
AND_THEN("the device server reports the correct polling period")
{
Tango::DeviceData in, out;
in << attr;
REQUIRE_NOTHROW(out = device->command_inout("AttrPollPeriod", in));
REQUIRE_THAT(out, TangoTest::AnyLikeContains(k_polling_period));
}
}
}
}
}
SCENARIO("Attribute polling period can be updated")
{
int idlver = GENERATE(TangoTest::idlversion(4));
GIVEN("a device proxy to a IDLv" << idlver << " device")
{
TangoTest::Context ctx{"attr_polling", "AttrPollingCfg", idlver};
auto device = ctx.get_proxy();
REQUIRE(idlver == device->get_idl_version());
AND_GIVEN("an attribute with polling already enabled")
{
std::string attr = "server_enabled_polling";
WHEN("the device proxy increases the polling period")
{
REQUIRE_NOTHROW(device->poll_attribute(attr, 2 * k_polling_period));
THEN("the device proxy reports the correct polling period")
{
REQUIRE(device->get_attribute_poll_period(attr) == 2 * k_polling_period);
AND_THEN("the device server reports the correct polling period")
{
Tango::DeviceData in, out;
in << attr;
REQUIRE_NOTHROW(out = device->command_inout("AttrPollPeriod", in));
REQUIRE_THAT(out, TangoTest::AnyLikeContains(2 * k_polling_period));
}
}
}
}
}
}
SCENARIO("Attribute polling can be disabled")
{
int idlver = GENERATE(TangoTest::idlversion(4));
GIVEN("a device proxy to a IDLv" << idlver << " device")
{
TangoTest::Context ctx{"attr_polling", "AttrPollingCfg", idlver};
auto device = ctx.get_proxy();
REQUIRE(idlver == device->get_idl_version());
AND_GIVEN("an attribute with polling already enabled")
{
std::string attr = "server_enabled_polling";
WHEN("the device proxy stops the polling")
{
REQUIRE_NOTHROW(device->stop_poll_attribute(attr));
THEN("the device proxy reports the attribute is no longer polled")
{
REQUIRE(!device->is_attribute_polled(attr));
AND_THEN("the device server reports the attribute is no longer polled")
{
Tango::DeviceData in, out;
in << attr;
REQUIRE_NOTHROW(out = device->command_inout("IsAttrPolled", in));
REQUIRE_THAT(out, TangoTest::AnyLikeContains(false));
}
}
}
}
}
}
template <class Base>
class AttrPollingEvents : public Base
{
public:
using Base::Base;
~AttrPollingEvents() override { }
void init_device() override
{
value = k_initial_value;
}
void read_attribute(Tango::Attribute &att)
{
if(throw_next)
{
throw_next = false;
TANGO_THROW_EXCEPTION(k_test_reason, k_a_helpful_desc);
}
att.set_value(&value);
}
void update_value()
{
value = k_new_value;
}
void throw_on_next_read()
{
throw_next = true;
}
static void attribute_factory(std::vector<Tango::Attr *> &attrs)
{
using Attr = TangoTest::AutoAttr<&AttrPollingEvents::read_attribute>;
attrs.push_back(new Attr("attr", Tango::DEV_BOOLEAN));
attrs.back()->set_polling_period(k_polling_period);
attrs.push_back(new Attr("attr_no_polling", Tango::DEV_BOOLEAN));
}
static void command_factory(std::vector<Tango::Command *> &cmds)
{
cmds.push_back(new TangoTest::AutoCommand<&AttrPollingEvents::throw_on_next_read>("ThrowOnNextRead"));
cmds.push_back(new TangoTest::AutoCommand<&AttrPollingEvents::update_value>("UpdateValue"));
}
private:
Tango::DevBoolean value;
bool throw_next = false;
};
TANGO_TEST_AUTO_DEV_TMPL_INSTANTIATE(AttrPollingEvents, 4)
SCENARIO("Polled attributes generate change events")
{
int idlver = GENERATE(TangoTest::idlversion(4));
GIVEN("a device proxy to a IDLv" << idlver << " device")
{
TangoTest::Context ctx{"attr_polling", "AttrPollingEvents", idlver};
auto device = ctx.get_proxy();
REQUIRE(idlver == device->get_idl_version());
AND_GIVEN("an attribute with polling enabled")
{
std::string attr{"attr"};
WHEN("we subscribe to change events for the attribute")
{
TangoTest::CallbackMock<Tango::EventData> callback;
REQUIRE_NOTHROW(device->subscribe_event(attr, Tango::CHANGE_EVENT, &callback));
THEN("we receive some events with the initial value")
{
// We get the following two initial events (the fact there
// are two is a side effect of the fix for #369):
//
// 1. In `subscribe_event` we do a `read_attribute` to
// generate the first event
// 2. Because we are the first subscriber to `"attr"`, the
// polling loop starts and sends an event because it is the
// first time it has read the attribute
//
// We do not assert anything about the second event, because
// it might not be present. There is a race condition in case 2
// above: If the polling loop triggers _after_ the subscription
// command (which sets up things on the server), but _before_ the
// ZMQ client has subscribed to the topic then we will miss the
// event.
auto maybe_initial_event = callback.pop_next_event();
REQUIRE(maybe_initial_event.has_value());
REQUIRE(!maybe_initial_event->err);
REQUIRE(maybe_initial_event->attr_value != nullptr);
REQUIRE_THAT(*maybe_initial_event->attr_value, TangoTest::AnyLikeContains(k_initial_value));
maybe_initial_event = callback.pop_next_event();
AND_WHEN("we write to the attribute")
{
device->command_inout("UpdateValue");
THEN("we receive an event with the new value")
{
auto maybe_new_event = callback.pop_next_event();
REQUIRE(maybe_new_event.has_value());
REQUIRE(!maybe_new_event->err);
REQUIRE(maybe_new_event->attr_value != nullptr);
REQUIRE_THAT(*maybe_new_event->attr_value, TangoTest::AnyLikeContains(k_new_value));
maybe_new_event = callback.pop_next_event(std::chrono::milliseconds{200});
REQUIRE(!maybe_new_event.has_value());
}
}
AND_WHEN("the attribute read throws an exception")
{
device->command_inout("ThrowOnNextRead");
THEN("we recieve an event with information about the exception")
{
auto maybe_ex_event = callback.pop_next_event();
REQUIRE(maybe_ex_event.has_value());
REQUIRE(maybe_ex_event->err);
REQUIRE(maybe_ex_event->errors.length() == 1);
REQUIRE(maybe_ex_event->errors[0].reason.in() == std::string{k_test_reason});
REQUIRE(maybe_ex_event->errors[0].desc.in() == std::string{k_a_helpful_desc});
AND_THEN("we recieve a good event when the next read succeeds")
{
auto maybe_good_event = callback.pop_next_event();
REQUIRE(maybe_good_event.has_value());
REQUIRE(!maybe_good_event->err);
REQUIRE(maybe_good_event->attr_value != nullptr);
REQUIRE_THAT(*maybe_good_event->attr_value,
TangoTest::AnyLikeContains(k_initial_value));
maybe_good_event = callback.pop_next_event(std::chrono::milliseconds{200});
REQUIRE(!maybe_good_event.has_value());
}
}
}
}
}
}
}
}
SCENARIO("Subscribing to change events for an attribute with no polling fails")
{
int idlver = GENERATE(TangoTest::idlversion(6));
GIVEN("a device proxy to a simple IDLv" << idlver << " device")
{
TangoTest::Context ctx{"no_polling", "AttrPollingEvents", idlver};
std::unique_ptr<Tango::DeviceProxy> device = ctx.get_proxy();
AND_GIVEN("an attribute with no polling")
{
std::string att{"attr_no_polling"};
TangoTest::CallbackMock<Tango::EventData> callback;
WHEN("we subscribe with stateless=false to change events")
{
THEN("the subscription fails")
{
using TangoTest::FirstErrorMatches, TangoTest::Reason;
REQUIRE_THROWS_MATCHES(device->subscribe_event(att, Tango::CHANGE_EVENT, &callback, false),
Tango::DevFailed,
FirstErrorMatches(Reason(Tango::API_AttributePollingNotStarted)));
}
}
WHEN("we subscribe with stateless=true to change events")
{
THEN("the subscription succeeds")
{
REQUIRE_NOTHROW(device->subscribe_event(att, Tango::CHANGE_EVENT, &callback, true));
AND_THEN("we receive an error event")
{
auto maybe_initial_event = callback.pop_next_event();
REQUIRE(maybe_initial_event.has_value());
REQUIRE(maybe_initial_event->err);
REQUIRE(std::string(Tango::API_AttributePollingNotStarted) ==
maybe_initial_event->errors[0].reason.in());
}
}
}
}
}
}
|