capability-test.c++ 23.2 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
// Copyright (c) 2013, Kenton Varda <temporal@gmail.com>
// All rights reserved.
//
// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions are met:
//
// 1. Redistributions of source code must retain the above copyright notice, this
//    list of conditions and the following disclaimer.
// 2. Redistributions in binary form must reproduce the above copyright notice,
//    this list of conditions and the following disclaimer in the documentation
//    and/or other materials provided with the distribution.
//
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
// ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
// WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
// DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
// ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
// (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
// LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
// ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
// SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

24 25 26 27 28 29
#include "schema.capnp.h"

#ifdef CAPNP_CAPABILITY_H_
#error "schema.capnp should not depend on capability.h, because it contains no interfaces."
#endif

Kenton Varda's avatar
Kenton Varda committed
30
#include <capnp/test.capnp.h>
31 32 33 34 35

#ifndef CAPNP_CAPABILITY_H_
#error "test.capnp did not include capability.h."
#endif

36 37 38 39 40 41 42 43 44 45
#include "capability.h"
#include "test-util.h"
#include <kj/debug.h>
#include <gtest/gtest.h>

namespace capnp {
namespace _ {
namespace {

TEST(Capability, Basic) {
46
  kj::EventLoop loop;
47
  kj::WaitScope waitScope(loop);
48

49
  int callCount = 0;
50
  test::TestInterface::Client client(kj::heap<TestInterfaceImpl>(callCount));
51 52 53 54 55 56 57 58 59 60

  auto request1 = client.fooRequest();
  request1.setI(123);
  request1.setJ(true);
  auto promise1 = request1.send();

  auto request2 = client.bazRequest();
  initTestMessage(request2.initS());
  auto promise2 = request2.send();

61
  bool barFailed = false;
62
  auto request3 = client.barRequest();
63
  auto promise3 = request3.send().then(
64 65
      [](Response<test::TestInterface::BarResults>&& response) {
        ADD_FAILURE() << "Expected bar() call to fail.";
66 67
      }, [&](kj::Exception&& e) {
        barFailed = true;
68 69 70 71
      });

  EXPECT_EQ(0, callCount);

72
  auto response1 = promise1.wait(waitScope);
73 74 75

  EXPECT_EQ("foo", response1.getX());

76
  auto response2 = promise2.wait(waitScope);
77

78
  promise3.wait(waitScope);
79 80

  EXPECT_EQ(2, callCount);
81
  EXPECT_TRUE(barFailed);
82 83 84
}

TEST(Capability, Inheritance) {
85
  kj::EventLoop loop;
86
  kj::WaitScope waitScope(loop);
87

88
  int callCount = 0;
89
  test::TestExtends::Client client1(kj::heap<TestExtendsImpl>(callCount));
90 91
  test::TestInterface::Client client2 = client1;
  auto client = client2.castAs<test::TestExtends>();
92 93 94 95 96 97 98 99 100 101

  auto request1 = client.fooRequest();
  request1.setI(321);
  auto promise1 = request1.send();

  auto request2 = client.graultRequest();
  auto promise2 = request2.send();

  EXPECT_EQ(0, callCount);

102
  auto response2 = promise2.wait(waitScope);
103 104 105

  checkTestMessage(response2);

106
  auto response1 = promise1.wait(waitScope);
107 108 109 110 111 112

  EXPECT_EQ("bar", response1.getX());

  EXPECT_EQ(2, callCount);
}

113
TEST(Capability, Pipelining) {
114
  kj::EventLoop loop;
115
  kj::WaitScope waitScope(loop);
116 117 118

  int callCount = 0;
  int chainedCallCount = 0;
119
  test::TestPipeline::Client client(kj::heap<TestPipelineImpl>(callCount));
120 121

  auto request = client.getCapRequest();
122
  request.setN(234);
123
  request.setInCap(test::TestInterface::Client(kj::heap<TestInterfaceImpl>(chainedCallCount)));
124 125 126

  auto promise = request.send();

127
  auto pipelineRequest = promise.getOutBox().getCap().fooRequest();
128 129 130
  pipelineRequest.setI(321);
  auto pipelinePromise = pipelineRequest.send();

131 132 133 134 135
  auto pipelineRequest2 = promise.getOutBox().getCap().castAs<test::TestExtends>().graultRequest();
  auto pipelinePromise2 = pipelineRequest2.send();

  promise = nullptr;  // Just to be annoying, drop the original promise.

136 137 138
  EXPECT_EQ(0, callCount);
  EXPECT_EQ(0, chainedCallCount);

139
  auto response = pipelinePromise.wait(waitScope);
140 141
  EXPECT_EQ("bar", response.getX());

142
  auto response2 = pipelinePromise2.wait(waitScope);
143 144 145 146 147 148
  checkTestMessage(response2);

  EXPECT_EQ(3, callCount);
  EXPECT_EQ(1, chainedCallCount);
}

149
TEST(Capability, TailCall) {
150
  kj::EventLoop loop;
151
  kj::WaitScope waitScope(loop);
152 153 154 155

  int calleeCallCount = 0;
  int callerCallCount = 0;

156 157
  test::TestTailCallee::Client callee(kj::heap<TestTailCalleeImpl>(calleeCallCount));
  test::TestTailCaller::Client caller(kj::heap<TestTailCallerImpl>(callerCallCount));
158 159 160 161 162 163 164 165 166

  auto request = caller.fooRequest();
  request.setI(456);
  request.setCallee(callee);

  auto promise = request.send();

  auto dependentCall0 = promise.getC().getCallSequenceRequest().send();

167
  auto response = promise.wait(waitScope);
168 169 170 171 172 173 174
  EXPECT_EQ(456, response.getI());
  EXPECT_EQ(456, response.getI());

  auto dependentCall1 = promise.getC().getCallSequenceRequest().send();

  auto dependentCall2 = response.getC().getCallSequenceRequest().send();

175 176 177
  EXPECT_EQ(0, dependentCall0.wait(waitScope).getN());
  EXPECT_EQ(1, dependentCall1.wait(waitScope).getN());
  EXPECT_EQ(2, dependentCall2.wait(waitScope).getN());
178 179 180 181 182

  EXPECT_EQ(1, calleeCallCount);
  EXPECT_EQ(1, callerCallCount);
}

183
TEST(Capability, AsyncCancelation) {
184
  // Tests allowCancellation().
185

186
  kj::EventLoop loop;
187
  kj::WaitScope waitScope(loop);
188 189 190

  auto paf = kj::newPromiseAndFulfiller<void>();
  bool destroyed = false;
191
  auto destructionPromise = paf.promise.then([&]() { destroyed = true; }).eagerlyEvaluate(nullptr);
192 193 194

  int callCount = 0;

195
  test::TestMoreStuff::Client client(kj::heap<TestMoreStuffImpl>(callCount));
196 197 198 199 200

  kj::Promise<void> promise = nullptr;

  bool returned = false;
  {
201
    auto request = client.expectCancelRequest();
202
    request.setCap(test::TestInterface::Client(kj::heap<TestCapDestructor>(kj::mv(paf.fulfiller))));
203
    promise = request.send().then(
204
        [&](Response<test::TestMoreStuff::ExpectCancelResults>&& response) {
205
      returned = true;
206
    }).eagerlyEvaluate(nullptr);
207
  }
208 209
  kj::evalLater([]() {}).wait(waitScope);
  kj::evalLater([]() {}).wait(waitScope);
210 211 212 213 214 215

  // We can detect that the method was canceled because it will drop the cap.
  EXPECT_FALSE(destroyed);
  EXPECT_FALSE(returned);

  promise = nullptr;  // request cancellation
216
  destructionPromise.wait(waitScope);
217 218 219 220 221

  EXPECT_TRUE(destroyed);
  EXPECT_FALSE(returned);
}

222 223 224
// =======================================================================================

TEST(Capability, DynamicClient) {
225
  kj::EventLoop loop;
226
  kj::WaitScope waitScope(loop);
227 228 229

  int callCount = 0;
  DynamicCapability::Client client =
230
      test::TestInterface::Client(kj::heap<TestInterfaceImpl>(callCount));
231 232 233 234 235 236 237 238 239 240 241 242

  auto request1 = client.newRequest("foo");
  request1.set("i", 123);
  request1.set("j", true);
  auto promise1 = request1.send();

  auto request2 = client.newRequest("baz");
  initDynamicTestMessage(request2.init("s").as<DynamicStruct>());
  auto promise2 = request2.send();

  bool barFailed = false;
  auto request3 = client.newRequest("bar");
243
  auto promise3 = request3.send().then(
244 245 246 247 248 249 250 251
      [](Response<DynamicStruct>&& response) {
        ADD_FAILURE() << "Expected bar() call to fail.";
      }, [&](kj::Exception&& e) {
        barFailed = true;
      });

  EXPECT_EQ(0, callCount);

252
  auto response1 = promise1.wait(waitScope);
253 254 255

  EXPECT_EQ("foo", response1.get("x").as<Text>());

256
  auto response2 = promise2.wait(waitScope);
257

258
  promise3.wait(waitScope);
259 260 261 262 263 264

  EXPECT_EQ(2, callCount);
  EXPECT_TRUE(barFailed);
}

TEST(Capability, DynamicClientInheritance) {
265
  kj::EventLoop loop;
266
  kj::WaitScope waitScope(loop);
267 268 269 270

  int callCount = 0;

  DynamicCapability::Client client1 =
271
      test::TestExtends::Client(kj::heap<TestExtendsImpl>(callCount));
272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289
  EXPECT_EQ(Schema::from<test::TestExtends>(), client1.getSchema());
  EXPECT_NE(Schema::from<test::TestInterface>(), client1.getSchema());

  DynamicCapability::Client client2 = client1.upcast(Schema::from<test::TestInterface>());
  EXPECT_EQ(Schema::from<test::TestInterface>(), client2.getSchema());

  EXPECT_ANY_THROW(client2.upcast(Schema::from<test::TestExtends>()));
  auto client = client2.castAs<DynamicCapability>(Schema::from<test::TestExtends>());

  auto request1 = client.newRequest("foo");
  request1.set("i", 321);
  auto promise1 = request1.send();

  auto request2 = client.newRequest("grault");
  auto promise2 = request2.send();

  EXPECT_EQ(0, callCount);

290
  auto response2 = promise2.wait(waitScope);
291 292 293

  checkDynamicTestMessage(response2.as<DynamicStruct>());

294
  auto response1 = promise1.wait(waitScope);
295 296 297

  EXPECT_EQ("bar", response1.get("x").as<Text>());

298
  EXPECT_EQ(2, callCount);
299 300 301
}

TEST(Capability, DynamicClientPipelining) {
302
  kj::EventLoop loop;
303
  kj::WaitScope waitScope(loop);
304 305 306 307

  int callCount = 0;
  int chainedCallCount = 0;
  DynamicCapability::Client client =
308
      test::TestPipeline::Client(kj::heap<TestPipelineImpl>(callCount));
309 310 311

  auto request = client.newRequest("getCap");
  request.set("n", 234);
312
  request.set("inCap", test::TestInterface::Client(kj::heap<TestInterfaceImpl>(chainedCallCount)));
313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329

  auto promise = request.send();

  auto outCap = promise.get("outBox").releaseAs<DynamicStruct>()
                       .get("cap").releaseAs<DynamicCapability>();
  auto pipelineRequest = outCap.newRequest("foo");
  pipelineRequest.set("i", 321);
  auto pipelinePromise = pipelineRequest.send();

  auto pipelineRequest2 = outCap.castAs<test::TestExtends>().graultRequest();
  auto pipelinePromise2 = pipelineRequest2.send();

  promise = nullptr;  // Just to be annoying, drop the original promise.

  EXPECT_EQ(0, callCount);
  EXPECT_EQ(0, chainedCallCount);

330
  auto response = pipelinePromise.wait(waitScope);
331 332
  EXPECT_EQ("bar", response.get("x").as<Text>());

333
  auto response2 = pipelinePromise2.wait(waitScope);
334 335 336
  checkTestMessage(response2);

  EXPECT_EQ(3, callCount);
337 338 339
  EXPECT_EQ(1, chainedCallCount);
}

340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367
// =======================================================================================

class TestInterfaceDynamicImpl final: public DynamicCapability::Server {
public:
  TestInterfaceDynamicImpl(int& callCount)
      : DynamicCapability::Server(Schema::from<test::TestInterface>()),
        callCount(callCount) {}

