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: |
Lang Hames | 3fde652 | 2016-04-18 19:55:43 +0000 | [diff] [blame] | 47 | typedef Function<2, void(bool)> BasicVoid; |
| 48 | typedef Function<3, int32_t(bool)> BasicInt; |
| 49 | typedef Function<4, void(int8_t, uint8_t, int16_t, uint16_t, |
| 50 | int32_t, uint32_t, int64_t, uint64_t, |
| 51 | bool, std::string, std::vector<int>)> AllTheTypes; |
Lang Hames | 4d0a5a9e | 2016-01-11 01:40:11 +0000 | [diff] [blame] | 52 | }; |
| 53 | |
| 54 | |
Lang Hames | 3fde652 | 2016-04-18 19:55:43 +0000 | [diff] [blame] | 55 | TEST_F(DummyRPC, TestAsyncBasicVoid) { |
Lang Hames | 4d0a5a9e | 2016-01-11 01:40:11 +0000 | [diff] [blame] | 56 | std::queue<char> Queue; |
| 57 | QueueChannel C(Queue); |
| 58 | |
Lang Hames | 3fde652 | 2016-04-18 19:55:43 +0000 | [diff] [blame] | 59 | // Make an async call. |
| 60 | auto ResOrErr = callAsync<BasicVoid>(C, true); |
| 61 | EXPECT_TRUE(!!ResOrErr) << "Simple call over queue failed"; |
Lang Hames | 4d0a5a9e | 2016-01-11 01:40:11 +0000 | [diff] [blame] | 62 | |
| 63 | { |
| 64 | // Expect a call to Proc1. |
Lang Hames | 3fde652 | 2016-04-18 19:55:43 +0000 | [diff] [blame] | 65 | auto EC = expect<BasicVoid>(C, |
Lang Hames | 4d0a5a9e | 2016-01-11 01:40:11 +0000 | [diff] [blame] | 66 | [&](bool &B) { |
| 67 | EXPECT_EQ(B, true) |
| 68 | << "Bool serialization broken"; |
| 69 | return std::error_code(); |
| 70 | }); |
| 71 | EXPECT_FALSE(EC) << "Simple expect over queue failed"; |
| 72 | } |
Lang Hames | 3fde652 | 2016-04-18 19:55:43 +0000 | [diff] [blame] | 73 | |
| 74 | { |
| 75 | // Wait for the result. |
| 76 | auto EC = waitForResult(C, ResOrErr->second, handleNone); |
| 77 | EXPECT_FALSE(EC) << "Could not read result."; |
| 78 | } |
| 79 | |
| 80 | // Verify that the function returned ok. |
| 81 | auto Val = ResOrErr->first.get(); |
| 82 | EXPECT_TRUE(Val) << "Remote void function failed to execute."; |
| 83 | } |
| 84 | |
| 85 | TEST_F(DummyRPC, TestAsyncBasicInt) { |
| 86 | std::queue<char> Queue; |
| 87 | QueueChannel C(Queue); |
| 88 | |
| 89 | // Make an async call. |
| 90 | auto ResOrErr = callAsync<BasicInt>(C, false); |
| 91 | EXPECT_TRUE(!!ResOrErr) << "Simple call over queue failed"; |
| 92 | |
| 93 | { |
| 94 | // Expect a call to Proc1. |
| 95 | auto EC = expect<BasicInt>(C, |
| 96 | [&](bool &B) { |
| 97 | EXPECT_EQ(B, false) |
| 98 | << "Bool serialization broken"; |
| 99 | return 42; |
| 100 | }); |
| 101 | EXPECT_FALSE(EC) << "Simple expect over queue failed"; |
| 102 | } |
| 103 | |
| 104 | { |
| 105 | // Wait for the result. |
| 106 | auto EC = waitForResult(C, ResOrErr->second, handleNone); |
| 107 | EXPECT_FALSE(EC) << "Could not read result."; |
| 108 | } |
| 109 | |
| 110 | // Verify that the function returned ok. |
| 111 | auto Val = ResOrErr->first.get(); |
| 112 | EXPECT_TRUE(!!Val) << "Remote int function failed to execute."; |
| 113 | EXPECT_EQ(*Val, 42) << "Remote int function return wrong value."; |
Lang Hames | 4d0a5a9e | 2016-01-11 01:40:11 +0000 | [diff] [blame] | 114 | } |
| 115 | |
| 116 | TEST_F(DummyRPC, TestSerialization) { |
| 117 | std::queue<char> Queue; |
| 118 | QueueChannel C(Queue); |
| 119 | |
Lang Hames | 3fde652 | 2016-04-18 19:55:43 +0000 | [diff] [blame] | 120 | // Make a call to Proc1. |
| 121 | std::vector<int> v({42, 7}); |
| 122 | auto ResOrErr = callAsync<AllTheTypes>(C, |
| 123 | -101, |
| 124 | 250, |
| 125 | -10000, |
| 126 | 10000, |
| 127 | -1000000000, |
| 128 | 1000000000, |
| 129 | -10000000000, |
| 130 | 10000000000, |
| 131 | true, |
| 132 | "foo", |
| 133 | v); |
| 134 | EXPECT_TRUE(!!ResOrErr) |
| 135 | << "Big (serialization test) call over queue failed"; |
Lang Hames | 4d0a5a9e | 2016-01-11 01:40:11 +0000 | [diff] [blame] | 136 | |
| 137 | { |
| 138 | // Expect a call to Proc1. |
| 139 | auto EC = expect<AllTheTypes>(C, |
| 140 | [&](int8_t &s8, |
| 141 | uint8_t &u8, |
| 142 | int16_t &s16, |
| 143 | uint16_t &u16, |
| 144 | int32_t &s32, |
| 145 | uint32_t &u32, |
| 146 | int64_t &s64, |
| 147 | uint64_t &u64, |
| 148 | bool &b, |
| 149 | std::string &s, |
| 150 | std::vector<int> &v) { |
| 151 | |
| 152 | EXPECT_EQ(s8, -101) |
| 153 | << "int8_t serialization broken"; |
| 154 | EXPECT_EQ(u8, 250) |
| 155 | << "uint8_t serialization broken"; |
| 156 | EXPECT_EQ(s16, -10000) |
| 157 | << "int16_t serialization broken"; |
| 158 | EXPECT_EQ(u16, 10000) |
| 159 | << "uint16_t serialization broken"; |
| 160 | EXPECT_EQ(s32, -1000000000) |
| 161 | << "int32_t serialization broken"; |
| 162 | EXPECT_EQ(u32, 1000000000ULL) |
| 163 | << "uint32_t serialization broken"; |
| 164 | EXPECT_EQ(s64, -10000000000) |
| 165 | << "int64_t serialization broken"; |
| 166 | EXPECT_EQ(u64, 10000000000ULL) |
| 167 | << "uint64_t serialization broken"; |
| 168 | EXPECT_EQ(b, true) |
| 169 | << "bool serialization broken"; |
| 170 | EXPECT_EQ(s, "foo") |
| 171 | << "std::string serialization broken"; |
| 172 | EXPECT_EQ(v, std::vector<int>({42, 7})) |
| 173 | << "std::vector serialization broken"; |
| 174 | return std::error_code(); |
| 175 | }); |
| 176 | EXPECT_FALSE(EC) << "Big (serialization test) call over queue failed"; |
| 177 | } |
Lang Hames | 3fde652 | 2016-04-18 19:55:43 +0000 | [diff] [blame] | 178 | |
| 179 | { |
| 180 | // Wait for the result. |
| 181 | auto EC = waitForResult(C, ResOrErr->second, handleNone); |
| 182 | EXPECT_FALSE(EC) << "Could not read result."; |
| 183 | } |
| 184 | |
| 185 | // Verify that the function returned ok. |
| 186 | auto Val = ResOrErr->first.get(); |
| 187 | EXPECT_TRUE(Val) << "Remote void function failed to execute."; |
Lang Hames | 4d0a5a9e | 2016-01-11 01:40:11 +0000 | [diff] [blame] | 188 | } |