forked from gazebosim/gz-sim
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Server_TEST.cc
1254 lines (1039 loc) · 39 KB
/
Server_TEST.cc
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
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
863
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* Copyright (C) 2018 Open Source Robotics Foundation
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*/
#include <gtest/gtest.h>
#include <csignal>
#include <vector>
#include <gz/common/StringUtils.hh>
#include <gz/common/Util.hh>
#include <gz/math/Rand.hh>
#include <gz/transport/Node.hh>
#include <gz/utils/ExtraTestMacros.hh>
#include <sdf/Mesh.hh>
#include "gz/sim/components/AxisAlignedBox.hh"
#include "gz/sim/components/Geometry.hh"
#include "gz/sim/components/Model.hh"
#include "gz/sim/Entity.hh"
#include "gz/sim/EntityComponentManager.hh"
#include "gz/sim/System.hh"
#include "gz/sim/SystemLoader.hh"
#include "gz/sim/Server.hh"
#include "gz/sim/Types.hh"
#include "gz/sim/Util.hh"
#include "test_config.hh"
#include "plugins/MockSystem.hh"
#include "../test/helpers/Relay.hh"
#include "../test/helpers/EnvTestFixture.hh"
using namespace gz;
using namespace gz::sim;
using namespace std::chrono_literals;
/////////////////////////////////////////////////
class ServerFixture : public InternalFixture<::testing::TestWithParam<int>>
{
};
/////////////////////////////////////////////////
// See https://github.com/gazebosim/gz-sim/issues/1175
TEST_P(ServerFixture, GZ_UTILS_TEST_DISABLED_ON_WIN32(DefaultServerConfig))
{
ServerConfig serverConfig;
EXPECT_TRUE(serverConfig.SdfFile().empty());
EXPECT_TRUE(serverConfig.SdfString().empty());
EXPECT_FALSE(serverConfig.UpdateRate());
EXPECT_FALSE(serverConfig.UseLevels());
EXPECT_FALSE(serverConfig.UseDistributedSimulation());
EXPECT_EQ(0u, serverConfig.NetworkSecondaries());
EXPECT_TRUE(serverConfig.NetworkRole().empty());
EXPECT_FALSE(serverConfig.UseLogRecord());
EXPECT_FALSE(serverConfig.LogRecordPath().empty());
EXPECT_TRUE(serverConfig.LogPlaybackPath().empty());
EXPECT_FALSE(serverConfig.LogRecordResources());
EXPECT_TRUE(serverConfig.LogRecordCompressPath().empty());
EXPECT_EQ(0u, serverConfig.Seed());
EXPECT_EQ(123ms, serverConfig.UpdatePeriod().value_or(123ms));
EXPECT_TRUE(serverConfig.ResourceCache().empty());
EXPECT_TRUE(serverConfig.PhysicsEngine().empty());
EXPECT_TRUE(serverConfig.Plugins().empty());
EXPECT_TRUE(serverConfig.LogRecordTopics().empty());
sim::Server server(serverConfig);
EXPECT_FALSE(server.Running());
EXPECT_FALSE(*server.Running(0));
EXPECT_EQ(std::nullopt, server.Running(1));
EXPECT_TRUE(*server.Paused());
EXPECT_EQ(0u, *server.IterationCount());
EXPECT_EQ(3u, *server.EntityCount());
EXPECT_TRUE(server.HasEntity("default"));
EXPECT_EQ(3u, *server.SystemCount());
}
/////////////////////////////////////////////////
TEST_P(ServerFixture, UpdateRate)
{
sim::ServerConfig serverConfig;
serverConfig.SetUpdateRate(1000.0);
EXPECT_DOUBLE_EQ(1000.0, *serverConfig.UpdateRate());
serverConfig.SetUpdateRate(-1000.0);
EXPECT_DOUBLE_EQ(1000.0, *serverConfig.UpdateRate());
serverConfig.SetUpdateRate(0.0);
EXPECT_DOUBLE_EQ(1000.0, *serverConfig.UpdateRate());
EXPECT_EQ(1ms, serverConfig.UpdatePeriod());
}
/////////////////////////////////////////////////
TEST_P(ServerFixture, ServerConfigPluginInfo)
{
ServerConfig::PluginInfo pluginInfo;
pluginInfo.SetEntityName("an_entity");
pluginInfo.SetEntityType("model");
sdf::Plugin sdfPlugin;
sdfPlugin.SetFilename("filename");
sdfPlugin.SetName("interface");
pluginInfo.SetPlugin(sdfPlugin);
ServerConfig serverConfig;
serverConfig.AddPlugin(pluginInfo);
const std::list<ServerConfig::PluginInfo> &plugins = serverConfig.Plugins();
ASSERT_FALSE(plugins.empty());
EXPECT_EQ("an_entity", plugins.front().EntityName());
EXPECT_EQ("model", plugins.front().EntityType());
GZ_UTILS_WARN_IGNORE__DEPRECATED_DECLARATION
EXPECT_EQ("filename", plugins.front().Filename());
EXPECT_EQ("interface", plugins.front().Name());
EXPECT_EQ(nullptr, plugins.front().Sdf());
GZ_UTILS_WARN_RESUME__DEPRECATED_DECLARATION
// Test operator=
{
ServerConfig::PluginInfo info;
info = plugins.front();
EXPECT_EQ(info.EntityName(), plugins.front().EntityName());
EXPECT_EQ(info.EntityType(), plugins.front().EntityType());
EXPECT_EQ(info.Plugin().Name(), plugins.front().Plugin().Name());
EXPECT_EQ(info.Plugin().Filename(), plugins.front().Plugin().Filename());
EXPECT_EQ(info.Plugin().Element(), plugins.front().Plugin().Element());
}
// Test copy constructor
{
ServerConfig::PluginInfo info(plugins.front());
EXPECT_EQ(info.EntityName(), plugins.front().EntityName());
EXPECT_EQ(info.EntityType(), plugins.front().EntityType());
EXPECT_EQ(info.Plugin().Name(), plugins.front().Plugin().Name());
EXPECT_EQ(info.Plugin().Filename(), plugins.front().Plugin().Filename());
EXPECT_EQ(info.Plugin().Element(), plugins.front().Plugin().Element());
}
// Test server config copy constructor
{
const ServerConfig &cfg(serverConfig);
const std::list<ServerConfig::PluginInfo> &cfgPlugins = cfg.Plugins();
ASSERT_FALSE(cfgPlugins.empty());
EXPECT_EQ(cfgPlugins.front().EntityName(), plugins.front().EntityName());
EXPECT_EQ(cfgPlugins.front().EntityType(), plugins.front().EntityType());
EXPECT_EQ(cfgPlugins.front().Plugin().Filename(),
plugins.front().Plugin().Filename());
EXPECT_EQ(cfgPlugins.front().Plugin().Name(),
plugins.front().Plugin().Name());
EXPECT_EQ(cfgPlugins.front().Plugin().Element(),
plugins.front().Plugin().Element());
}
}
/////////////////////////////////////////////////
TEST_P(ServerFixture, ServerConfigSdfPluginInfo)
{
ServerConfig::PluginInfo pluginInfo;
pluginInfo.SetEntityName("an_entity");
pluginInfo.SetEntityType("model");
sdf::Plugin plugin;
plugin.SetFilename("filename");
plugin.SetName("interface");
pluginInfo.SetPlugin(plugin);
gz::sim::ServerConfig serverConfig;
serverConfig.AddPlugin(pluginInfo);
const std::list<ServerConfig::PluginInfo> &plugins = serverConfig.Plugins();
ASSERT_FALSE(plugins.empty());
EXPECT_EQ("an_entity", plugins.front().EntityName());
EXPECT_EQ("model", plugins.front().EntityType());
EXPECT_EQ("filename", plugins.front().Plugin().Filename());
EXPECT_EQ("interface", plugins.front().Plugin().Name());
EXPECT_EQ(nullptr, plugins.front().Plugin().Element());
EXPECT_TRUE(plugins.front().Plugin().Contents().empty());
// Test operator=
{
ServerConfig::PluginInfo info;
info = plugins.front();
EXPECT_EQ(info.EntityName(), plugins.front().EntityName());
EXPECT_EQ(info.EntityType(), plugins.front().EntityType());
EXPECT_EQ(info.Plugin().Filename(), plugins.front().Plugin().Filename());
EXPECT_EQ(info.Plugin().Name(), plugins.front().Plugin().Name());
EXPECT_EQ(info.Plugin().ToElement()->ToString(""),
plugins.front().Plugin().ToElement()->ToString(""));
}
// Test copy constructor
{
ServerConfig::PluginInfo info(plugins.front());
EXPECT_EQ(info.EntityName(), plugins.front().EntityName());
EXPECT_EQ(info.EntityType(), plugins.front().EntityType());
EXPECT_EQ(info.Plugin().Filename(), plugins.front().Plugin().Filename());
EXPECT_EQ(info.Plugin().Name(), plugins.front().Plugin().Name());
EXPECT_EQ(info.Plugin().ToElement()->ToString(""),
plugins.front().Plugin().ToElement()->ToString(""));
}
// Test server config copy constructor
{
const ServerConfig &cfg(serverConfig);
const std::list<ServerConfig::PluginInfo> &cfgPlugins = cfg.Plugins();
ASSERT_FALSE(cfgPlugins.empty());
EXPECT_EQ(cfgPlugins.front().EntityName(), plugins.front().EntityName());
EXPECT_EQ(cfgPlugins.front().EntityType(), plugins.front().EntityType());
EXPECT_EQ(cfgPlugins.front().Plugin().Filename(),
plugins.front().Plugin().Filename());
EXPECT_EQ(cfgPlugins.front().Plugin().Name(),
plugins.front().Plugin().Name());
EXPECT_EQ(cfgPlugins.front().Plugin().ToElement()->ToString(""),
plugins.front().Plugin().ToElement()->ToString(""));
}
}
/////////////////////////////////////////////////
TEST_P(ServerFixture, GZ_UTILS_TEST_DISABLED_ON_WIN32(ServerConfigRealPlugin))
{
// Start server
ServerConfig serverConfig;
serverConfig.SetUpdateRate(10000);
serverConfig.SetSdfFile(common::joinPaths(PROJECT_SOURCE_PATH,
"test", "worlds", "shapes.sdf"));
sdf::ElementPtr sdf(new sdf::Element);
sdf->SetName("plugin");
sdf->AddAttribute("name", "string",
"gz::sim::TestModelSystem", true);
sdf->AddAttribute("filename", "string", "libTestModelSystem.so", true);
sdf::ElementPtr child(new sdf::Element);
child->SetParent(sdf);
child->SetName("model_key");
child->AddValue("string", "987", "1");
ServerConfig::PluginInfo pluginInfo;
pluginInfo.SetEntityName("box");
pluginInfo.SetEntityType("model");
sdf::Plugin plugin;
plugin.SetFilename("TestModelSystem");
plugin.SetName("gz::sim::TestModelSystem");
pluginInfo.SetPlugin(plugin);
serverConfig.AddPlugin(pluginInfo);
sim::Server server(serverConfig);
// The simulation runner should not be running.
EXPECT_FALSE(*server.Running(0));
// Run the server
EXPECT_TRUE(server.Run(false, 0, false));
EXPECT_FALSE(*server.Paused());
// The TestModelSystem should have created a service. Call the service to
// make sure the TestModelSystem was successfully loaded.
transport::Node node;
msgs::StringMsg rep;
bool result{false};
bool executed{false};
int sleep{0};
int maxSleep{30};
while (!executed && sleep < maxSleep)
{
gzdbg << "Requesting /test/service" << std::endl;
executed = node.Request("/test/service", 100, rep, result);
sleep ;
}
EXPECT_TRUE(executed);
EXPECT_TRUE(result);
EXPECT_EQ("TestModelSystem", rep.data());
}
/////////////////////////////////////////////////
TEST_P(ServerFixture,
GZ_UTILS_TEST_DISABLED_ON_WIN32(ServerConfigSensorPlugin))
{
// Start server
ServerConfig serverConfig;
serverConfig.SetSdfFile(common::joinPaths(PROJECT_SOURCE_PATH,
"test", "worlds", "air_pressure.sdf"));
sdf::ElementPtr sdf(new sdf::Element);
sdf->SetName("plugin");
sdf->AddAttribute("name", "string",
"gz::sim::TestSensorSystem", true);
sdf->AddAttribute("filename", "string", "libTestSensorSystem.so", true);
ServerConfig::PluginInfo pluginInfo;
pluginInfo.SetEntityName(
"air_pressure_sensor::air_pressure_model::link::air_pressure_sensor");
pluginInfo.SetEntityType("sensor");
sdf::Plugin plugin;
plugin.Load(sdf);
plugin.SetFilename("TestSensorSystem");
plugin.SetName("gz::sim::TestSensorSystem");
pluginInfo.SetPlugin(plugin);
serverConfig.AddPlugin(pluginInfo);
gzdbg << "Create server" << std::endl;
sim::Server server(serverConfig);
// The simulation runner should not be running.
EXPECT_FALSE(*server.Running(0));
EXPECT_EQ(3u, *server.SystemCount());
// Run the server
gzdbg << "Run server" << std::endl;
EXPECT_TRUE(server.Run(false, 0, false));
EXPECT_FALSE(*server.Paused());
// The TestSensorSystem should have created a service. Call the service to
// make sure the TestSensorSystem was successfully loaded.
gzdbg << "Request service" << std::endl;
transport::Node node;
msgs::StringMsg rep;
bool result{false};
bool executed{false};
int sleep{0};
int maxSleep{30};
while (!executed && sleep < maxSleep)
{
gzdbg << "Requesting /test/service/sensor" << std::endl;
executed = node.Request("/test/service/sensor", 100, rep, result);
sleep ;
}
EXPECT_TRUE(executed);
EXPECT_TRUE(result);
EXPECT_EQ("TestSensorSystem", rep.data());
}
/////////////////////////////////////////////////
TEST_P(ServerFixture, GZ_UTILS_TEST_DISABLED_ON_WIN32(SdfServerConfig))
{
ServerConfig serverConfig;
serverConfig.SetSdfString(TestWorldSansPhysics::World());
EXPECT_TRUE(serverConfig.SdfFile().empty());
EXPECT_FALSE(serverConfig.SdfString().empty());
// Setting the SDF file should override the string.
serverConfig.SetSdfFile(common::joinPaths(PROJECT_SOURCE_PATH,
"test", "worlds", "shapes.sdf"));
EXPECT_FALSE(serverConfig.SdfFile().empty());
EXPECT_TRUE(serverConfig.SdfString().empty());
sim::Server server(serverConfig);
EXPECT_FALSE(server.Running());
EXPECT_FALSE(*server.Running(0));
EXPECT_TRUE(*server.Paused());
EXPECT_EQ(0u, *server.IterationCount());
EXPECT_EQ(25u, *server.EntityCount());
EXPECT_EQ(3u, *server.SystemCount());
EXPECT_TRUE(server.HasEntity("box"));
EXPECT_FALSE(server.HasEntity("box", 1));
EXPECT_TRUE(server.HasEntity("sphere"));
EXPECT_TRUE(server.HasEntity("cylinder"));
EXPECT_TRUE(server.HasEntity("capsule"));
EXPECT_TRUE(server.HasEntity("ellipsoid"));
EXPECT_FALSE(server.HasEntity("bad", 0));
EXPECT_FALSE(server.HasEntity("bad", 1));
}
/////////////////////////////////////////////////
TEST_P(ServerFixture, GZ_UTILS_TEST_DISABLED_ON_WIN32(SdfRootServerConfig))
{
gz::sim::ServerConfig serverConfig;
serverConfig.SetSdfString(TestWorldSansPhysics::World());
EXPECT_TRUE(serverConfig.SdfFile().empty());
EXPECT_FALSE(serverConfig.SdfString().empty());
serverConfig.SetSdfFile(common::joinPaths(PROJECT_SOURCE_PATH,
"test", "worlds", "air_pressure.sdf"));
EXPECT_FALSE(serverConfig.SdfFile().empty());
EXPECT_TRUE(serverConfig.SdfString().empty());
sdf::Root root;
root.Load(common::joinPaths(PROJECT_SOURCE_PATH,
"test", "worlds", "shapes.sdf"));
// Setting the SDF Root should override the string and file.
serverConfig.SetSdfRoot(root);
EXPECT_TRUE(serverConfig.SdfRoot());
EXPECT_TRUE(serverConfig.SdfFile().empty());
EXPECT_TRUE(serverConfig.SdfString().empty());
sim::Server server(serverConfig);
EXPECT_FALSE(server.Running());
EXPECT_FALSE(*server.Running(0));
EXPECT_TRUE(*server.Paused());
EXPECT_EQ(0u, *server.IterationCount());
EXPECT_EQ(25u, *server.EntityCount());
EXPECT_EQ(3u, *server.SystemCount());
EXPECT_TRUE(server.HasEntity("box"));
EXPECT_FALSE(server.HasEntity("box", 1));
EXPECT_TRUE(server.HasEntity("sphere"));
EXPECT_TRUE(server.HasEntity("cylinder"));
EXPECT_TRUE(server.HasEntity("capsule"));
EXPECT_TRUE(server.HasEntity("ellipsoid"));
EXPECT_FALSE(server.HasEntity("bad", 0));
EXPECT_FALSE(server.HasEntity("bad", 1));
}
/////////////////////////////////////////////////
TEST_P(ServerFixture, GZ_UTILS_TEST_DISABLED_ON_WIN32(ServerConfigLogRecord))
{
auto logPath = common::joinPaths(PROJECT_BINARY_PATH, "test_log_path");
auto logFile = common::joinPaths(logPath, "state.tlog");
auto compressedFile = logPath ".zip";
gzdbg << "Log path [" << logPath << "]" << std::endl;
common::removeAll(logPath);
common::removeAll(compressedFile);
EXPECT_FALSE(common::exists(logFile));
EXPECT_FALSE(common::exists(compressedFile));
{
sim::ServerConfig serverConfig;
serverConfig.SetUseLogRecord(true);
serverConfig.SetLogRecordPath(logPath);
sim::Server server(serverConfig);
EXPECT_EQ(0u, *server.IterationCount());
EXPECT_EQ(3u, *server.EntityCount());
EXPECT_EQ(4u, *server.SystemCount());
EXPECT_TRUE(serverConfig.LogRecordTopics().empty());
serverConfig.AddLogRecordTopic("test_topic1");
EXPECT_EQ(1u, serverConfig.LogRecordTopics().size());
serverConfig.AddLogRecordTopic("test_topic2");
EXPECT_EQ(2u, serverConfig.LogRecordTopics().size());
serverConfig.ClearLogRecordTopics();
EXPECT_TRUE(serverConfig.LogRecordTopics().empty());
}
EXPECT_TRUE(common::exists(logFile));
EXPECT_FALSE(common::exists(compressedFile));
}
/////////////////////////////////////////////////
TEST_P(ServerFixture,
GZ_UTILS_TEST_DISABLED_ON_WIN32(ServerConfigLogRecordCompress))
{
auto logPath = common::joinPaths(PROJECT_BINARY_PATH, "test_log_path");
auto logFile = common::joinPaths(logPath, "state.tlog");
auto compressedFile = logPath ".zip";
gzdbg << "Log path [" << logPath << "]" << std::endl;
common::removeAll(logPath);
common::removeAll(compressedFile);
EXPECT_FALSE(common::exists(logFile));
EXPECT_FALSE(common::exists(compressedFile));
{
sim::ServerConfig serverConfig;
serverConfig.SetUseLogRecord(true);
serverConfig.SetLogRecordPath(logPath);
serverConfig.SetLogRecordCompressPath(compressedFile);
sim::Server server(serverConfig);
EXPECT_EQ(0u, *server.IterationCount());
EXPECT_EQ(3u, *server.EntityCount());
EXPECT_EQ(4u, *server.SystemCount());
}
EXPECT_FALSE(common::exists(logFile));
EXPECT_TRUE(common::exists(compressedFile));
}
/////////////////////////////////////////////////
TEST_P(ServerFixture, SdfStringServerConfig)
{
ServerConfig serverConfig;
serverConfig.SetSdfFile(common::joinPaths(PROJECT_SOURCE_PATH,
"test", "worlds", "shapes.sdf"));
EXPECT_FALSE(serverConfig.SdfFile().empty());
EXPECT_TRUE(serverConfig.SdfString().empty());
// Setting the string should override the file.
serverConfig.SetSdfString(TestWorldSansPhysics::World());
EXPECT_TRUE(serverConfig.SdfFile().empty());
EXPECT_FALSE(serverConfig.SdfString().empty());
EXPECT_FALSE(serverConfig.SdfRoot());
sim::Server server(serverConfig);
EXPECT_FALSE(server.Running());
EXPECT_FALSE(*server.Running(0));
EXPECT_TRUE(*server.Paused());
EXPECT_EQ(0u, *server.IterationCount());
EXPECT_EQ(3u, *server.EntityCount());
EXPECT_EQ(2u, *server.SystemCount());
}
/////////////////////////////////////////////////
TEST_P(ServerFixture, RunBlocking)
{
sim::Server server;
EXPECT_FALSE(server.Running());
EXPECT_FALSE(*server.Running(0));
EXPECT_TRUE(*server.Paused());
EXPECT_EQ(0u, server.IterationCount());
// Make the server run fast.
server.SetUpdatePeriod(1ns);
uint64_t expectedIters = 0;
for (uint64_t i = 1; i < 10; i)
{
EXPECT_FALSE(server.Running());
EXPECT_FALSE(*server.Running(0));
server.Run(true, i, false);
EXPECT_FALSE(server.Running());
EXPECT_FALSE(*server.Running(0));
expectedIters = i;
EXPECT_EQ(expectedIters, *server.IterationCount());
}
}
/////////////////////////////////////////////////
TEST_P(ServerFixture, RunNonBlockingPaused)
{
sim::Server server;
// The server should not be running.
EXPECT_FALSE(server.Running());
EXPECT_FALSE(*server.Running(0));
// The simulation runner should not be running.
EXPECT_FALSE(*server.Running(0));
// Invalid world index.
EXPECT_EQ(std::nullopt, server.Running(1));
EXPECT_TRUE(*server.Paused());
EXPECT_EQ(0u, *server.IterationCount());
// Make the server run fast.
server.SetUpdatePeriod(1ns);
EXPECT_TRUE(server.Run(false, 100, true));
EXPECT_TRUE(*server.Paused());
EXPECT_TRUE(server.Running());
// Add a small sleep because the non-blocking Run call causes the
// simulation runner to start asynchronously.
GZ_SLEEP_MS(500);
EXPECT_TRUE(*server.Running(0));
EXPECT_EQ(0u, server.IterationCount());
// Attempt to unpause an invalid world
EXPECT_FALSE(server.SetPaused(false, 1));
// Unpause the existing world
EXPECT_TRUE(server.SetPaused(false, 0));
EXPECT_FALSE(*server.Paused());
EXPECT_TRUE(server.Running());
while (*server.IterationCount() < 100)
GZ_SLEEP_MS(100);
EXPECT_EQ(100u, *server.IterationCount());
EXPECT_FALSE(server.Running());
EXPECT_FALSE(*server.Running(0));
}
/////////////////////////////////////////////////
TEST_P(ServerFixture, RunNonBlocking)
{
sim::Server server;
EXPECT_FALSE(server.Running());
EXPECT_FALSE(*server.Running(0));
EXPECT_EQ(0u, *server.IterationCount());
// Make the server run fast.
server.SetUpdatePeriod(1ns);
server.Run(false, 100, false);
while (*server.IterationCount() < 100)
GZ_SLEEP_MS(100);
EXPECT_EQ(100u, *server.IterationCount());
EXPECT_FALSE(server.Running());
EXPECT_FALSE(*server.Running(0));
}
/////////////////////////////////////////////////
TEST_P(ServerFixture, GZ_UTILS_TEST_DISABLED_ON_WIN32(RunOnceUnpaused))
{
sim::Server server;
EXPECT_FALSE(server.Running());
EXPECT_FALSE(*server.Running(0));
EXPECT_EQ(0u, *server.IterationCount());
// Load a system
sim::SystemLoader systemLoader;
sdf::Plugin sdfPlugin;
sdfPlugin.SetName("gz::sim::MockSystem");
sdfPlugin.SetFilename("MockSystem");
auto mockSystemPlugin = systemLoader.LoadPlugin(sdfPlugin);
ASSERT_TRUE(mockSystemPlugin.has_value());
// Check that it was loaded
const size_t systemCount = *server.SystemCount();
EXPECT_TRUE(*server.AddSystem(mockSystemPlugin.value()));
EXPECT_EQ(systemCount 1, *server.SystemCount());
// Query the interface from the plugin
auto system = mockSystemPlugin.value()->QueryInterface<sim::System>();
EXPECT_NE(system, nullptr);
auto mockSystem = dynamic_cast<sim::MockSystem*>(system);
EXPECT_NE(mockSystem, nullptr);
// No steps should have been executed
EXPECT_EQ(0u, mockSystem->preUpdateCallCount);
EXPECT_EQ(0u, mockSystem->updateCallCount);
EXPECT_EQ(0u, mockSystem->postUpdateCallCount);
// Make the server run fast
server.SetUpdatePeriod(1ns);
while (*server.IterationCount() < 100)
server.RunOnce(false);
// Check that the server provides the correct information
EXPECT_EQ(*server.IterationCount(), 100u);
EXPECT_FALSE(server.Running());
EXPECT_FALSE(*server.Running(0));
// Check that the system has been called correctly
EXPECT_EQ(100u, mockSystem->preUpdateCallCount);
EXPECT_EQ(100u, mockSystem->updateCallCount);
EXPECT_EQ(100u, mockSystem->postUpdateCallCount);
}
/////////////////////////////////////////////////
TEST_P(ServerFixture, GZ_UTILS_TEST_DISABLED_ON_WIN32(RunOncePaused))
{
sim::Server server;
EXPECT_FALSE(server.Running());
EXPECT_FALSE(*server.Running(0));
EXPECT_EQ(0u, *server.IterationCount());
// Load a system
sim::SystemLoader systemLoader;
sdf::Plugin sdfPlugin;
sdfPlugin.SetName("gz::sim::MockSystem");
sdfPlugin.SetFilename("MockSystem");
auto mockSystemPlugin = systemLoader.LoadPlugin(sdfPlugin);
ASSERT_TRUE(mockSystemPlugin.has_value());
// Check that it was loaded
const size_t systemCount = *server.SystemCount();
EXPECT_TRUE(*server.AddSystem(mockSystemPlugin.value()));
EXPECT_EQ(systemCount 1, *server.SystemCount());
// Query the interface from the plugin
auto system = mockSystemPlugin.value()->QueryInterface<sim::System>();
EXPECT_NE(system, nullptr);
auto mockSystem = dynamic_cast<sim::MockSystem*>(system);
EXPECT_NE(mockSystem, nullptr);
// No steps should have been executed
EXPECT_EQ(0u, mockSystem->preUpdateCallCount);
EXPECT_EQ(0u, mockSystem->updateCallCount);
EXPECT_EQ(0u, mockSystem->postUpdateCallCount);
// Make the server run fast
server.SetUpdatePeriod(1ns);
while (*server.IterationCount() < 100)
server.RunOnce(true);
// Check that the server provides the correct information
EXPECT_EQ(*server.IterationCount(), 100u);
EXPECT_FALSE(server.Running());
EXPECT_FALSE(*server.Running(0));
// Check that the system has been called correctly
EXPECT_EQ(100u, mockSystem->preUpdateCallCount);
EXPECT_EQ(100u, mockSystem->updateCallCount);
EXPECT_EQ(100u, mockSystem->postUpdateCallCount);
}
/////////////////////////////////////////////////
TEST_P(ServerFixture, RunNonBlockingMultiple)
{
ServerConfig serverConfig;
serverConfig.SetSdfString(TestWorldSansPhysics::World());
sim::Server server(serverConfig);
EXPECT_FALSE(server.Running());
EXPECT_FALSE(*server.Running(0));
EXPECT_EQ(0u, *server.IterationCount());
EXPECT_TRUE(server.Run(false, 100, false));
EXPECT_FALSE(server.Run(false, 100, false));
while (*server.IterationCount() < 100)
GZ_SLEEP_MS(100);
EXPECT_EQ(100u, *server.IterationCount());
EXPECT_FALSE(server.Running());
EXPECT_FALSE(*server.Running(0));
}
/////////////////////////////////////////////////
TEST_P(ServerFixture, SigInt)
{
sim::Server server;
EXPECT_FALSE(server.Running());
EXPECT_FALSE(*server.Running(0));
// Run forever, non-blocking.
server.Run(false, 0, false);
GZ_SLEEP_MS(500);
EXPECT_TRUE(server.Running());
EXPECT_TRUE(*server.Running(0));
std::raise(SIGTERM);
EXPECT_FALSE(server.Running());
EXPECT_FALSE(*server.Running(0));
}
/////////////////////////////////////////////////
TEST_P(ServerFixture, ServerControlStop)
{
// Test that the server correctly reacts to requests on /server_control
// service with `stop` set to either false or true.
sim::Server server;
EXPECT_FALSE(server.Running());
EXPECT_FALSE(*server.Running(0));
// Run forever, non-blocking.
server.Run(false, 0, false);
GZ_SLEEP_MS(500);
EXPECT_TRUE(server.Running());
EXPECT_TRUE(*server.Running(0));
transport::Node node;
msgs::ServerControl req;
msgs::Boolean res;
bool result{false};
bool executed{false};
int sleep{0};
int maxSleep{30};
// first, call with stop = false; the server should keep running
while (!executed && sleep < maxSleep)
{
gzdbg << "Requesting /server_control" << std::endl;
executed = node.Request("/server_control", req, 100, res, result);
sleep ;
}
EXPECT_TRUE(executed);
EXPECT_TRUE(result);
EXPECT_FALSE(res.data());
GZ_SLEEP_MS(500);
EXPECT_TRUE(server.Running());
EXPECT_TRUE(*server.Running(0));
// now call with stop = true; the server should stop
req.set_stop(true);
gzdbg << "Requesting /server_control" << std::endl;
executed = node.Request("/server_control", req, 100, res, result);
EXPECT_TRUE(executed);
EXPECT_TRUE(result);
EXPECT_TRUE(res.data());
GZ_SLEEP_MS(500);
EXPECT_FALSE(server.Running());
EXPECT_FALSE(*server.Running(0));
}
/////////////////////////////////////////////////
TEST_P(ServerFixture, GZ_UTILS_TEST_DISABLED_ON_WIN32(AddSystemWhileRunning))
{
ServerConfig serverConfig;
serverConfig.SetSdfFile(common::joinPaths(PROJECT_SOURCE_PATH,
"test", "worlds", "shapes.sdf"));
sim::Server server(serverConfig);
EXPECT_FALSE(server.Running());
EXPECT_FALSE(*server.Running(0));
server.SetUpdatePeriod(1us);
// Run the server to test whether we can add systems while system is running
server.Run(false, 0, false);
GZ_SLEEP_MS(500);
EXPECT_TRUE(server.Running());
EXPECT_TRUE(*server.Running(0));
EXPECT_EQ(3u, *server.SystemCount());
// Add system from plugin
sim::SystemLoader systemLoader;
sdf::Plugin sdfPlugin;
sdfPlugin.SetName("gz::sim::MockSystem");
sdfPlugin.SetFilename("MockSystem");
auto mockSystemPlugin = systemLoader.LoadPlugin(sdfPlugin);
ASSERT_TRUE(mockSystemPlugin.has_value());
auto result = server.AddSystem(mockSystemPlugin.value());
ASSERT_TRUE(result.has_value());
EXPECT_FALSE(result.value());
EXPECT_EQ(3u, *server.SystemCount());
// Add system pointer
auto mockSystem = std::make_shared<MockSystem>();
result = server.AddSystem(mockSystem);
ASSERT_TRUE(result.has_value());
EXPECT_FALSE(result.value());
EXPECT_EQ(3u, *server.SystemCount());
// Stop the server
std::raise(SIGTERM);
EXPECT_FALSE(server.Running());
EXPECT_FALSE(*server.Running(0));
}
/////////////////////////////////////////////////
TEST_P(ServerFixture, GZ_UTILS_TEST_DISABLED_ON_WIN32(AddSystemAfterLoad))
{
ServerConfig serverConfig;
serverConfig.SetSdfFile(common::joinPaths(PROJECT_SOURCE_PATH,
"test", "worlds", "shapes.sdf"));
sim::Server server(serverConfig);
EXPECT_FALSE(server.Running());
EXPECT_FALSE(*server.Running(0));
// Add system from plugin
sim::SystemLoader systemLoader;
sdf::Plugin sdfPlugin;
sdfPlugin.SetName("gz::sim::MockSystem");
sdfPlugin.SetFilename("MockSystem");
auto mockSystemPlugin = systemLoader.LoadPlugin(sdfPlugin);
ASSERT_TRUE(mockSystemPlugin.has_value());
auto system = mockSystemPlugin.value()->QueryInterface<sim::System>();
EXPECT_NE(system, nullptr);
auto mockSystem = dynamic_cast<sim::MockSystem*>(system);
ASSERT_NE(mockSystem, nullptr);
EXPECT_EQ(3u, *server.SystemCount());
EXPECT_EQ(0u, mockSystem->configureCallCount);
EXPECT_TRUE(*server.AddSystem(mockSystemPlugin.value()));
EXPECT_EQ(4u, *server.SystemCount());
EXPECT_EQ(1u, mockSystem->configureCallCount);
// Add system pointer
auto mockSystemLocal = std::make_shared<MockSystem>();
EXPECT_EQ(0u, mockSystemLocal->configureCallCount);
EXPECT_TRUE(server.AddSystem(mockSystemLocal));
EXPECT_EQ(5u, *server.SystemCount());
EXPECT_EQ(1u, mockSystemLocal->configureCallCount);
// Check that update callbacks are called
server.SetUpdatePeriod(1us);
EXPECT_EQ(0u, mockSystem->preUpdateCallCount);
EXPECT_EQ(0u, mockSystem->updateCallCount);
EXPECT_EQ(0u, mockSystem->postUpdateCallCount);
EXPECT_EQ(0u, mockSystemLocal->preUpdateCallCount);
EXPECT_EQ(0u, mockSystemLocal->updateCallCount);
EXPECT_EQ(0u, mockSystemLocal->postUpdateCallCount);
server.Run(true, 1, false);
EXPECT_EQ(1u, mockSystem->preUpdateCallCount);
EXPECT_EQ(1u, mockSystem->updateCallCount);
EXPECT_EQ(1u, mockSystem->postUpdateCallCount);
EXPECT_EQ(1u, mockSystemLocal->preUpdateCallCount);
EXPECT_EQ(1u, mockSystemLocal->updateCallCount);
EXPECT_EQ(1u, mockSystemLocal->postUpdateCallCount);
// Add to inexistent world
auto result = server.AddSystem(mockSystemPlugin.value(), 100);
EXPECT_FALSE(result.has_value());
result = server.AddSystem(mockSystemLocal, 100);
EXPECT_FALSE(result.has_value());
}
/////////////////////////////////////////////////
TEST_P(ServerFixture, Seed)
{
ServerConfig serverConfig;
EXPECT_EQ(0u, serverConfig.Seed());
unsigned int mySeed = 12345u;
serverConfig.SetSeed(mySeed);
EXPECT_EQ(mySeed, serverConfig.Seed());
EXPECT_EQ(mySeed, math::Rand::Seed());
}
/////////////////////////////////////////////////
TEST_P(ServerFixture, GZ_UTILS_TEST_DISABLED_ON_WIN32(ResourcePath))
{
common::setenv("GZ_SIM_RESOURCE_PATH",
(common::joinPaths(PROJECT_SOURCE_PATH, "test", "worlds:")
common::joinPaths(PROJECT_SOURCE_PATH,
"test", "worlds", "models")).c_str());
ServerConfig serverConfig;
serverConfig.SetSdfFile("resource_paths.sdf");
sim::Server server(serverConfig);
test::Relay testSystem;
unsigned int preUpdates{0};
testSystem.OnPreUpdate(
[&preUpdates](const sim::UpdateInfo &,
sim::EntityComponentManager &_ecm)
{
// Create AABB so it is populated
unsigned int eachCount{0};
_ecm.Each<components::Model>(
[&](const Entity &_entity, components::Model *) -> bool
{
auto bboxComp = _ecm.Component<components::AxisAlignedBox>(_entity);
EXPECT_EQ(bboxComp, nullptr);
_ecm.CreateComponent(_entity, components::AxisAlignedBox());
eachCount ;
return true;
});
EXPECT_EQ(1u, eachCount);
preUpdates ;
});
unsigned int postUpdates{0};
testSystem.OnPostUpdate([&postUpdates](const sim::UpdateInfo &,
const sim::EntityComponentManager &_ecm)
{
// Check geometry components
unsigned int eachCount{0};
_ecm.Each<components::Geometry>(
[&eachCount](const Entity &, const components::Geometry *_geom)
-> bool
{
auto mesh = _geom->Data().MeshShape();
// ASSERT would fail at compile with
// "void value not ignored as it ought to be"
EXPECT_NE(nullptr, mesh);
if (mesh)
{
EXPECT_EQ("model://scheme_resource_uri/meshes/box.dae",
mesh->Uri());
}
eachCount ;
return true;
});