capnproto

FORK: Cap'n Proto serialization/RPC system - core tools and C++ library
git clone https://git.neptards.moe/neptards/capnproto.git
Log | Files | Refs | README | LICENSE

json-rpc-test.c++ (3279B)


      1 // Copyright (c) 2018 Kenton Varda 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 #include "json-rpc.h"
     23 #include <kj/test.h>
     24 #include <capnp/test-util.h>
     25 
     26 namespace capnp {
     27 namespace _ {  // private
     28 namespace {
     29 
     30 KJ_TEST("json-rpc basics") {
     31   auto io = kj::setupAsyncIo();
     32   auto pipe = kj::newTwoWayPipe();
     33 
     34   JsonRpc::ContentLengthTransport clientTransport(*pipe.ends[0]);
     35   JsonRpc::ContentLengthTransport serverTransport(*pipe.ends[1]);
     36 
     37   int callCount = 0;
     38 
     39   JsonRpc client(clientTransport);
     40   JsonRpc server(serverTransport, toDynamic(kj::heap<TestInterfaceImpl>(callCount)));
     41 
     42   auto cap = client.getPeer<test::TestInterface>();
     43   auto req = cap.fooRequest();
     44   req.setI(123);
     45   req.setJ(true);
     46   auto resp = req.send().wait(io.waitScope);
     47   KJ_EXPECT(resp.getX() == "foo");
     48 
     49   KJ_EXPECT(callCount == 1);
     50 }
     51 
     52 KJ_TEST("json-rpc error") {
     53   auto io = kj::setupAsyncIo();
     54   auto pipe = kj::newTwoWayPipe();
     55 
     56   JsonRpc::ContentLengthTransport clientTransport(*pipe.ends[0]);
     57   JsonRpc::ContentLengthTransport serverTransport(*pipe.ends[1]);
     58 
     59   int callCount = 0;
     60 
     61   JsonRpc client(clientTransport);
     62   JsonRpc server(serverTransport, toDynamic(kj::heap<TestInterfaceImpl>(callCount)));
     63 
     64   auto cap = client.getPeer<test::TestInterface>();
     65   KJ_EXPECT_THROW_MESSAGE("Method not implemented", cap.barRequest().send().wait(io.waitScope));
     66 }
     67 
     68 KJ_TEST("json-rpc multiple calls") {
     69   auto io = kj::setupAsyncIo();
     70   auto pipe = kj::newTwoWayPipe();
     71 
     72   JsonRpc::ContentLengthTransport clientTransport(*pipe.ends[0]);
     73   JsonRpc::ContentLengthTransport serverTransport(*pipe.ends[1]);
     74 
     75   int callCount = 0;
     76 
     77   JsonRpc client(clientTransport);
     78   JsonRpc server(serverTransport, toDynamic(kj::heap<TestInterfaceImpl>(callCount)));
     79 
     80   auto cap = client.getPeer<test::TestInterface>();
     81   auto req1 = cap.fooRequest();
     82   req1.setI(123);
     83   req1.setJ(true);
     84   auto promise1 = req1.send();
     85 
     86   auto req2 = cap.bazRequest();
     87   initTestMessage(req2.initS());
     88   auto promise2 = req2.send();
     89 
     90   auto resp1 = promise1.wait(io.waitScope);
     91   KJ_EXPECT(resp1.getX() == "foo");
     92 
     93   auto resp2 = promise2.wait(io.waitScope);
     94 
     95   KJ_EXPECT(callCount == 2);
     96 }
     97 
     98 }  // namespace
     99 }  // namespace _ (private)
    100 }  // namespace capnp