File: cxx_server_event.cpp

package info (click to toggle)
tango 10.0.2%2Bdfsg1-4
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 89,936 kB
  • sloc: cpp: 201,786; sh: 1,645; python: 953; java: 800; perl: 467; javascript: 447; xml: 325; makefile: 272; sql: 72; ruby: 24
file content (205 lines) | stat: -rw-r--r-- 6,154 bytes parent folder | download | duplicates (4)
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
// NOLINTBEGIN(*)

//
// Created by ingvord on 12/14/16.
//

#include "cxx_common.h"

#undef SUITE_NAME
#define SUITE_NAME ServerEventTestSuite

class ServerEventTestSuite : public CxxTest::TestSuite
{
  protected:
    DeviceProxy *device1, *device2;
    string device1_name, device2_name, device1_instance_name, device2_instance_name, full_ds_name;
    DevLong eve_id;

  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");
        full_ds_name = CxxTest::TangoPrinter::get_param("fulldsname");

        CxxTest::TangoPrinter::validate_args();

        //
        // Initialization --------------------------------------------------
        //

        try
        {
            device1 = new DeviceProxy(device1_name);
            device2 = new DeviceProxy(device2_name);

            // TODO start server 2 and set fallback point
            CxxTest::TangoPrinter::start_server(device2_instance_name);
            CxxTest::TangoPrinter::restore_set("test2/debian8/20 started.");
        }
        catch(std::runtime_error &ex)
        {
            std::cerr << "start_server failed: \"" << ex.what() << "\"\n";
        }
        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 -------------------------------------------------------
    //

    //
    // Ask the device server to subscribe to an event
    //
    void test_device_server_subscribe_to_event(void)
    {
        TEST_LOG << endl << "new DeviceProxy(" << device1->name() << ") returned" << endl << endl;

        vector<string> vs{device2_name, "Short_attr", "periodic"};

        DeviceData dd_in, dd_out;
        dd_in << vs;
        TS_ASSERT_THROWS_NOTHING(dd_out = device1->command_inout("IOSubscribeEvent", dd_in));
        dd_out >> eve_id;
    }

    //
    // Wait for event to be executed
    //

    void test_wait_event(void)
    {
        std::this_thread::sleep_for(std::chrono::seconds(3));

        DeviceData da;
        TS_ASSERT_THROWS_NOTHING(da = device1->command_inout("IOGetCbExecuted"));
        Tango::DevLong cb;
        da >> cb;

        TEST_LOG << "cb executed = " << cb << endl;
        TS_ASSERT_LESS_THAN_EQUALS(2, cb);
        TS_ASSERT_LESS_THAN_EQUALS(cb, 4);
    }

    //
    // Ask server to unsubsribe from event
    //
    void test_server_unsubscribes_from_event(void)
    {
        DeviceData dd_un;
        dd_un << eve_id;

        DeviceData da;
        TS_ASSERT_THROWS_NOTHING(device1->command_inout("IOUnSubscribeEvent", dd_un));
        TS_ASSERT_THROWS_NOTHING(da = device1->command_inout("IOGetCbExecuted"));

        Tango::DevLong cb;
        da >> cb;

        std::this_thread::sleep_for(std::chrono::seconds(2));
        TS_ASSERT_THROWS_NOTHING(da = device1->command_inout("IOGetCbExecuted"));
        Tango::DevLong cb2;
        da >> cb2;

        TS_ASSERT_EQUALS(cb2, cb);
    }

    /**
     * Tests that the client can still receive events after the device server is
     * shut down, renamed in the database and then restarted. This scenario used
     * to fail as reported in #679.
     */
    void test_reconnection_after_ds_instance_rename()
    {
        const std::string new_instance_name = "renamed_ds";
        const std::string old_ds_name = full_ds_name;

        size_t exec_part_end = full_ds_name.find("/");
        std::string new_ds_name = full_ds_name.substr(0, exec_part_end + 1);
        new_ds_name += new_instance_name;

        const std::string attribute_name = "event_change_tst";

        CountingCallBack<Tango::EventData> callback{};

        int subscription{};
        TS_ASSERT_THROWS_NOTHING(subscription = device1->subscribe_event(attribute_name, Tango::USER_EVENT, &callback));

        TS_ASSERT_THROWS_NOTHING(device1->command_inout("IOPushEvent"));
        callback.wait_for([&]() { return callback.invocation_count() >= 2; });
        TS_ASSERT_EQUALS(2, callback.invocation_count());
        TS_ASSERT_EQUALS(0, callback.error_count());

        TS_ASSERT_THROWS_NOTHING(CxxTest::TangoPrinter::kill_server());

        Database db{};
        db.rename_server(old_ds_name, new_ds_name);

        TS_ASSERT_THROWS_NOTHING(CxxTest::TangoPrinter::start_server(new_instance_name));
        // std::this_thread::sleep_for(std::chrono::seconds(EVENT_HEARTBEAT_PERIOD)); // Wait for reconnection

        callback.wait_for([&]() { return callback.invocation_count() >= 5; });
        TS_ASSERT_EQUALS(4, callback.invocation_count());
        TS_ASSERT_EQUALS(1, callback.error_count());

        TS_ASSERT_THROWS_NOTHING(device1->command_inout("IOPushEvent"));

        callback.wait_for([&]() { return callback.invocation_count() >= 6; });
        TS_ASSERT_EQUALS(5, callback.invocation_count());
        TS_ASSERT_EQUALS(1, callback.error_count());

        TS_ASSERT_THROWS_NOTHING(device1->unsubscribe_event(subscription));

        db.rename_server(new_ds_name, old_ds_name);
    }
};

// NOLINTEND(*)