Lang Hames | 4d0a5a9e | 2016-01-11 01:40:11 +0000 | [diff] [blame] | 1 | //===----------- RPCUtilsTest.cpp - Unit tests the Orc RPC utils ----------===// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
| 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
| 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | |
| 10 | #include "llvm/ExecutionEngine/Orc/RPCChannel.h" |
| 11 | #include "llvm/ExecutionEngine/Orc/RPCUtils.h" |
| 12 | #include "gtest/gtest.h" |
| 13 | |
| 14 | #include <queue> |
| 15 | |
| 16 | using namespace llvm; |
| 17 | using namespace llvm::orc; |
| 18 | using namespace llvm::orc::remote; |
| 19 | |
| 20 | class QueueChannel : public RPCChannel { |
| 21 | public: |
| 22 | QueueChannel(std::queue<char> &Queue) : Queue(Queue) {} |
| 23 | |
| 24 | std::error_code readBytes(char *Dst, unsigned Size) override { |
| 25 | while (Size--) { |
| 26 | *Dst++ = Queue.front(); |
| 27 | Queue.pop(); |
| 28 | } |
| 29 | return std::error_code(); |
| 30 | } |
| 31 | |
| 32 | std::error_code appendBytes(const char *Src, unsigned Size) override { |
| 33 | while (Size--) |
| 34 | Queue.push(*Src++); |
| 35 | return std::error_code(); |
| 36 | } |
| 37 | |
| 38 | std::error_code send() override { return std::error_code(); } |
| 39 | |
| 40 | private: |
| 41 | std::queue<char> &Queue; |
| 42 | }; |
| 43 | |
| 44 | class DummyRPC : public testing::Test, |
| 45 | public RPC<QueueChannel> { |
| 46 | public: |
Nico Weber | ca94d0e | 2016-04-18 13:57:08 +0000 | [diff] [blame] | 47 | typedef Procedure<1, void(bool)> Proc1; |
| 48 | typedef Procedure<2, void(int8_t, uint8_t, int16_t, uint16_t, |
| 49 | int32_t, uint32_t, int64_t, uint64_t, |
| 50 | bool, std::string, std::vector<int>)> AllTheTypes; |
Lang Hames | 4d0a5a9e | 2016-01-11 01:40:11 +0000 | [diff] [blame] | 51 | }; |
| 52 | |
| 53 | |
Nico Weber | ca94d0e | 2016-04-18 13:57:08 +0000 | [diff] [blame] | 54 | TEST_F(DummyRPC, TestBasic) { |
Lang Hames | 4d0a5a9e | 2016-01-11 01:40:11 +0000 | [diff] [blame] | 55 | std::queue<char> Queue; |
| 56 | QueueChannel C(Queue); |
| 57 | |
Nico Weber | ca94d0e | 2016-04-18 13:57:08 +0000 | [diff] [blame] | 58 | { |
| 59 | // Make a call to Proc1. |
| 60 | auto EC = call<Proc1>(C, true); |
| 61 | EXPECT_FALSE(EC) << "Simple call over queue failed"; |
| 62 | } |
Lang Hames | 4d0a5a9e | 2016-01-11 01:40:11 +0000 | [diff] [blame] | 63 | |
| 64 | { |
| 65 | // Expect a call to Proc1. |
Nico Weber | ca94d0e | 2016-04-18 13:57:08 +0000 | [diff] [blame] | 66 | auto EC = expect<Proc1>(C, |
Lang Hames | 4d0a5a9e | 2016-01-11 01:40:11 +0000 | [diff] [blame] | 67 | [&](bool &B) { |
| 68 | EXPECT_EQ(B, true) |
| 69 | << "Bool serialization broken"; |
| 70 | return std::error_code(); |
| 71 | }); |
| 72 | EXPECT_FALSE(EC) << "Simple expect over queue failed"; |
| 73 | } |
| 74 | } |
| 75 | |
| 76 | TEST_F(DummyRPC, TestSerialization) { |
| 77 | std::queue<char> Queue; |
| 78 | QueueChannel C(Queue); |
| 79 | |
Nico Weber | ca94d0e | 2016-04-18 13:57:08 +0000 | [diff] [blame] | 80 | { |
| 81 | // Make a call to Proc1. |
| 82 | std::vector<int> v({42, 7}); |
| 83 | auto EC = call<AllTheTypes>(C, |
| 84 | -101, |
| 85 | 250, |
| 86 | -10000, |
| 87 | 10000, |
| 88 | -1000000000, |
| 89 | 1000000000, |
| 90 | -10000000000, |
| 91 | 10000000000, |
| 92 | true, |
| 93 | "foo", |
| 94 | v); |
| 95 | EXPECT_FALSE(EC) << "Big (serialization test) call over queue failed"; |
| 96 | } |
Lang Hames | 4d0a5a9e | 2016-01-11 01:40:11 +0000 | [diff] [blame] | 97 | |
| 98 | { |
| 99 | // Expect a call to Proc1. |
| 100 | auto EC = expect<AllTheTypes>(C, |
| 101 | [&](int8_t &s8, |
| 102 | uint8_t &u8, |
| 103 | int16_t &s16, |
| 104 | uint16_t &u16, |
| 105 | int32_t &s32, |
| 106 | uint32_t &u32, |
| 107 | int64_t &s64, |
| 108 | uint64_t &u64, |
| 109 | bool &b, |
| 110 | std::string &s, |
| 111 | std::vector<int> &v) { |
| 112 | |
| 113 | EXPECT_EQ(s8, -101) |
| 114 | << "int8_t serialization broken"; |
| 115 | EXPECT_EQ(u8, 250) |
| 116 | << "uint8_t serialization broken"; |
| 117 | EXPECT_EQ(s16, -10000) |
| 118 | << "int16_t serialization broken"; |
| 119 | EXPECT_EQ(u16, 10000) |
| 120 | << "uint16_t serialization broken"; |
| 121 | EXPECT_EQ(s32, -1000000000) |
| 122 | << "int32_t serialization broken"; |
| 123 | EXPECT_EQ(u32, 1000000000ULL) |
| 124 | << "uint32_t serialization broken"; |
| 125 | EXPECT_EQ(s64, -10000000000) |
| 126 | << "int64_t serialization broken"; |
| 127 | EXPECT_EQ(u64, 10000000000ULL) |
| 128 | << "uint64_t serialization broken"; |
| 129 | EXPECT_EQ(b, true) |
| 130 | << "bool serialization broken"; |
| 131 | EXPECT_EQ(s, "foo") |
| 132 | << "std::string serialization broken"; |
| 133 | EXPECT_EQ(v, std::vector<int>({42, 7})) |
| 134 | << "std::vector serialization broken"; |
| 135 | return std::error_code(); |
| 136 | }); |
| 137 | EXPECT_FALSE(EC) << "Big (serialization test) call over queue failed"; |
| 138 | } |
Lang Hames | 4d0a5a9e | 2016-01-11 01:40:11 +0000 | [diff] [blame] | 139 | } |