~alan-griffiths/mir/failed-migration-of-mir_acceptance_tests.TestDebugAPI

« back to all changes in this revision

Viewing changes to tests/unit-tests/test_glib_main_loop.cpp

  • Committer: Tarmac
  • Author(s): Alexandros Frantzis
  • Date: 2014-11-10 21:41:12 UTC
  • mfrom: (2026.1.8 glib-main-loop-fd)
  • Revision ID: tarmac-20141110214112-8mryou6qc4gy3lld
server: Add fd support to GLibMainLoop.

Approved by Alan Griffiths, PS Jenkins bot, Alberto Aguirre.

Show diffs side-by-side

added added

removed removed

Lines of Context:
18
18
 
19
19
#include "mir/glib_main_loop.h"
20
20
#include "mir_test/signal.h"
 
21
#include "mir_test/pipe.h"
21
22
 
22
23
#include <gtest/gtest.h>
23
24
#include <gmock/gmock.h>
200
201
    ASSERT_EQ(signum, handled_signum[1]);
201
202
    ASSERT_EQ(signum, handled_signum[2]);
202
203
}
 
204
 
 
205
TEST_F(GLibMainLoopTest, handles_fd)
 
206
{
 
207
    mt::Pipe p;
 
208
    char const data_to_write{'a'};
 
209
    int handled_fd{0};
 
210
    char data_read{0};
 
211
 
 
212
    ml.register_fd_handler(
 
213
        {p.read_fd()},
 
214
        this,
 
215
        [&handled_fd, &data_read, this](int fd)
 
216
        {
 
217
            handled_fd = fd;
 
218
            EXPECT_EQ(1, read(fd, &data_read, 1));
 
219
            ml.stop();
 
220
        });
 
221
 
 
222
    EXPECT_EQ(1, write(p.write_fd(), &data_to_write, 1));
 
223
 
 
224
    ml.run();
 
225
 
 
226
    EXPECT_EQ(data_to_write, data_read);
 
227
}
 
228
 
 
229
TEST_F(GLibMainLoopTest, multiple_fds_with_single_handler_handled)
 
230
{
 
231
    std::vector<mt::Pipe> const pipes(2);
 
232
    size_t const num_elems_to_send{10};
 
233
    std::vector<int> handled_fds;
 
234
    std::vector<size_t> elems_read;
 
235
    std::atomic<unsigned int> num_handled_fds{0};
 
236
 
 
237
    ml.register_fd_handler(
 
238
        {pipes[0].read_fd(), pipes[1].read_fd()},
 
239
        this,
 
240
        [&handled_fds, &elems_read, &num_handled_fds](int fd)
 
241
        {
 
242
            handled_fds.push_back(fd);
 
243
 
 
244
            size_t i;
 
245
            EXPECT_EQ(static_cast<ssize_t>(sizeof(i)),
 
246
                      read(fd, &i, sizeof(i)));
 
247
            elems_read.push_back(i);
 
248
 
 
249
            ++num_handled_fds;
 
250
        });
 
251
 
 
252
    std::thread fd_writing_thread{
 
253
        [this, num_elems_to_send, &pipes, &num_handled_fds]
 
254
        {
 
255
            for (size_t i = 0; i < num_elems_to_send; i++)
 
256
            {
 
257
                EXPECT_EQ(static_cast<ssize_t>(sizeof(i)),
 
258
                          write(pipes[i % pipes.size()].write_fd(), &i, sizeof(i)));
 
259
                while (num_handled_fds <= i) std::this_thread::yield();
 
260
            }
 
261
            ml.stop();
 
262
        }};
 
263
 
 
264
    ml.run();
 
265
 
 
266
    fd_writing_thread.join();
 
267
 
 
268
    ASSERT_EQ(num_elems_to_send, handled_fds.size());
 
269
    ASSERT_EQ(num_elems_to_send, elems_read.size());
 
270
 
 
271
    for (size_t i = 0; i < num_elems_to_send; i++)
 
272
    {
 
273
        EXPECT_EQ(pipes[i % pipes.size()].read_fd(), handled_fds[i]) << " index " << i;
 
274
        EXPECT_EQ(i, elems_read[i]) << " index " << i;
 
275
    }
 
276
}
 
277
 
 
278
TEST_F(GLibMainLoopTest, multiple_fd_handlers_are_called)
 
