| 1 |
// Copyright (c) 2013-2014 Sandstorm Development Group, Inc. and contributors |
| 2 |
// Licensed under the MIT License: |
| 3 |
// |
| 4 |
// Permission is hereby granted, free of charge, to any person obtaining a copy |
| 5 |
// of this software and associated documentation files (the "Software"), to deal |
| 6 |
// in the Software without restriction, including without limitation the rights |
| 7 |
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 8 |
// copies of the Software, and to permit persons to whom the Software is |
| 9 |
// furnished to do so, subject to the following conditions: |
| 10 |
// |
| 11 |
// The above copyright notice and this permission notice shall be included in |
| 12 |
// all copies or substantial portions of the Software. |
| 13 |
// |
| 14 |
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 15 |
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 16 |
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 17 |
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 18 |
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 19 |
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
| 20 |
// THE SOFTWARE. |
| 21 |
|
| 22 |
#define CAPNP_TESTING_CAPNP 1 |
| 23 |
|
| 24 |
#include "rpc.h" |
| 25 |
#include "test-util.h" |
| 26 |
#include "schema.h" |
| 27 |
#include "serialize.h" |
| 28 |
#include <kj/debug.h> |
| 29 |
#include <kj/string-tree.h> |
| 30 |
#include <kj/compat/gtest.h> |
| 31 |
#include <capnp/rpc.capnp.h> |
| 32 |
#include <map> |
| 33 |
#include <queue> |
| 34 |
|
| 35 |
// TODO(cleanup): Auto-generate stringification functions for union discriminants. |
| 36 |
namespace capnp { |
| 37 |
namespace rpc { |
| 38 |
inline kj::String KJ_STRINGIFY(Message::Which which) { |
| 39 |
return kj::str(static_cast<uint16_t>(which)); |
| 40 |
} |
| 41 |
} // namespace rpc |
| 42 |
} // namespace capnp |
| 43 |
|
| 44 |
namespace capnp { |
| 45 |
namespace _ { // private |
| 46 |
namespace { |
| 47 |
|
| 48 |
class RpcDumper { |
| 49 |
// Class which stringifies RPC messages for debugging purposes, including decoding params and |
| 50 |
// results based on the call's interface and method IDs and extracting cap descriptors. |
| 51 |
// |
| 52 |
// TODO(cleanup): Clean this code up and move it to someplace reusable, so it can be used as |
| 53 |
// a packet inspector / debugging tool for Cap'n Proto network traffic. |
| 54 |
|
| 55 |
public: |
| 56 |
void addSchema(InterfaceSchema schema) { |
| 57 |
schemas[schema.getProto().getId()] = schema; |
| 58 |
} |
| 59 |
|
| 60 |
enum Sender { |
| 61 |
CLIENT, |
| 62 |
SERVER |
| 63 |
}; |
| 64 |
|
| 65 |
kj::String dump(rpc::Message::Reader message, Sender sender) { |
| 66 |
const char* senderName = sender == CLIENT ? "client" : "server"; |
| 67 |
|
| 68 |
switch (message.which()) { |
| 69 |
case rpc::Message::CALL: { |
| 70 |
auto call = message.getCall(); |
| 71 |
auto iter = schemas.find(call.getInterfaceId()); |
| 72 |
if (iter == schemas.end()) { |
| 73 |
break; |
| 74 |
} |
| 75 |
InterfaceSchema schema = iter->second; |
| 76 |
auto methods = schema.getMethods(); |
| 77 |
if (call.getMethodId() >= methods.size()) { |
| 78 |
break; |
| 79 |
} |
| 80 |
InterfaceSchema::Method method = methods[call.getMethodId()]; |
| 81 |
|
| 82 |
auto schemaProto = schema.getProto(); |
| 83 |
auto interfaceName = |
| 84 |
schemaProto.getDisplayName().slice(schemaProto.getDisplayNamePrefixLength()); |
| 85 |
|
| 86 |
auto methodProto = method.getProto(); |
| 87 |
auto paramType = method.getParamType(); |
| 88 |
auto resultType = method.getResultType(); |
| 89 |
|
| 90 |
if (call.getSendResultsTo().isCaller()) { |
| 91 |
returnTypes[std::make_pair(sender, call.getQuestionId())] = resultType; |
| 92 |
} |
| 93 |
|
| 94 |
auto payload = call.getParams(); |
| 95 |
auto params = kj::str(payload.getContent().getAs<DynamicStruct>(paramType)); |
| 96 |
|
| 97 |
auto sendResultsTo = call.getSendResultsTo(); |
| 98 |
|
| 99 |
return kj::str(senderName, "(", call.getQuestionId(), "): call ", |
| 100 |
call.getTarget(), " <- ", interfaceName, ".", |
| 101 |
methodProto.getName(), params, |
| 102 |
" caps:[", kj::strArray(payload.getCapTable(), ", "), "]", |
| 103 |
sendResultsTo.isCaller() ? kj::str() |
| 104 |
: kj::str(" sendResultsTo:", sendResultsTo)); |
| 105 |
} |
| 106 |
|
| 107 |
case rpc::Message::RETURN: { |
| 108 |
auto ret = message.getReturn(); |
| 109 |
|
| 110 |
auto iter = returnTypes.find( |
| 111 |
std::make_pair(sender == CLIENT ? SERVER : CLIENT, ret.getAnswerId())); |
| 112 |
if (iter == returnTypes.end()) { |
| 113 |
break; |
| 114 |
} |
| 115 |
|
| 116 |
auto schema = iter->second; |
| 117 |
returnTypes.erase(iter); |
| 118 |
if (ret.which() != rpc::Return::RESULTS) { |
| 119 |
// Oops, no results returned. We don't check this earlier because we want to make sure |
| 120 |
// returnTypes.erase() gets a chance to happen. |
| 121 |
break; |
| 122 |
} |
| 123 |
|
| 124 |
auto payload = ret.getResults(); |
| 125 |
|
| 126 |
if (schema.getProto().isStruct()) { |
| 127 |
auto results = kj::str(payload.getContent().getAs<DynamicStruct>(schema.asStruct())); |
| 128 |
|
| 129 |
return kj::str(senderName, "(", ret.getAnswerId(), "): return ", results, |
| 130 |
" caps:[", kj::strArray(payload.getCapTable(), ", "), "]"); |
| 131 |
} else if (schema.getProto().isInterface()) { |
| 132 |
payload.getContent().getAs<DynamicCapability>(schema.asInterface()); |
| 133 |
return kj::str(senderName, "(", ret.getAnswerId(), "): return cap ", |
| 134 |
kj::strArray(payload.getCapTable(), ", ")); |
| 135 |
} else { |
| 136 |
break; |
| 137 |
} |
| 138 |
} |
| 139 |
|
| 140 |
case rpc::Message::BOOTSTRAP: { |
| 141 |
auto restore = message.getBootstrap(); |
| 142 |
|
| 143 |
returnTypes[std::make_pair(sender, restore.getQuestionId())] = InterfaceSchema(); |
| 144 |
|
| 145 |
return kj::str(senderName, "(", restore.getQuestionId(), "): bootstrap ", |
| 146 |
restore.getDeprecatedObjectId().getAs<test::TestSturdyRefObjectId>()); |
| 147 |
} |
| 148 |
|
| 149 |
default: |
| 150 |
break; |
| 151 |
} |
| 152 |
|
| 153 |
return kj::str(senderName, ": ", message); |
| 154 |
} |
| 155 |
|
| 156 |
private: |
| 157 |
std::map<uint64_t, InterfaceSchema> schemas; |
| 158 |
std::map<std::pair<Sender, uint32_t>, Schema> returnTypes; |
| 159 |
}; |
| 160 |
|
| 161 |
// ======================================================================================= |
| 162 |
|
| 163 |
class TestNetworkAdapter; |
| 164 |
|
| 165 |
class TestNetwork { |
| 166 |
public: |
| 167 |
TestNetwork() { |
| 168 |
dumper.addSchema(Schema::from<test::TestInterface>()); |
| 169 |
dumper.addSchema(Schema::from<test::TestExtends>()); |
| 170 |
dumper.addSchema(Schema::from<test::TestPipeline>()); |
| 171 |
dumper.addSchema(Schema::from<test::TestCallOrder>()); |
| 172 |
dumper.addSchema(Schema::from<test::TestTailCallee>()); |
| 173 |
dumper.addSchema(Schema::from<test::TestTailCaller>()); |
| 174 |
dumper.addSchema(Schema::from<test::TestMoreStuff>()); |
| 175 |
} |
| 176 |
~TestNetwork() noexcept(false); |
| 177 |
|
| 178 |
TestNetworkAdapter& add(kj::StringPtr name); |
| 179 |
|
| 180 |
kj::Maybe<TestNetworkAdapter&> find(kj::StringPtr name) { |
| 181 |
auto iter = map.find(name); |
| 182 |
if (iter == map.end()) { |
| 183 |
return nullptr; |
| 184 |
} else { |
| 185 |
return *iter->second; |
| 186 |
} |
| 187 |
} |
| 188 |
|
| 189 |
RpcDumper dumper; |
| 190 |
|
| 191 |
private: |
| 192 |
std::map<kj::StringPtr, kj::Own<TestNetworkAdapter>> map; |
| 193 |
}; |
| 194 |
|
| 195 |
typedef VatNetwork< |
| 196 |
test::TestSturdyRefHostId, test::TestProvisionId, test::TestRecipientId, |
| 197 |
test::TestThirdPartyCapId, test::TestJoinResult> TestNetworkAdapterBase; |
| 198 |
|
| 199 |
class TestNetworkAdapter final: public TestNetworkAdapterBase { |
| 200 |
public: |
| 201 |
TestNetworkAdapter(TestNetwork& network, kj::StringPtr self): network(network), self(self) {} |
| 202 |
|
| 203 |
~TestNetworkAdapter() { |
| 204 |
kj::Exception exception = KJ_EXCEPTION(FAILED, "Network was destroyed."); |
| 205 |
for (auto& entry: connections) { |
| 206 |
entry.second->disconnect(kj::cp(exception)); |
| 207 |
} |
| 208 |
} |
| 209 |
|
| 210 |
uint getSentCount() { return sent; } |
| 211 |
uint getReceivedCount() { return received; } |
| 212 |
|
| 213 |
void onSend(kj::Function<bool(MessageBuilder& message)> callback) { |
| 214 |
// Invokes the given callback every time a message is sent. Callback can return false to cause |
| 215 |
// send() to do nothing. |
| 216 |
sendCallback = kj::mv(callback); |
| 217 |
} |
| 218 |
|
| 219 |
typedef TestNetworkAdapterBase::Connection Connection; |
| 220 |
|
| 221 |
class ConnectionImpl final |
| 222 |
: public Connection, public kj::Refcounted, public kj::TaskSet::ErrorHandler { |
| 223 |
public: |
| 224 |
ConnectionImpl(TestNetworkAdapter& network, RpcDumper::Sender sender) |
| 225 |
: network(network), sender(sender), tasks(kj::heap<kj::TaskSet>(*this)) {} |
| 226 |
|
| 227 |
void attach(ConnectionImpl& other) { |
| 228 |
KJ_REQUIRE(partner == nullptr); |
| 229 |
KJ_REQUIRE(other.partner == nullptr); |
| 230 |
partner = other; |
| 231 |
other.partner = *this; |
| 232 |
} |
| 233 |
|
| 234 |
void disconnect(kj::Exception&& exception) { |
| 235 |
while (!fulfillers.empty()) { |
| 236 |
fulfillers.front()->reject(kj::cp(exception)); |
| 237 |
fulfillers.pop(); |
| 238 |
} |
| 239 |
|
| 240 |
networkException = kj::mv(exception); |
| 241 |
|
| 242 |
tasks = nullptr; |
| 243 |
} |
| 244 |
|
| 245 |
class IncomingRpcMessageImpl final: public IncomingRpcMessage, public kj::Refcounted { |
| 246 |
public: |
| 247 |
IncomingRpcMessageImpl(kj::Array<word> data) |
| 248 |
: data(kj::mv(data)), |
| 249 |
message(this->data) {} |
| 250 |
|
| 251 |
AnyPointer::Reader getBody() override { |
| 252 |
return message.getRoot<AnyPointer>(); |
| 253 |
} |
| 254 |
|
| 255 |
size_t sizeInWords() override { |
| 256 |
return data.size(); |
| 257 |
} |
| 258 |
|
| 259 |
kj::Array<word> data; |
| 260 |
FlatArrayMessageReader message; |
| 261 |
}; |
| 262 |
|
| 263 |
class OutgoingRpcMessageImpl final: public OutgoingRpcMessage { |
| 264 |
public: |
| 265 |
OutgoingRpcMessageImpl(ConnectionImpl& connection, uint firstSegmentWordSize) |
| 266 |
: connection(connection), |
| 267 |
message(firstSegmentWordSize == 0 ? SUGGESTED_FIRST_SEGMENT_WORDS |
| 268 |
: firstSegmentWordSize) {} |
| 269 |
|
| 270 |
AnyPointer::Builder getBody() override { |
| 271 |
return message.getRoot<AnyPointer>(); |
| 272 |
} |
| 273 |
|
| 274 |
void send() override { |
| 275 |
if (!connection.network.sendCallback(message)) return; |
| 276 |
|
| 277 |
if (connection.networkException != nullptr) { |
| 278 |
return; |
| 279 |
} |
| 280 |
|
| 281 |
++connection.network.sent; |
| 282 |
|
| 283 |
// Uncomment to get a debug dump. |
| 284 |
// kj::String msg = connection.network.network.dumper.dump( |
| 285 |
// message.getRoot<rpc::Message>(), connection.sender); |
| 286 |
// KJ_ DBG(msg); |
| 287 |
|
| 288 |
auto incomingMessage = kj::heap<IncomingRpcMessageImpl>(messageToFlatArray(message)); |
| 289 |
|
| 290 |
auto connectionPtr = &connection; |
| 291 |
connection.tasks->add(kj::evalLater( |
| 292 |
[connectionPtr,message=kj::mv(incomingMessage)]() mutable { |
| 293 |
KJ_IF_MAYBE(p, connectionPtr->partner) { |
| 294 |
if (p->fulfillers.empty()) { |
| 295 |
p->messages.push(kj::mv(message)); |
| 296 |
} else { |
| 297 |
++p->network.received; |
| 298 |
p->fulfillers.front()->fulfill( |
| 299 |
kj::Own<IncomingRpcMessage>(kj::mv(message))); |
| 300 |
p->fulfillers.pop(); |
| 301 |
} |
| 302 |
} |
| 303 |
})); |
| 304 |
} |
| 305 |
|
| 306 |
size_t sizeInWords() override { |
| 307 |
return message.sizeInWords(); |
| 308 |
} |
| 309 |
|
| 310 |
private: |
| 311 |
ConnectionImpl& connection; |
| 312 |
MallocMessageBuilder message; |
| 313 |
}; |
| 314 |
|
| 315 |
test::TestSturdyRefHostId::Reader getPeerVatId() override { |
| 316 |
// Not actually implemented for the purpose of this test. |
| 317 |
return test::TestSturdyRefHostId::Reader(); |
| 318 |
} |
| 319 |
|
| 320 |
kj::Own<OutgoingRpcMessage> newOutgoingMessage(uint firstSegmentWordSize) override { |
| 321 |
return kj::heap<OutgoingRpcMessageImpl>(*this, firstSegmentWordSize); |
| 322 |
} |
| 323 |
kj::Promise<kj::Maybe<kj::Own<IncomingRpcMessage>>> receiveIncomingMessage() override { |
| 324 |
KJ_IF_MAYBE(e, networkException) { |
| 325 |
return kj::cp(*e); |
| 326 |
} |
| 327 |
|
| 328 |
if (messages.empty()) { |
| 329 |
KJ_IF_MAYBE(f, fulfillOnEnd) { |
| 330 |
f->get()->fulfill(); |
| 331 |
return kj::Maybe<kj::Own<IncomingRpcMessage>>(nullptr); |
| 332 |
} else { |
| 333 |
auto paf = kj::newPromiseAndFulfiller<kj::Maybe<kj::Own<IncomingRpcMessage>>>(); |
| 334 |
fulfillers.push(kj::mv(paf.fulfiller)); |
| 335 |
return kj::mv(paf.promise); |
| 336 |
} |
| 337 |
} else { |
| 338 |
++network.received; |
| 339 |
auto result = kj::mv(messages.front()); |
| 340 |
messages.pop(); |
| 341 |
return kj::Maybe<kj::Own<IncomingRpcMessage>>(kj::mv(result)); |
| 342 |
} |
| 343 |
} |
| 344 |
kj::Promise<void> shutdown() override { |
| 345 |
KJ_IF_MAYBE(e, network.shutdownExceptionToThrow) { |
| 346 |
return kj::cp(*e); |
| 347 |
} |
| 348 |
KJ_IF_MAYBE(p, partner) { |
| 349 |
auto paf = kj::newPromiseAndFulfiller<void>(); |
| 350 |
p->fulfillOnEnd = kj::mv(paf.fulfiller); |
| 351 |
return kj::mv(paf.promise); |
| 352 |
} else { |
| 353 |
return kj::READY_NOW; |
| 354 |
} |
| 355 |
} |
| 356 |
|
| 357 |
void taskFailed(kj::Exception&& exception) override { |
| 358 |
ADD_FAILURE() << kj::str(exception).cStr(); |
| 359 |
} |
| 360 |
|
| 361 |
private: |
| 362 |
TestNetworkAdapter& network; |
| 363 |
RpcDumper::Sender sender KJ_UNUSED_MEMBER; |
| 364 |
kj::Maybe<ConnectionImpl&> partner; |
| 365 |
|
| 366 |
kj::Maybe<kj::Exception> networkException; |
| 367 |
|
| 368 |
std::queue<kj::Own<kj::PromiseFulfiller<kj::Maybe<kj::Own<IncomingRpcMessage>>>>> fulfillers; |
| 369 |
std::queue<kj::Own<IncomingRpcMessage>> messages; |
| 370 |
kj::Maybe<kj::Own<kj::PromiseFulfiller<void>>> fulfillOnEnd; |
| 371 |
|
| 372 |
kj::Own<kj::TaskSet> tasks; |
| 373 |
}; |
| 374 |
|
| 375 |
kj::Maybe<kj::Own<Connection>> connect(test::TestSturdyRefHostId::Reader hostId) override { |
| 376 |
if (hostId.getHost() == self) { |
| 377 |
return nullptr; |
| 378 |
} |
| 379 |
|
| 380 |
TestNetworkAdapter& dst = KJ_REQUIRE_NONNULL(network.find(hostId.getHost())); |
| 381 |
|
| 382 |
auto iter = connections.find(&dst); |
| 383 |
if (iter == connections.end()) { |
| 384 |
auto local = kj::refcounted<ConnectionImpl>(*this, RpcDumper::CLIENT); |
| 385 |
auto remote = kj::refcounted<ConnectionImpl>(dst, RpcDumper::SERVER); |
| 386 |
local->attach(*remote); |
| 387 |
|
| 388 |
connections[&dst] = kj::addRef(*local); |
| 389 |
dst.connections[this] = kj::addRef(*remote); |
| 390 |
|
| 391 |
if (dst.fulfillerQueue.empty()) { |
| 392 |
dst.connectionQueue.push(kj::mv(remote)); |
| 393 |
} else { |
| 394 |
dst.fulfillerQueue.front()->fulfill(kj::mv(remote)); |
| 395 |
dst.fulfillerQueue.pop(); |
| 396 |
} |
| 397 |
|
| 398 |
return kj::Own<Connection>(kj::mv(local)); |
| 399 |
} else { |
| 400 |
return kj::Own<Connection>(kj::addRef(*iter->second)); |
| 401 |
} |
| 402 |
} |
| 403 |
|
| 404 |
kj::Promise<kj::Own<Connection>> accept() override { |
| 405 |
if (connectionQueue.empty()) { |
| 406 |
auto paf = kj::newPromiseAndFulfiller<kj::Own<Connection>>(); |
| 407 |
fulfillerQueue.push(kj::mv(paf.fulfiller)); |
| 408 |
return kj::mv(paf.promise); |
| 409 |
} else { |
| 410 |
auto result = kj::mv(connectionQueue.front()); |
| 411 |
connectionQueue.pop(); |
| 412 |
return kj::mv(result); |
| 413 |
} |
| 414 |
} |
| 415 |
|
| 416 |
void setShutdownExceptionToThrow(kj::Exception&& e) { |
| 417 |
shutdownExceptionToThrow = kj::mv(e); |
| 418 |
} |
| 419 |
|
| 420 |
private: |
| 421 |
TestNetwork& network; |
| 422 |
kj::StringPtr self; |
| 423 |
uint sent = 0; |
| 424 |
uint received = 0; |
| 425 |
kj::Maybe<kj::Exception> shutdownExceptionToThrow = nullptr; |
| 426 |
|
| 427 |
std::map<const TestNetworkAdapter*, kj::Own<ConnectionImpl>> connections; |
| 428 |
std::queue<kj::Own<kj::PromiseFulfiller<kj::Own<Connection>>>> fulfillerQueue; |
| 429 |
std::queue<kj::Own<Connection>> connectionQueue; |
| 430 |
|
| 431 |
kj::Function<bool(MessageBuilder& message)> sendCallback = [](MessageBuilder&) { return true; }; |
| 432 |
}; |
| 433 |
|
| 434 |
TestNetwork::~TestNetwork() noexcept(false) {} |
| 435 |
|
| 436 |
TestNetworkAdapter& TestNetwork::add(kj::StringPtr name) { |
| 437 |
return *(map[name] = kj::heap<TestNetworkAdapter>(*this, name)); |
| 438 |
} |
| 439 |
|
| 440 |
// ======================================================================================= |
| 441 |
|
| 442 |
class TestRestorer final: public SturdyRefRestorer<test::TestSturdyRefObjectId> { |
| 443 |
public: |
| 444 |
int callCount = 0; |
| 445 |
int handleCount = 0; |
| 446 |
|
| 447 |
Capability::Client restore(test::TestSturdyRefObjectId::Reader objectId) override { |
| 448 |
switch (objectId.getTag()) { |
| 449 |
case test::TestSturdyRefObjectId::Tag::TEST_INTERFACE: |
| 450 |
return kj::heap<TestInterfaceImpl>(callCount); |
| 451 |
case test::TestSturdyRefObjectId::Tag::TEST_EXTENDS: |
| 452 |
return Capability::Client(newBrokenCap("No TestExtends implemented.")); |
| 453 |
case test::TestSturdyRefObjectId::Tag::TEST_PIPELINE: |
| 454 |
return kj::heap<TestPipelineImpl>(callCount); |
| 455 |
case test::TestSturdyRefObjectId::Tag::TEST_TAIL_CALLEE: |
| 456 |
return kj::heap<TestTailCalleeImpl>(callCount); |
| 457 |
case test::TestSturdyRefObjectId::Tag::TEST_TAIL_CALLER: |
| 458 |
return kj::heap<TestTailCallerImpl>(callCount); |
| 459 |
case test::TestSturdyRefObjectId::Tag::TEST_MORE_STUFF: |
| 460 |
return kj::heap<TestMoreStuffImpl>(callCount, handleCount); |
| 461 |
} |
| 462 |
KJ_UNREACHABLE; |
| 463 |
} |
| 464 |
}; |
| 465 |
|
| 466 |
struct TestContext { |
| 467 |
kj::EventLoop loop; |
| 468 |
kj::WaitScope waitScope; |
| 469 |
TestNetwork network; |
| 470 |
TestRestorer restorer; |
| 471 |
TestNetworkAdapter& clientNetwork; |
| 472 |
TestNetworkAdapter& serverNetwork; |
| 473 |
RpcSystem<test::TestSturdyRefHostId> rpcClient; |
| 474 |
RpcSystem<test::TestSturdyRefHostId> rpcServer; |
| 475 |
|
| 476 |
TestContext() |
| 477 |
: waitScope(loop), |
| 478 |
clientNetwork(network.add("client")), |
| 479 |
serverNetwork(network.add("server")), |
| 480 |
rpcClient(makeRpcClient(clientNetwork)), |
| 481 |
rpcServer(makeRpcServer(serverNetwork, restorer)) {} |
| 482 |
TestContext(Capability::Client bootstrap) |
| 483 |
: waitScope(loop), |
| 484 |
clientNetwork(network.add("client")), |
| 485 |
serverNetwork(network.add("server")), |
| 486 |
rpcClient(makeRpcClient(clientNetwork)), |
| 487 |
rpcServer(makeRpcServer(serverNetwork, bootstrap)) {} |
| 488 |
|
| 489 |
Capability::Client connect(test::TestSturdyRefObjectId::Tag tag) { |
| 490 |
MallocMessageBuilder refMessage(128); |
| 491 |
auto ref = refMessage.initRoot<test::TestSturdyRef>(); |
| 492 |
auto hostId = ref.initHostId(); |
| 493 |
hostId.setHost("server"); |
| 494 |
ref.getObjectId().initAs<test::TestSturdyRefObjectId>().setTag(tag); |
| 495 |
|
| 496 |
return rpcClient.restore(hostId, ref.getObjectId()); |
| 497 |
} |
| 498 |
}; |
| 499 |
|
| 500 |
TEST(Rpc, Basic) { |
| 501 |
TestContext context; |
| 502 |
|
| 503 |
auto client = context.connect(test::TestSturdyRefObjectId::Tag::TEST_INTERFACE) |
| 504 |
.castAs<test::TestInterface>(); |
| 505 |
|
| 506 |
auto request1 = client.fooRequest(); |
| 507 |
request1.setI(123); |
| 508 |
request1.setJ(true); |
| 509 |
auto promise1 = request1.send(); |
| 510 |
|
| 511 |
// We used to call bar() after baz(), hence the numbering, but this masked the case where the |
| 512 |
// RPC system actually disconnected on bar() (thus returning an exception, which we decided |
| 513 |
// was expected). |
| 514 |
bool barFailed = false; |
| 515 |
auto request3 = client.barRequest(); |
| 516 |
auto promise3 = request3.send().then( |
| 517 |
[](Response<test::TestInterface::BarResults>&& response) { |
| 518 |
ADD_FAILURE() << "Expected bar() call to fail."; |
| 519 |
}, [&](kj::Exception&& e) { |
| 520 |
barFailed = true; |
| 521 |
}); |
| 522 |
|
| 523 |
auto request2 = client.bazRequest(); |
| 524 |
initTestMessage(request2.initS()); |
| 525 |
auto promise2 = request2.send(); |
| 526 |
|
| 527 |
EXPECT_EQ(0, context.restorer.callCount); |
| 528 |
|
| 529 |
auto response1 = promise1.wait(context.waitScope); |
| 530 |
|
| 531 |
EXPECT_EQ("foo", response1.getX()); |
| 532 |
|
| 533 |
auto response2 = promise2.wait(context.waitScope); |
| 534 |
|
| 535 |
promise3.wait(context.waitScope); |
| 536 |
|
| 537 |
EXPECT_EQ(2, context.restorer.callCount); |
| 538 |
EXPECT_TRUE(barFailed); |
| 539 |
} |
| 540 |
|
| 541 |
TEST(Rpc, Pipelining) { |
| 542 |
TestContext context; |
| 543 |
|
| 544 |
auto client = context.connect(test::TestSturdyRefObjectId::Tag::TEST_PIPELINE) |
| 545 |
.castAs<test::TestPipeline>(); |
| 546 |
|
| 547 |
int chainedCallCount = 0; |
| 548 |
|
| 549 |
auto request = client.getCapRequest(); |
| 550 |
request.setN(234); |
| 551 |
request.setInCap(kj::heap<TestInterfaceImpl>(chainedCallCount)); |
| 552 |
|
| 553 |
auto promise = request.send(); |
| 554 |
|
| 555 |
auto pipelineRequest = promise.getOutBox().getCap().fooRequest(); |
| 556 |
pipelineRequest.setI(321); |
| 557 |
auto pipelinePromise = pipelineRequest.send(); |
| 558 |
|
| 559 |
auto pipelineRequest2 = promise.getOutBox().getCap().castAs<test::TestExtends>().graultRequest(); |
| 560 |
auto pipelinePromise2 = pipelineRequest2.send(); |
| 561 |
|
| 562 |
promise = nullptr; // Just to be annoying, drop the original promise. |
| 563 |
|
| 564 |
EXPECT_EQ(0, context.restorer.callCount); |
| 565 |
EXPECT_EQ(0, chainedCallCount); |
| 566 |
|
| 567 |
auto response = pipelinePromise.wait(context.waitScope); |
| 568 |
EXPECT_EQ("bar", response.getX()); |
| 569 |
|
| 570 |
auto response2 = pipelinePromise2.wait(context.waitScope); |
| 571 |
checkTestMessage(response2); |
| 572 |
|
| 573 |
EXPECT_EQ(3, context.restorer.callCount); |
| 574 |
EXPECT_EQ(1, chainedCallCount); |
| 575 |
} |
| 576 |
|
| 577 |
KJ_TEST("RPC sendForPipeline()") { |
| 578 |
TestContext context; |
| 579 |
|
| 580 |
auto client = context.connect(test::TestSturdyRefObjectId::Tag::TEST_PIPELINE) |
| 581 |
.castAs<test::TestPipeline>(); |
| 582 |
|
| 583 |
int chainedCallCount = 0; |
| 584 |
|
| 585 |
auto request = client.getCapRequest(); |
| 586 |
request.setN(234); |
| 587 |
request.setInCap(kj::heap<TestInterfaceImpl>(chainedCallCount)); |
| 588 |
|
| 589 |
auto pipeline = request.sendForPipeline(); |
| 590 |
|
| 591 |
auto pipelineRequest = pipeline.getOutBox().getCap().fooRequest(); |
| 592 |
pipelineRequest.setI(321); |
| 593 |
auto pipelinePromise = pipelineRequest.send(); |
| 594 |
|
| 595 |
auto pipelineRequest2 = pipeline.getOutBox().getCap().castAs<test::TestExtends>().graultRequest(); |
| 596 |
auto pipelinePromise2 = pipelineRequest2.send(); |
| 597 |
|
| 598 |
pipeline = nullptr; // Just to be annoying, drop the original pipeline. |
| 599 |
|
| 600 |
EXPECT_EQ(0, context.restorer.callCount); |
| 601 |
EXPECT_EQ(0, chainedCallCount); |
| 602 |
|
| 603 |
auto response = pipelinePromise.wait(context.waitScope); |
| 604 |
EXPECT_EQ("bar", response.getX()); |
| 605 |
|
| 606 |
auto response2 = pipelinePromise2.wait(context.waitScope); |
| 607 |
checkTestMessage(response2); |
| 608 |
|
| 609 |
EXPECT_EQ(3, context.restorer.callCount); |
| 610 |
EXPECT_EQ(1, chainedCallCount); |
| 611 |
} |
| 612 |
|
| 613 |
KJ_TEST("RPC context.setPipeline") { |
| 614 |
TestContext context; |
| 615 |
|
| 616 |
auto client = context.connect(test::TestSturdyRefObjectId::Tag::TEST_PIPELINE) |
| 617 |
.castAs<test::TestPipeline>(); |
| 618 |
|
| 619 |
auto promise = client.getCapPipelineOnlyRequest().send(); |
| 620 |
|
| 621 |
auto pipelineRequest = promise.getOutBox().getCap().fooRequest(); |
| 622 |
pipelineRequest.setI(321); |
| 623 |
auto pipelinePromise = pipelineRequest.send(); |
| 624 |
|
| 625 |
auto pipelineRequest2 = promise.getOutBox().getCap().castAs<test::TestExtends>().graultRequest(); |
| 626 |
auto pipelinePromise2 = pipelineRequest2.send(); |
| 627 |
|
| 628 |
EXPECT_EQ(0, context.restorer.callCount); |
| 629 |
|
| 630 |
auto response = pipelinePromise.wait(context.waitScope); |
| 631 |
EXPECT_EQ("bar", response.getX()); |
| 632 |
|
| 633 |
auto response2 = pipelinePromise2.wait(context.waitScope); |
| 634 |
checkTestMessage(response2); |
| 635 |
|
| 636 |
EXPECT_EQ(3, context.restorer.callCount); |
| 637 |
|
| 638 |
// The original promise never completed. |
| 639 |
KJ_EXPECT(!promise.poll(context.waitScope)); |
| 640 |
} |
| 641 |
|
| 642 |
TEST(Rpc, Release) { |
| 643 |
TestContext context; |
| 644 |
|
| 645 |
auto client = context.connect(test::TestSturdyRefObjectId::Tag::TEST_MORE_STUFF) |
| 646 |
.castAs<test::TestMoreStuff>(); |
| 647 |
|
| 648 |
auto handle1 = client.getHandleRequest().send().wait(context.waitScope).getHandle(); |
| 649 |
auto promise = client.getHandleRequest().send(); |
| 650 |
auto handle2 = promise.wait(context.waitScope).getHandle(); |
| 651 |
|
| 652 |
EXPECT_EQ(2, context.restorer.handleCount); |
| 653 |
|
| 654 |
handle1 = nullptr; |
| 655 |
|
| 656 |
for (uint i = 0; i < 16; i++) kj::evalLater([]() {}).wait(context.waitScope); |
| 657 |
EXPECT_EQ(1, context.restorer.handleCount); |
| 658 |
|
| 659 |
handle2 = nullptr; |
| 660 |
|
| 661 |
for (uint i = 0; i < 16; i++) kj::evalLater([]() {}).wait(context.waitScope); |
| 662 |
EXPECT_EQ(1, context.restorer.handleCount); |
| 663 |
|
| 664 |
promise = nullptr; |
| 665 |
|
| 666 |
for (uint i = 0; i < 16; i++) kj::evalLater([]() {}).wait(context.waitScope); |
| 667 |
EXPECT_EQ(0, context.restorer.handleCount); |
| 668 |
} |
| 669 |
|
| 670 |
TEST(Rpc, ReleaseOnCancel) { |
| 671 |
// At one time, there was a bug where if a Return contained capabilities, but the client had |
| 672 |
// canceled the request and already send a Finish (which presumably didn't reach the server before |
| 673 |
// the Return), then we'd leak those caps. Test for that. |
| 674 |
|
| 675 |
TestContext context; |
| 676 |
|
| 677 |
auto client = context.connect(test::TestSturdyRefObjectId::Tag::TEST_MORE_STUFF) |
| 678 |
.castAs<test::TestMoreStuff>(); |
| 679 |
client.whenResolved().wait(context.waitScope); |
| 680 |
|
| 681 |
{ |
| 682 |
auto promise = client.getHandleRequest().send(); |
| 683 |
|
| 684 |
// If the server receives cancellation too early, it won't even return a capability in the |
| 685 |
// results, it will just return "canceled". We want to emulate the case where the return message |
| 686 |
// and the cancel (finish) message cross paths. It turns out that exactly two evalLater()s get |
| 687 |
// us there. |
| 688 |
// |
| 689 |
// TODO(cleanup): This is fragile, but I'm not sure how else to write it without a ton |
| 690 |
// of scaffolding. |
| 691 |
kj::evalLater([]() {}).wait(context.waitScope); |
| 692 |
kj::evalLater([]() {}).wait(context.waitScope); |
| 693 |
} |
| 694 |
|
| 695 |
for (uint i = 0; i < 16; i++) kj::evalLater([]() {}).wait(context.waitScope); |
| 696 |
EXPECT_EQ(0, context.restorer.handleCount); |
| 697 |
} |
| 698 |
|
| 699 |
TEST(Rpc, TailCall) { |
| 700 |
TestContext context; |
| 701 |
|
| 702 |
auto caller = context.connect(test::TestSturdyRefObjectId::Tag::TEST_TAIL_CALLER) |
| 703 |
.castAs<test::TestTailCaller>(); |
| 704 |
|
| 705 |
int calleeCallCount = 0; |
| 706 |
|
| 707 |
test::TestTailCallee::Client callee(kj::heap<TestTailCalleeImpl>(calleeCallCount)); |
| 708 |
|
| 709 |
auto request = caller.fooRequest(); |
| 710 |
request.setI(456); |
| 711 |
request.setCallee(callee); |
| 712 |
|
| 713 |
auto promise = request.send(); |
| 714 |
|
| 715 |
auto dependentCall0 = promise.getC().getCallSequenceRequest().send(); |
| 716 |
|
| 717 |
auto response = promise.wait(context.waitScope); |
| 718 |
EXPECT_EQ(456, response.getI()); |
| 719 |
EXPECT_EQ("from TestTailCaller", response.getT()); |
| 720 |
|
| 721 |
auto dependentCall1 = promise.getC().getCallSequenceRequest().send(); |
| 722 |
|
| 723 |
EXPECT_EQ(0, dependentCall0.wait(context.waitScope).getN()); |
| 724 |
EXPECT_EQ(1, dependentCall1.wait(context.waitScope).getN()); |
| 725 |
|
| 726 |
// TODO(someday): We used to initiate dependentCall2 here before waiting on the first two calls, |
| 727 |
// and the ordering was still "correct". But this was apparently by accident. Calling getC() on |
| 728 |
// the final response returns a different capability from calling getC() on the promise. There |
| 729 |
// are no guarantees on the ordering of calls on the response capability vs. the earlier |
| 730 |
// promise. When ordering matters, applications should take the original promise capability and |
| 731 |
// keep using that. In theory the RPC system could create continuity here, but it would be |
| 732 |
// annoying: for each capability that had been fetched on the promise, it would need to |
| 733 |
// traverse to the same capability in the final response and swap it out in-place for the |
| 734 |
// pipelined cap returned earlier. Maybe we'll determine later that that's really needed but |
| 735 |
// for now I'm not gonna do it. |
| 736 |
auto dependentCall2 = response.getC().getCallSequenceRequest().send(); |
| 737 |
|
| 738 |
EXPECT_EQ(2, dependentCall2.wait(context.waitScope).getN()); |
| 739 |
|
| 740 |
EXPECT_EQ(1, calleeCallCount); |
| 741 |
EXPECT_EQ(1, context.restorer.callCount); |
| 742 |
} |
| 743 |
|
| 744 |
class TestHangingTailCallee final: public test::TestTailCallee::Server { |
| 745 |
public: |
| 746 |
TestHangingTailCallee(int& callCount, int& cancelCount) |
| 747 |
: callCount(callCount), cancelCount(cancelCount) {} |
| 748 |
|
| 749 |
kj::Promise<void> foo(FooContext context) override { |
| 750 |
++callCount; |
| 751 |
return kj::Promise<void>(kj::NEVER_DONE) |
| 752 |
.attach(kj::defer([&cancelCount = cancelCount]() { ++cancelCount; })); |
| 753 |
} |
| 754 |
|
| 755 |
private: |
| 756 |
int& callCount; |
| 757 |
int& cancelCount; |
| 758 |
}; |
| 759 |
|
| 760 |
class TestRacingTailCaller final: public test::TestTailCaller::Server { |
| 761 |
public: |
| 762 |
TestRacingTailCaller(kj::Promise<void> unblock): unblock(kj::mv(unblock)) {} |
| 763 |
|
| 764 |
kj::Promise<void> foo(FooContext context) override { |
| 765 |
return unblock.then([context]() mutable { |
| 766 |
auto tailRequest = context.getParams().getCallee().fooRequest(); |
| 767 |
return context.tailCall(kj::mv(tailRequest)); |
| 768 |
}); |
| 769 |
} |
| 770 |
|
| 771 |
private: |
| 772 |
kj::Promise<void> unblock; |
| 773 |
}; |
| 774 |
|
| 775 |
TEST(Rpc, TailCallCancel) { |
| 776 |
TestContext context; |
| 777 |
|
| 778 |
auto caller = context.connect(test::TestSturdyRefObjectId::Tag::TEST_TAIL_CALLER) |
| 779 |
.castAs<test::TestTailCaller>(); |
| 780 |
|
| 781 |
int callCount = 0, cancelCount = 0; |
| 782 |
|
| 783 |
test::TestTailCallee::Client callee(kj::heap<TestHangingTailCallee>(callCount, cancelCount)); |
| 784 |
|
| 785 |
{ |
| 786 |
auto request = caller.fooRequest(); |
| 787 |
request.setCallee(callee); |
| 788 |
|
| 789 |
auto promise = request.send(); |
| 790 |
|
| 791 |
KJ_ASSERT(callCount == 0); |
| 792 |
KJ_ASSERT(cancelCount == 0); |
| 793 |
|
| 794 |
KJ_ASSERT(!promise.poll(context.waitScope)); |
| 795 |
|
| 796 |
KJ_ASSERT(callCount == 1); |
| 797 |
KJ_ASSERT(cancelCount == 0); |
| 798 |
} |
| 799 |
|
| 800 |
kj::Promise<void>(kj::NEVER_DONE).poll(context.waitScope); |
| 801 |
|
| 802 |
KJ_ASSERT(callCount == 1); |
| 803 |
KJ_ASSERT(cancelCount == 1); |
| 804 |
} |
| 805 |
|
| 806 |
TEST(Rpc, TailCallCancelRace) { |
| 807 |
auto paf = kj::newPromiseAndFulfiller<void>(); |
| 808 |
TestContext context(kj::heap<TestRacingTailCaller>(kj::mv(paf.promise))); |
| 809 |
|
| 810 |
MallocMessageBuilder serverHostIdBuilder; |
| 811 |
auto serverHostId = serverHostIdBuilder.getRoot<test::TestSturdyRefHostId>(); |
| 812 |
serverHostId.setHost("server"); |
| 813 |
|
| 814 |
auto caller = context.rpcClient.bootstrap(serverHostId).castAs<test::TestTailCaller>(); |
| 815 |
|
| 816 |
int callCount = 0, cancelCount = 0; |
| 817 |
|
| 818 |
test::TestTailCallee::Client callee(kj::heap<TestHangingTailCallee>(callCount, cancelCount)); |
| 819 |
|
| 820 |
{ |
| 821 |
auto request = caller.fooRequest(); |
| 822 |
request.setCallee(callee); |
| 823 |
|
| 824 |
auto promise = request.send(); |
| 825 |
|
| 826 |
KJ_ASSERT(callCount == 0); |
| 827 |
KJ_ASSERT(cancelCount == 0); |
| 828 |
|
| 829 |
KJ_ASSERT(!promise.poll(context.waitScope)); |
| 830 |
|
| 831 |
KJ_ASSERT(callCount == 0); |
| 832 |
KJ_ASSERT(cancelCount == 0); |
| 833 |
|
| 834 |
// Unblock the server and at the same time cancel the client. |
| 835 |
paf.fulfiller->fulfill(); |
| 836 |
} |
| 837 |
|
| 838 |
kj::Promise<void>(kj::NEVER_DONE).poll(context.waitScope); |
| 839 |
|
| 840 |
KJ_ASSERT(callCount == 1); |
| 841 |
KJ_ASSERT(cancelCount == 1); |
| 842 |
} |
| 843 |
|
| 844 |
TEST(Rpc, Cancellation) { |
| 845 |
// Tests cancellation. |
| 846 |
|
| 847 |
TestContext context; |
| 848 |
|
| 849 |
auto paf = kj::newPromiseAndFulfiller<void>(); |
| 850 |
bool destroyed = false; |
| 851 |
auto destructionPromise = paf.promise.then([&]() { destroyed = true; }).eagerlyEvaluate(nullptr); |
| 852 |
|
| 853 |
auto client = context.connect(test::TestSturdyRefObjectId::Tag::TEST_MORE_STUFF) |
| 854 |
.castAs<test::TestMoreStuff>(); |
| 855 |
|
| 856 |
kj::Promise<void> promise = nullptr; |
| 857 |
|
| 858 |
bool returned = false; |
| 859 |
{ |
| 860 |
auto request = client.expectCancelRequest(); |
| 861 |
request.setCap(kj::heap<TestCapDestructor>(kj::mv(paf.fulfiller))); |
| 862 |
promise = request.send().then( |
| 863 |
[&](Response<test::TestMoreStuff::ExpectCancelResults>&& response) { |
| 864 |
returned = true; |
| 865 |
}).eagerlyEvaluate(nullptr); |
| 866 |
} |
| 867 |
kj::evalLater([]() {}).wait(context.waitScope); |
| 868 |
kj::evalLater([]() {}).wait(context.waitScope); |
| 869 |
kj::evalLater([]() {}).wait(context.waitScope); |
| 870 |
kj::evalLater([]() {}).wait(context.waitScope); |
| 871 |
kj::evalLater([]() {}).wait(context.waitScope); |
| 872 |
kj::evalLater([]() {}).wait(context.waitScope); |
| 873 |
|
| 874 |
// We can detect that the method was canceled because it will drop the cap. |
| 875 |
EXPECT_FALSE(destroyed); |
| 876 |
EXPECT_FALSE(returned); |
| 877 |
|
| 878 |
promise = nullptr; // request cancellation |
| 879 |
destructionPromise.wait(context.waitScope); |
| 880 |
|
| 881 |
EXPECT_TRUE(destroyed); |
| 882 |
EXPECT_FALSE(returned); |
| 883 |
} |
| 884 |
|
| 885 |
TEST(Rpc, PromiseResolve) { |
| 886 |
TestContext context; |
| 887 |
|
| 888 |
auto client = context.connect(test::TestSturdyRefObjectId::Tag::TEST_MORE_STUFF) |
| 889 |
.castAs<test::TestMoreStuff>(); |
| 890 |
|
| 891 |
int chainedCallCount = 0; |
| 892 |
|
| 893 |
auto request = client.callFooRequest(); |
| 894 |
auto request2 = client.callFooWhenResolvedRequest(); |
| 895 |
|
| 896 |
auto paf = kj::newPromiseAndFulfiller<test::TestInterface::Client>(); |
| 897 |
|
| 898 |
{ |
| 899 |
auto fork = paf.promise.fork(); |
| 900 |
request.setCap(fork.addBranch()); |
| 901 |
request2.setCap(fork.addBranch()); |
| 902 |
} |
| 903 |
|
| 904 |
auto promise = request.send(); |
| 905 |
auto promise2 = request2.send(); |
| 906 |
|
| 907 |
// Make sure getCap() has been called on the server side by sending another call and waiting |
| 908 |
// for it. |
| 909 |
EXPECT_EQ(2, client.getCallSequenceRequest().send().wait(context.waitScope).getN()); |
| 910 |
EXPECT_EQ(3, context.restorer.callCount); |
| 911 |
|
| 912 |
// OK, now fulfill the local promise. |
| 913 |
paf.fulfiller->fulfill(kj::heap<TestInterfaceImpl>(chainedCallCount)); |
| 914 |
|
| 915 |
// We should now be able to wait for getCap() to finish. |
| 916 |
EXPECT_EQ("bar", promise.wait(context.waitScope).getS()); |
| 917 |
EXPECT_EQ("bar", promise2.wait(context.waitScope).getS()); |
| 918 |
|
| 919 |
EXPECT_EQ(3, context.restorer.callCount); |
| 920 |
EXPECT_EQ(2, chainedCallCount); |
| 921 |
} |
| 922 |
|
| 923 |
TEST(Rpc, RetainAndRelease) { |
| 924 |
TestContext context; |
| 925 |
|
| 926 |
auto paf = kj::newPromiseAndFulfiller<void>(); |
| 927 |
bool destroyed = false; |
| 928 |
auto destructionPromise = paf.promise.then([&]() { destroyed = true; }).eagerlyEvaluate(nullptr); |
| 929 |
|
| 930 |
{ |
| 931 |
auto client = context.connect(test::TestSturdyRefObjectId::Tag::TEST_MORE_STUFF) |
| 932 |
.castAs<test::TestMoreStuff>(); |
| 933 |
|
| 934 |
{ |
| 935 |
auto request = client.holdRequest(); |
| 936 |
request.setCap(kj::heap<TestCapDestructor>(kj::mv(paf.fulfiller))); |
| 937 |
request.send().wait(context.waitScope); |
| 938 |
} |
| 939 |
|
| 940 |
// Do some other call to add a round trip. |
| 941 |
EXPECT_EQ(1, client.getCallSequenceRequest().send().wait(context.waitScope).getN()); |
| 942 |
|
| 943 |
// Shouldn't be destroyed because it's being held by the server. |
| 944 |
EXPECT_FALSE(destroyed); |
| 945 |
|
| 946 |
// We can ask it to call the held capability. |
| 947 |
EXPECT_EQ("bar", client.callHeldRequest().send().wait(context.waitScope).getS()); |
| 948 |
|
| 949 |
{ |
| 950 |
// We can get the cap back from it. |
| 951 |
auto capCopy = client.getHeldRequest().send().wait(context.waitScope).getCap(); |
| 952 |
|
| 953 |
{ |
| 954 |
// And call it, without any network communications. |
| 955 |
uint oldSentCount = context.clientNetwork.getSentCount(); |
| 956 |
auto request = capCopy.fooRequest(); |
| 957 |
request.setI(123); |
| 958 |
request.setJ(true); |
| 959 |
EXPECT_EQ("foo", request.send().wait(context.waitScope).getX()); |
| 960 |
EXPECT_EQ(oldSentCount, context.clientNetwork.getSentCount()); |
| 961 |
} |
| 962 |
|
| 963 |
{ |
| 964 |
// We can send another copy of the same cap to another method, and it works. |
| 965 |
auto request = client.callFooRequest(); |
| 966 |
request.setCap(capCopy); |
| 967 |
EXPECT_EQ("bar", request.send().wait(context.waitScope).getS()); |
| 968 |
} |
| 969 |
} |
| 970 |
|
| 971 |
// Give some time to settle. |
| 972 |
EXPECT_EQ(5, client.getCallSequenceRequest().send().wait(context.waitScope).getN()); |
| 973 |
EXPECT_EQ(6, client.getCallSequenceRequest().send().wait(context.waitScope).getN()); |
| 974 |
EXPECT_EQ(7, client.getCallSequenceRequest().send().wait(context.waitScope).getN()); |
| 975 |
|
| 976 |
// Can't be destroyed, we haven't released it. |
| 977 |
EXPECT_FALSE(destroyed); |
| 978 |
} |
| 979 |
|
| 980 |
// We released our client, which should cause the server to be released, which in turn will |
| 981 |
// release the cap pointing back to us. |
| 982 |
destructionPromise.wait(context.waitScope); |
| 983 |
EXPECT_TRUE(destroyed); |
| 984 |
} |
| 985 |
|
| 986 |
TEST(Rpc, Cancel) { |
| 987 |
TestContext context; |
| 988 |
|
| 989 |
auto client = context.connect(test::TestSturdyRefObjectId::Tag::TEST_MORE_STUFF) |
| 990 |
.castAs<test::TestMoreStuff>(); |
| 991 |
|
| 992 |
auto paf = kj::newPromiseAndFulfiller<void>(); |
| 993 |
bool destroyed = false; |
| 994 |
auto destructionPromise = paf.promise.then([&]() { destroyed = true; }).eagerlyEvaluate(nullptr); |
| 995 |
|
| 996 |
{ |
| 997 |
auto request = client.neverReturnRequest(); |
| 998 |
request.setCap(kj::heap<TestCapDestructor>(kj::mv(paf.fulfiller))); |
| 999 |
|
| 1000 |
{ |
| 1001 |
auto responsePromise = request.send(); |
| 1002 |
|
| 1003 |
// Allow some time to settle. |
| 1004 |
EXPECT_EQ(1, client.getCallSequenceRequest().send().wait(context.waitScope).getN()); |
| 1005 |
EXPECT_EQ(2, client.getCallSequenceRequest().send().wait(context.waitScope).getN()); |
| 1006 |
|
| 1007 |
// The cap shouldn't have been destroyed yet because the call never returned. |
| 1008 |
EXPECT_FALSE(destroyed); |
| 1009 |
} |
| 1010 |
} |
| 1011 |
|
| 1012 |
// Now the cap should be released. |
| 1013 |
destructionPromise.wait(context.waitScope); |
| 1014 |
EXPECT_TRUE(destroyed); |
| 1015 |
} |
| 1016 |
|
| 1017 |
TEST(Rpc, SendTwice) { |
| 1018 |
TestContext context; |
| 1019 |
|
| 1020 |
auto client = context.connect(test::TestSturdyRefObjectId::Tag::TEST_MORE_STUFF) |
| 1021 |
.castAs<test::TestMoreStuff>(); |
| 1022 |
|
| 1023 |
auto paf = kj::newPromiseAndFulfiller<void>(); |
| 1024 |
bool destroyed = false; |
| 1025 |
auto destructionPromise = paf.promise.then([&]() { destroyed = true; }).eagerlyEvaluate(nullptr); |
| 1026 |
|
| 1027 |
auto cap = test::TestInterface::Client(kj::heap<TestCapDestructor>(kj::mv(paf.fulfiller))); |
| 1028 |
|
| 1029 |
{ |
| 1030 |
auto request = client.callFooRequest(); |
| 1031 |
request.setCap(cap); |
| 1032 |
|
| 1033 |
EXPECT_EQ("bar", request.send().wait(context.waitScope).getS()); |
| 1034 |
} |
| 1035 |
|
| 1036 |
// Allow some time for the server to release `cap`. |
| 1037 |
EXPECT_EQ(1, client.getCallSequenceRequest().send().wait(context.waitScope).getN()); |
| 1038 |
|
| 1039 |
{ |
| 1040 |
// More requests with the same cap. |
| 1041 |
auto request = client.callFooRequest(); |
| 1042 |
auto request2 = client.callFooRequest(); |
| 1043 |
request.setCap(cap); |
| 1044 |
request2.setCap(kj::mv(cap)); |
| 1045 |
|
| 1046 |
auto promise = request.send(); |
| 1047 |
auto promise2 = request2.send(); |
| 1048 |
|
| 1049 |
EXPECT_EQ("bar", promise.wait(context.waitScope).getS()); |
| 1050 |
EXPECT_EQ("bar", promise2.wait(context.waitScope).getS()); |
| 1051 |
} |
| 1052 |
|
| 1053 |
// Now the cap should be released. |
| 1054 |
destructionPromise.wait(context.waitScope); |
| 1055 |
EXPECT_TRUE(destroyed); |
| 1056 |
} |
| 1057 |
|
| 1058 |
RemotePromise<test::TestCallOrder::GetCallSequenceResults> getCallSequence( |
| 1059 |
test::TestCallOrder::Client& client, uint expected) { |
| 1060 |
auto req = client.getCallSequenceRequest(); |
| 1061 |
req.setExpected(expected); |
| 1062 |
return req.send(); |
| 1063 |
} |
| 1064 |
|
| 1065 |
TEST(Rpc, Embargo) { |
| 1066 |
TestContext context; |
| 1067 |
|
| 1068 |
auto client = context.connect(test::TestSturdyRefObjectId::Tag::TEST_MORE_STUFF) |
| 1069 |
.castAs<test::TestMoreStuff>(); |
| 1070 |
|
| 1071 |
auto cap = test::TestCallOrder::Client(kj::heap<TestCallOrderImpl>()); |
| 1072 |
|
| 1073 |
auto earlyCall = client.getCallSequenceRequest().send(); |
| 1074 |
|
| 1075 |
auto echoRequest = client.echoRequest(); |
| 1076 |
echoRequest.setCap(cap); |
| 1077 |
auto echo = echoRequest.send(); |
| 1078 |
|
| 1079 |
auto pipeline = echo.getCap(); |
| 1080 |
|
| 1081 |
auto call0 = getCallSequence(pipeline, 0); |
| 1082 |
auto call1 = getCallSequence(pipeline, 1); |
| 1083 |
|
| 1084 |
earlyCall.wait(context.waitScope); |
| 1085 |
|
| 1086 |
auto call2 = getCallSequence(pipeline, 2); |
| 1087 |
|
| 1088 |
auto resolved = echo.wait(context.waitScope).getCap(); |
| 1089 |
|
| 1090 |
auto call3 = getCallSequence(pipeline, 3); |
| 1091 |
auto call4 = getCallSequence(pipeline, 4); |
| 1092 |
auto call5 = getCallSequence(pipeline, 5); |
| 1093 |
|
| 1094 |
EXPECT_EQ(0, call0.wait(context.waitScope).getN()); |
| 1095 |
EXPECT_EQ(1, call1.wait(context.waitScope).getN()); |
| 1096 |
EXPECT_EQ(2, call2.wait(context.waitScope).getN()); |
| 1097 |
EXPECT_EQ(3, call3.wait(context.waitScope).getN()); |
| 1098 |
EXPECT_EQ(4, call4.wait(context.waitScope).getN()); |
| 1099 |
EXPECT_EQ(5, call5.wait(context.waitScope).getN()); |
| 1100 |
} |
| 1101 |
|
| 1102 |
TEST(Rpc, EmbargoUnwrap) { |
| 1103 |
// Test that embargos properly block unwraping a capability using CapabilityServerSet. |
| 1104 |
|
| 1105 |
TestContext context; |
| 1106 |
|
| 1107 |
capnp::CapabilityServerSet<test::TestCallOrder> capSet; |
| 1108 |
|
| 1109 |
auto client = context.connect(test::TestSturdyRefObjectId::Tag::TEST_MORE_STUFF) |
| 1110 |
.castAs<test::TestMoreStuff>(); |
| 1111 |
|
| 1112 |
auto cap = capSet.add(kj::heap<TestCallOrderImpl>()); |
| 1113 |
|
| 1114 |
auto earlyCall = client.getCallSequenceRequest().send(); |
| 1115 |
|
| 1116 |
auto echoRequest = client.echoRequest(); |
| 1117 |
echoRequest.setCap(cap); |
| 1118 |
auto echo = echoRequest.send(); |
| 1119 |
|
| 1120 |
auto pipeline = echo.getCap(); |
| 1121 |
|
| 1122 |
auto unwrap = capSet.getLocalServer(pipeline) |
| 1123 |
.then([](kj::Maybe<test::TestCallOrder::Server&> unwrapped) { |
| 1124 |
return kj::downcast<TestCallOrderImpl>(KJ_ASSERT_NONNULL(unwrapped)).getCount(); |
| 1125 |
}).eagerlyEvaluate(nullptr); |
| 1126 |
|
| 1127 |
auto call0 = getCallSequence(pipeline, 0); |
| 1128 |
auto call1 = getCallSequence(pipeline, 1); |
| 1129 |
|
| 1130 |
earlyCall.wait(context.waitScope); |
| 1131 |
|
| 1132 |
auto call2 = getCallSequence(pipeline, 2); |
| 1133 |
|
| 1134 |
auto resolved = echo.wait(context.waitScope).getCap(); |
| 1135 |
|
| 1136 |
auto call3 = getCallSequence(pipeline, 4); |
| 1137 |
auto call4 = getCallSequence(pipeline, 4); |
| 1138 |
auto call5 = getCallSequence(pipeline, 5); |
| 1139 |
|
| 1140 |
EXPECT_EQ(0, call0.wait(context.waitScope).getN()); |
| 1141 |
EXPECT_EQ(1, call1.wait(context.waitScope).getN()); |
| 1142 |
EXPECT_EQ(2, call2.wait(context.waitScope).getN()); |
| 1143 |
EXPECT_EQ(3, call3.wait(context.waitScope).getN()); |
| 1144 |
EXPECT_EQ(4, call4.wait(context.waitScope).getN()); |
| 1145 |
EXPECT_EQ(5, call5.wait(context.waitScope).getN()); |
| 1146 |
|
| 1147 |
uint unwrappedAt = unwrap.wait(context.waitScope); |
| 1148 |
KJ_EXPECT(unwrappedAt >= 3, unwrappedAt); |
| 1149 |
} |
| 1150 |
|
| 1151 |
template <typename T> |
| 1152 |
void expectPromiseThrows(kj::Promise<T>&& promise, kj::WaitScope& waitScope) { |
| 1153 |
EXPECT_TRUE(promise.then([](T&&) { return false; }, [](kj::Exception&&) { return true; }) |
| 1154 |
.wait(waitScope)); |
| 1155 |
} |
| 1156 |
|
| 1157 |
template <> |
| 1158 |
void expectPromiseThrows(kj::Promise<void>&& promise, kj::WaitScope& waitScope) { |
| 1159 |
EXPECT_TRUE(promise.then([]() { return false; }, [](kj::Exception&&) { return true; }) |
| 1160 |
.wait(waitScope)); |
| 1161 |
} |
| 1162 |
|
| 1163 |
TEST(Rpc, EmbargoError) { |
| 1164 |
TestContext context; |
| 1165 |
|
| 1166 |
auto client = context.connect(test::TestSturdyRefObjectId::Tag::TEST_MORE_STUFF) |
| 1167 |
.castAs<test::TestMoreStuff>(); |
| 1168 |
|
| 1169 |
auto paf = kj::newPromiseAndFulfiller<test::TestCallOrder::Client>(); |
| 1170 |
|
| 1171 |
auto cap = test::TestCallOrder::Client(kj::mv(paf.promise)); |
| 1172 |
|
| 1173 |
auto earlyCall = client.getCallSequenceRequest().send(); |
| 1174 |
|
| 1175 |
auto echoRequest = client.echoRequest(); |
| 1176 |
echoRequest.setCap(cap); |
| 1177 |
auto echo = echoRequest.send(); |
| 1178 |
|
| 1179 |
auto pipeline = echo.getCap(); |
| 1180 |
|
| 1181 |
auto call0 = getCallSequence(pipeline, 0); |
| 1182 |
auto call1 = getCallSequence(pipeline, 1); |
| 1183 |
|
| 1184 |
earlyCall.wait(context.waitScope); |
| 1185 |
|
| 1186 |
auto call2 = getCallSequence(pipeline, 2); |
| 1187 |
|
| 1188 |
auto resolved = echo.wait(context.waitScope).getCap(); |
| 1189 |
|
| 1190 |
auto call3 = getCallSequence(pipeline, 3); |
| 1191 |
auto call4 = getCallSequence(pipeline, 4); |
| 1192 |
auto call5 = getCallSequence(pipeline, 5); |
| 1193 |
|
| 1194 |
paf.fulfiller->rejectIfThrows([]() { KJ_FAIL_ASSERT("foo") { break; } }); |
| 1195 |
|
| 1196 |
expectPromiseThrows(kj::mv(call0), context.waitScope); |
| 1197 |
expectPromiseThrows(kj::mv(call1), context.waitScope); |
| 1198 |
expectPromiseThrows(kj::mv(call2), context.waitScope); |
| 1199 |
expectPromiseThrows(kj::mv(call3), context.waitScope); |
| 1200 |
expectPromiseThrows(kj::mv(call4), context.waitScope); |
| 1201 |
expectPromiseThrows(kj::mv(call5), context.waitScope); |
| 1202 |
|
| 1203 |
// Verify that we're still connected (there were no protocol errors). |
| 1204 |
getCallSequence(client, 1).wait(context.waitScope); |
| 1205 |
} |
| 1206 |
|
| 1207 |
TEST(Rpc, EmbargoNull) { |
| 1208 |
// Set up a situation where we pipeline on a capability that ends up coming back null. This |
| 1209 |
// should NOT cause a Disembargo to be sent, but due to a bug in earlier versions of Cap'n Proto, |
| 1210 |
// a Disembargo was indeed sent to the null capability, which caused the server to disconnect |
| 1211 |
// due to protocol error. |
| 1212 |
|
| 1213 |
TestContext context; |
| 1214 |
|
| 1215 |
auto client = context.connect(test::TestSturdyRefObjectId::Tag::TEST_MORE_STUFF) |
| 1216 |
.castAs<test::TestMoreStuff>(); |
| 1217 |
|
| 1218 |
auto promise = client.getNullRequest().send(); |
| 1219 |
|
| 1220 |
auto cap = promise.getNullCap(); |
| 1221 |
|
| 1222 |
auto call0 = cap.getCallSequenceRequest().send(); |
| 1223 |
|
| 1224 |
promise.wait(context.waitScope); |
| 1225 |
|
| 1226 |
auto call1 = cap.getCallSequenceRequest().send(); |
| 1227 |
|
| 1228 |
expectPromiseThrows(kj::mv(call0), context.waitScope); |
| 1229 |
expectPromiseThrows(kj::mv(call1), context.waitScope); |
| 1230 |
|
| 1231 |
// Verify that we're still connected (there were no protocol errors). |
| 1232 |
getCallSequence(client, 0).wait(context.waitScope); |
| 1233 |
} |
| 1234 |
|
| 1235 |
TEST(Rpc, CallBrokenPromise) { |
| 1236 |
// Tell the server to call back to a promise client, then resolve the promise to an error. |
| 1237 |
|
| 1238 |
TestContext context; |
| 1239 |
|
| 1240 |
auto client = context.connect(test::TestSturdyRefObjectId::Tag::TEST_MORE_STUFF) |
| 1241 |
.castAs<test::TestMoreStuff>(); |
| 1242 |
auto paf = kj::newPromiseAndFulfiller<test::TestInterface::Client>(); |
| 1243 |
|
| 1244 |
{ |
| 1245 |
auto req = client.holdRequest(); |
| 1246 |
req.setCap(kj::mv(paf.promise)); |
| 1247 |
req.send().wait(context.waitScope); |
| 1248 |
} |
| 1249 |
|
| 1250 |
bool returned = false; |
| 1251 |
auto req = client.callHeldRequest().send() |
| 1252 |
.then([&](capnp::Response<test::TestMoreStuff::CallHeldResults>&&) { |
| 1253 |
returned = true; |
| 1254 |
}, [&](kj::Exception&& e) { |
| 1255 |
returned = true; |
| 1256 |
kj::throwRecoverableException(kj::mv(e)); |
| 1257 |
}).eagerlyEvaluate(nullptr); |
| 1258 |
|
| 1259 |
kj::evalLater([]() {}).wait(context.waitScope); |
| 1260 |
kj::evalLater([]() {}).wait(context.waitScope); |
| 1261 |
kj::evalLater([]() {}).wait(context.waitScope); |
| 1262 |
kj::evalLater([]() {}).wait(context.waitScope); |
| 1263 |
kj::evalLater([]() {}).wait(context.waitScope); |
| 1264 |
kj::evalLater([]() {}).wait(context.waitScope); |
| 1265 |
kj::evalLater([]() {}).wait(context.waitScope); |
| 1266 |
kj::evalLater([]() {}).wait(context.waitScope); |
| 1267 |
|
| 1268 |
EXPECT_FALSE(returned); |
| 1269 |
|
| 1270 |
paf.fulfiller->rejectIfThrows([]() { KJ_FAIL_ASSERT("foo") { break; } }); |
| 1271 |
|
| 1272 |
expectPromiseThrows(kj::mv(req), context.waitScope); |
| 1273 |
EXPECT_TRUE(returned); |
| 1274 |
|
| 1275 |
kj::evalLater([]() {}).wait(context.waitScope); |
| 1276 |
kj::evalLater([]() {}).wait(context.waitScope); |
| 1277 |
kj::evalLater([]() {}).wait(context.waitScope); |
| 1278 |
kj::evalLater([]() {}).wait(context.waitScope); |
| 1279 |
kj::evalLater([]() {}).wait(context.waitScope); |
| 1280 |
kj::evalLater([]() {}).wait(context.waitScope); |
| 1281 |
kj::evalLater([]() {}).wait(context.waitScope); |
| 1282 |
kj::evalLater([]() {}).wait(context.waitScope); |
| 1283 |
|
| 1284 |
// Verify that we're still connected (there were no protocol errors). |
| 1285 |
getCallSequence(client, 1).wait(context.waitScope); |
| 1286 |
} |
| 1287 |
|
| 1288 |
TEST(Rpc, Abort) { |
| 1289 |
// Verify that aborts are received. |
| 1290 |
|
| 1291 |
TestContext context; |
| 1292 |
|
| 1293 |
MallocMessageBuilder refMessage(128); |
| 1294 |
auto hostId = refMessage.initRoot<test::TestSturdyRefHostId>(); |
| 1295 |
hostId.setHost("server"); |
| 1296 |
|
| 1297 |
auto conn = KJ_ASSERT_NONNULL(context.clientNetwork.connect(hostId)); |
| 1298 |
|
| 1299 |
{ |
| 1300 |
// Send an invalid message (Return to non-existent question). |
| 1301 |
auto msg = conn->newOutgoingMessage(128); |
| 1302 |
auto body = msg->getBody().initAs<rpc::Message>().initReturn(); |
| 1303 |
body.setAnswerId(1234); |
| 1304 |
body.setCanceled(); |
| 1305 |
msg->send(); |
| 1306 |
} |
| 1307 |
|
| 1308 |
auto reply = KJ_ASSERT_NONNULL(conn->receiveIncomingMessage().wait(context.waitScope)); |
| 1309 |
EXPECT_EQ(rpc::Message::ABORT, reply->getBody().getAs<rpc::Message>().which()); |
| 1310 |
|
| 1311 |
EXPECT_TRUE(conn->receiveIncomingMessage().wait(context.waitScope) == nullptr); |
| 1312 |
} |
| 1313 |
|
| 1314 |
KJ_TEST("handles exceptions thrown during disconnect") { |
| 1315 |
// This is similar to the earlier "abort" test, but throws an exception on |
| 1316 |
// connection shutdown, to exercise the RpcConnectionState error handler. |
| 1317 |
|
| 1318 |
TestContext context; |
| 1319 |
|
| 1320 |
MallocMessageBuilder refMessage(128); |
| 1321 |
auto hostId = refMessage.initRoot<test::TestSturdyRefHostId>(); |
| 1322 |
hostId.setHost("server"); |
| 1323 |
|
| 1324 |
context.serverNetwork.setShutdownExceptionToThrow( |
| 1325 |
KJ_EXCEPTION(FAILED, "a_disconnect_exception")); |
| 1326 |
|
| 1327 |
auto conn = KJ_ASSERT_NONNULL(context.clientNetwork.connect(hostId)); |
| 1328 |
|
| 1329 |
{ |
| 1330 |
// Send an invalid message (Return to non-existent question). |
| 1331 |
auto msg = conn->newOutgoingMessage(128); |
| 1332 |
auto body = msg->getBody().initAs<rpc::Message>().initReturn(); |
| 1333 |
body.setAnswerId(1234); |
| 1334 |
body.setCanceled(); |
| 1335 |
msg->send(); |
| 1336 |
} |
| 1337 |
|
| 1338 |
{ |
| 1339 |
// The internal exception handler of RpcSystemBase logs exceptions thrown |
| 1340 |
// during disconnect, which the test framework will flag as a failure if we |
| 1341 |
// don't explicitly tell it to expect the logged output. |
| 1342 |
KJ_EXPECT_LOG(ERROR, "a_disconnect_exception"); |
| 1343 |
|
| 1344 |
// Force outstanding promises to completion. The server should detect the |
| 1345 |
// invalid message and disconnect, which should cause the connection's |
| 1346 |
// disconnect() to throw an exception that will then be handled by a |
| 1347 |
// RpcConnectionState handler. Since other state instances were freed prior |
| 1348 |
// to the handler invocation, this caused failures in earlier versions of |
| 1349 |
// the code when run under asan. |
| 1350 |
kj::Promise<void>(kj::NEVER_DONE).poll(context.waitScope); |
| 1351 |
} |
| 1352 |
} |
| 1353 |
|
| 1354 |
KJ_TEST("loopback bootstrap()") { |
| 1355 |
int callCount = 0; |
| 1356 |
test::TestInterface::Client bootstrap = kj::heap<TestInterfaceImpl>(callCount); |
| 1357 |
|
| 1358 |
MallocMessageBuilder hostIdBuilder; |
| 1359 |
auto hostId = hostIdBuilder.getRoot<test::TestSturdyRefHostId>(); |
| 1360 |
hostId.setHost("server"); |
| 1361 |
|
| 1362 |
TestContext context(bootstrap); |
| 1363 |
auto client = context.rpcServer.bootstrap(hostId).castAs<test::TestInterface>(); |
| 1364 |
|
| 1365 |
auto request = client.fooRequest(); |
| 1366 |
request.setI(123); |
| 1367 |
request.setJ(true); |
| 1368 |
auto response = request.send().wait(context.waitScope); |
| 1369 |
|
| 1370 |
KJ_EXPECT(response.getX() == "foo"); |
| 1371 |
KJ_EXPECT(callCount == 1); |
| 1372 |
} |
| 1373 |
|
| 1374 |
KJ_TEST("method throws exception") { |
| 1375 |
TestContext context; |
| 1376 |
|
| 1377 |
auto client = context.connect(test::TestSturdyRefObjectId::Tag::TEST_MORE_STUFF) |
| 1378 |
.castAs<test::TestMoreStuff>(); |
| 1379 |
|
| 1380 |
kj::Maybe<kj::Exception> maybeException; |
| 1381 |
client.throwExceptionRequest().send().ignoreResult() |
| 1382 |
.catch_([&](kj::Exception&& e) { |
| 1383 |
maybeException = kj::mv(e); |
| 1384 |
}).wait(context.waitScope); |
| 1385 |
|
| 1386 |
auto exception = KJ_ASSERT_NONNULL(maybeException); |
| 1387 |
KJ_EXPECT(exception.getDescription() == "remote exception: test exception"); |
| 1388 |
KJ_EXPECT(exception.getRemoteTrace() == nullptr); |
| 1389 |
} |
| 1390 |
|
| 1391 |
KJ_TEST("method throws exception won't redundantly add remote exception prefix") { |
| 1392 |
TestContext context; |
| 1393 |
|
| 1394 |
auto client = context.connect(test::TestSturdyRefObjectId::Tag::TEST_MORE_STUFF) |
| 1395 |
.castAs<test::TestMoreStuff>(); |
| 1396 |
|
| 1397 |
kj::Maybe<kj::Exception> maybeException; |
| 1398 |
client.throwRemoteExceptionRequest().send().ignoreResult() |
| 1399 |
.catch_([&](kj::Exception&& e) { |
| 1400 |
maybeException = kj::mv(e); |
| 1401 |
}).wait(context.waitScope); |
| 1402 |
|
| 1403 |
auto exception = KJ_ASSERT_NONNULL(maybeException); |
| 1404 |
KJ_EXPECT(exception.getDescription() == "remote exception: test exception"); |
| 1405 |
KJ_EXPECT(exception.getRemoteTrace() == nullptr); |
| 1406 |
} |
| 1407 |
|
| 1408 |
KJ_TEST("method throws exception with trace encoder") { |
| 1409 |
TestContext context; |
| 1410 |
|
| 1411 |
context.rpcServer.setTraceEncoder([](const kj::Exception& e) { |
| 1412 |
return kj::str("trace for ", e.getDescription()); |
| 1413 |
}); |
| 1414 |
|
| 1415 |
auto client = context.connect(test::TestSturdyRefObjectId::Tag::TEST_MORE_STUFF) |
| 1416 |
.castAs<test::TestMoreStuff>(); |
| 1417 |
|
| 1418 |
kj::Maybe<kj::Exception> maybeException; |
| 1419 |
client.throwExceptionRequest().send().ignoreResult() |
| 1420 |
.catch_([&](kj::Exception&& e) { |
| 1421 |
maybeException = kj::mv(e); |
| 1422 |
}).wait(context.waitScope); |
| 1423 |
|
| 1424 |
auto exception = KJ_ASSERT_NONNULL(maybeException); |
| 1425 |
KJ_EXPECT(exception.getDescription() == "remote exception: test exception"); |
| 1426 |
KJ_EXPECT(exception.getRemoteTrace() == "trace for test exception"); |
| 1427 |
} |
| 1428 |
|
| 1429 |
KJ_TEST("when OutgoingRpcMessage::send() throws, we don't leak exports") { |
| 1430 |
// When OutgoingRpcMessage::send() throws an exception on a Call message, we need to clean up |
| 1431 |
// anything that had been added to the export table as part of the call. At one point this |
| 1432 |
// cleanup was missing, so exports would leak. |
| 1433 |
|
| 1434 |
TestContext context; |
| 1435 |
|
| 1436 |
uint32_t expectedExportNumber = 0; |
| 1437 |
uint interceptCount = 0; |
| 1438 |
bool shouldThrowFromSend = false; |
| 1439 |
context.clientNetwork.onSend([&](MessageBuilder& builder) { |
| 1440 |
auto message = builder.getRoot<rpc::Message>().asReader(); |
| 1441 |
if (message.isCall()) { |
| 1442 |
auto call = message.getCall(); |
| 1443 |
if (call.getInterfaceId() == capnp::typeId<test::TestMoreStuff>() && |
| 1444 |
call.getMethodId() == 0) { |
| 1445 |
// callFoo() request, expect a capability in the param caps. Specifically we expect a |
| 1446 |
// promise, because that's what we send below. |
| 1447 |
auto capTable = call.getParams().getCapTable(); |
| 1448 |
KJ_ASSERT(capTable.size() == 1); |
| 1449 |
auto desc = capTable[0]; |
| 1450 |
KJ_ASSERT(desc.isSenderPromise()); |
| 1451 |
KJ_ASSERT(desc.getSenderPromise() == expectedExportNumber); |
| 1452 |
|
| 1453 |
++interceptCount; |
| 1454 |
if (shouldThrowFromSend) { |
| 1455 |
kj::throwRecoverableException(KJ_EXCEPTION(FAILED, "intercepted")); |
| 1456 |
return false; // only matters when -fno-exceptions |
| 1457 |
} |
| 1458 |
} |
| 1459 |
} |
| 1460 |
return true; |
| 1461 |
}); |
| 1462 |
|
| 1463 |
auto client = context.connect(test::TestSturdyRefObjectId::Tag::TEST_MORE_STUFF) |
| 1464 |
.castAs<test::TestMoreStuff>(); |
| 1465 |
|
| 1466 |
{ |
| 1467 |
shouldThrowFromSend = true; |
| 1468 |
auto req = client.callFooRequest(); |
| 1469 |
req.setCap(kj::Promise<test::TestInterface::Client>(kj::NEVER_DONE)); |
| 1470 |
req.send().then([](auto&&) { |
| 1471 |
KJ_FAIL_ASSERT("should have thrown"); |
| 1472 |
}, [](kj::Exception&& e) { |
| 1473 |
KJ_EXPECT(e.getDescription() == "intercepted", e); |
| 1474 |
}).wait(context.waitScope); |
| 1475 |
} |
| 1476 |
|
| 1477 |
KJ_EXPECT(interceptCount == 1); |
| 1478 |
|
| 1479 |
// Sending again should use the same export number, because the export table entry should have |
| 1480 |
// been released when send() threw. (At one point, this was a bug...) |
| 1481 |
{ |
| 1482 |
shouldThrowFromSend = true; |
| 1483 |
auto req = client.callFooRequest(); |
| 1484 |
req.setCap(kj::Promise<test::TestInterface::Client>(kj::NEVER_DONE)); |
| 1485 |
req.send().then([](auto&&) { |
| 1486 |
KJ_FAIL_ASSERT("should have thrown"); |
| 1487 |
}, [](kj::Exception&& e) { |
| 1488 |
KJ_EXPECT(e.getDescription() == "intercepted", e); |
| 1489 |
}).wait(context.waitScope); |
| 1490 |
} |
| 1491 |
|
| 1492 |
KJ_EXPECT(interceptCount == 2); |
| 1493 |
|
| 1494 |
// Now lets start a call that doesn't throw. The export number should still be zero because |
| 1495 |
// the previous exports were released. |
| 1496 |
{ |
| 1497 |
shouldThrowFromSend = false; |
| 1498 |
auto req = client.callFooRequest(); |
| 1499 |
req.setCap(kj::Promise<test::TestInterface::Client>(kj::NEVER_DONE)); |
| 1500 |
auto promise = req.send(); |
| 1501 |
KJ_EXPECT(!promise.poll(context.waitScope)); |
| 1502 |
|
| 1503 |
KJ_EXPECT(interceptCount == 3); |
| 1504 |
} |
| 1505 |
|
| 1506 |
// We canceled the previous call, BUT the exported capability is still present until the other |
| 1507 |
// side drops it, which it won't because the call isn't marked cancelable and never completes. |
| 1508 |
// Now, let's send another call. This time, we expect a new export number will actually be |
| 1509 |
// allocated. |
| 1510 |
{ |
| 1511 |
shouldThrowFromSend = false; |
| 1512 |
expectedExportNumber = 1; |
| 1513 |
auto req = client.callFooRequest(); |
| 1514 |
auto paf = kj::newPromiseAndFulfiller<test::TestInterface::Client>(); |
| 1515 |
req.setCap(kj::mv(paf.promise)); |
| 1516 |
auto promise = req.send(); |
| 1517 |
KJ_EXPECT(!promise.poll(context.waitScope)); |
| 1518 |
|
| 1519 |
KJ_EXPECT(interceptCount == 4); |
| 1520 |
|
| 1521 |
// Now let's actually let the RPC complete so we can verify the RPC system isn't broken or |
| 1522 |
// anything. |
| 1523 |
int callCount = 0; |
| 1524 |
paf.fulfiller->fulfill(kj::heap<TestInterfaceImpl>(callCount)); |
| 1525 |
auto resp = promise.wait(context.waitScope); |
| 1526 |
KJ_EXPECT(resp.getS() == "bar"); |
| 1527 |
KJ_EXPECT(callCount == 1); |
| 1528 |
} |
| 1529 |
|
| 1530 |
// Now if we do yet another call, it'll reuse export number 1. |
| 1531 |
{ |
| 1532 |
shouldThrowFromSend = false; |
| 1533 |
expectedExportNumber = 1; |
| 1534 |
auto req = client.callFooRequest(); |
| 1535 |
req.setCap(kj::Promise<test::TestInterface::Client>(kj::NEVER_DONE)); |
| 1536 |
auto promise = req.send(); |
| 1537 |
KJ_EXPECT(!promise.poll(context.waitScope)); |
| 1538 |
|
| 1539 |
KJ_EXPECT(interceptCount == 5); |
| 1540 |
} |
| 1541 |
} |
| 1542 |
|
| 1543 |
KJ_TEST("export the same promise twice") { |
| 1544 |
TestContext context; |
| 1545 |
|
| 1546 |
bool exportIsPromise; |
| 1547 |
uint32_t expectedExportNumber; |
| 1548 |
uint interceptCount = 0; |
| 1549 |
context.clientNetwork.onSend([&](MessageBuilder& builder) { |
| 1550 |
auto message = builder.getRoot<rpc::Message>().asReader(); |
| 1551 |
if (message.isCall()) { |
| 1552 |
auto call = message.getCall(); |
| 1553 |
if (call.getInterfaceId() == capnp::typeId<test::TestMoreStuff>() && |
| 1554 |
call.getMethodId() == 0) { |
| 1555 |
// callFoo() request, expect a capability in the param caps. Specifically we expect a |
| 1556 |
// promise, because that's what we send below. |
| 1557 |
auto capTable = call.getParams().getCapTable(); |
| 1558 |
KJ_ASSERT(capTable.size() == 1); |
| 1559 |
auto desc = capTable[0]; |
| 1560 |
if (exportIsPromise) { |
| 1561 |
KJ_ASSERT(desc.isSenderPromise()); |
| 1562 |
KJ_ASSERT(desc.getSenderPromise() == expectedExportNumber); |
| 1563 |
} else { |
| 1564 |
KJ_ASSERT(desc.isSenderHosted()); |
| 1565 |
KJ_ASSERT(desc.getSenderHosted() == expectedExportNumber); |
| 1566 |
} |
| 1567 |
|
| 1568 |
++interceptCount; |
| 1569 |
} |
| 1570 |
} |
| 1571 |
return true; |
| 1572 |
}); |
| 1573 |
|
| 1574 |
auto client = context.connect(test::TestSturdyRefObjectId::Tag::TEST_MORE_STUFF) |
| 1575 |
.castAs<test::TestMoreStuff>(); |
| 1576 |
|
| 1577 |
auto sendReq = [&](test::TestInterface::Client cap) { |
| 1578 |
auto req = client.callFooRequest(); |
| 1579 |
req.setCap(kj::mv(cap)); |
| 1580 |
return req.send(); |
| 1581 |
}; |
| 1582 |
|
| 1583 |
auto expectNeverDone = [&](auto& promise) { |
| 1584 |
if (promise.poll(context.waitScope)) { |
| 1585 |
promise.wait(context.waitScope); // let it throw if it's going to |
| 1586 |
KJ_FAIL_ASSERT("promise finished without throwing"); |
| 1587 |
} |
| 1588 |
}; |
| 1589 |
|
| 1590 |
int callCount = 0; |
| 1591 |
test::TestInterface::Client normalCap = kj::heap<TestInterfaceImpl>(callCount); |
| 1592 |
test::TestInterface::Client promiseCap = kj::Promise<test::TestInterface::Client>(kj::NEVER_DONE); |
| 1593 |
|
| 1594 |
// Send request with a promise capability in the params. |
| 1595 |
exportIsPromise = true; |
| 1596 |
expectedExportNumber = 0; |
| 1597 |
auto promise1 = sendReq(promiseCap); |
| 1598 |
expectNeverDone(promise1); |
| 1599 |
KJ_EXPECT(interceptCount == 1); |
| 1600 |
|
| 1601 |
// Send a second request with the same promise should use the same export table entry. |
| 1602 |
auto promise2 = sendReq(promiseCap); |
| 1603 |
expectNeverDone(promise2); |
| 1604 |
KJ_EXPECT(interceptCount == 2); |
| 1605 |
|
| 1606 |
// Sending a request with a different promise should use a different export table entry. |
| 1607 |
expectedExportNumber = 1; |
| 1608 |
auto promise3 = sendReq(kj::Promise<test::TestInterface::Client>(kj::NEVER_DONE)); |
| 1609 |
expectNeverDone(promise3); |
| 1610 |
KJ_EXPECT(interceptCount == 3); |
| 1611 |
|
| 1612 |
// Now try sending a non-promise cap. We'll send all these requests at once before waiting on |
| 1613 |
// any of them since these will actually complete. |
| 1614 |
exportIsPromise = false; |
| 1615 |
expectedExportNumber = 2; |
| 1616 |
auto promise4 = sendReq(normalCap); |
| 1617 |
auto promise5 = sendReq(normalCap); |
| 1618 |
expectedExportNumber = 3; |
| 1619 |
auto promise6 = sendReq(kj::heap<TestInterfaceImpl>(callCount)); |
| 1620 |
KJ_EXPECT(interceptCount == 6); |
| 1621 |
|
| 1622 |
KJ_EXPECT(promise4.wait(context.waitScope).getS() == "bar"); |
| 1623 |
KJ_EXPECT(promise5.wait(context.waitScope).getS() == "bar"); |
| 1624 |
KJ_EXPECT(promise6.wait(context.waitScope).getS() == "bar"); |
| 1625 |
KJ_EXPECT(callCount == 3); |
| 1626 |
} |
| 1627 |
|
| 1628 |
} // namespace |
| 1629 |
} // namespace _ (private) |
| 1630 |
} // namespace capnp |