summaryrefslogtreecommitdiff
path: root/modules/remote/tests/remote_loopback.cpp
blob: eca107ffcbb57a9de290b9fd0965ad78ef23108a (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
#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">
>;

using GroupedSchemas = saw::tmpl_group<
	UInt64,
	String,
	Array<Int32>,
	Float64
>;
}

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(); ++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() == 4u, "Wrong received value.");
	}
}

SAW_TEST("Remote Loopback Grouped Data"){
	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_srv = rmt.template data_listen<sch::GroupedSchemas, 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<sch::GroupedSchemas, 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::UInt64> foo{421};
	id<sch::UInt64> 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, "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<sch::UInt64> alloc_id = [&](){
		auto eov = client.allocate<sch::UInt64>(data<sch::Void>{});
		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, "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().get() == 5u, "Wrong received value.");
	}

	data<sch::FixedArray<sch::UInt64,1>> arr_meta{{128u}};
	
	id<sch::Array<sch::Int32>> arr_alloc_id = [&](){
		auto eov = client.allocate<sch::Array<sch::Int32>>(arr_meta);
		SAW_EXPECT(eov.is_value(), "Failed send.");
		return eov.get_value();
	}();
	{
		auto eov = client.find(arr_alloc_id);
		SAW_EXPECT(eov.is_value(), "Failed find.");
		auto& f_val = eov.get_value();
		SAW_EXPECT(f_val, "Nullptr in find.");
		SAW_EXPECT(f_val->size() == arr_meta.at(0).get(), "Wrong initialized size.");
	}
}
}