Skip to content

Commit c69133d

Browse files
committed
http has a copy of run_sync
1 parent ae63860 commit c69133d

6 files changed

Lines changed: 138 additions & 15 deletions

File tree

CMakePresets.json

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
{
2+
"version": 6,
3+
"cmakeMinimumRequired": {
4+
"major": 3,
5+
"minor": 25,
6+
"patch": 0
7+
},
8+
"configurePresets": [],
9+
"buildPresets": []
10+
}

src/server/route_handler.cpp

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@
99

1010
#include <boost/http/server/route_handler.hpp>
1111
#include <boost/http/string_body.hpp>
12-
#include <boost/capy/ex/run_sync.hpp>
1312

1413
namespace boost {
1514
namespace http {

test/unit/run_sync.hpp

Lines changed: 108 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,108 @@
1+
//
2+
// Copyright (c) 2025 Vinnie Falco (vinnie dot falco at gmail dot com)
3+
//
4+
// Distributed under the Boost Software License, Version 1.0. (See accompanying
5+
// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
6+
//
7+
// Official repository: https://github.com/cppalliance/http
8+
//
9+
10+
#ifndef BOOST_HTTP_TEST_RUN_SYNC_HPP
11+
#define BOOST_HTTP_TEST_RUN_SYNC_HPP
12+
13+
#include <boost/capy/coro.hpp>
14+
#include <boost/capy/ex/execution_context.hpp>
15+
#include <boost/capy/task.hpp>
16+
17+
#include <coroutine>
18+
#include <exception>
19+
#include <type_traits>
20+
#include <utility>
21+
22+
namespace boost {
23+
namespace http {
24+
25+
// Trivial execution context for synchronous execution.
26+
class sync_context : public capy::execution_context
27+
{
28+
};
29+
30+
// Trivial executor for synchronous execution.
31+
struct sync_executor
32+
{
33+
static sync_context ctx_;
34+
35+
bool operator==(sync_executor const&) const noexcept { return true; }
36+
capy::execution_context& context() const noexcept { return ctx_; }
37+
void on_work_started() const noexcept {}
38+
void on_work_finished() const noexcept {}
39+
40+
capy::coro dispatch(capy::coro h) const
41+
{
42+
return h;
43+
}
44+
45+
void post(capy::coro h) const
46+
{
47+
h.resume();
48+
}
49+
};
50+
51+
inline sync_context sync_executor::ctx_;
52+
53+
// Synchronous task runner for tests.
54+
class sync_runner
55+
{
56+
public:
57+
sync_runner() = default;
58+
59+
sync_runner(sync_runner const&) = delete;
60+
sync_runner& operator=(sync_runner const&) = delete;
61+
sync_runner(sync_runner&&) = default;
62+
sync_runner& operator=(sync_runner&&) = default;
63+
64+
template<typename T>
65+
T operator()(capy::task<T> t) &&
66+
{
67+
auto h = t.handle();
68+
t.release();
69+
sync_executor ex;
70+
71+
h.promise().set_continuation(std::noop_coroutine(), ex);
72+
h.promise().set_executor(ex);
73+
74+
ex.dispatch(capy::coro{h}).resume();
75+
76+
std::exception_ptr ep = h.promise().exception();
77+
78+
if constexpr (std::is_void_v<T>)
79+
{
80+
h.destroy();
81+
if (ep)
82+
std::rethrow_exception(ep);
83+
}
84+
else
85+
{
86+
if (ep)
87+
{
88+
h.destroy();
89+
std::rethrow_exception(ep);
90+
}
91+
auto result = std::move(h.promise().result());
92+
h.destroy();
93+
return result;
94+
}
95+
}
96+
};
97+
98+
inline
99+
sync_runner
100+
run_sync()
101+
{
102+
return sync_runner{};
103+
}
104+
105+
} // namespace http
106+
} // namespace boost
107+
108+
#endif

test/unit/server/flat_router.cpp

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,8 @@
1313
// Full functional tests are in beast2/test/unit/server/router.cpp
1414

1515
#include <boost/http/server/router.hpp>
16-
#include <boost/capy/ex/run_sync.hpp>
1716

17+
#include "run_sync.hpp"
1818
#include "test_suite.hpp"
1919

2020
namespace boost {
@@ -39,11 +39,11 @@ struct flat_router_test
3939
flat_router fr2(fr1);
4040

4141
params req;
42-
capy::run_sync()(fr1.dispatch(
42+
run_sync()(fr1.dispatch(
4343
http::method::get, urls::url_view("/"), req));
4444
BOOST_TEST_EQ(*counter, 1);
4545

46-
capy::run_sync()(fr2.dispatch(
46+
run_sync()(fr2.dispatch(
4747
http::method::get, urls::url_view("/"), req));
4848
BOOST_TEST_EQ(*counter, 2);
4949
}
@@ -70,11 +70,11 @@ struct flat_router_test
7070
fr2 = fr1;
7171

7272
params req;
73-
capy::run_sync()(fr1.dispatch(
73+
run_sync()(fr1.dispatch(
7474
http::method::get, urls::url_view("/"), req));
7575
BOOST_TEST_EQ(*counter, 1);
7676

77-
capy::run_sync()(fr2.dispatch(
77+
run_sync()(fr2.dispatch(
7878
http::method::get, urls::url_view("/"), req));
7979
BOOST_TEST_EQ(*counter, 2);
8080
}

test/unit/server/route_handler.cpp

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,14 +13,21 @@
1313
#include <boost/http/server/router.hpp>
1414
#include <boost/http/server/flat_router.hpp>
1515
#include <boost/http/request.hpp>
16-
#include <boost/capy/ex/run_sync.hpp>
1716

17+
#include "run_sync.hpp"
1818
#include "test_route_handler.hpp"
1919
#include "test_suite.hpp"
2020

21+
#include <array>
22+
2123
namespace boost {
2224
namespace http {
2325

26+
struct any_write_buffers
27+
{
28+
std::array<capy::const_buffer, 16> v_;
29+
};
30+
2431
struct route_handler_test
2532
{
2633
using test_router = router<route_params>;
@@ -33,7 +40,7 @@ struct route_handler_test
3340
{
3441
flat_router fr(std::move(r));
3542
test_route_params p;
36-
auto rv = capy::run_sync()(fr.dispatch(
43+
auto rv = run_sync()(fr.dispatch(
3744
verb, urls::url_view(url), p));
3845
if(BOOST_TEST_EQ(rv.message(), rv0.message()))
3946
BOOST_TEST(rv == rv0);

test/unit/server/router.cpp

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,7 @@
1212
#include <boost/http/server/flat_router.hpp>
1313
#include <boost/http/server/detail/router_base.hpp>
1414

15-
#include <boost/capy/ex/run_sync.hpp>
16-
15+
#include "run_sync.hpp"
1716
#include "test_suite.hpp"
1817

1918
namespace boost {
@@ -108,7 +107,7 @@ struct router_test
108107
{
109108
flat_router fr(std::move(r));
110109
params req;
111-
auto rv = capy::run_sync()(fr.dispatch(
110+
auto rv = run_sync()(fr.dispatch(
112111
http::method::get, urls::url_view(url), req));
113112
BOOST_TEST_EQ(rv.message(), rv0.message());
114113
}
@@ -121,7 +120,7 @@ struct router_test
121120
{
122121
flat_router fr(std::move(r));
123122
params req;
124-
auto rv = capy::run_sync()(fr.dispatch(
123+
auto rv = run_sync()(fr.dispatch(
125124
verb, urls::url_view(url), req));
126125
BOOST_TEST_EQ(rv.message(), rv0.message());
127126
}
@@ -134,7 +133,7 @@ struct router_test
134133
{
135134
flat_router fr(std::move(r));
136135
params req;
137-
auto rv = capy::run_sync()(fr.dispatch(
136+
auto rv = run_sync()(fr.dispatch(
138137
verb, urls::url_view(url), req));
139138
BOOST_TEST_EQ(rv.message(), rv0.message());
140139
}
@@ -411,7 +410,7 @@ struct router_test
411410
flat_router fr(std::move(r));
412411
params req;
413412
BOOST_TEST_THROWS(
414-
capy::run_sync()(fr.dispatch(
413+
run_sync()(fr.dispatch(
415414
http::method::unknown, urls::url_view("/"), req)),
416415
std::invalid_argument);
417416
}
@@ -423,7 +422,7 @@ struct router_test
423422
flat_router fr(std::move(r));
424423
params req;
425424
BOOST_TEST_THROWS(
426-
capy::run_sync()(fr.dispatch(
425+
run_sync()(fr.dispatch(
427426
"", urls::url_view("/"), req)),
428427
std::invalid_argument);
429428
}

0 commit comments

Comments
 (0)