blob: 015f87be27b689076d73839cde616ac5d6ed6c8c [file] [log] [blame]
Brad Bishopa51f8fe2017-06-15 15:16:18 -04001#include <gtest/gtest.h>
2#include <thread>
3#include <unistd.h>
4#include "sdevent/event.hpp"
5#include "sdevent/io.hpp"
6
7TEST(IoTest, TestIo)
8{
9 // Validate an sd event io callback can be
10 // constructed, added to an event loop, and
11 // that the callback is invoked.
12
13 auto loop = sdevent::event::newDefault();
14 auto expected = 100;
15 volatile auto actual = 0;
16
17 std::array<int, 2> fds;
18 auto rc = pipe(fds.data());
19 ASSERT_EQ(rc, 0);
20
21 auto t = std::thread([&loop](){loop.loop();});
22
23 sdevent::event::io::IO io(
24 loop,
25 fds.data()[0],
26 [&fds, &actual, &loop](auto& s)
27 {
28 auto tmp = 0;
29 auto rc = read(fds.data()[0], &tmp, sizeof(tmp));
30 ASSERT_GT(rc, 0);
31 actual = tmp;
32 loop.exit();
33 });
34
35 rc = write(fds.data()[1], &expected, sizeof(expected));
36 ASSERT_GT(rc, 0);
37 t.join();
38 close(fds.data()[0]);
39 close(fds.data()[1]);
40
41 ASSERT_EQ(expected, actual);
42}