-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathTauPiSerializationTest.cpp
More file actions
113 lines (95 loc) · 3.04 KB
/
TauPiSerializationTest.cpp
File metadata and controls
113 lines (95 loc) · 3.04 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
#include <gtest/gtest.h>
#include <chrono>
#include <memory>
#include <string>
#include "KAI/Core/BinaryStream.h"
#include "KAI/Core/BuiltinTypes/Array.h"
#include "KAI/Core/BuiltinTypes/String.h"
#include "KAI/Core/Object/Label.h"
#include "KAI/Core/Registry.h"
#include "KAI/Language/Tau/Generate/GenerateAgent.h"
#include "KAI/Language/Tau/Generate/GenerateProxy.h"
#include "KAI/Network/Address.h"
#include "KAI/Network/Agent.h"
#include "KAI/Network/Node.h"
#include "KAI/Network/ProxyBase.h"
using namespace kai;
using namespace kai::net;
namespace {
void RegisterMinimalTypes(Registry ®) {
reg.AddClass<int>();
String::Register(reg);
Array::Register(reg);
BinaryStream::Register(reg);
Label::Register(reg);
}
class Calculator {
public:
int Add(int a, int b) { return a + b; }
};
class ICalcAgent : public Agent<Calculator> {
public:
explicit ICalcAgent(Node &node)
: Agent<Calculator>(node, std::make_shared<Calculator>()) {
BindMethod("Add", &Calculator::Add);
}
};
class ICalcProxy : public ProxyBase {
public:
using ProxyBase::StreamType;
ICalcProxy(Node &node, NetHandle handle) : ProxyBase(node, handle) {}
Future<int> Add(int a, int b) { return Exec<int>("Add", a, b); }
};
} // namespace
TEST(TauPiSerializationTest, LocalNodeRoundTrip) {
const std::string tauScript = R"(
namespace Test {
interface ICalc {
Future<int> Add(int a, int b);
}
}
)";
Registry registry;
RegisterMinimalTypes(registry);
Node nodeA;
Node nodeB;
nodeA.SetRegistry(®istry);
nodeB.SetRegistry(®istry);
int port = 0;
for (int candidate = 20000; candidate < 20100; ++candidate) {
nodeA.Listen(IpAddress("127.0.0.1"), candidate);
if (nodeA.IsRunning()) {
port = candidate;
break;
}
}
if (port == 0) {
GTEST_SKIP() << "Failed to bind a local port for nodeA";
}
nodeB.Connect(IpAddress("127.0.0.1"), port);
nodeA.SetUpdatePump([&]() { nodeB.Update(); });
nodeB.SetUpdatePump([&]() { nodeA.Update(); });
const auto start = std::chrono::steady_clock::now();
while (nodeA.GetConnectionCount() == 0 ||
nodeB.GetConnectionCount() == 0) {
nodeA.Update();
nodeB.Update();
if (std::chrono::steady_clock::now() - start >
std::chrono::seconds(2)) {
FAIL() << "Timed out waiting for node connection";
}
}
std::string proxyOutput;
tau::Generate::GenerateProxy proxyGen(tauScript.c_str(), proxyOutput);
ASSERT_FALSE(proxyGen.Failed);
ASSERT_NE(proxyOutput.find("ICalcProxy"), std::string::npos);
std::string agentOutput;
tau::Generate::GenerateAgent agentGen(tauScript.c_str(), agentOutput);
ASSERT_FALSE(agentGen.Failed);
ASSERT_NE(agentOutput.find("ICalcAgent"), std::string::npos);
ICalcAgent agent(nodeA);
ICalcProxy proxy(nodeB, agent.Handle());
auto future = proxy.Add(2, 3);
int result = nodeB.WaitFor(future);
EXPECT_EQ(result, 5);
}