-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtimerlat_pipe_load_lib_test.cc
124 lines (102 loc) · 3.61 KB
/
timerlat_pipe_load_lib_test.cc
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
#include "timerlat_pipe_load.hh"
#include <sched.h>
#include <signal.h>
#include <cstdint>
#include <exception>
#include <stdexcept>
#include "gmock/gmock-matchers.h"
#include "gtest/gtest.h"
#include "gtest/internal/gtest-port.h"
using namespace std;
using namespace std::chrono;
using namespace std::chrono_literals;
namespace fs = std::filesystem;
namespace timerlat_load {
namespace local_testing {
TEST(TimerlatPipeLoadTest, CreateResponder) {
FifoTimer ft;
ASSERT_TRUE(fs::create_directory(ft.fifodir()));
const std::string fifoname{ft.fifodir() + "/myfifo"};
EXPECT_NE(-1, mkfifoat(-1 /*NOT USED*/, fifoname.c_str(), 0777));
// The writer must be created before the ifstream, as otherwise open() will
// block forever.
std::function<void(const std::string &)> fn = responding_fn;
EXPECT_TRUE(ft.create_responder(fn));
ft.ifs = std::ifstream{fifoname, std::ifstream::in};
EXPECT_TRUE(ft.ifs.good());
}
void do_nothing(const std::string ¬hing) {
size_t ignored = nothing.length();
ignored++;
// SIGCHLD is ignored. Substituting SIGPIPE causes the test to exit with error
// 141.
// https://stackoverflow.com/questions/18880606/socket-connection-getting-closed-abruptly-with-code-141
// the shell adds 128 so you can distinguish between exit codes (usually low
// numbers) and fatal signals (also low numbers). Otherwise death by SIGHUP
// would look the same as exit(1).
raise(SIGCHLD);
return;
}
TEST(TimerlatPipeLoadTest, MinimalResponder) {
FifoTimer ft;
std::function<void(const std::string &)> fn = do_nothing;
EXPECT_TRUE(ft.create_responder(fn));
}
void do_nothing_fail(const std::string ¬hing) {
size_t ignored = nothing.length();
ignored++;
raise(SIGPIPE);
return;
}
TEST(TimerlatPipeLoadTest, BadMinimalResponder) {
FifoTimer ft;
sigset_t set;
sigemptyset(&set);
sigaddset(&set, SIGPIPE);
EXPECT_EQ(0, pthread_sigmask(SIG_BLOCK, &set, nullptr));
std::function<void(const std::string &)> fn = do_nothing_fail;
EXPECT_TRUE(ft.create_responder(fn));
// The following causes SIGABRT. The catch of the exception doesn't work,
// probably because googletest receives it rather than the test's own code.
// std::function<void(const std::string &)> empty_fn;
// try {
// EXPECT_FALSE(ft.create_responder(empty_fn));
// } catch (std::bad_function_call &e) {
// std::cerr << "Oops, an exception." << std::endl;
// }
std::function<void(const std::string &)> empty_fn;
::testing::internal::CaptureStderr();
EXPECT_FALSE(ft.create_responder(empty_fn));
const std::string output = ::testing::internal::GetCapturedStderr();
EXPECT_THAT(output, ::testing::HasSubstr(
"Supplied thread function is not executable."));
}
TEST(TimerlatPipeLoadTest, Start) {
FifoTimer ft;
ASSERT_TRUE(ft.start());
EXPECT_TRUE(ft.ifs.good());
const std::string fifoname{ft.fifodir() + "/myfifo"};
ASSERT_TRUE(fs::exists(fifoname));
ASSERT_TRUE(fs::is_fifo(fifoname));
ifstream rfs(fifoname);
ASSERT_TRUE(rfs.good());
char pipe_buffer[PIPE_BUF_SIZE];
ft.ifs.readsome(pipe_buffer, PIPE_BUF_SIZE);
EXPECT_EQ(static_cast<size_t>(ft.ifs.gcount()), PIPE_BUF_SIZE);
}
TEST(TimerlatPipeLoadTest, CalculateDelay) {
FifoTimer ft;
ASSERT_TRUE(ft.start());
ofstream tlfs(ft.fifodir() + "/rtlafile");
string trash{"111111111"};
tlfs.write(trash.c_str(), trash.length());
const size_t pos = tlfs.tellp();
ASSERT_EQ(trash.length(), pos);
tlfs.close();
// Re-open as ifstream.
ifstream tlfs0(ft.fifodir() + "/rtlafile");
ft.calculate_roundtrip_delays(tlfs0);
tlfs0.close();
}
} // namespace local_testing
} // namespace timerlat_load