summaryrefslogtreecommitdiff
path: root/tests/test_descriptor-set.cpp
blob: 443cdf58f3fc949235d442cb39793256eeb2c9ca (plain)
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
/*
 *  Copyright (c) 2000 - 2014 Samsung Electronics Co., Ltd All Rights Reserved
 *
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License
 */
/*
 * @file       test_descriptor-set.cpp
 * @author     Krzysztof Jackiewicz (k.jackiewicz@samsung.com)
 * @version    1.0
 */

#include <unistd.h>
#include <errno.h>
#include <string.h>

#include <thread>
#include <memory>

#include <boost/test/unit_test.hpp>
#include <boost/test/results_reporter.hpp>

#include <test_common.h>
#include <test_watched-thread.h>

#include <descriptor-set.h>
#include <dpl/errno_string.h>

using namespace CKM;

namespace {

const int POLL_TIMEOUT = 8000;
const int POLL_TIMEOUT_SHORT = 1000;

typedef std::unique_ptr<int[], std::function<void(int *)>> PipePtr;

const short POLLALL = std::numeric_limits<short>::max();

void closePipe(int* fd) {
    close(fd[0]);
    close(fd[1]);
}

/*
 * Declares pipe descriptor array
 * Creates pipe and checks for error
 * Wraps pipe in unique_ptr
 */
#define PIPE(fd) \
    int (fd)[2]; \
    BOOST_REQUIRE_MESSAGE(0 == pipe((fd)),"Pipe creation failed: " << GetErrnoString()); \
    PipePtr fd##Ptr((fd), closePipe);

void unexpectedCallback(int, short) {
    BOOST_FAIL("Unexpected callback");
}

void readFd(int fd, int expectedFd, short revents) {
    char buf[1];
    BOOST_REQUIRE_MESSAGE(fd == expectedFd, "Unexpected descriptor");
    BOOST_REQUIRE_MESSAGE(revents & POLLIN, "Unexpected event");
    BOOST_REQUIRE_MESSAGE(1 == TEMP_FAILURE_RETRY(read(fd,buf,1)),
                          "Pipe read failed" << GetErrnoString());
}

void writeFd(int fd, int expectedFd, short revents) {
    BOOST_REQUIRE_MESSAGE(fd == expectedFd, "Unexpected descriptor");
    BOOST_REQUIRE_MESSAGE(revents & POLLOUT, "Unexpected event");
    BOOST_REQUIRE_MESSAGE(1 == TEMP_FAILURE_RETRY(write(fd,"j",1)),
                          "Pipe writing failed" << GetErrnoString());
}

} // anonymous namespace

BOOST_AUTO_TEST_SUITE(DESCRIPTOR_SET_TEST)

/*
 * Wait on empty descriptor set. Function should return immediately.
 */
BOOST_AUTO_TEST_CASE(T010_Empty) {
    DescriptorSet descriptors;

    BOOST_REQUIRE_NO_THROW(descriptors.wait(POLL_TIMEOUT));
}

/*
 * Add and remove (twice) descriptor. Wait on empty set. No callback should be called. wait() should
 * return immediately.
 */
BOOST_AUTO_TEST_CASE(T020_AddRemove) {
    DescriptorSet descriptors;
    descriptors.add(10, POLLALL, unexpectedCallback);
    descriptors.remove(10);
    descriptors.remove(10);

    BOOST_REQUIRE_NO_THROW(descriptors.wait(POLL_TIMEOUT));
}

/*
 * Add 2 descriptors and purge all. Wait on empty set. No callback should be called. wait() should
 * return immediately.
 */
BOOST_AUTO_TEST_CASE(T030_AddPurge) {
    DescriptorSet descriptors;
    descriptors.add(10, POLLALL, unexpectedCallback);
    descriptors.add(20, POLLALL, unexpectedCallback);
    descriptors.purge();

    BOOST_REQUIRE_NO_THROW(descriptors.wait(POLL_TIMEOUT));
}

/*
 * Add pipe[1] descriptor and wait for write possibility. Provided callback should be called
 * immediately.
 */
BOOST_AUTO_TEST_CASE(T040_Callback) {
    DescriptorSet descriptors;
    bool callback = false;

    PIPE(fd);

    descriptors.add(fd[1],POLLALL, [&callback](int, short revents)
    {
        callback = true;
        BOOST_REQUIRE_MESSAGE(revents & POLLOUT, "Not able to write");
    });

    BOOST_REQUIRE_NO_THROW(descriptors.wait(POLL_TIMEOUT));
    BOOST_REQUIRE_MESSAGE(callback, "Callback was not called");
}

/*
 * Add pipe[1] descriptor twice with different callbacks. The first one should be overwritten and
 * shouldn't be called. The second one should be called instead.
 */
