Debian Codegraph @

Nasal/test_emesary.nut

raw
1 #---------------------------------------------------------------------------
2 #
3 # Title : EMESARY tests
4 #
5 # File Type : Unit test
6 #
7 # Author : Richard Harrison ([email protected])
8 #
9 # Creation Date : 14 June 2020
10 #
11 # Copyright (C) 2020 Richard Harrison Released under GPL V2
12 #
13 #---------------------------------------------------------------------------*/
14
15 # fgcommand("nasal-test", props.Node.new({"path":"test_emesary.nut"}));
16
17
18 # note you can omit this if not needed
19 var setUp = func {
20 logprint(LOG_INFO, "Emesary tests begin");
21 };
22
23 # same, cab be ommitted
24 var tearDown = func {
25 logprint(LOG_INFO, "Emesary tests finished");
26 };
27
28
29 var test_emesary_transmit_receive = func {
30 logprint(LOG_INFO, "Emesary test transmit receive");
31
32 var baseRecipientCount = emesary.GlobalTransmitter.RecipientCount();
33 logprint(LOG_INFO, " (notice) already have ",baseRecipientCount);
34
35 var TestFailCount = 0;
36 var TestSuccessCount = 0;
37
38 var TestNotification =
39 {
40 new: func(_value)
41 {
42 var new_class = emesary.Notification.new("TestNotification", _value);
43 return new_class;
44 },
45 };
46 var TestNotProcessedNotification =
47 {
48 new: func(_value)
49 {
50 var new_class = emesary.Notification.new("TestNotProcessedNotification", _value);
51 return new_class;
52 },
53 };
54 var RadarReturnNotification =
55 {
56 new: func(_value, _x, _y, _z)
57 {
58 var new_class = emesary.Notification.new("RadarReturnNotification", _value);
59 new_class.x = _x;
60 new_class.y = _y;
61 new_class.z = _z;
62 return new_class;
63 },
64 };
65
66 var TestRecipient =
67 {
68 new: func(_ident)
69 {
70 var new_class = emesary.Recipient.new(_ident);
71 new_class.count = 0;
72 new_class.Receive = func(notification)
73 {
74 if (notification.NotificationType == "TestNotification")
75 {
76 me.count = me.count + 1;
77 return emesary.Transmitter.ReceiptStatus_OK;
78 }
79 return emesary.Transmitter.ReceiptStatus_NotProcessed;
80 };
81 return new_class;
82 },
83 };
84
85 var TestRadarRecipient =
86 {
87 new: func(_ident)
88 {
89 var new_class = emesary.Recipient.new(_ident);
90 new_class.Value = "";
91 new_class.Receive = func(notification)
92 {
93 if (notification.NotificationType == "RadarReturnNotification"){
94 new_class.ReturnValue = sprintf("%s:%s.%s.%s",notification.NotificationType, notification.x, notification.y, notification.z);
95 notification.ReturnValue = new_class;
96 return emesary.Transmitter.ReceiptStatus_OK;
97 }
98 return emesary.Transmitter.ReceiptStatus_NotProcessed;
99 };
100 return new_class;
101 },
102 };
103
104 var PerformTest = func(tid, expected_value, method)
105 {
106 var testResult = method();
107 if (testResult)
108 {
109 TestSuccessCount = TestSuccessCount + 1;
110 logprint(LOG_INFO, sprintf(" %s [Pass] :%s == %s",tid,expected_value, testResult));
111 }
112 else
113 {
114 TestFailCount = TestFailCount + 1;
115 logprint(LOG_ALERT, sprintf(" %s [Fail] : %s != %s",tid,expected_value, testResult));
116 }
117 unitTest.assert(expected_value, testResult, tid);
118
119 }
120 var tt = TestRecipient.new("tt recipient");
121 var tt1 = TestRecipient.new("tt1 recipient1");
122 var tt3 = TestRecipient.new("tt3 recipient3");
123 var tt_radar_recipient = TestRadarRecipient.new("tt_radar_recipient: Radar Test recipient2");
124
125 PerformTest("Create Notification", "TestNotification.Test notification",
126 func
127 {
128 var tn = TestNotification.new("Test notification");
129 return tn.NotificationType~"."~tn.Ident;
130 });
131
132 PerformTest("Register tt", 1 + baseRecipientCount,
133 func
134 {
135 emesary.GlobalTransmitter.Register(tt);
136 return emesary.GlobalTransmitter.RecipientCount();
137 });
138 PerformTest("Register tt1", 2 + baseRecipientCount,
139 func
140 {
141 emesary.GlobalTransmitter.Register(tt1);
142 return emesary.GlobalTransmitter.RecipientCount();
143 });
144 PerformTest("Register tt_radar_recipient", 3 + baseRecipientCount,
145 func
146 {
147 emesary.GlobalTransmitter.Register(tt_radar_recipient);
148 return emesary.GlobalTransmitter.RecipientCount();
149 });
150 PerformTest("Register tt3", 4 + baseRecipientCount,
151 func
152 {
153 emesary.GlobalTransmitter.Register(tt3);
154 return emesary.GlobalTransmitter.RecipientCount();
155 });
156
157 PerformTest("Notify", 1,
158 func
159 {
160 var rv = emesary.GlobalTransmitter.NotifyAll(TestNotification.new("Test notification"));
161 return !emesary.Transmitter.IsFailed(rv) and rv != emesary.Transmitter.ReceiptStatus_NotProcessed and tt.count == 1;
162 });
163
164 PerformTest("DeRegister tt1", 3 + baseRecipientCount,
165 func
166 {
167 emesary.GlobalTransmitter.DeRegister(tt1);
168 return emesary.GlobalTransmitter.RecipientCount();
169 });
170
171 # tt1_count = tt1.count;
172 PerformTest("NotifyAfterDeregister", tt1.count,
173 func
174 {
175 emesary.GlobalTransmitter.NotifyAll(TestNotification.new("Test notification"));
176 # return tt1.count == tt1_count;
177 return tt1.count;
178 });
179
180 tt.count = 0;
181
182 PerformTest("Recipient.Active", 1,
183 func
184 {
185 var rv = emesary.GlobalTransmitter.NotifyAll(TestNotification.new("Test notification"));
186 if (!emesary.Transmitter.IsFailed(rv) and rv != emesary.Transmitter.ReceiptStatus_NotProcessed)
187 return tt.count;
188 else
189 return -1000;
190 });
191
192
193 PerformTest("Test Not Processed Notification", emesary.Transmitter.ReceiptStatus_NotProcessed,
194 func
195 {
196 var rv = emesary.GlobalTransmitter.NotifyAll(TestNotProcessedNotification.new("Not Processed"));
197 return rv;
198 });
199
200 PerformTest("NotifyAll", "RadarReturnNotification.x0.y0.z0",
201 func
202 {
203 emesary.GlobalTransmitter.NotifyAll(RadarReturnNotification.new("Radar notification", "x0","y0","z0"));
204 return tt_radar_recipient.ReturnValue;
205 });
206
207 PerformTest("Deregister", 0 + baseRecipientCount,
208 func
209 {
210 emesary.GlobalTransmitter.DeRegister(tt);
211 emesary.GlobalTransmitter.DeRegister(tt3);
212 emesary.GlobalTransmitter.DeRegister(tt_radar_recipient);
213 return emesary.GlobalTransmitter.RecipientCount();
214 });
215
216
217 }
218 #---1
219
220
221 # all test macros take an option 'message' argument
222 test_transfer = func {
223 logprint(LOG_INFO, "Emesary: Test_transfers");
224 var decoded = 10;
225 var coded = "1234";
226 var init = 1;
227 var counter = 11;
228 var pos = 4;
229 while (init or (size(coded) == 4 and pos == 4 and decoded == counter-1 and find(emesary_mp_bridge.OutgoingMPBridge.MessageEndChar,coded) == -1 and find(emesary_mp_bridge.OutgoingMPBridge.SeperatorChar,coded) == -1 and counter != 65600)) {
230 coded = emesary.BinaryAsciiTransfer.encodeInt(counter,4);
231 if (init) {
232 init = 0;
233 }
234 decoded = emesary.BinaryAsciiTransfer.decodeInt(coded,4,0);
235 pos = decoded.pos;
236 decoded = decoded.value;
237 counter += 1;
238 }
239 unitTest.assert_equal(counter, 65600, sprintf("Decoded=%d Coded=%s Integer=%d Pos=%d Size=%d", decoded, coded, counter-1, pos, size(coded)));
240
241 for(i=-1;i<=1;i+=0.1){
242 var dv = emesary.TransferNorm.encode(i,2);
243 var v = emesary.TransferNorm.decode(dv, 2,0);
244 var delta = math.abs(i - v.value);
245 unitTest.assert(delta <= 0.01, sprintf("Norm: Fail: %f => %f : d=%f",i,v.value,delta));
246 }
247
248 for(i=-124;i<124;i+=1){
249 var dv = emesary.TransferByte.encode(i);
250 var v = emesary.TransferByte.decode(dv, 0);
251 unitTest.assert(i == v.value, sprintf("Byte: fail: %d => %d ",i,v.value));
252 }
253
254 var pos = 0;
255 var v = emesary.BinaryAsciiTransfer.encodeNumeric(123, 1, 1.0);
256 var dv=emesary.BinaryAsciiTransfer.decodeNumeric(v,1,1.0 ,pos);
257 unitTest.assert_equal(dv.value,123, "BinaryAsciiTransfer.encodeNumeric");
258
259 for(i=-123; i <= 123; i+=1){
260 var v = emesary.BinaryAsciiTransfer.encodeNumeric(i,1, 1.0);
261 var pos = 0;
262 var dv=emesary.BinaryAsciiTransfer.decodeNumeric(v,1,1.0 ,pos);
263 unitTest.assert_equal(dv.value, i, "BinaryAsciiTransfer.encodeNumeric(1)");
264 }
265
266 teststring = func(s){
267 dv = emesary.TransferString.encode(s);
268 nv = emesary.TransferString.decode(dv,0);
269 unitTest.assert_equal(s, nv.value, "emesary.TransferString");
270 }
271 teststring("");
272 teststring("@");
273 teststring("--");
274 teststring("qqqqqqqqqq-");
275 }
276
277
278 test_mp_bridge = func {
279 logprint(LOG_INFO,"Emesary MP bridge tests start");
280
281 var testMpPropertyPath = "/ai[0]/models[0]/multiplayer[11]";
282 var mp_listener = nil;
283 var testRoutedNotifications = [notifications.GeoEventNotification.new(nil)];
284 var testBridgedTransmitter = emesary.Transmitter.new("geoOutgoingBridge");
285 var testOutgoingBridge = emesary_mp_bridge.OutgoingMPBridge.new("F-14mp.geo",testRoutedNotifications, 18, "", testBridgedTransmitter);
286 testOutgoingBridge.MPStringMaxLen = 150;
287
288 var msgList = [];
289 var testRecipient = emesary.Recipient.new("Test");
290
291 valuesWithinRange = func(value1, value2, range){
292 return math.abs(value1 - value2) <= range;
293 }
294 testRecipient.FailCount = 0;
295 testRecipient.Receive = func(notification) {
296 if (notification.NotificationType == "GeoEventNotification") {
297 # print ("recv(0): type=",notification.NotificationType, " fromIncoming=",notification.FromIncomingBridge);
298 if (notification.Name=="DONE") {
299 printf("Emesary MP Bridge tests completed; %d failure",me.FailCount);
300
301 # now clean up;
302 emesary.GlobalTransmitter.DeRegister(testRecipient);
303
304 setprop("/ai/models/model-removed",testMpPropertyPath);
305
306 if (mp_listener != nil)
307 removelistener(mp_listener);
308
309 return emesary.Transmitter.ReceiptStatus_NotProcessed; # we're not processing it, just looking
310 }
311
312 foreach (var msg; msgList) {
313 if (msg.SecondaryKind == notification.SecondaryKind) {
314 #print("Check msg ",msg.Kind);
315 var failed = 0;
316
317 if (math.abs(msg.Position.lat() - notification.Position.lat()) > 0.00001 or
318 math.abs(msg.Position.lon() - notification.Position.lon()) > 0.00001 or
319 math.abs(msg.Position.alt() - notification.Position.alt()) > 1) {
320 failed = 1;
321 printf("Fail Position:(%d) %s != %s",msg.Kind, msg.Position, notification.Position);
322 }
323
324 if (msg.Name != notification.Name) {
325 failed = 1;
326 printf("Fail Name:(%d) %s != %s",msg.Kind, msg.Name, notification.Name);
327 }
328
329 if (msg.Kind != notification.Kind) {
330 failed = 1;
331 printf("Fail Kind:(%d) %s != %s",msg.Kind, msg.Kind, notification.Kind);
332 }
333
334 if (msg.SecondaryKind != notification.SecondaryKind) {
335 failed = 1;
336 printf("Fail SecondaryKind:(%d) %s != %s",msg.Kind, msg.SecondaryKind, notification.SecondaryKind);
337 }
338
339 if (!valuesWithinRange(msg.u_fps, notification.u_fps,1)) {
340 failed = 1;
341 printf("Fail u_fps:(%d) %s != %s",msg.Kind, msg.u_fps, notification.u_fps);
342 }
343
344 if (!valuesWithinRange(msg.v_fps, notification.v_fps,1)) {
345 failed = 1;
346 printf("Fail v_fps:(%d) %s != %s",msg.Kind, msg.v_fps, notification.v_fps);
347 }
348
349 if (!valuesWithinRange(msg.w_fps, notification.w_fps,1)) {
350 failed = 1;
351 printf("Fail w_fps:(%d) %s != %s",msg.Kind, msg.w_fps, notification.w_fps);
352 }
353
354 if (msg.RemoteCallsign != notification.RemoteCallsign) {
355 failed = 1;
356 printf("Fail RemoteCallsign:(%d) %s != %s",msg.Kind, msg.RemoteCallsign, notification.RemoteCallsign);
357 }
358
359 if (msg.Flags != notification.Flags) {
360 failed = 1;
361 printf("Fail Flags:(%d) %s != %s",msg.Kind, msg.Flags, notification.Flags);
362 }
363 if (!failed)
364 printf("%s (%d) received correctly",notification.NotificationType, notification.Kind);
365 else
366 me.FailCount = me.FailCount + 1;
367 return emesary.Transmitter.ReceiptStatus_NotProcessed; # we're not processing it, just looking
368 }
369 }
370 print("Failed to locate notification Key=",msg.SecondaryKind);
371 # debug.dump(notification);
372 }
373 return emesary.Transmitter.ReceiptStatus_NotProcessed; # we're not processing it, just looking
374 }
375
376 # register the test recipient on the global transmitter as that is where messages will be bridged to
377 emesary.GlobalTransmitter.Register(testRecipient);
378
379 # start the incoming bridge - which will connect from the remote bridge (testBridgedTransmitter)
380 # and route the notifications so that they come out of the global transmitter.
381 emesary_mp_bridge.IncomingMPBridge.startMPBridge(testRoutedNotifications, 18, emesary.GlobalTransmitter);
382
383 #
384 # Create the incoming bridge by faking a model appearing
385 #
386 var node = props.getNode(testMpPropertyPath,1);
387 node.getNode("callsign",1).setValue("test-mp");
388 var mpNode = node.getNode("sim/multiplay",1);
389 bridgeNode = mpNode.getNode("emesary/bridge[18]",1);
390
391 #
392 # lastly we need to manually copy the properties from where the outgoing bridge puts them
393 # to where the incoming bridge expects them.
394 # This is normally performed by the multiplayer code - but for test purposes this
395 # allows testing on a single instance.
396 mp_listener = setlistener("/sim/multiplay/emesary/bridge[18]", func(v){
397 bridgeNode.setValue(v.getValue());
398 },0,0);
399
400 # This is what causes the incoming bridge to create a connection between the multiplayer node
401 # and will result in an active incoming bridge
402 setprop("/ai/models/model-added", testMpPropertyPath);
403
404 #
405 # send out test messages
406 for (var idx=0; idx < 20; idx += 1) {
407 var msg = notifications.GeoEventNotification.new("mhit", "AIM"~idx, 19, idx);
408 testBridgedTransmitter.NotifyAll(msg);
409 append(msgList, msg);
410 }
411 msg = notifications.GeoEventNotification.new("mhit", "DONE", 19, 100);
412 testBridgedTransmitter.NotifyAll(msg);
413 }
414