From def442b9dbb66153055fcd87e4326c40de09ae44 Mon Sep 17 00:00:00 2001 From: gaschler Date: Mon, 18 Dec 2017 20:47:00 +0100 Subject: [PATCH] Make MapBuilderServer testable (#771) Pass MapBuilder to the constructor so the MapBuilder can be mocked. --- cartographer_grpc/client_server_test.cc | 5 ++++- cartographer_grpc/map_builder_server.cc | 9 +++++---- cartographer_grpc/map_builder_server.h | 5 +++-- cartographer_grpc/map_builder_server_main.cc | 6 +++++- 4 files changed, 17 insertions(+), 8 deletions(-) diff --git a/cartographer_grpc/client_server_test.cc b/cartographer_grpc/client_server_test.cc index 2b9c9f2..25ddc50 100644 --- a/cartographer_grpc/client_server_test.cc +++ b/cartographer_grpc/client_server_test.cc @@ -44,8 +44,11 @@ class ClientServerTest : public ::testing::Test { cartographer::mapping::test::ResolveLuaParameters(kMapBuilderServerLua); auto map_builder_server_options = CreateMapBuilderServerOptions(map_builder_server_parameters.get()); + auto map_builder = + cartographer::common::make_unique( + map_builder_server_options.map_builder_options()); server_ = cartographer::common::make_unique( - map_builder_server_options); + map_builder_server_options, std::move(map_builder)); EXPECT_TRUE(server_ != nullptr); } diff --git a/cartographer_grpc/map_builder_server.cc b/cartographer_grpc/map_builder_server.cc index d30c962..8c5db4d 100644 --- a/cartographer_grpc/map_builder_server.cc +++ b/cartographer_grpc/map_builder_server.cc @@ -39,7 +39,7 @@ MapBuilderServer::MapBuilderContext::MapBuilderContext( cartographer::mapping::MapBuilder& MapBuilderServer::MapBuilderContext::map_builder() { - return map_builder_server_->map_builder_; + return *map_builder_server_->map_builder_; } cartographer::common::BlockingQueue< @@ -66,13 +66,14 @@ MapBuilderServer::MapBuilderContext:: void MapBuilderServer::MapBuilderContext::AddSensorDataToTrajectory( const SensorData& sensor_data) { sensor_data.sensor_data->AddToTrajectoryBuilder( - map_builder_server_->map_builder_.GetTrajectoryBuilder( + map_builder_server_->map_builder_->GetTrajectoryBuilder( sensor_data.trajectory_id)); } MapBuilderServer::MapBuilderServer( - const proto::MapBuilderServerOptions& map_builder_server_options) - : map_builder_(map_builder_server_options.map_builder_options()) { + const proto::MapBuilderServerOptions& map_builder_server_options, + std::unique_ptr map_builder) + : map_builder_(std::move(map_builder)) { framework::Server::Builder server_builder; server_builder.SetServerAddress(map_builder_server_options.server_address()); server_builder.SetNumGrpcThreads( diff --git a/cartographer_grpc/map_builder_server.h b/cartographer_grpc/map_builder_server.h index 2896525..2b11de9 100644 --- a/cartographer_grpc/map_builder_server.h +++ b/cartographer_grpc/map_builder_server.h @@ -74,7 +74,8 @@ class MapBuilderServer { friend MapBuilderContext; MapBuilderServer( - const proto::MapBuilderServerOptions& map_builder_server_options); + const proto::MapBuilderServerOptions& map_builder_server_options, + std::unique_ptr map_builder); // Starts the gRPC server and the SLAM thread. void Start(); @@ -105,7 +106,7 @@ class MapBuilderServer { bool shutting_down_ = false; std::unique_ptr slam_thread_; std::unique_ptr grpc_server_; - cartographer::mapping::MapBuilder map_builder_; + std::unique_ptr map_builder_; cartographer::common::BlockingQueue> sensor_data_queue_; cartographer::common::Mutex local_slam_subscriptions_lock_; diff --git a/cartographer_grpc/map_builder_server_main.cc b/cartographer_grpc/map_builder_server_main.cc index 4a2b6bb..f30160d 100644 --- a/cartographer_grpc/map_builder_server_main.cc +++ b/cartographer_grpc/map_builder_server_main.cc @@ -34,7 +34,11 @@ void Run(const std::string& configuration_directory, proto::MapBuilderServerOptions map_builder_server_options = LoadMapBuilderServerOptions(configuration_directory, configuration_basename); - MapBuilderServer map_builder_server(map_builder_server_options); + auto map_builder = + cartographer::common::make_unique( + map_builder_server_options.map_builder_options()); + MapBuilderServer map_builder_server(map_builder_server_options, + std::move(map_builder)); map_builder_server.Start(); map_builder_server.WaitForShutdown(); }