forked from mutouyun/cpp-ipc
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest_ipc.cpp
executable file
·185 lines (152 loc) · 5.59 KB
/
test_ipc.cpp
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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
#include <vector>
#include <iostream>
#include <mutex>
#include <atomic>
#include <cstring>
#include "libipc/ipc.h"
#include "libipc/buffer.h"
#include "libipc/memory/resource.h"
#include "test.h"
#include "thread_pool.h"
#include "capo/random.hpp"
using namespace ipc;
namespace {
constexpr int LoopCount = 10000;
constexpr int MultiMax = 8;
constexpr int TestBuffMax = 65536;
struct msg_head {
int id_;
};
class rand_buf : public buffer {
public:
rand_buf() {
int size = capo::random<>{(int)sizeof(msg_head), TestBuffMax}();
*this = buffer(new char[size], size, [](void * p, std::size_t) {
delete [] static_cast<char *>(p);
});
}
rand_buf(msg_head const &msg) {
*this = buffer(new msg_head{msg}, sizeof(msg), [](void * p, std::size_t) {
delete static_cast<msg_head *>(p);
});
}
rand_buf(rand_buf &&) = default;
rand_buf(rand_buf const & rhs) {
if (rhs.empty()) return;
void * mem = new char[rhs.size()];
std::memcpy(mem, rhs.data(), rhs.size());
*this = buffer(mem, rhs.size(), [](void * p, std::size_t) {
delete [] static_cast<char *>(p);
});
}
rand_buf(buffer && rhs)
: buffer(std::move(rhs)) {
}
void set_id(int k) noexcept {
get<msg_head *>()->id_ = k;
}
int get_id() const noexcept {
return get<msg_head *>()->id_;
}
using buffer::operator=;
};
template <relat Rp, relat Rc, trans Ts>
void test_basic(char const * name) {
using que_t = chan<Rp, Rc, Ts>;
rand_buf test1, test2;
que_t que1 { name };
EXPECT_FALSE(que1.send(test1));
EXPECT_FALSE(que1.try_send(test2));
que_t que2 { que1.name(), ipc::receiver };
ASSERT_TRUE(que1.send(test1));
ASSERT_TRUE(que1.try_send(test2));
EXPECT_EQ(que2.recv(), test1);
EXPECT_EQ(que2.recv(), test2);
}
class data_set {
std::vector<rand_buf> datas_;
public:
data_set() {
datas_.resize(LoopCount);
for (int i = 0; i < LoopCount; ++i) {
datas_[i].set_id(i);
}
}
std::vector<rand_buf> const &get() const noexcept {
return datas_;
}
} const data_set__;
template <relat Rp, relat Rc, trans Ts, typename Que = chan<Rp, Rc, Ts>>
void test_sr(char const * name, int s_cnt, int r_cnt) {
ipc_ut::sender().start(static_cast<std::size_t>(s_cnt));
ipc_ut::reader().start(static_cast<std::size_t>(r_cnt));
std::atomic_thread_fence(std::memory_order_seq_cst);
ipc_ut::test_stopwatch sw;
for (int k = 0; k < s_cnt; ++k) {
ipc_ut::sender() << [name, &sw, r_cnt, k] {
Que que { name, ipc::sender };
ASSERT_TRUE(que.wait_for_recv(r_cnt));
sw.start();
for (int i = 0; i < (int)data_set__.get().size(); ++i) {
ASSERT_TRUE(que.send(data_set__.get()[i]));
}
};
}
for (int k = 0; k < r_cnt; ++k) {
ipc_ut::reader() << [name] {
Que que { name, ipc::receiver };
for (;;) {
rand_buf got { que.recv() };
ASSERT_FALSE(got.empty());
int i = got.get_id();
if (i == -1) {
return;
}
ASSERT_TRUE((i >= 0) && (i < (int)data_set__.get().size()));
auto const &data_set = data_set__.get()[i];
if (data_set != got) {
printf("data_set__.get()[%d] != got, size = %zd/%zd\n",
i, data_set.size(), got.size());
ASSERT_TRUE(false);
}
}
};
}
ipc_ut::sender().wait_for_done();
Que que { name };
ASSERT_TRUE(que.wait_for_recv(r_cnt));
for (int k = 0; k < r_cnt; ++k) {
que.send(rand_buf{msg_head{-1}});
}
ipc_ut::reader().wait_for_done();
sw.print_elapsed<std::chrono::microseconds>(s_cnt, r_cnt, (int)data_set__.get().size(), name);
}
} // internal-linkage
TEST(IPC, basic) {
test_basic<relat::single, relat::single, trans::unicast >("ssu");
//test_basic<relat::single, relat::multi , trans::unicast >("smu");
//test_basic<relat::multi , relat::multi , trans::unicast >("mmu");
test_basic<relat::single, relat::multi , trans::broadcast>("smb");
test_basic<relat::multi , relat::multi , trans::broadcast>("mmb");
}
TEST(IPC, 1v1) {
test_sr<relat::single, relat::single, trans::unicast >("ssu", 1, 1);
//test_sr<relat::single, relat::multi , trans::unicast >("smu", 1, 1);
//test_sr<relat::multi , relat::multi , trans::unicast >("mmu", 1, 1);
test_sr<relat::single, relat::multi , trans::broadcast>("smb", 1, 1);
test_sr<relat::multi , relat::multi , trans::broadcast>("mmb", 1, 1);
}
TEST(IPC, 1vN) {
//test_sr<relat::single, relat::multi , trans::unicast >("smu", 1, MultiMax);
//test_sr<relat::multi , relat::multi , trans::unicast >("mmu", 1, MultiMax);
test_sr<relat::single, relat::multi , trans::broadcast>("smb", 1, MultiMax);
test_sr<relat::multi , relat::multi , trans::broadcast>("mmb", 1, MultiMax);
}
TEST(IPC, Nv1) {
//test_sr<relat::multi , relat::multi , trans::unicast >("mmu", MultiMax, 1);
test_sr<relat::multi , relat::multi , trans::broadcast>("mmb", MultiMax, 1);
}
TEST(IPC, NvN) {
//test_sr<relat::multi , relat::multi , trans::unicast >("mmu", MultiMax, MultiMax);
test_sr<relat::multi , relat::multi , trans::broadcast>("mmb", MultiMax, MultiMax);
}