279
{
 
280
    std::vector<mt::Pipe> const pipes(3);
 
281
    std::vector<int> const elems_to_send{10,11,12};
 
282
    std::vector<int> handled_fds{0,0,0};
 
283
    std::vector<int> elems_read{0,0,0};
 
284
 
 
285
    ml.register_fd_handler(
 
286
        {pipes[0].read_fd()},
 
287
        this,
 
288
        [&handled_fds, &elems_read, this](int fd)
 
289
        {
 
290
            EXPECT_EQ(static_cast<ssize_t>(sizeof(elems_read[0])),
 
291
                      read(fd, &elems_read[0], sizeof(elems_read[0])));
 
292
            handled_fds[0] = fd;
 
293
            if (handled_fds[0] != 0 &&
 
294
                handled_fds[1] != 0 &&
 
295
                handled_fds[2] != 0)
 
296
            {
 
297
                ml.stop();
 
298
            }
 
299
        });
 
300
 
 
301
    ml.register_fd_handler(
 
302
        {pipes[1].read_fd()},
 
303
        this,
 
304
        [&handled_fds, &elems_read, this](int fd)
 
305
        {
 
306
            EXPECT_EQ(static_cast<ssize_t>(sizeof(elems_read[1])),
 
307
                      read(fd, &elems_read[1], sizeof(elems_read[1])));
 
308
            handled_fds[1] = fd;
 
309
            if (handled_fds[0] != 0 &&
 
310
                handled_fds[1] != 0 &&
 
311
                handled_fds[2] != 0)
 
312
            {
 
313
                ml.stop();
 
314
            }
 
315
        });
 
316
 
 
317
    ml.register_fd_handler(
 
318
        {pipes[2].read_fd()},
 
319
        this,
 
320
        [&handled_fds, &elems_read, this](int fd)
 
321
        {
 
322
            EXPECT_EQ(static_cast<ssize_t>(sizeof(elems_read[2])),
 
323
                      read(fd, &elems_read[2], sizeof(elems_read[2])));
 
324
            handled_fds[2] = fd;
 
325
            if (handled_fds[0] != 0 &&
 
326
                handled_fds[1] != 0 &&
 
327
                handled_fds[2] != 0)
 
328
            {
 
329
                ml.stop();
 
330
            }
 
331
        });
 
332
 
 
333
    EXPECT_EQ(static_cast<ssize_t>(sizeof(elems_to_send[0])),
 
334
              write(pipes[0].write_fd(), &elems_to_send[0], sizeof(elems_to_send[0])));
 
335
    EXPECT_EQ(static_cast<ssize_t>(sizeof(elems_to_send[1])),
 
336
              write(pipes[1].write_fd(), &elems_to_send[1], sizeof(elems_to_send[1])));
 
337
    EXPECT_EQ(static_cast<ssize_t>(sizeof(elems_to_send[2])),
 
338
              write(pipes[2].write_fd(), &elems_to_send[2], sizeof(elems_to_send[2])));
 
339
 
 
340
    ml.run();
 
341
 
 
342
    EXPECT_EQ(pipes[0].read_fd(), handled_fds[0]);
 
343
    EXPECT_EQ(pipes[1].read_fd(), handled_fds[1]);
 
344
    EXPECT_EQ(pipes[2].read_fd(), handled_fds[2]);
 
345
 
 
346
    EXPECT_EQ(elems_to_send[0], elems_read[0]);
 
347
    EXPECT_EQ(elems_to_send[1], elems_read[1]);
 
348
    EXPECT_EQ(elems_to_send[2], elems_read[2]);
 
349
}
 
350
 
 
351
TEST_F(GLibMainLoopTest,
 
352
       unregister_prevents_callback_and_does_not_harm_other_callbacks)
 
353
{
 
354
    mt::Pipe p1, p2;
 
355
    char const data_to_write{'a'};
 
356
    int p2_handler_executes{-1};
 
357
    char data_read{0};
 
358
 
 
359
    ml.register_fd_handler(
 
360
        {p1.read_fd()},
 
361
        this,
 
362
        [this](int)
 
363
        {
 
364
            FAIL() << "unregistered handler called";
 
365
            ml.stop();
 
366
        });
 
367
 
 
368
    ml.register_fd_handler(
 
369
        {p2.read_fd()},
 
370
        this+2,
 
371
        [&p2_handler_executes,&data_read,this](int fd)
 
372
        {
 
373
            p2_handler_executes = fd;
 
374
            EXPECT_EQ(1, read(fd, &data_read, 1));
 
375
            ml.stop();
 
376
        });
 
377
 
 
378
    ml.unregister_fd_handler(this);
 
379
 
 
380
    EXPECT_EQ(1, write(p1.write_fd(), &data_to_write, 1));
 
381
    EXPECT_EQ(1, write(p2.write_fd(), &data_to_write, 1));
 
382
 
 
383
    ml.run();
 
384
 
 
385
    EXPECT_EQ(data_to_write, data_read);
 
386
    EXPECT_EQ(p2.read_fd(), p2_handler_executes);
 
387
}
 
388
 
 
389
TEST_F(GLibMainLoopTest, unregister_does_not_close_fds)
 
390
{
 
391
    mt::Pipe p1, p2;
 
392
    char const data_to_write{'b'};
 
393
    char data_read{0};
 
394
 
 
395
    ml.register_fd_handler(
 
396
        {p1.read_fd()},
 
397
        this,
 
398
        [this](int)
 
399
        {
 
400
            FAIL() << "unregistered handler called";
 
401
            ml.stop();
 
402
        });
 
403
 
 
404
    ml.unregister_fd_handler(this);
 
405
 
 
406
    ml.register_fd_handler(
 
407
        {p1.read_fd()},
 
408
        this,
 
409
        [this,&data_read](int fd)
 
410
        {
 
411
            EXPECT_EQ(1, read(fd, &data_read, 1));
 
412
            ml.stop();
 
413
        });
 
414
 
 
415
    EXPECT_EQ(1, write(p1.write_fd(), &data_to_write, 1));
 
416
 
 
417
    ml.run();
 
418
 
 
419
    EXPECT_EQ(data_to_write, data_read);
 
420
}