[Orc] Add support for remote JITing to the ORC API.
This patch adds utilities to ORC for managing a remote JIT target. It consists
of:
1. A very primitive RPC system for making calls over a byte-stream. See
RPCChannel.h, RPCUtils.h.
2. An RPC API defined in the above system for managing memory, looking up
symbols, creating stubs, etc. on a remote target. See OrcRemoteTargetRPCAPI.h.
3. An interface for creating high-level JIT components (memory managers,
callback managers, stub managers, etc.) that operate over the RPC API. See
OrcRemoteTargetClient.h.
4. A helper class for building servers that can handle the RPC calls. See
OrcRemoteTargetServer.h.
The system is designed to work neatly with the existing ORC components and
functionality. In particular, the ORC callback API (and consequently the
CompileOnDemandLayer) is supported, enabling lazy compilation of remote code.
Assuming this doesn't trigger any builder failures, a follow-up patch will be
committed which tests these utilities by using them to replace LLI's existing
remote-JITing demo code.
llvm-svn: 257305
2016-01-11 02:40:11 +01:00
|
|
|
//===----------- RPCUtilsTest.cpp - Unit tests the Orc RPC utils ----------===//
|
|
|
|
//
|
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#include "llvm/ExecutionEngine/Orc/RPCUtils.h"
|
2017-04-06 03:49:21 +02:00
|
|
|
#include "QueueChannel.h"
|
[Orc] Add support for remote JITing to the ORC API.
This patch adds utilities to ORC for managing a remote JIT target. It consists
of:
1. A very primitive RPC system for making calls over a byte-stream. See
RPCChannel.h, RPCUtils.h.
2. An RPC API defined in the above system for managing memory, looking up
symbols, creating stubs, etc. on a remote target. See OrcRemoteTargetRPCAPI.h.
3. An interface for creating high-level JIT components (memory managers,
callback managers, stub managers, etc.) that operate over the RPC API. See
OrcRemoteTargetClient.h.
4. A helper class for building servers that can handle the RPC calls. See
OrcRemoteTargetServer.h.
The system is designed to work neatly with the existing ORC components and
functionality. In particular, the ORC callback API (and consequently the
CompileOnDemandLayer) is supported, enabling lazy compilation of remote code.
Assuming this doesn't trigger any builder failures, a follow-up patch will be
committed which tests these utilities by using them to replace LLI's existing
remote-JITing demo code.
llvm-svn: 257305
2016-01-11 02:40:11 +01:00
|
|
|
#include "gtest/gtest.h"
|
|
|
|
|
|
|
|
#include <queue>
|
|
|
|
|
|
|
|
using namespace llvm;
|
|
|
|
using namespace llvm::orc;
|
2016-11-11 22:42:09 +01:00
|
|
|
using namespace llvm::orc::rpc;
|
[Orc] Add support for remote JITing to the ORC API.
This patch adds utilities to ORC for managing a remote JIT target. It consists
of:
1. A very primitive RPC system for making calls over a byte-stream. See
RPCChannel.h, RPCUtils.h.
2. An RPC API defined in the above system for managing memory, looking up
symbols, creating stubs, etc. on a remote target. See OrcRemoteTargetRPCAPI.h.
3. An interface for creating high-level JIT components (memory managers,
callback managers, stub managers, etc.) that operate over the RPC API. See
OrcRemoteTargetClient.h.
4. A helper class for building servers that can handle the RPC calls. See
OrcRemoteTargetServer.h.
The system is designed to work neatly with the existing ORC components and
functionality. In particular, the ORC callback API (and consequently the
CompileOnDemandLayer) is supported, enabling lazy compilation of remote code.
Assuming this doesn't trigger any builder failures, a follow-up patch will be
committed which tests these utilities by using them to replace LLI's existing
remote-JITing demo code.
llvm-svn: 257305
2016-01-11 02:40:11 +01:00
|
|
|
|
2016-12-21 01:59:33 +01:00
|
|
|
class RPCFoo {};
|
|
|
|
|
2016-12-21 03:08:23 +01:00
|
|
|
namespace llvm {
|
|
|
|
namespace orc {
|
|
|
|
namespace rpc {
|
2016-12-21 01:59:33 +01:00
|
|
|
|
2016-12-21 03:08:23 +01:00
|
|
|
template <>
|
|
|
|
class RPCTypeName<RPCFoo> {
|
|
|
|
public:
|
|
|
|
static const char* getName() { return "RPCFoo"; }
|
|
|
|
};
|
2016-12-21 01:59:33 +01:00
|
|
|
|
2016-12-21 03:08:23 +01:00
|
|
|
template <>
|
|
|
|
class SerializationTraits<QueueChannel, RPCFoo, RPCFoo> {
|
|
|
|
public:
|
|
|
|
static Error serialize(QueueChannel&, const RPCFoo&) {
|
|
|
|
return Error::success();
|
|
|
|
}
|
|
|
|
|
|
|
|
static Error deserialize(QueueChannel&, RPCFoo&) {
|
|
|
|
return Error::success();
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
} // end namespace rpc
|
|
|
|
} // end namespace orc
|
|
|
|
} // end namespace llvm
|
2016-12-21 01:59:33 +01:00
|
|
|
|
|
|
|
class RPCBar {};
|
|
|
|
|
2017-04-13 05:51:35 +02:00
|
|
|
class DummyError : public ErrorInfo<DummyError> {
|
|
|
|
public:
|
|
|
|
|
|
|
|
static char ID;
|
|
|
|
|
|
|
|
DummyError(uint32_t Val) : Val(Val) {}
|
|
|
|
|
|
|
|
std::error_code convertToErrorCode() const override {
|
|
|
|
// Use a nonsense error code - we want to verify that errors
|
|
|
|
// transmitted over the network are replaced with
|
|
|
|
// OrcErrorCode::UnknownErrorCodeFromRemote.
|
|
|
|
return orcError(OrcErrorCode::RemoteAllocatorDoesNotExist);
|
|
|
|
}
|
|
|
|
|
|
|
|
void log(raw_ostream &OS) const override {
|
|
|
|
OS << "Dummy error " << Val;
|
|
|
|
}
|
|
|
|
|
|
|
|
uint32_t getValue() const { return Val; }
|
|
|
|
|
|
|
|
public:
|
|
|
|
uint32_t Val;
|
|
|
|
};
|
|
|
|
|
|
|
|
char DummyError::ID = 0;
|
|
|
|
|
|
|
|
template <typename ChannelT>
|
|
|
|
void registerDummyErrorSerialization() {
|
|
|
|
static bool AlreadyRegistered = false;
|
|
|
|
if (!AlreadyRegistered) {
|
|
|
|
SerializationTraits<ChannelT, Error>::
|
|
|
|
template registerErrorType<DummyError>(
|
|
|
|
"DummyError",
|
|
|
|
[](ChannelT &C, const DummyError &DE) {
|
|
|
|
return serializeSeq(C, DE.getValue());
|
|
|
|
},
|
|
|
|
[](ChannelT &C, Error &Err) -> Error {
|
|
|
|
ErrorAsOutParameter EAO(&Err);
|
|
|
|
uint32_t Val;
|
|
|
|
if (auto Err = deserializeSeq(C, Val))
|
|
|
|
return Err;
|
|
|
|
Err = make_error<DummyError>(Val);
|
|
|
|
return Error::success();
|
|
|
|
});
|
|
|
|
AlreadyRegistered = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-12-21 03:08:23 +01:00
|
|
|
namespace llvm {
|
|
|
|
namespace orc {
|
|
|
|
namespace rpc {
|
2016-12-21 01:59:33 +01:00
|
|
|
|
2016-12-21 03:08:23 +01:00
|
|
|
template <>
|
|
|
|
class SerializationTraits<QueueChannel, RPCFoo, RPCBar> {
|
|
|
|
public:
|
|
|
|
static Error serialize(QueueChannel&, const RPCBar&) {
|
|
|
|
return Error::success();
|
|
|
|
}
|
|
|
|
|
|
|
|
static Error deserialize(QueueChannel&, RPCBar&) {
|
|
|
|
return Error::success();
|
|
|
|
}
|
2016-12-21 01:59:33 +01:00
|
|
|
};
|
|
|
|
|
2016-12-21 03:08:23 +01:00
|
|
|
} // end namespace rpc
|
|
|
|
} // end namespace orc
|
|
|
|
} // end namespace llvm
|
|
|
|
|
2017-01-08 02:13:47 +01:00
|
|
|
namespace DummyRPCAPI {
|
2016-11-11 22:42:09 +01:00
|
|
|
|
|
|
|
class VoidBool : public Function<VoidBool, void(bool)> {
|
|
|
|
public:
|
|
|
|
static const char* getName() { return "VoidBool"; }
|
|
|
|
};
|
|
|
|
|
|
|
|
class IntInt : public Function<IntInt, int32_t(int32_t)> {
|
|
|
|
public:
|
|
|
|
static const char* getName() { return "IntInt"; }
|
|
|
|
};
|
|
|
|
|
2017-02-24 21:56:43 +01:00
|
|
|
class VoidString : public Function<VoidString, void(std::string)> {
|
|
|
|
public:
|
|
|
|
static const char* getName() { return "VoidString"; }
|
|
|
|
};
|
|
|
|
|
2016-11-11 22:42:09 +01:00
|
|
|
class AllTheTypes
|
|
|
|
: public Function<AllTheTypes,
|
|
|
|
void(int8_t, uint8_t, int16_t, uint16_t, int32_t,
|
|
|
|
uint32_t, int64_t, uint64_t, bool, std::string,
|
|
|
|
std::vector<int>)> {
|
|
|
|
public:
|
|
|
|
static const char* getName() { return "AllTheTypes"; }
|
2016-04-19 06:43:09 +02:00
|
|
|
};
|
2016-12-21 01:59:33 +01:00
|
|
|
|
|
|
|
class CustomType : public Function<CustomType, RPCFoo(RPCFoo)> {
|
|
|
|
public:
|
|
|
|
static const char* getName() { return "CustomType"; }
|
|
|
|
};
|
|
|
|
|
2017-04-13 05:51:35 +02:00
|
|
|
class ErrorFunc : public Function<ErrorFunc, Error()> {
|
|
|
|
public:
|
|
|
|
static const char* getName() { return "ErrorFunc"; }
|
|
|
|
};
|
|
|
|
|
|
|
|
class ExpectedFunc : public Function<ExpectedFunc, Expected<uint32_t>()> {
|
|
|
|
public:
|
|
|
|
static const char* getName() { return "ExpectedFunc"; }
|
|
|
|
};
|
|
|
|
|
2017-01-08 19:36:49 +01:00
|
|
|
}
|
2016-04-19 06:43:09 +02:00
|
|
|
|
2017-01-07 23:48:12 +01:00
|
|
|
class DummyRPCEndpoint : public SingleThreadedRPCEndpoint<QueueChannel> {
|
2016-11-11 22:42:09 +01:00
|
|
|
public:
|
2017-04-06 03:49:21 +02:00
|
|
|
DummyRPCEndpoint(QueueChannel &C)
|
|
|
|
: SingleThreadedRPCEndpoint(C, true) {}
|
[Orc] Add support for remote JITing to the ORC API.
This patch adds utilities to ORC for managing a remote JIT target. It consists
of:
1. A very primitive RPC system for making calls over a byte-stream. See
RPCChannel.h, RPCUtils.h.
2. An RPC API defined in the above system for managing memory, looking up
symbols, creating stubs, etc. on a remote target. See OrcRemoteTargetRPCAPI.h.
3. An interface for creating high-level JIT components (memory managers,
callback managers, stub managers, etc.) that operate over the RPC API. See
OrcRemoteTargetClient.h.
4. A helper class for building servers that can handle the RPC calls. See
OrcRemoteTargetServer.h.
The system is designed to work neatly with the existing ORC components and
functionality. In particular, the ORC callback API (and consequently the
CompileOnDemandLayer) is supported, enabling lazy compilation of remote code.
Assuming this doesn't trigger any builder failures, a follow-up patch will be
committed which tests these utilities by using them to replace LLI's existing
remote-JITing demo code.
llvm-svn: 257305
2016-01-11 02:40:11 +01:00
|
|
|
};
|
|
|
|
|
2016-11-11 22:42:09 +01:00
|
|
|
|
2017-02-08 01:13:51 +01:00
|
|
|
void freeVoidBool(bool B) {
|
2017-02-14 00:42:27 +01:00
|
|
|
}
|
2017-02-08 01:13:51 +01:00
|
|
|
|
|
|
|
TEST(DummyRPC, TestFreeFunctionHandler) {
|
2017-04-06 03:49:21 +02:00
|
|
|
auto Channels = createPairedQueueChannels();
|
|
|
|
DummyRPCEndpoint Server(*Channels.first);
|
2017-02-08 01:13:51 +01:00
|
|
|
Server.addHandler<DummyRPCAPI::VoidBool>(freeVoidBool);
|
2017-02-14 00:42:27 +01:00
|
|
|
}
|
2017-02-08 01:13:51 +01:00
|
|
|
|
2017-02-14 06:40:01 +01:00
|
|
|
TEST(DummyRPC, TestCallAsyncVoidBool) {
|
2017-04-06 03:49:21 +02:00
|
|
|
auto Channels = createPairedQueueChannels();
|
|
|
|
DummyRPCEndpoint Client(*Channels.first);
|
|
|
|
DummyRPCEndpoint Server(*Channels.second);
|
2016-04-18 21:55:43 +02:00
|
|
|
|
2016-11-16 18:31:09 +01:00
|
|
|
std::thread ServerThread([&]() {
|
|
|
|
Server.addHandler<DummyRPCAPI::VoidBool>(
|
|
|
|
[](bool B) {
|
|
|
|
EXPECT_EQ(B, true)
|
|
|
|
<< "Server void(bool) received unexpected result";
|
|
|
|
});
|
2016-04-18 21:55:43 +02:00
|
|
|
|
2016-11-16 18:31:09 +01:00
|
|
|
{
|
|
|
|
// Poke the server to handle the negotiate call.
|
|
|
|
auto Err = Server.handleOne();
|
|
|
|
EXPECT_FALSE(!!Err) << "Server failed to handle call to negotiate";
|
|
|
|
}
|
|
|
|
|
|
|
|
{
|
|
|
|
// Poke the server to handle the VoidBool call.
|
|
|
|
auto Err = Server.handleOne();
|
|
|
|
EXPECT_FALSE(!!Err) << "Server failed to handle call to void(bool)";
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
{
|
|
|
|
// Make an async call.
|
|
|
|
auto Err = Client.callAsync<DummyRPCAPI::VoidBool>(
|
|
|
|
[](Error Err) {
|
|
|
|
EXPECT_FALSE(!!Err) << "Async void(bool) response handler failed";
|
|
|
|
return Error::success();
|
|
|
|
}, true);
|
|
|
|
EXPECT_FALSE(!!Err) << "Client.callAsync failed for void(bool)";
|
|
|
|
}
|
|
|
|
|
|
|
|
{
|
|
|
|
// Poke the client to process the result of the void(bool) call.
|
|
|
|
auto Err = Client.handleOne();
|
|
|
|
EXPECT_FALSE(!!Err) << "Client failed to handle response from void(bool)";
|
|
|
|
}
|
|
|
|
|
|
|
|
ServerThread.join();
|
|
|
|
}
|
|
|
|
|
2017-02-14 06:40:01 +01:00
|
|
|
TEST(DummyRPC, TestCallAsyncIntInt) {
|
2017-04-06 03:49:21 +02:00
|
|
|
auto Channels = createPairedQueueChannels();
|
|
|
|
DummyRPCEndpoint Client(*Channels.first);
|
|
|
|
DummyRPCEndpoint Server(*Channels.second);
|
2016-11-16 18:31:09 +01:00
|
|
|
|
|
|
|
std::thread ServerThread([&]() {
|
|
|
|
Server.addHandler<DummyRPCAPI::IntInt>(
|
|
|
|
[](int X) -> int {
|
|
|
|
EXPECT_EQ(X, 21) << "Server int(int) receieved unexpected result";
|
|
|
|
return 2 * X;
|
|
|
|
});
|
|
|
|
|
|
|
|
{
|
|
|
|
// Poke the server to handle the negotiate call.
|
|
|
|
auto Err = Server.handleOne();
|
|
|
|
EXPECT_FALSE(!!Err) << "Server failed to handle call to negotiate";
|
|
|
|
}
|
|
|
|
|
|
|
|
{
|
|
|
|
// Poke the server to handle the int(int) call.
|
|
|
|
auto Err = Server.handleOne();
|
|
|
|
EXPECT_FALSE(!!Err) << "Server failed to handle call to int(int)";
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
{
|
|
|
|
auto Err = Client.callAsync<DummyRPCAPI::IntInt>(
|
|
|
|
[](Expected<int> Result) {
|
|
|
|
EXPECT_TRUE(!!Result) << "Async int(int) response handler failed";
|
|
|
|
EXPECT_EQ(*Result, 42)
|
|
|
|
<< "Async int(int) response handler received incorrect result";
|
|
|
|
return Error::success();
|
|
|
|
}, 21);
|
|
|
|
EXPECT_FALSE(!!Err) << "Client.callAsync failed for int(int)";
|
|
|
|
}
|
|
|
|
|
|
|
|
{
|
|
|
|
// Poke the client to process the result.
|
|
|
|
auto Err = Client.handleOne();
|
|
|
|
EXPECT_FALSE(!!Err) << "Client failed to handle response from void(bool)";
|
|
|
|
}
|
|
|
|
|
2017-02-14 06:40:01 +01:00
|
|
|
ServerThread.join();
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST(DummyRPC, TestAsyncIntIntHandler) {
|
2017-04-06 03:49:21 +02:00
|
|
|
auto Channels = createPairedQueueChannels();
|
|
|
|
DummyRPCEndpoint Client(*Channels.first);
|
|
|
|
DummyRPCEndpoint Server(*Channels.second);
|
2017-02-14 06:40:01 +01:00
|
|
|
|
|
|
|
std::thread ServerThread([&]() {
|
|
|
|
Server.addAsyncHandler<DummyRPCAPI::IntInt>(
|
|
|
|
[](std::function<Error(Expected<int32_t>)> SendResult,
|
|
|
|
int32_t X) {
|
|
|
|
EXPECT_EQ(X, 21) << "Server int(int) receieved unexpected result";
|
|
|
|
return SendResult(2 * X);
|
|
|
|
});
|
|
|
|
|
|
|
|
{
|
|
|
|
// Poke the server to handle the negotiate call.
|
|
|
|
auto Err = Server.handleOne();
|
|
|
|
EXPECT_FALSE(!!Err) << "Server failed to handle call to negotiate";
|
|
|
|
}
|
|
|
|
|
|
|
|
{
|
|
|
|
// Poke the server to handle the VoidBool call.
|
|
|
|
auto Err = Server.handleOne();
|
|
|
|
EXPECT_FALSE(!!Err) << "Server failed to handle call to void(bool)";
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
{
|
|
|
|
auto Err = Client.callAsync<DummyRPCAPI::IntInt>(
|
|
|
|
[](Expected<int> Result) {
|
|
|
|
EXPECT_TRUE(!!Result) << "Async int(int) response handler failed";
|
|
|
|
EXPECT_EQ(*Result, 42)
|
|
|
|
<< "Async int(int) response handler received incorrect result";
|
|
|
|
return Error::success();
|
|
|
|
}, 21);
|
|
|
|
EXPECT_FALSE(!!Err) << "Client.callAsync failed for int(int)";
|
|
|
|
}
|
|
|
|
|
|
|
|
{
|
|
|
|
// Poke the client to process the result.
|
|
|
|
auto Err = Client.handleOne();
|
|
|
|
EXPECT_FALSE(!!Err) << "Client failed to handle response from void(bool)";
|
|
|
|
}
|
|
|
|
|
2017-02-15 06:39:35 +01:00
|
|
|
ServerThread.join();
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST(DummyRPC, TestAsyncIntIntHandlerMethod) {
|
2017-04-06 03:49:21 +02:00
|
|
|
auto Channels = createPairedQueueChannels();
|
|
|
|
DummyRPCEndpoint Client(*Channels.first);
|
|
|
|
DummyRPCEndpoint Server(*Channels.second);
|
2017-02-15 06:39:35 +01:00
|
|
|
|
|
|
|
class Dummy {
|
|
|
|
public:
|
|
|
|
Error handler(std::function<Error(Expected<int32_t>)> SendResult,
|
|
|
|
int32_t X) {
|
|
|
|
EXPECT_EQ(X, 21) << "Server int(int) receieved unexpected result";
|
|
|
|
return SendResult(2 * X);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
std::thread ServerThread([&]() {
|
|
|
|
Dummy D;
|
|
|
|
Server.addAsyncHandler<DummyRPCAPI::IntInt>(D, &Dummy::handler);
|
|
|
|
|
|
|
|
{
|
|
|
|
// Poke the server to handle the negotiate call.
|
|
|
|
auto Err = Server.handleOne();
|
|
|
|
EXPECT_FALSE(!!Err) << "Server failed to handle call to negotiate";
|
|
|
|
}
|
|
|
|
|
|
|
|
{
|
|
|
|
// Poke the server to handle the VoidBool call.
|
|
|
|
auto Err = Server.handleOne();
|
|
|
|
EXPECT_FALSE(!!Err) << "Server failed to handle call to void(bool)";
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
{
|
|
|
|
auto Err = Client.callAsync<DummyRPCAPI::IntInt>(
|
|
|
|
[](Expected<int> Result) {
|
|
|
|
EXPECT_TRUE(!!Result) << "Async int(int) response handler failed";
|
|
|
|
EXPECT_EQ(*Result, 42)
|
|
|
|
<< "Async int(int) response handler received incorrect result";
|
|
|
|
return Error::success();
|
|
|
|
}, 21);
|
|
|
|
EXPECT_FALSE(!!Err) << "Client.callAsync failed for int(int)";
|
|
|
|
}
|
|
|
|
|
|
|
|
{
|
|
|
|
// Poke the client to process the result.
|
|
|
|
auto Err = Client.handleOne();
|
|
|
|
EXPECT_FALSE(!!Err) << "Client failed to handle response from void(bool)";
|
|
|
|
}
|
|
|
|
|
2017-02-24 21:56:43 +01:00
|
|
|
ServerThread.join();
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST(DummyRPC, TestCallAsyncVoidString) {
|
2017-04-06 03:49:21 +02:00
|
|
|
auto Channels = createPairedQueueChannels();
|
|
|
|
DummyRPCEndpoint Client(*Channels.first);
|
|
|
|
DummyRPCEndpoint Server(*Channels.second);
|
2017-02-24 21:56:43 +01:00
|
|
|
|
|
|
|
std::thread ServerThread([&]() {
|
|
|
|
Server.addHandler<DummyRPCAPI::VoidString>(
|
|
|
|
[](const std::string &S) {
|
|
|
|
EXPECT_EQ(S, "hello")
|
|
|
|
<< "Server void(std::string) received unexpected result";
|
|
|
|
});
|
|
|
|
|
|
|
|
// Poke the server to handle the negotiate call.
|
|
|
|
for (int I = 0; I < 4; ++I) {
|
|
|
|
auto Err = Server.handleOne();
|
|
|
|
EXPECT_FALSE(!!Err) << "Server failed to handle call";
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
{
|
|
|
|
// Make an call using a std::string.
|
|
|
|
auto Err = Client.callB<DummyRPCAPI::VoidString>(std::string("hello"));
|
|
|
|
EXPECT_FALSE(!!Err) << "Client.callAsync failed for void(std::string)";
|
|
|
|
}
|
|
|
|
|
|
|
|
{
|
|
|
|
// Make an call using a std::string.
|
|
|
|
auto Err = Client.callB<DummyRPCAPI::VoidString>(StringRef("hello"));
|
|
|
|
EXPECT_FALSE(!!Err) << "Client.callAsync failed for void(std::string)";
|
|
|
|
}
|
|
|
|
|
|
|
|
{
|
|
|
|
// Make an call using a std::string.
|
|
|
|
auto Err = Client.callB<DummyRPCAPI::VoidString>("hello");
|
|
|
|
EXPECT_FALSE(!!Err) << "Client.callAsync failed for void(string)";
|
|
|
|
}
|
|
|
|
|
2016-11-16 18:31:09 +01:00
|
|
|
ServerThread.join();
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST(DummyRPC, TestSerialization) {
|
2017-04-06 03:49:21 +02:00
|
|
|
auto Channels = createPairedQueueChannels();
|
|
|
|
DummyRPCEndpoint Client(*Channels.first);
|
|
|
|
DummyRPCEndpoint Server(*Channels.second);
|
2016-11-16 18:31:09 +01:00
|
|
|
|
|
|
|
std::thread ServerThread([&]() {
|
|
|
|
Server.addHandler<DummyRPCAPI::AllTheTypes>(
|
|
|
|
[&](int8_t S8, uint8_t U8, int16_t S16, uint16_t U16,
|
|
|
|
int32_t S32, uint32_t U32, int64_t S64, uint64_t U64,
|
|
|
|
bool B, std::string S, std::vector<int> V) {
|
|
|
|
|
|
|
|
EXPECT_EQ(S8, -101) << "int8_t serialization broken";
|
|
|
|
EXPECT_EQ(U8, 250) << "uint8_t serialization broken";
|
|
|
|
EXPECT_EQ(S16, -10000) << "int16_t serialization broken";
|
|
|
|
EXPECT_EQ(U16, 10000) << "uint16_t serialization broken";
|
|
|
|
EXPECT_EQ(S32, -1000000000) << "int32_t serialization broken";
|
|
|
|
EXPECT_EQ(U32, 1000000000ULL) << "uint32_t serialization broken";
|
|
|
|
EXPECT_EQ(S64, -10000000000) << "int64_t serialization broken";
|
|
|
|
EXPECT_EQ(U64, 10000000000ULL) << "uint64_t serialization broken";
|
|
|
|
EXPECT_EQ(B, true) << "bool serialization broken";
|
|
|
|
EXPECT_EQ(S, "foo") << "std::string serialization broken";
|
|
|
|
EXPECT_EQ(V, std::vector<int>({42, 7}))
|
|
|
|
<< "std::vector serialization broken";
|
|
|
|
return Error::success();
|
|
|
|
});
|
|
|
|
|
|
|
|
{
|
|
|
|
// Poke the server to handle the negotiate call.
|
|
|
|
auto Err = Server.handleOne();
|
|
|
|
EXPECT_FALSE(!!Err) << "Server failed to handle call to negotiate";
|
|
|
|
}
|
|
|
|
|
|
|
|
{
|
|
|
|
// Poke the server to handle the AllTheTypes call.
|
|
|
|
auto Err = Server.handleOne();
|
|
|
|
EXPECT_FALSE(!!Err) << "Server failed to handle call to void(bool)";
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
{
|
|
|
|
// Make an async call.
|
|
|
|
std::vector<int> v({42, 7});
|
|
|
|
auto Err = Client.callAsync<DummyRPCAPI::AllTheTypes>(
|
|
|
|
[](Error Err) {
|
|
|
|
EXPECT_FALSE(!!Err) << "Async AllTheTypes response handler failed";
|
|
|
|
return Error::success();
|
|
|
|
},
|
|
|
|
static_cast<int8_t>(-101), static_cast<uint8_t>(250),
|
|
|
|
static_cast<int16_t>(-10000), static_cast<uint16_t>(10000),
|
|
|
|
static_cast<int32_t>(-1000000000), static_cast<uint32_t>(1000000000),
|
|
|
|
static_cast<int64_t>(-10000000000), static_cast<uint64_t>(10000000000),
|
|
|
|
true, std::string("foo"), v);
|
|
|
|
EXPECT_FALSE(!!Err) << "Client.callAsync failed for AllTheTypes";
|
|
|
|
}
|
|
|
|
|
|
|
|
{
|
|
|
|
// Poke the client to process the result of the AllTheTypes call.
|
|
|
|
auto Err = Client.handleOne();
|
|
|
|
EXPECT_FALSE(!!Err) << "Client failed to handle response from AllTheTypes";
|
|
|
|
}
|
|
|
|
|
|
|
|
ServerThread.join();
|
|
|
|
}
|
2016-12-21 01:59:33 +01:00
|
|
|
|
|
|
|
TEST(DummyRPC, TestCustomType) {
|
2017-04-06 03:49:21 +02:00
|
|
|
auto Channels = createPairedQueueChannels();
|
|
|
|
DummyRPCEndpoint Client(*Channels.first);
|
|
|
|
DummyRPCEndpoint Server(*Channels.second);
|
2016-12-21 01:59:33 +01:00
|
|
|
|
|
|
|
std::thread ServerThread([&]() {
|
|
|
|
Server.addHandler<DummyRPCAPI::CustomType>(
|
|
|
|
[](RPCFoo F) {});
|
|
|
|
|
|
|
|
{
|
|
|
|
// Poke the server to handle the negotiate call.
|
|
|
|
auto Err = Server.handleOne();
|
|
|
|
EXPECT_FALSE(!!Err) << "Server failed to handle call to negotiate";
|
|
|
|
}
|
|
|
|
|
|
|
|
{
|
|
|
|
// Poke the server to handle the CustomType call.
|
|
|
|
auto Err = Server.handleOne();
|
|
|
|
EXPECT_FALSE(!!Err) << "Server failed to handle call to RPCFoo(RPCFoo)";
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
{
|
|
|
|
// Make an async call.
|
|
|
|
auto Err = Client.callAsync<DummyRPCAPI::CustomType>(
|
|
|
|
[](Expected<RPCFoo> FOrErr) {
|
|
|
|
EXPECT_TRUE(!!FOrErr)
|
|
|
|
<< "Async RPCFoo(RPCFoo) response handler failed";
|
|
|
|
return Error::success();
|
|
|
|
}, RPCFoo());
|
|
|
|
EXPECT_FALSE(!!Err) << "Client.callAsync failed for RPCFoo(RPCFoo)";
|
|
|
|
}
|
|
|
|
|
|
|
|
{
|
|
|
|
// Poke the client to process the result of the RPCFoo() call.
|
|
|
|
auto Err = Client.handleOne();
|
|
|
|
EXPECT_FALSE(!!Err)
|
|
|
|
<< "Client failed to handle response from RPCFoo(RPCFoo)";
|
|
|
|
}
|
|
|
|
|
|
|
|
ServerThread.join();
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST(DummyRPC, TestWithAltCustomType) {
|
2017-04-06 03:49:21 +02:00
|
|
|
auto Channels = createPairedQueueChannels();
|
|
|
|
DummyRPCEndpoint Client(*Channels.first);
|
|
|
|
DummyRPCEndpoint Server(*Channels.second);
|
2016-12-21 01:59:33 +01:00
|
|
|
|
|
|
|
std::thread ServerThread([&]() {
|
|
|
|
Server.addHandler<DummyRPCAPI::CustomType>(
|
|
|
|
[](RPCBar F) {});
|
|
|
|
|
|
|
|
{
|
|
|
|
// Poke the server to handle the negotiate call.
|
|
|
|
auto Err = Server.handleOne();
|
|
|
|
EXPECT_FALSE(!!Err) << "Server failed to handle call to negotiate";
|
|
|
|
}
|
|
|
|
|
|
|
|
{
|
|
|
|
// Poke the server to handle the CustomType call.
|
|
|
|
auto Err = Server.handleOne();
|
|
|
|
EXPECT_FALSE(!!Err) << "Server failed to handle call to RPCFoo(RPCFoo)";
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
{
|
|
|
|
// Make an async call.
|
|
|
|
auto Err = Client.callAsync<DummyRPCAPI::CustomType>(
|
|
|
|
[](Expected<RPCBar> FOrErr) {
|
|
|
|
EXPECT_TRUE(!!FOrErr)
|
|
|
|
<< "Async RPCFoo(RPCFoo) response handler failed";
|
|
|
|
return Error::success();
|
|
|
|
}, RPCBar());
|
|
|
|
EXPECT_FALSE(!!Err) << "Client.callAsync failed for RPCFoo(RPCFoo)";
|
|
|
|
}
|
|
|
|
|
|
|
|
{
|
|
|
|
// Poke the client to process the result of the RPCFoo() call.
|
|
|
|
auto Err = Client.handleOne();
|
|
|
|
EXPECT_FALSE(!!Err)
|
|
|
|
<< "Client failed to handle response from RPCFoo(RPCFoo)";
|
|
|
|
}
|
|
|
|
|
|
|
|
ServerThread.join();
|
|
|
|
}
|
2016-12-25 22:55:05 +01:00
|
|
|
|
2017-04-13 08:20:45 +02:00
|
|
|
// FIXME: Temporarily disabled to investigate bot failure.
|
|
|
|
|
|
|
|
// TEST(DummyRPC, ReturnErrorSuccess) {
|
|
|
|
// registerDummyErrorSerialization<QueueChannel>();
|
|
|
|
|
|
|
|
// auto Channels = createPairedQueueChannels();
|
|
|
|
// DummyRPCEndpoint Client(*Channels.first);
|
|
|
|
// DummyRPCEndpoint Server(*Channels.second);
|
|
|
|
|
|
|
|
// std::thread ServerThread([&]() {
|
|
|
|
// Server.addHandler<DummyRPCAPI::ErrorFunc>(
|
|
|
|
// []() {
|
|
|
|
// return Error::success();
|
|
|
|
// });
|
|
|
|
|
|
|
|
// // Handle the negotiate plus one call.
|
|
|
|
// for (unsigned I = 0; I != 2; ++I)
|
|
|
|
// cantFail(Server.handleOne());
|
|
|
|
// });
|
|
|
|
|
|
|
|
// cantFail(Client.callAsync<DummyRPCAPI::ErrorFunc>(
|
|
|
|
// [&](Error Err) {
|
|
|
|
// EXPECT_FALSE(!!Err) << "Expected success value";
|
|
|
|
// return Error::success();
|
|
|
|
// }));
|
|
|
|
|
|
|
|
// cantFail(Client.handleOne());
|
|
|
|
|
|
|
|
// ServerThread.join();
|
|
|
|
// }
|
|
|
|
|
|
|
|
// TEST(DummyRPC, ReturnErrorFailure) {
|
|
|
|
// registerDummyErrorSerialization<QueueChannel>();
|
|
|
|
|
|
|
|
// auto Channels = createPairedQueueChannels();
|
|
|
|
// DummyRPCEndpoint Client(*Channels.first);
|
|
|
|
// DummyRPCEndpoint Server(*Channels.second);
|
|
|
|
|
|
|
|
// std::thread ServerThread([&]() {
|
|
|
|
// Server.addHandler<DummyRPCAPI::ErrorFunc>(
|
|
|
|
// []() {
|
|
|
|
// return make_error<DummyError>(42);
|
|
|
|
// });
|
|
|
|
|
|
|
|
// // Handle the negotiate plus one call.
|
|
|
|
// for (unsigned I = 0; I != 2; ++I)
|
|
|
|
// cantFail(Server.handleOne());
|
|
|
|
// });
|
|
|
|
|
|
|
|
// cantFail(Client.callAsync<DummyRPCAPI::ErrorFunc>(
|
|
|
|
// [&](Error Err) {
|
|
|
|
// EXPECT_TRUE(Err.isA<DummyError>())
|
|
|
|
// << "Incorrect error type";
|
|
|
|
// return handleErrors(
|
|
|
|
// std::move(Err),
|
|
|
|
// [](const DummyError &DE) {
|
|
|
|
// EXPECT_EQ(DE.getValue(), 42ULL)
|
|
|
|
// << "Incorrect DummyError serialization";
|
|
|
|
// });
|
|
|
|
// }));
|
|
|
|
|
|
|
|
// cantFail(Client.handleOne());
|
|
|
|
|
|
|
|
// ServerThread.join();
|
|
|
|
// }
|
|
|
|
|
|
|
|
// TEST(DummyRPC, ReturnExpectedSuccess) {
|
|
|
|
// registerDummyErrorSerialization<QueueChannel>();
|
|
|
|
|
|
|
|
// auto Channels = createPairedQueueChannels();
|
|
|
|
// DummyRPCEndpoint Client(*Channels.first);
|
|
|
|
// DummyRPCEndpoint Server(*Channels.second);
|
|
|
|
|
|
|
|
// std::thread ServerThread([&]() {
|
|
|
|
// Server.addHandler<DummyRPCAPI::ExpectedFunc>(
|
|
|
|
// []() -> uint32_t {
|
|
|
|
// return 42;
|
|
|
|
// });
|
|
|
|
|
|
|
|
// // Handle the negotiate plus one call.
|
|
|
|
// for (unsigned I = 0; I != 2; ++I)
|
|
|
|
// cantFail(Server.handleOne());
|
|
|
|
// });
|
|
|
|
|
|
|
|
// cantFail(Client.callAsync<DummyRPCAPI::ExpectedFunc>(
|
|
|
|
// [&](Expected<uint32_t> ValOrErr) {
|
|
|
|
// EXPECT_TRUE(!!ValOrErr)
|
|
|
|
// << "Expected success value";
|
|
|
|
// EXPECT_EQ(*ValOrErr, 42ULL)
|
|
|
|
// << "Incorrect Expected<uint32_t> deserialization";
|
|
|
|
// return Error::success();
|
|
|
|
// }));
|
|
|
|
|
|
|
|
// cantFail(Client.handleOne());
|
|
|
|
|
|
|
|
// ServerThread.join();
|
|
|
|
// }
|
|
|
|
|
|
|
|
// TEST(DummyRPC, ReturnExpectedFailure) {
|
|
|
|
// registerDummyErrorSerialization<QueueChannel>();
|
|
|
|
|
|
|
|
// auto Channels = createPairedQueueChannels();
|
|
|
|
// DummyRPCEndpoint Client(*Channels.first);
|
|
|
|
// DummyRPCEndpoint Server(*Channels.second);
|
|
|
|
|
|
|
|
// std::thread ServerThread([&]() {
|
|
|
|
// Server.addHandler<DummyRPCAPI::ExpectedFunc>(
|
|
|
|
// []() -> Expected<uint32_t> {
|
|
|
|
// return make_error<DummyError>(7);
|
|
|
|
// });
|
|
|
|
|
|
|
|
// // Handle the negotiate plus one call.
|
|
|
|
// for (unsigned I = 0; I != 2; ++I)
|
|
|
|
// cantFail(Server.handleOne());
|
|
|
|
// });
|
|
|
|
|
|
|
|
// cantFail(Client.callAsync<DummyRPCAPI::ExpectedFunc>(
|
|
|
|
// [&](Expected<uint32_t> ValOrErr) {
|
|
|
|
// EXPECT_FALSE(!!ValOrErr)
|
|
|
|
// << "Expected failure value";
|
|
|
|
// auto Err = ValOrErr.takeError();
|
|
|
|
// EXPECT_TRUE(Err.isA<DummyError>())
|
|
|
|
// << "Incorrect error type";
|
|
|
|
// return handleErrors(
|
|
|
|
// std::move(Err),
|
|
|
|
// [](const DummyError &DE) {
|
|
|
|
// EXPECT_EQ(DE.getValue(), 7ULL)
|
|
|
|
// << "Incorrect DummyError serialization";
|
|
|
|
// });
|
|
|
|
// }));
|
|
|
|
|
|
|
|
// cantFail(Client.handleOne());
|
|
|
|
|
|
|
|
// ServerThread.join();
|
|
|
|
// }
|
2017-04-13 05:51:35 +02:00
|
|
|
|
2016-12-25 22:55:05 +01:00
|
|
|
TEST(DummyRPC, TestParallelCallGroup) {
|
2017-04-06 03:49:21 +02:00
|
|
|
auto Channels = createPairedQueueChannels();
|
|
|
|
DummyRPCEndpoint Client(*Channels.first);
|
|
|
|
DummyRPCEndpoint Server(*Channels.second);
|
2016-12-25 22:55:05 +01:00
|
|
|
|
|
|
|
std::thread ServerThread([&]() {
|
|
|
|
Server.addHandler<DummyRPCAPI::IntInt>(
|
|
|
|
[](int X) -> int {
|
|
|
|
return 2 * X;
|
|
|
|
});
|
|
|
|
|
|
|
|
// Handle the negotiate, plus three calls.
|
|
|
|
for (unsigned I = 0; I != 4; ++I) {
|
|
|
|
auto Err = Server.handleOne();
|
|
|
|
EXPECT_FALSE(!!Err) << "Server failed to handle call to int(int)";
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
{
|
|
|
|
int A, B, C;
|
2017-01-24 07:13:47 +01:00
|
|
|
ParallelCallGroup PCG;
|
2016-12-25 22:55:05 +01:00
|
|
|
|
|
|
|
{
|
2017-01-24 07:13:47 +01:00
|
|
|
auto Err = PCG.call(
|
|
|
|
rpcAsyncDispatch<DummyRPCAPI::IntInt>(Client),
|
2016-12-25 22:55:05 +01:00
|
|
|
[&A](Expected<int> Result) {
|
|
|
|
EXPECT_TRUE(!!Result) << "Async int(int) response handler failed";
|
|
|
|
A = *Result;
|
|
|
|
return Error::success();
|
|
|
|
}, 1);
|
|
|
|
EXPECT_FALSE(!!Err) << "First parallel call failed for int(int)";
|
|
|
|
}
|
|
|
|
|
|
|
|
{
|
2017-01-24 07:13:47 +01:00
|
|
|
auto Err = PCG.call(
|
|
|
|
rpcAsyncDispatch<DummyRPCAPI::IntInt>(Client),
|
2016-12-25 22:55:05 +01:00
|
|
|
[&B](Expected<int> Result) {
|
|
|
|
EXPECT_TRUE(!!Result) << "Async int(int) response handler failed";
|
|
|
|
B = *Result;
|
|
|
|
return Error::success();
|
|
|
|
}, 2);
|
|
|
|
EXPECT_FALSE(!!Err) << "Second parallel call failed for int(int)";
|
|
|
|
}
|
|
|
|
|
|
|
|
{
|
2017-01-24 07:13:47 +01:00
|
|
|
auto Err = PCG.call(
|
|
|
|
rpcAsyncDispatch<DummyRPCAPI::IntInt>(Client),
|
2016-12-25 22:55:05 +01:00
|
|
|
[&C](Expected<int> Result) {
|
|
|
|
EXPECT_TRUE(!!Result) << "Async int(int) response handler failed";
|
|
|
|
C = *Result;
|
|
|
|
return Error::success();
|
|
|
|
}, 3);
|
|
|
|
EXPECT_FALSE(!!Err) << "Third parallel call failed for int(int)";
|
|
|
|
}
|
|
|
|
|
|
|
|
// Handle the three int(int) results.
|
|
|
|
for (unsigned I = 0; I != 3; ++I) {
|
|
|
|
auto Err = Client.handleOne();
|
|
|
|
EXPECT_FALSE(!!Err) << "Client failed to handle response from void(bool)";
|
|
|
|
}
|
|
|
|
|
2017-01-24 07:13:47 +01:00
|
|
|
PCG.wait();
|
2016-12-25 22:55:05 +01:00
|
|
|
|
|
|
|
EXPECT_EQ(A, 2) << "First parallel call returned bogus result";
|
|
|
|
EXPECT_EQ(B, 4) << "Second parallel call returned bogus result";
|
|
|
|
EXPECT_EQ(C, 6) << "Third parallel call returned bogus result";
|
|
|
|
}
|
|
|
|
|
|
|
|
ServerThread.join();
|
|
|
|
}
|
2017-01-08 02:13:47 +01:00
|
|
|
|
|
|
|
TEST(DummyRPC, TestAPICalls) {
|
|
|
|
|
|
|
|
using DummyCalls1 = APICalls<DummyRPCAPI::VoidBool, DummyRPCAPI::IntInt>;
|
|
|
|
using DummyCalls2 = APICalls<DummyRPCAPI::AllTheTypes>;
|
|
|
|
using DummyCalls3 = APICalls<DummyCalls1, DummyRPCAPI::CustomType>;
|
|
|
|
using DummyCallsAll = APICalls<DummyCalls1, DummyCalls2, DummyRPCAPI::CustomType>;
|
|
|
|
|
|
|
|
static_assert(DummyCalls1::Contains<DummyRPCAPI::VoidBool>::value,
|
|
|
|
"Contains<Func> template should return true here");
|
|
|
|
static_assert(!DummyCalls1::Contains<DummyRPCAPI::CustomType>::value,
|
|
|
|
"Contains<Func> template should return false here");
|
|
|
|
|
2017-04-06 03:49:21 +02:00
|
|
|
auto Channels = createPairedQueueChannels();
|
|
|
|
DummyRPCEndpoint Client(*Channels.first);
|
|
|
|
DummyRPCEndpoint Server(*Channels.second);
|
2017-01-08 02:13:47 +01:00
|
|
|
|
|
|
|
std::thread ServerThread(
|
|
|
|
[&]() {
|
|
|
|
Server.addHandler<DummyRPCAPI::VoidBool>([](bool b) { });
|
|
|
|
Server.addHandler<DummyRPCAPI::IntInt>([](int x) { return x; });
|
|
|
|
Server.addHandler<DummyRPCAPI::CustomType>([](RPCFoo F) {});
|
|
|
|
|
|
|
|
for (unsigned I = 0; I < 4; ++I) {
|
|
|
|
auto Err = Server.handleOne();
|
|
|
|
(void)!!Err;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
{
|
|
|
|
auto Err = DummyCalls1::negotiate(Client);
|
|
|
|
EXPECT_FALSE(!!Err) << "DummyCalls1::negotiate failed";
|
|
|
|
}
|
|
|
|
|
|
|
|
{
|
|
|
|
auto Err = DummyCalls3::negotiate(Client);
|
|
|
|
EXPECT_FALSE(!!Err) << "DummyCalls3::negotiate failed";
|
|
|
|
}
|
|
|
|
|
|
|
|
{
|
|
|
|
auto Err = DummyCallsAll::negotiate(Client);
|
2017-04-13 03:03:06 +02:00
|
|
|
EXPECT_TRUE(Err.isA<CouldNotNegotiate>())
|
|
|
|
<< "Expected CouldNotNegotiate error for attempted negotiate of "
|
2017-01-08 02:13:47 +01:00
|
|
|
"unsupported function";
|
2017-04-13 03:03:06 +02:00
|
|
|
consumeError(std::move(Err));
|
2017-01-08 02:13:47 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
ServerThread.join();
|
|
|
|
}
|
2017-01-21 08:46:03 +01:00
|
|
|
|
|
|
|
TEST(DummyRPC, TestRemoveHandler) {
|
2017-04-06 03:49:21 +02:00
|
|
|
auto Channels = createPairedQueueChannels();
|
|
|
|
DummyRPCEndpoint Server(*Channels.second);
|
2017-01-21 08:46:03 +01:00
|
|
|
|
|
|
|
Server.addHandler<DummyRPCAPI::VoidBool>(
|
|
|
|
[](bool B) {
|
|
|
|
EXPECT_EQ(B, true)
|
|
|
|
<< "Server void(bool) received unexpected result";
|
|
|
|
});
|
|
|
|
|
|
|
|
Server.removeHandler<DummyRPCAPI::VoidBool>();
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST(DummyRPC, TestClearHandlers) {
|
2017-04-06 03:49:21 +02:00
|
|
|
auto Channels = createPairedQueueChannels();
|
|
|
|
DummyRPCEndpoint Server(*Channels.second);
|
2017-01-21 08:46:03 +01:00
|
|
|
|
|
|
|
Server.addHandler<DummyRPCAPI::VoidBool>(
|
|
|
|
[](bool B) {
|
|
|
|
EXPECT_EQ(B, true)
|
|
|
|
<< "Server void(bool) received unexpected result";
|
|
|
|
});
|
|
|
|
|
|
|
|
Server.clearHandlers();
|
|
|
|
}
|