summaryrefslogtreecommitdiffstats
path: root/sdevent/test/iotest.cpp
blob: 015f87be27b689076d73839cde616ac5d6ed6c8c (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
#include <gtest/gtest.h>
#include <thread>
#include <unistd.h>
#include "sdevent/event.hpp"
#include "sdevent/io.hpp"

TEST(IoTest, TestIo)
{
    // Validate an sd event io callback can be
    // constructed, added to an event loop, and
    // that the callback is invoked.

    auto loop = sdevent::event::newDefault();
    auto expected = 100;
    volatile auto actual = 0;

    std::array<int, 2> fds;
    auto rc = pipe(fds.data());
    ASSERT_EQ(rc, 0);

    auto t = std::thread([&loop](){loop.loop();});

    sdevent::event::io::IO io(
        loop,
        fds.data()[0],
        [&fds, &actual, &loop](auto& s)
        {
            auto tmp = 0;
            auto rc = read(fds.data()[0], &tmp, sizeof(tmp));
            ASSERT_GT(rc, 0);
            actual = tmp;
            loop.exit();
        });

    rc = write(fds.data()[1], &expected, sizeof(expected));
    ASSERT_GT(rc, 0);
    t.join();
    close(fds.data()[0]);
    close(fds.data()[1]);

    ASSERT_EQ(expected, actual);
}
OpenPOWER on IntegriCloud