5#include <mp/test/foo.capnp.h>
6#include <mp/test/foo.capnp.proxy.h>
9#include <capnp/capability.h>
12#include <condition_variable>
18#include <kj/async-io.h>
25#include <mp/proxy.capnp.h>
45static_assert(std::is_integral_v<
decltype(
kMP_MAJOR_VERSION)>,
"MP_MAJOR_VERSION must be an integral constant");
46static_assert(std::is_integral_v<
decltype(
kMP_MINOR_VERSION)>,
"MP_MINOR_VERSION must be an integral constant");
67 std::promise<std::unique_ptr<ProxyClient<messages::FooInterface>>>
client_promise;
68 std::unique_ptr<ProxyClient<messages::FooInterface>>
client;
83 auto server_connection =
84 std::make_unique<Connection>(loop, kj::mv(pipe.ends[0]), [&](
Connection& connection) {
85 auto server_proxy = kj::heap<ProxyServer<messages::FooInterface>>(
86 std::make_shared<FooImplementation>(), connection);
87 server = server_proxy;
88 return capnp::Capability::Client(kj::mv(server_proxy));
93 server_connection->onDisconnect([&] { server_connection.reset(); });
95 auto client_connection = std::make_unique<Connection>(loop, kj::mv(pipe.ends[1]));
96 auto client_proxy = std::make_unique<ProxyClient<messages::FooInterface>>(
97 client_connection->m_rpc_system->bootstrap(
ServerVatId().vat_id).castAs<messages::FooInterface>(),
98 client_connection.get(), client_owns_connection);
99 if (client_owns_connection) {
100 (void)client_connection.release();
109 client = client_promise.get_future().get();
115 bool destroyed =
false;
116 client->m_context.cleanup_fns.emplace_front([&destroyed] { destroyed =
true; });
118 KJ_EXPECT(destroyed);
129 KJ_EXPECT(foo->add(1, 2) == 3);
131 foo->addOut(3, 4,
ret);
133 foo->addInOut(3,
ret);
134 KJ_EXPECT(
ret == 10);
140 in.
vbool.push_back(
false);
141 in.
vbool.push_back(
true);
142 in.
vbool.push_back(
false);
144 KJ_EXPECT(in.
name ==
out.name);
145 KJ_EXPECT(in.
setint.size() ==
out.setint.size());
147 KJ_EXPECT(*
init == *outit);
149 KJ_EXPECT(in.
vbool.size() ==
out.vbool.size());
150 for (
size_t i = 0; i < in.
vbool.size(); ++i) {
151 KJ_EXPECT(in.
vbool[i] ==
out.vbool[i]);
166 int call(
int arg)
override
168 KJ_EXPECT(arg == m_expect);
171 int callExtended(
int arg)
override
173 KJ_EXPECT(arg == m_expect + 10);
179 foo->initThreadMap();
180 Callback callback(1, 2);
181 KJ_EXPECT(foo->callback(callback, 1) == 2);
182 KJ_EXPECT(foo->callbackUnique(std::make_unique<Callback>(3, 4), 3) == 4);
183 KJ_EXPECT(foo->callbackShared(std::make_shared<Callback>(5, 6), 5) == 6);
184 auto saved = std::make_shared<Callback>(7, 8);
185 KJ_EXPECT(saved.use_count() == 1);
186 foo->saveCallback(saved);
187 KJ_EXPECT(saved.use_count() == 2);
188 foo->callbackSaved(7);
189 KJ_EXPECT(foo->callbackSaved(7) == 8);
190 foo->saveCallback(
nullptr);
191 KJ_EXPECT(saved.use_count() == 1);
192 KJ_EXPECT(foo->callbackExtended(callback, 11) == 12);
197 FooCustom custom_out = foo->passCustom(custom_in);
198 KJ_EXPECT(custom_in.
v1 == custom_out.
v1);
199 KJ_EXPECT(custom_in.
v2 == custom_out.
v2);
205 FooMessage message2{foo->passMessage(message1)};
206 KJ_EXPECT(message2.message ==
"init build read call build read");
210 foo->passMutable(mut);
211 KJ_EXPECT(mut.
message ==
"init build pass call return read");
213 KJ_EXPECT(foo->passFn([]{ return 10; }) == 10);
216KJ_TEST(
"Call IPC method after client connection is closed")
220 KJ_EXPECT(foo->add(1, 2) == 3);
221 setup.client_disconnect();
223 bool disconnected{
false};
226 }
catch (
const std::runtime_error& e) {
227 KJ_EXPECT(std::string_view{e.what()} ==
"IPC client method called after disconnect.");
230 KJ_EXPECT(disconnected);
233KJ_TEST(
"Calling IPC method after server connection is closed")
237 KJ_EXPECT(foo->add(1, 2) == 3);
238 setup.server_disconnect();
240 bool disconnected{
false};
243 }
catch (
const std::runtime_error& e) {
244 KJ_EXPECT(std::string_view{e.what()} ==
"IPC client method call interrupted by disconnect.");
247 KJ_EXPECT(disconnected);
250KJ_TEST(
"Calling IPC method and disconnecting during the call")
254 KJ_EXPECT(foo->add(1, 2) == 3);
258 setup.server->m_impl->m_fn =
setup.client_disconnect;
260 bool disconnected{
false};
263 }
catch (
const std::runtime_error& e) {
264 KJ_EXPECT(std::string_view{e.what()} ==
"IPC client method call interrupted by disconnect.");
267 KJ_EXPECT(disconnected);
270KJ_TEST(
"Calling IPC method, disconnecting and blocking during the call")
290 std::promise<void> signal;
293 KJ_EXPECT(foo->add(1, 2) == 3);
295 foo->initThreadMap();
296 setup.server->m_impl->m_fn = [&] {
298 setup.client_disconnect();
299 signal.get_future().get();
302 bool disconnected{
false};
305 }
catch (
const std::runtime_error& e) {
306 KJ_EXPECT(std::string_view{e.what()} ==
"IPC client method call interrupted by disconnect.");
309 KJ_EXPECT(disconnected);
319KJ_TEST(
"Make simultaneous IPC calls on single remote thread")
323 std::promise<void> signal;
325 foo->initThreadMap();
328 setup.server->m_impl->m_fn = [&] {};
331 Thread::Client *callback_thread, *request_thread;
332 foo->m_context.loop->sync([&] {
333 Lock lock(tc.waiter->m_mutex);
334 callback_thread = &tc.callback_threads.at(foo->m_context.connection)->m_client;
335 request_thread = &tc.request_threads.at(foo->m_context.connection)->m_client;
339 std::atomic<int> expected = 100;
341 setup.server->m_impl->m_int_fn = [&](
int n) {
347 auto client{foo->m_client};
348 std::atomic<size_t> running{3};
349 foo->m_context.loop->sync([&]
351 for (
size_t i = 0; i < running; i++)
353 auto request{client.callIntFnAsyncRequest()};
354 auto context{request.initContext()};
355 context.setCallbackThread(*callback_thread);
356 context.setThread(*request_thread);
357 request.setArg(100 * (i+1));
358 foo->m_context.loop->m_task_set->add(request.send().then(
359 [&running, &tc, i](
auto&& results) {
360 assert(results.getResult() == static_cast<int32_t>(100 * (i+1)));
362 tc.waiter->m_cv.notify_all();
367 Lock lock(tc.waiter->m_mutex);
368 tc.waiter->wait(lock, [&running] {
return running == 0; });
370 KJ_EXPECT(expected == 400);
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
Major and minor version numbers.
#define MP_MAJOR_VERSION
Major version number.
#define MP_MINOR_VERSION
Minor version number.