  int& callCount;

  kj::Promise<void> call(InterfaceSchema::Method method,
                         CallContext<DynamicStruct, DynamicStruct> context) {
    auto methodName = method.getProto().getName();
    if (methodName == "foo") {
      ++callCount;
      auto params = context.getParams();
      EXPECT_EQ(123, params.get("i").as<int>());
      EXPECT_TRUE(params.get("j").as<bool>());
      context.getResults().set("x", "foo");
      return kj::READY_NOW;
    } else if (methodName == "baz") {
      ++callCount;
      auto params = context.getParams();
      checkDynamicTestMessage(params.get("s").as<DynamicStruct>());
      context.releaseParams();
      EXPECT_ANY_THROW(context.getParams());
      return kj::READY_NOW;
    } else {
368 369
      KJ_FAIL_ASSERT("Method not implemented", methodName) { break; }
      return kj::READY_NOW;
370 371 372 373 374
    }
  }
};

TEST(Capability, DynamicServer) {
375
  kj::EventLoop loop;
376
  kj::WaitScope waitScope(loop);
377 378 379

  int callCount = 0;
  test::TestInterface::Client client =
380
      DynamicCapability::Client(kj::heap<TestInterfaceDynamicImpl>(callCount))
381 382 383 384 385 386 387 388 389 390 391 392 393
          .castAs<test::TestInterface>();

  auto request1 = client.fooRequest();
  request1.setI(123);
  request1.setJ(true);
  auto promise1 = request1.send();

  auto request2 = client.bazRequest();
  initTestMessage(request2.initS());
  auto promise2 = request2.send();

  bool barFailed = false;
  auto request3 = client.barRequest();
394
  auto promise3 = request3.send().then(
395 396 397 398 399 400 401 402
      [](Response<test::TestInterface::BarResults>&& response) {
        ADD_FAILURE() << "Expected bar() call to fail.";
      }, [&](kj::Exception&& e) {
        barFailed = true;
      });

  EXPECT_EQ(0, callCount);

403
  auto response1 = promise1.wait(waitScope);
404 405 406

  EXPECT_EQ("foo", response1.getX());

407
  auto response2 = promise2.wait(waitScope);
408

409
  promise3.wait(waitScope);
410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444

  EXPECT_EQ(2, callCount);
  EXPECT_TRUE(barFailed);
}

class TestExtendsDynamicImpl final: public DynamicCapability::Server {
public:
  TestExtendsDynamicImpl(int& callCount)
      : DynamicCapability::Server(Schema::from<test::TestExtends>()),
        callCount(callCount) {}

