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
|
#include "catch2_common.h"
#include "tango/server/except.h"
constexpr static const Tango::DevDouble k_alarm_level = 20;
constexpr static const Tango::DevDouble k_alarming_value = 99;
constexpr static const char *k_test_reason = "Test_Reason";
constexpr static const char *k_a_helpful_desc = "A helpful description";
template <class Base>
class AlarmDev : public Base
{
public:
using Base::Base;
~AlarmDev() override { }
void init_device() override { }
bool has_alarm_after_push()
{
Tango::MultiAttribute *multi_attr = Base::get_device_attr();
attr_value = k_alarming_value;
Base::push_change_event("attr", &attr_value);
return multi_attr->check_alarm("attr");
}
bool has_alarm_after_set()
{
Tango::MultiAttribute *multi_attr = Base::get_device_attr();
Tango::Attribute &attr = multi_attr->get_attr_by_name("attr");
attr_value = k_alarming_value;
attr.set_value(&attr_value);
return multi_attr->check_alarm("attr");
}
bool has_alarm_after_force()
{
Tango::MultiAttribute *multi_attr = Base::get_device_attr();
Tango::Attribute &attr = multi_attr->get_attr_by_name("attr");
attr.set_value_date_quality(
&attr_value, Tango::make_TimeVal(std::chrono::steady_clock::now()), Tango::ATTR_ALARM);
return multi_attr->check_alarm("attr");
}
bool has_alarm_after_second_check()
{
Tango::MultiAttribute *multi_attr = Base::get_device_attr();
Tango::Attribute &attr = multi_attr->get_attr_by_name("attr");
attr_value = k_alarming_value;
attr.set_value(&attr_value);
multi_attr->check_alarm("attr");
return multi_attr->check_alarm("attr");
}
bool has_alarm_after_push_except()
{
Tango::MultiAttribute *multi_attr = Base::get_device_attr();
try
{
TANGO_THROW_EXCEPTION(k_test_reason, k_a_helpful_desc);
}
catch(Tango::DevFailed &ex)
{
Base::push_change_event("attr", &ex);
}
return multi_attr->check_alarm("attr");
}
void read_attribute(Tango::Attribute &attr)
{
attr.set_value(&attr_value);
}
static void attribute_factory(std::vector<Tango::Attr *> &attrs)
{
Tango::UserDefaultAttrProp props;
props.set_max_alarm(std::to_string(k_alarm_level).c_str());
props.set_abs_change("0.1");
attrs.push_back(new TangoTest::AutoAttr<&AlarmDev::read_attribute>("attr", Tango::DEV_DOUBLE));
attrs.back()->set_default_properties(props);
attrs.back()->set_change_event(true, true);
}
static void command_factory(std::vector<Tango::Command *> &cmds)
{
cmds.push_back(new TangoTest::AutoCommand<&AlarmDev::has_alarm_after_set>("has_alarm_after_set"));
cmds.push_back(new TangoTest::AutoCommand<&AlarmDev::has_alarm_after_push>("has_alarm_after_push"));
cmds.push_back(
new TangoTest::AutoCommand<&AlarmDev::has_alarm_after_push_except>("has_alarm_after_push_except"));
cmds.push_back(new TangoTest::AutoCommand<&AlarmDev::has_alarm_after_force>("has_alarm_after_force"));
cmds.push_back(
new TangoTest::AutoCommand<&AlarmDev::has_alarm_after_second_check>("has_alarm_after_second_check"));
}
private:
Tango::DevDouble attr_value = 0.0;
};
TANGO_TEST_AUTO_DEV_TMPL_INSTANTIATE(AlarmDev, 1)
SCENARIO("check_alarm reports alarms correctly")
{
int idlver = GENERATE(TangoTest::idlversion(1));
GIVEN("a device proxy to a simple IDLv" << idlver << " device")
{
TangoTest::Context ctx{"alarm", "AlarmDev", idlver};
std::unique_ptr<Tango::DeviceProxy> device = ctx.get_proxy();
WHEN("we call check_alarm after setting alarming value")
{
Tango::DeviceData result;
REQUIRE_NOTHROW(result = device->command_inout("has_alarm_after_set"));
THEN("the command returns true")
{
using TangoTest::AnyLikeContains;
REQUIRE_THAT(result, AnyLikeContains(true));
}
}
if(idlver >= 4)
{
WHEN("we subscribe the change events for the attribute")
{
TangoTest::CallbackMock<Tango::EventData> callback;
REQUIRE_NOTHROW(device->subscribe_event("attr", Tango::CHANGE_EVENT, &callback));
// discard the initial events we get when we subscribe
auto maybe_initial_event = callback.pop_next_event();
REQUIRE(maybe_initial_event.has_value());
AND_WHEN("we call check_alarm after push_change_event")
{
Tango::DeviceData result;
REQUIRE_NOTHROW(result = device->command_inout("has_alarm_after_push"));
THEN("we should receive a change event")
{
auto maybe_event = callback.pop_next_event();
REQUIRE(maybe_event.has_value());
REQUIRE(!maybe_event->err);
REQUIRE(maybe_event->event == "change");
REQUIRE(maybe_event->attr_value != nullptr);
REQUIRE(maybe_event->attr_value->get_quality() == Tango::ATTR_ALARM);
AND_THEN("the command returns false")
{
using TangoTest::AnyLikeContains;
REQUIRE_THAT(result, AnyLikeContains(false));
}
}
}
AND_WHEN("we call check_alarm after pushing an exception")
{
Tango::DeviceData result;
REQUIRE_NOTHROW(result = device->command_inout("has_alarm_after_push_except"));
THEN("we should receive an error event")
{
auto maybe_event = callback.pop_next_event();
REQUIRE(maybe_event.has_value());
REQUIRE(maybe_event->err);
REQUIRE(maybe_event->errors[0].reason.in() == std::string{k_test_reason});
AND_THEN("the command returns false")
{
using TangoTest::AnyLikeContains;
REQUIRE_THAT(result, AnyLikeContains(false));
}
}
}
}
}
WHEN("we call check_alarm_after push_change_event without subscribing")
{
Tango::DeviceData result;
REQUIRE_NOTHROW(result = device->command_inout("has_alarm_after_push"));
AND_THEN("the command returns false")
{
using TangoTest::AnyLikeContains;
REQUIRE_THAT(result, AnyLikeContains(false));
}
}
WHEN("we call check_alarm after forcing the quality to alarm")
{
Tango::DeviceData result;
REQUIRE_NOTHROW(result = device->command_inout("has_alarm_after_force"));
THEN("the command returns true")
{
using TangoTest::AnyLikeContains;
REQUIRE_THAT(result, AnyLikeContains(true));
}
}
WHEN("we call check_alarm after forcing the quality to alarm")
{
Tango::DeviceData result;
REQUIRE_NOTHROW(result = device->command_inout("has_alarm_after_second_check"));
THEN("the command returns true")
{
using TangoTest::AnyLikeContains;
REQUIRE_THAT(result, AnyLikeContains(true));
}
}
}
}
|