BOOST_AUTO_TEST_CASE(T050_DoubleAdd) {
    DescriptorSet descriptors;
    bool callback = false;

    PIPE(fd);

    descriptors.add(fd[1], POLLALL, unexpectedCallback);
    descriptors.add(fd[1], POLLALL, [&callback](int, short revents)
    {
        callback = true;
        BOOST_REQUIRE_MESSAGE(revents & POLLOUT, "Not able to write");
    });

    BOOST_REQUIRE_NO_THROW(descriptors.wait(POLL_TIMEOUT));
    BOOST_REQUIRE_MESSAGE(callback, "Callback was not called");
}

/*
 * Add pipe[0] descriptor and wait. Callback should not be called. Instead the 8s timeout should
 * occur and a proper exception should be thrown.
 */
BOOST_AUTO_TEST_CASE(T060_Timeout) {
    DescriptorSet descriptors;

    PIPE(fd);

    descriptors.add(fd[0],POLLALL, unexpectedCallback);

    BOOST_REQUIRE_THROW(descriptors.wait(POLL_TIMEOUT_SHORT), CKM::DescriptorSet::Timeout);
}

/*
 * Create pipe and try to write it. Start thread that will read it.
 */
BOOST_AUTO_TEST_CASE(T070_Write) {
    DescriptorSet descriptors;
    bool callback = false;

    PIPE(fd);

    descriptors.add(fd[1],POLLOUT, [&fd, &callback](int desc, short revents)
    {
        callback = true;
        writeFd(desc, fd[1], revents);
    } );

    {
        auto thread = CreateWatchedThread([fd]
        {
            char buf[1];
            ssize_t tmp = TEMP_FAILURE_RETRY(read(fd[0], buf, 1));
            THREAD_REQUIRE_MESSAGE(tmp == 1, "Pipe reading failed " << GetErrnoString());
        });

        BOOST_REQUIRE_NO_THROW(descriptors.wait(POLL_TIMEOUT));
    }

    BOOST_REQUIRE_MESSAGE(callback, "Callback not called");
}

/*
 * Create pipe and try to read it. Start thread that will write it.
 */
BOOST_AUTO_TEST_CASE(T080_Read) {
    DescriptorSet descriptors;
    bool callback = false;

    PIPE(fd);

    descriptors.add(fd[0],POLLIN, [&](int desc, short revents)
    {
        callback = true;
        readFd(desc, fd[0], revents);
    } );

    {
        auto thread = CreateWatchedThread([fd]
        {
            ssize_t tmp = TEMP_FAILURE_RETRY(write(fd[1], "j", 1));
            THREAD_REQUIRE_MESSAGE(tmp == 1, "Pipe writing failed " << GetErrnoString());
        });

        BOOST_REQUIRE_NO_THROW(descriptors.wait(POLL_TIMEOUT));
    }

    BOOST_REQUIRE_MESSAGE(callback, "Callback not called");
}

/*
 * Create two pipes. Try to read first one. Start the thread that writes it. In the callback read
 * the pipe, remove it from the descriptor set and try to write the second pipe. The thread will
 * read it. In second pipe callback remove the second pipe descriptor from the set.
 */
BOOST_AUTO_TEST_CASE(T090_WriteAfterRead) {
    DescriptorSet descriptors;
    bool callback1 = false;
    bool callback2 = false;

    PIPE(fd);
    PIPE(fd2);

    descriptors.add(fd[0],POLLIN, [&](int desc, short revents)
    {
        callback1 = true;
        readFd(desc, fd[0], revents);

        descriptors.remove(desc);
        descriptors.add(fd2[1],POLLOUT, [&](int desc, short revents) {
            callback2 = true;
            writeFd(desc, fd2[1], revents);
            descriptors.remove(desc);
        } );
    } );

    {
        auto thread = CreateWatchedThread([fd,fd2]
        {
            ssize_t tmp = TEMP_FAILURE_RETRY(write(fd[1], "j", 1));
            BOOST_REQUIRE_MESSAGE(tmp == 1, "Pipe writing failed " << GetErrnoString());

            char buf[1];
            tmp = TEMP_FAILURE_RETRY(read(fd2[0], buf, 1));
            THREAD_REQUIRE_MESSAGE(tmp == 1, "Pipe reading failed " << GetErrnoString());
        });

        BOOST_REQUIRE_NO_THROW(descriptors.wait(POLL_TIMEOUT));
        BOOST_REQUIRE_NO_THROW(descriptors.wait(POLL_TIMEOUT));
    }

    BOOST_REQUIRE_MESSAGE(callback1, "First callback not called");
    BOOST_REQUIRE_MESSAGE(callback2, "Second callback not called");
}

BOOST_AUTO_TEST_SUITE_END()