Skip to content

Commit 4b952ec

Browse files
committed
refactor: move make_local_*_pair to test-only header
The public make_local_stream_pair / make_local_datagram_pair were primarily used to construct test fixtures; production callers exist in theory but are vanishingly rare compared to test and benchmark usage. PR #252's Windows implementation was specifically motivated by tests on IOCP. Move both helpers into include/boost/corosio/test/local_socket_pair.hpp under boost::corosio::test, alongside the existing templated stream variant that the perf benchmarks already use. The test/ helper drives bind+accept+connect via the public acceptor API, which works on every backend after PR #252 enabled AF_UNIX SOCK_STREAM on Windows IOCP -- so the bespoke socketpair-emulation Windows code in src/corosio/src/local_socket_pair.cpp is no longer needed. - Delete include/boost/corosio/local_socket_pair.hpp - Delete src/corosio/src/local_socket_pair.cpp - Remove the umbrella include in boost/corosio.hpp - Add make_local_datagram_pair to test/local_socket_pair.hpp (POSIX-only) - Migrate the three test files that called the public helpers (local_stream_socket.cpp, local_datagram_socket.cpp, reactor_paths.cpp) via using-declarations so call sites stay identical
1 parent d224941 commit 4b952ec

7 files changed

Lines changed: 109 additions & 332 deletions

File tree

‎include/boost/corosio.hpp‎

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,6 @@
3535
#include <boost/corosio/local_stream.hpp>
3636
#include <boost/corosio/local_stream_socket.hpp>
3737
#include <boost/corosio/local_stream_acceptor.hpp>
38-
#include <boost/corosio/local_socket_pair.hpp>
3938

4039
// local_datagram.hpp and local_datagram_socket.hpp are POSIX-only;
4140
// Windows does not support AF_UNIX datagram sockets (SOCK_DGRAM).

‎include/boost/corosio/local_socket_pair.hpp‎

Lines changed: 0 additions & 64 deletions
This file was deleted.

‎include/boost/corosio/test/local_socket_pair.hpp‎

Lines changed: 96 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,16 @@
1010
#ifndef BOOST_COROSIO_TEST_LOCAL_SOCKET_PAIR_HPP
1111
#define BOOST_COROSIO_TEST_LOCAL_SOCKET_PAIR_HPP
1212

13+
#include <boost/corosio/detail/platform.hpp>
1314
#include <boost/corosio/io_context.hpp>
1415
#include <boost/corosio/local_endpoint.hpp>
1516
#include <boost/corosio/local_stream_acceptor.hpp>
1617
#include <boost/corosio/local_stream_socket.hpp>
18+
19+
#if BOOST_COROSIO_POSIX
20+
#include <boost/corosio/local_datagram_socket.hpp>
21+
#endif
22+
1723
#include <boost/capy/ex/run_async.hpp>
1824
#include <boost/capy/task.hpp>
1925

@@ -29,11 +35,10 @@ namespace boost::corosio::test {
2935

3036
/** Create a connected pair of AF_UNIX stream sockets via bind+accept+connect.
3137
32-
Unlike the library-side @ref make_local_stream_pair (POSIX-only,
33-
socketpair-based), this helper drives the public acceptor API so
34-
it can produce native template wrappers like
35-
`native_local_stream_socket<Backend>` — the path benchmarks need
36-
to exercise the shadowed read_some/write_some/connect ops.
38+
Drives the public acceptor API so it can produce either concrete
39+
`local_stream_socket` or native template wrappers like
40+
`native_local_stream_socket<Backend>` for tests and benchmarks
41+
that need to exercise the shadowed read_some/write_some/connect ops.
3742
3843
@tparam Socket Concrete or native local stream socket type.
3944
@tparam Acceptor Matching acceptor type.
@@ -137,6 +142,92 @@ make_local_stream_pair(io_context& ctx)
137142
return {std::move(s1), std::move(s2)};
138143
}
139144

145+
#if BOOST_COROSIO_POSIX
146+
147+
/** Create a connected pair of AF_UNIX datagram sockets via bind+connect.
148+
149+
POSIX-only — Windows does not support AF_UNIX SOCK_DGRAM. Each
150+
socket binds an abstract or filesystem path and connects to the
151+
other, producing a symmetric pair suitable for testing
152+
`send`/`recv` and the connected `recv_from` path.
153+
154+
@param ctx I/O context backing both sockets.
155+
156+
@return Connected pair `{a, b}` where each side's `send` reaches
157+
the other's `recv`.
158+
*/
159+
inline std::pair<local_datagram_socket, local_datagram_socket>
160+
make_local_datagram_pair(io_context& ctx)
161+
{
162+
namespace fs = std::filesystem;
163+
164+
static std::random_device rd;
165+
static std::mt19937_64 gen{rd()};
166+
167+
auto pick_path = []() {
168+
for (int attempt = 0; attempt < 16; ++attempt)
169+
{
170+
std::string name = "co_dpair_";
171+
name += std::to_string(gen());
172+
auto candidate = fs::temp_directory_path() / name;
173+
std::error_code ec;
174+
if (fs::create_directory(candidate, ec))
175+
return (candidate / "s").string();
176+
}
177+
throw std::runtime_error("make_local_datagram_pair: temp path failed");
178+
};
179+
180+
auto path_a = pick_path();
181+
auto path_b = pick_path();
182+
183+
local_datagram_socket a(ctx);
184+
local_datagram_socket b(ctx);
185+
a.open();
186+
b.open();
187+
188+
if (auto ec = a.bind(local_endpoint(path_a)))
189+
throw std::runtime_error(
190+
"local_datagram_pair bind a failed: " + ec.message());
191+
if (auto ec = b.bind(local_endpoint(path_b)))
192+
throw std::runtime_error(
193+
"local_datagram_pair bind b failed: " + ec.message());
194+
195+
std::error_code ec_a, ec_b;
196+
bool done_a = false, done_b = false;
197+
198+
capy::run_async(ctx.get_executor())(
199+
[](local_datagram_socket& s, local_endpoint ep,
200+
std::error_code& ec_out, bool& done_out) -> capy::task<> {
201+
auto [ec] = co_await s.connect(ep);
202+
ec_out = ec;
203+
done_out = true;
204+
}(a, local_endpoint(path_b), ec_a, done_a));
205+
206+
capy::run_async(ctx.get_executor())(
207+
[](local_datagram_socket& s, local_endpoint ep,
208+
std::error_code& ec_out, bool& done_out) -> capy::task<> {
209+
auto [ec] = co_await s.connect(ep);
210+
ec_out = ec;
211+
done_out = true;
212+
}(b, local_endpoint(path_a), ec_b, done_b));
213+
214+
ctx.run();
215+
ctx.restart();
216+
217+
std::error_code rm_ec;
218+
fs::remove(fs::path(path_a), rm_ec);
219+
fs::remove(fs::path(path_a).parent_path(), rm_ec);
220+
fs::remove(fs::path(path_b), rm_ec);
221+
fs::remove(fs::path(path_b).parent_path(), rm_ec);
222+
223+
if (!done_a || ec_a || !done_b || ec_b)
224+
throw std::runtime_error("local_datagram_pair connect failed");
225+
226+
return {std::move(a), std::move(b)};
227+
}
228+
229+
#endif // BOOST_COROSIO_POSIX
230+
140231
} // namespace boost::corosio::test
141232

142233
#endif

0 commit comments

Comments
 (0)