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
|
// NOLINTBEGIN(*)
//
// Created by ingvord on 12/14/16.
//
#include <thread>
#include "cxx_common.h"
#undef SUITE_NAME
#define SUITE_NAME RecoZmqTestSuite
class RecoZmqTestSuite : public CxxTest::TestSuite
{
protected:
DeviceProxy *device1, *device2;
string device1_name, device2_name, device1_instance_name, device2_instance_name;
CountingCallBack<Tango::EventData> eventCallback;
public:
SUITE_NAME() :
device1_instance_name{"test"}, // TODO pass via cl
device2_instance_name{"test2"}
{
//
// Arguments check -------------------------------------------------
//
device1_name = CxxTest::TangoPrinter::get_param("device1");
device2_name = CxxTest::TangoPrinter::get_param("device20");
CxxTest::TangoPrinter::validate_args();
//
// Initialization --------------------------------------------------
//
try
{
device1 = new DeviceProxy(device1_name);
device2 = new DeviceProxy(device2_name);
// TODO start server 2 and set fallback point
TS_ASSERT_THROWS_NOTHING(CxxTest::TangoPrinter::start_server(device2_instance_name));
CxxTest::TangoPrinter::restore_set("test2/debian8/20 started.");
// sleep 18 && start_server "@INST_NAME@" &
thread(
[this]()
{
std::this_thread::sleep_for(std::chrono::seconds(18));
try
{
CxxTest::TangoPrinter::start_server(device1_instance_name);
}
catch(const std::runtime_error &ex)
{
std::cerr << "start_server failed in background thread: \"" << ex.what() << "\"\n";
}
})
.detach();
// sleep 62 && start_server "@INST_NAME@" &
thread(
[this]()
{
std::this_thread::sleep_for(std::chrono::seconds(62));
try
{
CxxTest::TangoPrinter::start_server(device1_instance_name);
}
catch(const std::runtime_error &ex)
{
std::cerr << "start_server failed in background thread: \"" << ex.what() << "\"\n";
}
})
.detach();
}
catch(CORBA::Exception &e)
{
Except::print_exception(e);
exit(-1);
}
}
virtual ~SUITE_NAME()
{
if(CxxTest::TangoPrinter::is_restore_set("test2/debian8/20 started."))
{
try
{
CxxTest::TangoPrinter::kill_server();
}
catch(const std::runtime_error &ex)
{
std::cerr << "kill_server failed during teardown: \"" << ex.what() << "\"\n";
}
}
try
{
CxxTest::TangoPrinter::start_server(device1_instance_name);
}
catch(const std::runtime_error &ex)
{
std::cerr << "start_server failed during teardown: \"" << ex.what() << "\"\n";
}
delete device1;
delete device2;
}
static SUITE_NAME *createSuite()
{
return new SUITE_NAME();
}
static void destroySuite(SUITE_NAME *suite)
{
delete suite;
}
//
// Tests -------------------------------------------------------
//
//
// Subscribe to a user event
//
void test_subscribe_to_user_event(void)
{
string att_name("event_change_tst");
const vector<string> filters;
eventCallback.reset_counts();
TS_ASSERT_THROWS_NOTHING(device1->subscribe_event(att_name, Tango::USER_EVENT, &eventCallback, filters));
//
// Fire one event
//
TS_ASSERT_THROWS_NOTHING(device1->command_inout("IOPushEvent"));
TS_ASSERT_THROWS_NOTHING(device1->command_inout("IOPushEvent"));
eventCallback.wait_for([&]() { return eventCallback.invocation_count() >= 3; });
TEST_LOG << "Callback execution before re-connection = " << eventCallback.invocation_count() << endl;
TEST_LOG << "Callback error before re-connection = " << eventCallback.error_count() << endl;
TS_ASSERT_EQUALS(3, eventCallback.invocation_count());
TS_ASSERT_EQUALS(0, eventCallback.error_count());
//
// Kill device server (using its admin device)
//
string adm_name = device1->adm_name();
DeviceProxy admin_dev(adm_name);
TS_ASSERT_THROWS_NOTHING(admin_dev.command_inout("kill"));
//
// Wait for some error and re-connection
//
eventCallback.wait_for([&]() { return eventCallback.success_count() >= 4; });
//
// Check error and re-connection
//
TEST_LOG << "Callback execution after re-connection = " << eventCallback.invocation_count() << endl;
TEST_LOG << "Callback error after re-connection = " << eventCallback.error_count() << endl;
TS_ASSERT_LESS_THAN_EQUALS(1, eventCallback.error_count());
TS_ASSERT_EQUALS(4, eventCallback.success_count());
//
// Fire another event
//
TS_ASSERT_THROWS_NOTHING(device1->command_inout("IOPushEvent"));
TS_ASSERT_THROWS_NOTHING(device1->command_inout("IOPushEvent"));
eventCallback.wait_for([&]() { return eventCallback.success_count() >= 6; });
TEST_LOG << "Callback execution after re-connection and event = " << eventCallback.invocation_count() << endl;
TEST_LOG << "Callback error after re-connection and event = " << eventCallback.error_count() << endl;
TS_ASSERT_EQUALS(6, eventCallback.success_count());
}
//
// Clear call back counters and kill device server once more
//
void test_clear_cb_kill_ds(void)
{
eventCallback.reset_counts();
string adm_name = device1->adm_name();
DeviceProxy admin_dev(adm_name);
TS_ASSERT_THROWS_NOTHING(admin_dev.command_inout("kill"));
//
// Wait for some error and re-connection
//
eventCallback.wait_for([&]() { return eventCallback.success_count() >= 1; });
//
// Check error and re-connection
//
TEST_LOG << "Callback execution after second re-connection = " << eventCallback.invocation_count() << endl;
TEST_LOG << "Callback error after second re-connection = " << eventCallback.error_count() << endl;
TS_ASSERT_LESS_THAN_EQUALS(1, eventCallback.error_count());
TS_ASSERT_EQUALS(1, eventCallback.success_count());
//
// Fire yet another event
//
TS_ASSERT_THROWS_NOTHING(device1->command_inout("IOPushEvent"));
eventCallback.wait_for([&]() { return eventCallback.success_count() >= 2; });
TEST_LOG << "Callback execution after second re-connection and event = " << eventCallback.invocation_count()
<< endl;
TEST_LOG << "Callback error after second re-connection and event = " << eventCallback.error_count() << endl;
TS_ASSERT_EQUALS(2, eventCallback.success_count());
TS_ASSERT_LESS_THAN_EQUALS(1, eventCallback.error_count());
}
};
// NOLINTEND(*)
|