5#include <mp/test/foo.capnp.h>
6#include <mp/test/foo.capnp.proxy.h>
9#include <capnp/capability.h>
11#include <condition_variable>
16#include <kj/async-io.h>
19#include <kj/exception.h>
25#include <mp/proxy.capnp.h>
34#include <system_error>
46static_assert(std::is_integral_v<
decltype(
kMP_MAJOR_VERSION)>,
"MP_MAJOR_VERSION must be an integral constant");
47static_assert(std::is_integral_v<
decltype(
kMP_MINOR_VERSION)>,
"MP_MINOR_VERSION must be an integral constant");
68 std::promise<std::unique_ptr<ProxyClient<messages::FooInterface>>>
client_promise;
69 std::unique_ptr<ProxyClient<messages::FooInterface>>
client;
84 auto server_connection =
85 std::make_unique<Connection>(loop, kj::mv(pipe.ends[0]), [&](
Connection& connection) {
86 auto server_proxy = kj::heap<ProxyServer<messages::FooInterface>>(
87 std::make_shared<FooImplementation>(), connection);
88 server = server_proxy;
89 return capnp::Capability::Client(kj::mv(server_proxy));
94 server_connection->onDisconnect([&] { server_connection.reset(); });
96 auto client_connection = std::make_unique<Connection>(loop, kj::mv(pipe.ends[1]));
97 auto client_proxy = std::make_unique<ProxyClient<messages::FooInterface>>(
98 client_connection->m_rpc_system->bootstrap(
ServerVatId().vat_id).castAs<messages::FooInterface>(),
99 client_connection.get(), client_owns_connection);
100 if (client_owns_connection) {
101 client_connection.release();
110 client = client_promise.get_future().get();
116 bool destroyed =
false;
117 client->m_context.cleanup_fns.emplace_front([&destroyed] { destroyed =
true; });
119 KJ_EXPECT(destroyed);
130 KJ_EXPECT(foo->add(1, 2) == 3);
132 foo->addOut(3, 4,
ret);
134 foo->addInOut(3,
ret);
135 KJ_EXPECT(
ret == 10);
141 in.
vbool.push_back(
false);
142 in.
vbool.push_back(
true);
143 in.
vbool.push_back(
false);
145 KJ_EXPECT(in.
name ==
out.name);
146 KJ_EXPECT(in.
setint.size() ==
out.setint.size());
148 KJ_EXPECT(*
init == *outit);
150 KJ_EXPECT(in.
vbool.size() ==
out.vbool.size());
151 for (
size_t i = 0; i < in.
vbool.size(); ++i) {
152 KJ_EXPECT(in.
vbool[i] ==
out.vbool[i]);
167 int call(
int arg)
override
169 KJ_EXPECT(arg == m_expect);
172 int callExtended(
int arg)
override
174 KJ_EXPECT(arg == m_expect + 10);
180 foo->initThreadMap();
181 Callback callback(1, 2);
182 KJ_EXPECT(foo->callback(callback, 1) == 2);
183 KJ_EXPECT(foo->callbackUnique(std::make_unique<Callback>(3, 4), 3) == 4);
184 KJ_EXPECT(foo->callbackShared(std::make_shared<Callback>(5, 6), 5) == 6);
185 auto saved = std::make_shared<Callback>(7, 8);
186 KJ_EXPECT(saved.use_count() == 1);
187 foo->saveCallback(saved);
188 KJ_EXPECT(saved.use_count() == 2);
189 foo->callbackSaved(7);
190 KJ_EXPECT(foo->callbackSaved(7) == 8);
191 foo->saveCallback(
nullptr);
192 KJ_EXPECT(saved.use_count() == 1);
193 KJ_EXPECT(foo->callbackExtended(callback, 11) == 12);
198 FooCustom custom_out = foo->passCustom(custom_in);
199 KJ_EXPECT(custom_in.
v1 == custom_out.
v1);
200 KJ_EXPECT(custom_in.
v2 == custom_out.
v2);
206 FooMessage message2{foo->passMessage(message1)};
207 KJ_EXPECT(message2.message ==
"init build read call build read");
211 foo->passMutable(mut);
212 KJ_EXPECT(mut.
message ==
"init build pass call return read");
214 KJ_EXPECT(foo->passFn([]{ return 10; }) == 10);
217KJ_TEST(
"Call IPC method after client connection is closed")
221 KJ_EXPECT(foo->add(1, 2) == 3);
222 setup.client_disconnect();
224 bool disconnected{
false};
227 }
catch (
const std::runtime_error& e) {
228 KJ_EXPECT(std::string_view{e.what()} ==
"IPC client method called after disconnect.");
231 KJ_EXPECT(disconnected);
234KJ_TEST(
"Calling IPC method after server connection is closed")
238 KJ_EXPECT(foo->add(1, 2) == 3);
239 setup.server_disconnect();
241 bool disconnected{
false};
244 }
catch (
const std::runtime_error& e) {
245 KJ_EXPECT(std::string_view{e.what()} ==
"IPC client method call interrupted by disconnect.");
248 KJ_EXPECT(disconnected);
251KJ_TEST(
"Calling IPC method and disconnecting during the call")
255 KJ_EXPECT(foo->add(1, 2) == 3);
259 setup.server->m_impl->m_fn =
setup.client_disconnect;
261 bool disconnected{
false};
264 }
catch (
const std::runtime_error& e) {
265 KJ_EXPECT(std::string_view{e.what()} ==
"IPC client method call interrupted by disconnect.");
268 KJ_EXPECT(disconnected);
271KJ_TEST(
"Calling IPC method, disconnecting and blocking during the call")
291 std::promise<void> signal;
294 KJ_EXPECT(foo->add(1, 2) == 3);
296 foo->initThreadMap();
297 setup.server->m_impl->m_fn = [&] {
299 setup.client_disconnect();
300 signal.get_future().get();
303 bool disconnected{
false};
306 }
catch (
const std::runtime_error& e) {
307 KJ_EXPECT(std::string_view{e.what()} ==
"IPC client method call interrupted by disconnect.");
310 KJ_EXPECT(disconnected);
320KJ_TEST(
"Make simultaneous IPC calls to trigger 'thread busy' error")
324 std::promise<void> signal;
326 foo->initThreadMap();
329 setup.server->m_impl->m_fn = [&] {};
332 Thread::Client *callback_thread, *request_thread;
333 foo->m_context.loop->sync([&] {
334 Lock lock(tc.waiter->m_mutex);
335 callback_thread = &tc.callback_threads.at(foo->m_context.connection)->m_client;
336 request_thread = &tc.request_threads.at(foo->m_context.connection)->m_client;
339 setup.server->m_impl->m_fn = [&] {
342 signal.get_future().get();
344 catch (
const std::future_error& e)
346 KJ_EXPECT(e.code() == std::make_error_code(std::future_errc::future_already_retrieved));
350 auto client{foo->m_client};
351 bool caught_thread_busy =
false;
354 std::atomic<size_t> running{3};
355 foo->m_context.loop->sync([&]
357 for (
size_t i = 0; i < running; i++)
359 auto request{client.callFnAsyncRequest()};
360 auto context{request.initContext()};
361 context.setCallbackThread(*callback_thread);
362 context.setThread(*request_thread);
363 foo->m_context.loop->m_task_set->add(request.send().then(
364 [&](
auto&& results) {
366 tc.waiter->m_cv.notify_all();
368 [&](kj::Exception&& e) {
369 KJ_EXPECT(std::string_view{e.getDescription().cStr()} ==
370 "remote exception: std::exception: thread busy");
371 caught_thread_busy =
true;
374 tc.waiter->m_cv.notify_all();
380 Lock lock(tc.waiter->m_mutex);
381 tc.waiter->wait(lock, [&running] {
return running == 0; });
383 KJ_EXPECT(caught_thread_busy);
Object holding network & rpc state associated with either an incoming server connection,...
Event loop implementation.
kj::AsyncIoContext m_io_context
Capnp IO context.
Event loop smart pointer automatically managing m_num_clients.
Test setup class creating a two way connection between a ProxyServer<FooInterface> object and a Proxy...
std::promise< std::unique_ptr< ProxyClient< messages::FooInterface > > > client_promise
TestSetup(bool client_owns_connection=true)
std::function< void()> server_disconnect
std::function< void()> client_disconnect
std::thread thread
Thread variable should be after other struct members so the thread does not start until the other mem...
ProxyServer< messages::FooInterface > * server
std::unique_ptr< ProxyClient< messages::FooInterface > > client
constexpr auto kMP_MAJOR_VERSION
Check version.h header values.
constexpr auto kMP_MINOR_VERSION
Functions to serialize / deserialize common bitcoin types.
thread_local ThreadContext g_thread_context
KJ_TEST("SpawnProcess does not run callback in child")
Log level
The severity level of this message.
std::string message
Message to be logged.
Mapping from capnp interface type to proxy client implementation (specializations are generated by pr...
Vat id for server side of connection.
The thread_local ThreadContext g_thread_context struct provides information about individual threads ...
std::vector< bool > vbool
#define MP_MAJOR_VERSION
Major version number.
#define MP_MINOR_VERSION
Minor version number.