summaryrefslogtreecommitdiff
path: root/modules/remote/tests/remote_loopback.cpp
blob: 4460ad1747bd0afdad74d4b9a218857c1ef6244c (plain)
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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
#include <forstio/test/suite.hpp>

#include "../c++/remote_loopback.hpp"

namespace {
namespace sch {
using namespace saw::schema;

using TestInterface = Interface<
	Member<Function<UInt32, Int64>, "foo">
>;

}

SAW_TEST("Remote Loopback RPC"){
	using namespace saw;

	event_loop loop;
	wait_scope wait{loop};

	remote<rmt::Loopback> rmt;

	auto eov = rmt.parse_address(0u);
	SAW_EXPECT(eov.is_value(), "Didn't parse correctly");
	auto& addr = eov.get_value();

	auto eo_u32_srv = rmt.template data_listen<sch::UInt32, encode::Native>(*addr);
	SAW_EXPECT(eo_u32_srv.is_value(), std::string{"Couldn't listen: "} + std::string{eo_u32_srv.get_error().get_category()});
	auto& u32_srv = eo_u32_srv.get_value();
	
	auto eo_i64_srv = rmt.template data_listen<sch::Int64, encode::Native>(*addr);
	SAW_EXPECT(eo_i64_srv.is_value(), std::string{"Couldn't listen: "} + std::string{eo_i64_srv.get_error().get_category()});
	auto& i64_srv = eo_i64_srv.get_value();

	auto foo_func = [](data<sch::UInt32, encode::Native> input) -> error_or<data<sch::Int64, encode::Native>> {
		data<sch::Int64, encode::Native> rv;
		rv.set(input.get() * 2);
		return rv;
	};

	auto iface = interface_factory<sch::TestInterface, encode::Native>::create(std::move(foo_func));
	
	auto eo_rpc_srv = rmt.template rpc_listen<sch::TestInterface, encode::Native>(*addr, std::move(iface));
	SAW_EXPECT(eo_rpc_srv.is_value(), std::string{"Couldn't listen: "} + std::string{eo_rpc_srv.get_error().get_category()});
	auto& rpc_srv = eo_rpc_srv.get_value();

	id<sch::UInt32> id32{0u};
	id<sch::Int64> id64{0u};

	data<sch::UInt32, encode::Native> inp{21};

	{
		auto eo_send = u32_srv->send(inp, id32);
		SAW_EXPECT(eo_send.is_value(), "Failed send.");
	}
	{
		auto eo_called = rpc_srv->template call<"foo">(id32, id64);
		SAW_EXPECT(eo_called.is_value(), (std::string{"Failed call: "} + std::string{eo_called.get_error().get_category()} ));
	}
	{
		auto eo_recv = i64_srv->receive(id64);
		SAW_EXPECT(eo_recv.is_value(), "Failed receive.");
		auto& recv = eo_recv.get_value();
		SAW_EXPECT((recv.get() == 42), (std::string{"Wrong value received: "} + std::to_string(recv.get())));
	}
}

SAW_TEST("Remote Loopback RPC Runtime Dispatch"){
	using namespace saw;

	event_loop loop;
	wait_scope wait{loop};

	remote<rmt::Loopback> rmt;

	auto eov = rmt.parse_address(0u);
	SAW_EXPECT(eov.is_value(), "Didn't parse correctly");
	auto& addr = eov.get_value();

	auto eo_u32_srv = rmt.template data_listen<sch::UInt32, encode::Native>(*addr);
	SAW_EXPECT(eo_u32_srv.is_value(), std::string{"Couldn't listen: "} + std::string{eo_u32_srv.get_error().get_category()});
	auto& u32_srv = eo_u32_srv.get_value();

	auto eo_i64_srv = rmt.template data_listen<sch::Int64, encode::Native>(*addr);
	SAW_EXPECT(eo_i64_srv.is_value(), std::string{"Couldn't listen: "} + std::string{eo_i64_srv.get_error().get_category()});
	auto& i64_srv = eo_i64_srv.get_value();

	auto foo_func = [](data<sch::UInt32, encode::Native> input) -> error_or<data<sch::Int64, encode::Native>> {
		data<sch::Int64, encode::Native> rv;
		rv.set(input.get() * 2);
		return rv;
	};

	auto iface = interface_factory<sch::TestInterface, encode::Native>::create(std::move(foo_func));

	auto eo_rpc_srv = rmt.template rpc_listen<sch::TestInterface, encode::Native>(*addr, std::move(iface));
	SAW_EXPECT(eo_rpc_srv.is_value(), std::string{"Couldn't listen: "} + std::string{eo_rpc_srv.get_error().get_category()});
	auto& rpc_srv = eo_rpc_srv.get_value();

	id<sch::UInt32> id32{0u};
	/**
	 * Deliberately not 0, since the server picks that one for the first
	 * response below.
	 */
	id<sch::Int64> id64{7u};

	data<sch::UInt32, encode::Native> inp{21};

	{
		auto eo_send = u32_srv->send(inp, id32);
		SAW_EXPECT(eo_send.is_value(), "Failed send.");
	}

	/**
	 * This is the path a backend takes which transports the call itself.
	 * Only the function id and the raw parameter id are known. The id the
	 * server picked for the response is meant to travel back in the reply.
	 */
	{
		auto eo_called = rpc_srv->call_by_id(rpc_function_id<"foo">::value, id32.get_value());
		SAW_EXPECT(eo_called.is_value(), (std::string{"Failed call: "} + std::string{eo_called.get_error().get_category()}));

		id<sch::Int64> resp_id{eo_called.get_value()};
		auto eo_recv = i64_srv->receive(resp_id);
		SAW_EXPECT(eo_recv.is_value(), "Failed receive.");
		auto& recv = eo_recv.get_value();
		SAW_EXPECT((recv.get() == 42), (std::string{"Wrong value received: "} + std::to_string(recv.get())));
	}
	/**
	 * Calling into a slot the caller picked beforehand.
	 */
	{
		auto eo_called = rpc_srv->call_by_id(rpc_function_id<"foo">::value, id32.get_value(), id64.get_value());
		SAW_EXPECT(eo_called.is_value(), (std::string{"Failed call: "} + std::string{eo_called.get_error().get_category()}));

		auto eo_recv = i64_srv->receive(id64);
		SAW_EXPECT(eo_recv.is_value(), "Failed receive.");
		auto& recv = eo_recv.get_value();
		SAW_EXPECT((recv.get() == 42), (std::string{"Wrong value received: "} + std::to_string(recv.get())));
	}
	{
		auto eo_called = rpc_srv->call_by_id(rpc_function_id<"foo">::value + 1u, id32.get_value());
		SAW_EXPECT(eo_called.is_error(), "An unknown function id should not be callable.");
	}
}

SAW_TEST("Remote Loopback RPC Client"){
	using namespace saw;

	event_loop loop;
	wait_scope wait{loop};

	remote<rmt::Loopback> rmt;

	auto eov = rmt.parse_address(0u);
	SAW_EXPECT(eov.is_value(), "Didn't parse correctly");
	auto& addr = eov.get_value();

	auto eo_u32_srv = rmt.template data_listen<sch::UInt32, encode::Native>(*addr);
	SAW_EXPECT(eo_u32_srv.is_value(), std::string{"Couldn't listen: "} + std::string{eo_u32_srv.get_error().get_category()});

	auto eo_i64_srv = rmt.template data_listen<sch::Int64, encode::Native>(*addr);
	SAW_EXPECT(eo_i64_srv.is_value(), std::string{"Couldn't listen: "} + std::string{eo_i64_srv.get_error().get_category()});

	auto foo_func = [](data<sch::UInt32, encode::Native> input) -> error_or<data<sch::Int64, encode::Native>> {
		data<sch::Int64, encode::Native> rv;
		rv.set(input.get() * 2);
		return rv;
	};

	auto iface = interface_factory<sch::TestInterface, encode::Native>::create(std::move(foo_func));

	auto eo_rpc_srv = rmt.template rpc_listen<sch::TestInterface, encode::Native>(*addr, std::move(iface));
	SAW_EXPECT(eo_rpc_srv.is_value(), std::string{"Couldn't listen: "} + std::string{eo_rpc_srv.get_error().get_category()});

	auto cvr_client = rmt.template rpc_connect<sch::TestInterface, encode::Native>(*addr);
	auto eo_client = cvr_client.take();
	SAW_EXPECT(eo_client.is_value(), "Couldn't connect.");
	auto& client = eo_client.get_value();

	data<sch::UInt32, encode::Native> inp{21};

	auto cvr_rmt_dat = client.template call<"foo">(std::move(inp));
	auto eo_rmt_dat = cvr_rmt_dat.take();
	SAW_EXPECT(eo_rmt_dat.is_value(), (std::string{"Failed call: "} + std::string{eo_rmt_dat.get_error().get_category()}));
	auto& rmt_dat = eo_rmt_dat.get_value();

	auto eo_resp = rmt_dat.wait(wait);
	SAW_EXPECT(eo_resp.is_value(), "Failed receive.");
	SAW_EXPECT((eo_resp.get_value().get() == 42), (std::string{"Wrong value received: "} + std::to_string(eo_resp.get_value().get())));
}

SAW_TEST("Remote Loopback Data"){
	using namespace saw;

	event_loop loop;
	wait_scope wait{loop};

	remote<rmt::Loopback> rmt;

	using Schema = sch::Array<sch::Float64>;

	auto eov = rmt.parse_address(0u);
	SAW_EXPECT(eov.is_value(), "Didn't parse correctly");
	auto& addr = eov.get_value();

	auto eo_srv = rmt.template data_listen<Schema, encode::Native>(*addr);
	SAW_EXPECT(eo_srv.is_value(), std::string{"Couldn't listen: "} + std::string{eo_srv.get_error().get_category()});
	auto& srv = eo_srv.get_value();

	auto cvr_client = rmt.template data_connect<Schema, encode::Native>(*addr);
	auto eo_client = cvr_client.take();
	SAW_EXPECT(eo_client.is_value(), "Couldn't connect.");
	auto& client = eo_client.get_value();

	data<Schema> foo{4};
	for(uint64_t i = 0; i < foo.size().get(); ++i){
		foo.at(i).set(i * 2.0);
	}
	id<Schema> sent_id = [&](){
		auto eov = client.send(foo);
		SAW_EXPECT(eov.is_value(), "Failed send.");
		return eov.get_value();
	}();

	{
		auto conv = client.receive(sent_id);
		auto eov = conv.take();

		SAW_EXPECT(eov.is_value(), "Failed receive.");
		// SAW_EXPECT(eov.get_value() == foo, "Wrong received value.");
	}
	{
		auto eov = client.find(sent_id);
		SAW_EXPECT(eov.is_value(), "Failed find.");
		auto& f_val = eov.get_value();
		// SAW_EXPECT(f_val.is_valid(), "Nullptr in find.");
		// SAW_EXPECT(*f_val == foo, "Wrong received value.");
	}
	{
		auto eov = client.erase(sent_id);
		SAW_EXPECT(eov.is_value(), "Failed erase.");
	}
	{
		auto conv = client.receive(sent_id);
		auto eov = conv.take();
		SAW_EXPECT(!eov.is_value(), "Failed receive. Value should already be erased.");
	}
	
	id<Schema> alloc_id = [&](){
		auto eov = client.allocate(data<sch::FixedArray<sch::UInt64,1>>{{4}});
		SAW_EXPECT(eov.is_value(), "Failed send.");
		return eov.get_value();
	}();
	{
		auto eov = client.find(alloc_id);
		SAW_EXPECT(eov.is_value(), "Failed find.");
		auto& f_val = eov.get_value();
		// SAW_EXPECT(f_val.is_valid(), "Nullptr in find.");
		// f_val->set(5u);
	}
	{
		auto conv = client.receive(alloc_id);
		auto eov = conv.take();
		SAW_EXPECT(eov.is_value(), "Failed receive.");
		SAW_EXPECT(eov.get_value().size().get() == 4u, "Wrong received value.");
	}
}

}