File: catch2_event_reconnection.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 (177 lines) | stat: -rw-r--r-- 5,481 bytes parent folder | download | duplicates (3)
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
#include "catch2_common.h"
#include <regex>

#include <tango/internal/utils.h>

namespace
{
const std::string TestExceptReason = "Ahhh!";
const Tango::DevShort k_inital_short = 5678;
using CallbackMockType = TangoTest::CallbackMock<Tango::EventData>;

} // anonymous namespace

template <class Base>
class SimpleEventDevice : public Base
{
  public:
    using Base::Base;

    void init_device() override { }

    void push_change_event(Tango::DevString attr)
    {
        if(!strcmp(attr, "Short_attr"))
        {
            Base::push_change_event(attr, &short_value);
        }
        else
        {
            TANGO_THROW_EXCEPTION(TestExceptReason, "This is a test");
        }
    }

    void read_attribute(Tango::Attribute &att)
    {
        if(att.get_name() == "Short_attr")
        {
            att.set_value(&short_value);
        }
        else
        {
            TANGO_THROW_EXCEPTION(TestExceptReason, "This is a test");
        }
    }

    static void attribute_factory(std::vector<Tango::Attr *> &attrs)
    {
        auto short_attr = new TangoTest::AutoAttr<&SimpleEventDevice::read_attribute>("Short_attr", Tango::DEV_SHORT);
        short_attr->set_change_event(true, false);
        attrs.push_back(short_attr);
    }

    static void command_factory(std::vector<Tango::Command *> &cmds)
    {
        cmds.push_back(new TangoTest::AutoCommand<&SimpleEventDevice::push_change_event>("PushChangeEvent"));
    }

  private:
    Tango::DevShort short_value{k_inital_short};
};

/* Returns true if all occurrences of
 * Attribute::set_client_lib(N,change)
 * in 'input' use the same N
 */
bool checkSameClientLib(const std::string &input)
{
    std::regex re(R"(Attribute::set_client_lib\(([0-9]+),change\))");
    std::smatch match;
    std::string::const_iterator it = input.cbegin();

    bool seenFirst = false;
    int firstValue = Tango::detail::INVALID_IDL_VERSION;

    while(std::regex_search(it, input.cend(), match, re))
    {
        int val = parse_as<int>(match[1].str());

        if(!seenFirst)
        {
            firstValue = val;
            seenFirst = true;
        }
        else if(val != firstValue)
        {
            return false;
        }

        it = match.suffix().first;
    }

    if(!seenFirst)
    {
        return false;
    }

    return true;
}

TANGO_TEST_AUTO_DEV_TMPL_INSTANTIATE(SimpleEventDevice, 4)

SCENARIO("Event re-subscribes with the same IDL", "[slow]")
{
    const std::string log_path = TangoTest::get_current_log_file_path();

    // the previous full contents across GENERATE
    static std::string prev_contents;

    int idlver = GENERATE(TangoTest::idlversion(4));
    GIVEN("a device proxy to a simple IDLv" << idlver << " device")
    {
        TangoTest::Context ctx{"event_reconnection", "SimpleEventDevice", idlver};
        std::shared_ptr<Tango::DeviceProxy> device = ctx.get_proxy();

        REQUIRE(idlver == device->get_idl_version());

        AND_GIVEN("an change event subscription to that attribute")
        {
            TangoTest::CallbackMock<Tango::EventData> callback;
            auto event_id = device->subscribe_event("Short_attr", Tango::CHANGE_EVENT, &callback);

            auto maybe_event = callback.pop_next_event();
            REQUIRE(maybe_event != std::nullopt);

            WHEN("when we restart server")
            {
                ctx.stop_server();
                ctx.restart_server();

                WHEN("an change event is generated after another error event")
                {
                    using namespace Catch::Matchers;

                    auto maybe_event = callback.pop_next_event(std::chrono::seconds{20});

                    REQUIRE(maybe_event != std::nullopt);
                    REQUIRE(maybe_event->err);
                    REQUIRE(std::string(Tango::API_EventTimeout) == maybe_event->errors[0].reason.in());

                    maybe_event = callback.pop_next_event(std::chrono::seconds{20});

                    REQUIRE(maybe_event != std::nullopt);
                    REQUIRE(maybe_event->event == Tango::EventName[Tango::CHANGE_EVENT]);

                    THEN("an change event is generated after another error event")
                    {
                        std::string all_server_log = load_file(log_path);

                        std::string new_chunk;
                        if(all_server_log.size() > prev_contents.size())
                        {
                            new_chunk = all_server_log.substr(prev_contents.size());
                        }
                        else
                        {
                            // log was truncated or rotated, then treat entire file as new
                            new_chunk = all_server_log;
                        }
                        prev_contents = std::move(all_server_log);

                        // sanity-check: we should actually have something new
                        REQUIRE(!new_chunk.empty());

                        REQUIRE(checkSameClientLib(new_chunk));

                        std::regex re(R"(zmq_client_idl_to_event_data_version\(([0-9]+),change\))");
                        std::smatch m;
                        REQUIRE(std::regex_search(new_chunk, m, re));
                        int found = parse_as<int>(m[1].str());
                        CHECK(found == idlver);
                    }
                }
            }
            device->unsubscribe_event(event_id);
        }
    }
}