rpc_test_utils.h
1 // Copyright 2018, Beeri 15. All rights reserved.
2 // Author: Roman Gershman (romange@gmail.com)
3 //
4 #pragma once
5 
6 #include "base/gtest.h"
7 #include "util/asio/io_context_pool.h"
8 #include "util/rpc/rpc_connection.h"
9 
10 namespace util {
11 class ReconnectableSocket;
12 class AcceptServer;
13 class FiberSyncSocket;
14 
15 namespace rpc {
16 
17 template <typename Src, typename Dest>
18 void Copy(const Src& src, Dest* dest) {
19  dest->resize(src.size());
20  std::copy(src.begin(), src.end(), dest->begin());
21 }
22 
23 class TestBridge final : public ConnectionBridge {
24  bool clear_;
25 
26  public:
27  TestBridge(bool clear) : clear_(clear) {
28  }
29 
30  ~TestBridge();
31 
32  void Join() final;
33 
34  // header and letter are input/output parameters.
35  // HandleEnvelope reads first the input and if everything is parsed fine, it sends
36  // back another header, letter pair.
37  void HandleEnvelope(uint64_t rpc_id, Envelope* envelope, EnvelopeWriter writer) final;
38 };
39 
40 class TestInterface final : public ServiceInterface {
41  bool clear_ = false;
42 
43  public:
44  void set_clear(bool c) {
45  clear_ = c;
46  }
47  ConnectionBridge* CreateConnectionBridge() override {
48  return new TestBridge{clear_};
49  }
50 };
51 
52 class ServerTest : public testing::Test {
53  public:
54  ServerTest();
55 
56  protected:
57  static void SetUpTestCase() {
58  }
59 
60  static void TearDownTestCase() {
61  }
62 
63  void SetUp() override;
64 
65  void TearDown() override;
66 
67  std::unique_ptr<TestInterface> service_;
68  std::unique_ptr<AcceptServer> server_;
69  std::unique_ptr<IoContextPool> pool_;
70  std::unique_ptr<FiberSyncSocket> sock2_;
71 
72  ::boost::system::error_code ec_;
73  uint16_t port_ = 0;
74 };
75 
76 } // namespace rpc
77 } // namespace util