  int& callCount;

  kj::Promise<void> call(InterfaceSchema::Method method,
                         CallContext<DynamicStruct, DynamicStruct> context) {
    auto methodName = method.getProto().getName();
    if (methodName == "foo") {
      ++callCount;
      auto params = context.getParams();
      EXPECT_EQ(321, params.get("i").as<int>());
      EXPECT_FALSE(params.get("j").as<bool>());
      context.getResults().set("x", "bar");
      return kj::READY_NOW;
    } else if (methodName == "grault") {
      ++callCount;
      context.releaseParams();
      initDynamicTestMessage(context.getResults());
      return kj::READY_NOW;
    } else {
      KJ_FAIL_ASSERT("Method not implemented", methodName);
    }
  }
};

TEST(Capability, DynamicServerInheritance) {
445
  kj::EventLoop loop;
446
  kj::WaitScope waitScope(loop);
447 448 449

  int callCount = 0;
  test::TestExtends::Client client1 =
450
      DynamicCapability::Client(kj::heap<TestExtendsDynamicImpl>(callCount))
451 452 453 454 455 456 457 458 459 460 461 462 463
          .castAs<test::TestExtends>();
  test::TestInterface::Client client2 = client1;
  auto client = client2.castAs<test::TestExtends>();

  auto request1 = client.fooRequest();
  request1.setI(321);
  auto promise1 = request1.send();

  auto request2 = client.graultRequest();
  auto promise2 = request2.send();

  EXPECT_EQ(0, callCount);

464
  auto response2 = promise2.wait(waitScope);
465 466 467

  checkTestMessage(response2);

468
  auto response1 = promise1.wait(waitScope);
469 470 471 472 473 474 475 476 477

  EXPECT_EQ("bar", response1.getX());

  EXPECT_EQ(2, callCount);
}

class TestPipelineDynamicImpl final: public DynamicCapability::Server {
public:
  TestPipelineDynamicImpl(int& callCount)
478
      : DynamicCapability::Server(Schema::from<test::TestPipeline>()),
479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509
        callCount(callCount) {}

