Newer
Older
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
// SPDX-License-Identifier: LGPL-3.0-or-later
// Copyright © 2020-2021 Florian Fischer
#include <sys/eventfd.h> // for eventfd
#include <sys/types.h> // for ssize_t
#include <cassert> // for assert
#include <cerrno> // for EBADF, ECANCELED
#include <cstdint> // for uint64_t, int32_t
#include <cstdlib> // for exit, EXIT_SUCCESS
#include "Common.hpp" // for DIE_MSG_ERRNO, DIE_MSG
#include "io/Future.hpp" // for ReadFuture, CloseFuture, WriteFuture
using emper::io::CloseFuture;
using emper::io::ReadFuture;
using emper::io::WriteFuture;
static void successChain() {
int efd = eventfd(0, 0);
if (efd == -1) {
DIE_MSG_ERRNO("eventfd failed");
}
uint64_t write_buf = 42;
WriteFuture writeFuture(efd, &write_buf, sizeof(write_buf), 0);
uint64_t read_buf;
ReadFuture readFuture(efd, &read_buf, sizeof(read_buf), 0);
readFuture.setDependency(writeFuture);
CloseFuture closeFuture(efd);
closeFuture.setDependency(readFuture);
ssize_t res = closeFuture.submitAndWait();
if (res == -1) {
DIE_MSG_ERRNO("linked requests chain failed");
}
if (read_buf != 42) {
DIE_MSG("dependent read value differs from written value");
}
}
static void failureChain() {
// NOLINTNEXTLINE(modernize-avoid-c-arrays)
char buf[32];
ReadFuture invalidReadFuture(42, &buf, sizeof(buf), 0);
ReadFuture readFuture(0, &buf, sizeof(buf), 0);
readFuture.setDependency(invalidReadFuture);
int32_t res = readFuture.submitAndWait();
assert(res == -ECANCELED);
res = invalidReadFuture.wait();
assert(res == -EBADF);
}
void emperTest() {
successChain();
failureChain();
exit(EXIT_SUCCESS);
}