blob: 77632e35eb1245781d32335c3069fabec3289e07 [file] [log] [blame]
Lang Hames4d0a5a9e2016-01-11 01:40:11 +00001//===----------- 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
16using namespace llvm;
17using namespace llvm::orc;
18using namespace llvm::orc::remote;
19
20class QueueChannel : public RPCChannel {
21public:
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
40private:
41 std::queue<char> &Queue;
42};
43
44class DummyRPC : public testing::Test,
45 public RPC<QueueChannel> {
46public:
Lang Hames3fde6522016-04-18 19:55:43 +000047 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 Hames4d0a5a9e2016-01-11 01:40:11 +000052};
53
54
Lang Hames3fde6522016-04-18 19:55:43 +000055TEST_F(DummyRPC, TestAsyncBasicVoid) {
Lang Hames4d0a5a9e2016-01-11 01:40:11 +000056 std::queue<char> Queue;
57 QueueChannel C(Queue);
58
Lang Hames3fde6522016-04-18 19:55:43 +000059 // Make an async call.
60 auto ResOrErr = callAsync<BasicVoid>(C, true);
61 EXPECT_TRUE(!!ResOrErr) << "Simple call over queue failed";
Lang Hames4d0a5a9e2016-01-11 01:40:11 +000062
63 {
64 // Expect a call to Proc1.
Lang Hames3fde6522016-04-18 19:55:43 +000065 auto EC = expect<BasicVoid>(C,
Lang Hames4d0a5a9e2016-01-11 01:40:11 +000066 [&](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 Hames3fde6522016-04-18 19:55:43 +000073
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
85TEST_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 Hames4d0a5a9e2016-01-11 01:40:11 +0000114}
115
116TEST_F(DummyRPC, TestSerialization) {
117 std::queue<char> Queue;
118 QueueChannel C(Queue);
119
Lang Hames3fde6522016-04-18 19:55:43 +0000120 // 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 Hames4d0a5a9e2016-01-11 01:40:11 +0000136
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 Hames3fde6522016-04-18 19:55:43 +0000178
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 Hames4d0a5a9e2016-01-11 01:40:11 +0000188}