  int& callCount;

  kj::Promise<void> call(InterfaceSchema::Method method,
                         CallContext<DynamicStruct, DynamicStruct> context) {
    auto methodName = method.getProto().getName();
    if (methodName == "getCap") {
      ++callCount;

      auto params = context.getParams();
      EXPECT_EQ(234, params.get("n").as<uint32_t>());

      auto cap = params.get("inCap").as<DynamicCapability>();
      context.releaseParams();

      auto request = cap.newRequest("foo");
      request.set("i", 123);
      request.set("j", true);

      return request.send().then(
          [this,context](capnp::Response<DynamicStruct>&& response) mutable {
            EXPECT_EQ("foo", response.get("x").as<Text>());

            auto result = context.getResults();
            result.set("s", "bar");

            auto box = result.init("outBox").as<DynamicStruct>();

            // Too lazy to write a whole separate test for each of these cases...  so just make
            // sure they both compile here, and only actually test the latter.
510 511
            box.set("cap", kj::heap<TestExtendsDynamicImpl>(callCount));
            box.set("cap", kj::heap<TestExtendsImpl>(callCount));
512 513 514 515 516 517 518 519
          });
    } else {
      KJ_FAIL_ASSERT("Method not implemented", methodName);
    }
  }
};

TEST(Capability, DynamicServerPipelining) {
520
  kj::EventLoop loop;
521
  kj::WaitScope waitScope(loop);
522 523 524

  int callCount = 0;
  int chainedCallCount = 0;
525 526 527
  test::TestPipeline::Client client =
      DynamicCapability::Client(kj::heap<TestPipelineDynamicImpl>(callCount))
          .castAs<test::TestPipeline>();
528 529 530

  auto request = client.getCapRequest();
  request.setN(234);
531
  request.setInCap(test::TestInterface::Client(kj::heap<TestInterfaceImpl>(chainedCallCount)));
532 533 534 535 536 537 538 539 540 541 542 543 544 545 546

  auto promise = request.send();

  auto pipelineRequest = promise.getOutBox().getCap().fooRequest();
  pipelineRequest.setI(321);
  auto pipelinePromise = pipelineRequest.send();

  auto pipelineRequest2 = promise.getOutBox().getCap().castAs<test::TestExtends>().graultRequest();
  auto pipelinePromise2 = pipelineRequest2.send();

  promise = nullptr;  // Just to be annoying, drop the original promise.

  EXPECT_EQ(0, callCount);
  EXPECT_EQ(0, chainedCallCount);

547
  auto response = pipelinePromise.wait(waitScope);
548 549
  EXPECT_EQ("bar", response.getX());

550
  auto response2 = pipelinePromise2.wait(waitScope);
551 552 553 554 555 556
  checkTestMessage(response2);

  EXPECT_EQ(3, callCount);
  EXPECT_EQ(1, chainedCallCount);
}

557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617
class TestTailCallerDynamicImpl final: public DynamicCapability::Server {
public:
  TestTailCallerDynamicImpl(int& callCount)
      : DynamicCapability::Server(Schema::from<test::TestTailCaller>()),
        callCount(callCount) {}

  int& callCount;

  kj::Promise<void> call(InterfaceSchema::Method method,
                         CallContext<DynamicStruct, DynamicStruct> context) {
    auto methodName = method.getProto().getName();
    if (methodName == "foo") {
      ++callCount;

      auto params = context.getParams();
      auto tailRequest = params.get("callee").as<DynamicCapability>().newRequest("foo");
      tailRequest.set("i", params.get("i"));
      tailRequest.set("t", "from TestTailCaller");
      return context.tailCall(kj::mv(tailRequest));
    } else {
      KJ_FAIL_ASSERT("Method not implemented", methodName);
    }
  }
};

TEST(Capability, DynamicServerTailCall) {
  kj::EventLoop loop;
  kj::WaitScope waitScope(loop);

  int calleeCallCount = 0;
  int callerCallCount = 0;

  test::TestTailCallee::Client callee(kj::heap<TestTailCalleeImpl>(calleeCallCount));
  test::TestTailCaller::Client caller =
      DynamicCapability::Client(kj::heap<TestTailCallerDynamicImpl>(callerCallCount))
          .castAs<test::TestTailCaller>();

  auto request = caller.fooRequest();
  request.setI(456);
  request.setCallee(callee);

  auto promise = request.send();

  auto dependentCall0 = promise.getC().getCallSequenceRequest().send();

  auto response = promise.wait(waitScope);
  EXPECT_EQ(456, response.getI());
  EXPECT_EQ(456, response.getI());

  auto dependentCall1 = promise.getC().getCallSequenceRequest().send();

  auto dependentCall2 = response.getC().getCallSequenceRequest().send();

  EXPECT_EQ(0, dependentCall0.wait(waitScope).getN());
  EXPECT_EQ(1, dependentCall1.wait(waitScope).getN());
  EXPECT_EQ(2, dependentCall2.wait(waitScope).getN());

  EXPECT_EQ(1, calleeCallCount);
  EXPECT_EQ(1, callerCallCount);
}

618 619
// =======================================================================================

620 621
void verifyClient(test::TestInterface::Client client, const int& callCount,
                  kj::WaitScope& waitScope) {
622 623 624 625
  int origCount = callCount;
  auto request = client.fooRequest();
  request.setI(123);
  request.setJ(true);
626
  auto response = request.send().wait(waitScope);
627 628 629 630
  EXPECT_EQ("foo", response.getX());
  EXPECT_EQ(origCount + 1, callCount);
}

631 632
void verifyClient(DynamicCapability::Client client, const int& callCount,
                  kj::WaitScope& waitScope) {
633 634 635 636
  int origCount = callCount;
  auto request = client.newRequest("foo");
  request.set("i", 123);
  request.set("j", true);
637
  auto response = request.send().wait(waitScope);
638 639 640 641
  EXPECT_EQ("foo", response.get("x").as<Text>());
  EXPECT_EQ(origCount + 1, callCount);
}

642
TEST(Capability, AnyPointersAndOrphans) {
643
  kj::EventLoop loop;
644
  kj::WaitScope waitScope(loop);
645 646 647 648 649 650 651

  int callCount1 = 0;
  int callCount2 = 0;

  // We use a TestPipeline instance here merely as a way to conveniently obtain an imbued message
  // instance.
  test::TestPipeline::Client baseClient(nullptr);
652 653
  test::TestInterface::Client client1(kj::heap<TestInterfaceImpl>(callCount1));
  test::TestInterface::Client client2(kj::heap<TestInterfaceImpl>(callCount2));
654 655 656 657 658 659 660 661 662 663 664

  auto request = baseClient.testPointersRequest();
  request.setCap(client1);

  EXPECT_TRUE(request.hasCap());

  Orphan<test::TestInterface> orphan = request.disownCap();
  EXPECT_FALSE(orphan == nullptr);

  EXPECT_FALSE(request.hasCap());

665 666
  verifyClient(orphan.get(), callCount1, waitScope);
  verifyClient(orphan.getReader(), callCount1, waitScope);
667 668 669 670

  request.getObj().adopt(kj::mv(orphan));
  EXPECT_TRUE(orphan == nullptr);

671 672
  verifyClient(request.getObj().getAs<test::TestInterface>(), callCount1, waitScope);
  verifyClient(request.asReader().getObj().getAs<test::TestInterface>(), callCount1, waitScope);
673
  verifyClient(request.getObj().getAs<DynamicCapability>(
674
      Schema::from<test::TestInterface>()), callCount1, waitScope);
675
  verifyClient(request.asReader().getObj().getAs<DynamicCapability>(
676
      Schema::from<test::TestInterface>()), callCount1, waitScope);
677 678 679 680 681

  request.getObj().clear();
  EXPECT_FALSE(request.hasObj());

  request.getObj().setAs<test::TestInterface>(client2);
682
  verifyClient(request.getObj().getAs<test::TestInterface>(), callCount2, waitScope);
683 684 685

  Orphan<DynamicCapability> dynamicOrphan = request.getObj().disownAs<DynamicCapability>(
      Schema::from<test::TestInterface>());
686 687
  verifyClient(dynamicOrphan.get(), callCount2, waitScope);
  verifyClient(dynamicOrphan.getReader(), callCount2, waitScope);
688 689

  Orphan<DynamicValue> dynamicValueOrphan = kj::mv(dynamicOrphan);
690
  verifyClient(dynamicValueOrphan.get().as<DynamicCapability>(), callCount2, waitScope);
691 692 693

  orphan = dynamicValueOrphan.releaseAs<test::TestInterface>();
  EXPECT_FALSE(orphan == nullptr);
694
  verifyClient(orphan.get(), callCount2, waitScope);
695 696 697 698

  request.adoptCap(kj::mv(orphan));
  EXPECT_TRUE(orphan == nullptr);

699
  verifyClient(request.getCap(), callCount2, waitScope);
700 701

  Orphan<DynamicCapability> dynamicOrphan2 = request.disownCap();
702 703
  verifyClient(dynamicOrphan2.get(), callCount2, waitScope);
  verifyClient(dynamicOrphan2.getReader(), callCount2, waitScope);
704 705 706
}

TEST(Capability, Lists) {
707
  kj::EventLoop loop;
708
  kj::WaitScope waitScope(loop);
709 710 711 712

  int callCount1 = 0;
  int callCount2 = 0;
  int callCount3 = 0;
713 714 715 716
  test::TestPipeline::Client baseClient(kj::heap<TestPipelineImpl>(callCount1));
  test::TestInterface::Client client1(kj::heap<TestInterfaceImpl>(callCount1));
  test::TestInterface::Client client2(kj::heap<TestInterfaceImpl>(callCount2));
  test::TestInterface::Client client3(kj::heap<TestInterfaceImpl>(callCount3));
717 718 719 720 721 722 723 724

  auto request = baseClient.testPointersRequest();

  auto list = request.initList(3);
  list.set(0, client1);
  list.set(1, client2);
  list.set(2, client3);

725 726 727
  verifyClient(list[0], callCount1, waitScope);
  verifyClient(list[1], callCount2, waitScope);
  verifyClient(list[2], callCount3, waitScope);
728 729

  auto listReader = request.asReader().getList();
730 731 732
  verifyClient(listReader[0], callCount1, waitScope);
  verifyClient(listReader[1], callCount2, waitScope);
  verifyClient(listReader[2], callCount3, waitScope);
733 734

  auto dynamicList = toDynamic(list);
735 736 737
  verifyClient(dynamicList[0].as<DynamicCapability>(), callCount1, waitScope);
  verifyClient(dynamicList[1].as<DynamicCapability>(), callCount2, waitScope);
  verifyClient(dynamicList[2].as<DynamicCapability>(), callCount3, waitScope);
738 739

  auto dynamicListReader = toDynamic(listReader);
740 741 742
  verifyClient(dynamicListReader[0].as<DynamicCapability>(), callCount1, waitScope);
  verifyClient(dynamicListReader[1].as<DynamicCapability>(), callCount2, waitScope);
  verifyClient(dynamicListReader[2].as<DynamicCapability>(), callCount3, waitScope);
743 744
}

745 746 747
}  // namespace
}  // namespace _
}  // namespace capnp