Make MapBuilderServer testable (#771)

Pass MapBuilder to the constructor so the MapBuilder can be mocked.
master
gaschler 2017-12-18 20:47:00 +01:00 committed by Wally B. Feed
parent 5bb81a9b4a
commit def442b9db
4 changed files with 17 additions and 8 deletions

View File

@ -44,8 +44,11 @@ class ClientServerTest : public ::testing::Test {
cartographer::mapping::test::ResolveLuaParameters(kMapBuilderServerLua); cartographer::mapping::test::ResolveLuaParameters(kMapBuilderServerLua);
auto map_builder_server_options = auto map_builder_server_options =
CreateMapBuilderServerOptions(map_builder_server_parameters.get()); CreateMapBuilderServerOptions(map_builder_server_parameters.get());
auto map_builder =
cartographer::common::make_unique<cartographer::mapping::MapBuilder>(
map_builder_server_options.map_builder_options());
server_ = cartographer::common::make_unique<MapBuilderServer>( server_ = cartographer::common::make_unique<MapBuilderServer>(
map_builder_server_options); map_builder_server_options, std::move(map_builder));
EXPECT_TRUE(server_ != nullptr); EXPECT_TRUE(server_ != nullptr);
} }

View File

@ -39,7 +39,7 @@ MapBuilderServer::MapBuilderContext::MapBuilderContext(
cartographer::mapping::MapBuilder& cartographer::mapping::MapBuilder&
MapBuilderServer::MapBuilderContext::map_builder() { MapBuilderServer::MapBuilderContext::map_builder() {
return map_builder_server_->map_builder_; return *map_builder_server_->map_builder_;
} }
cartographer::common::BlockingQueue< cartographer::common::BlockingQueue<
@ -66,13 +66,14 @@ MapBuilderServer::MapBuilderContext::
void MapBuilderServer::MapBuilderContext::AddSensorDataToTrajectory( void MapBuilderServer::MapBuilderContext::AddSensorDataToTrajectory(
const SensorData& sensor_data) { const SensorData& sensor_data) {
sensor_data.sensor_data->AddToTrajectoryBuilder( sensor_data.sensor_data->AddToTrajectoryBuilder(
map_builder_server_->map_builder_.GetTrajectoryBuilder( map_builder_server_->map_builder_->GetTrajectoryBuilder(
sensor_data.trajectory_id)); sensor_data.trajectory_id));
} }
MapBuilderServer::MapBuilderServer( MapBuilderServer::MapBuilderServer(
const proto::MapBuilderServerOptions& map_builder_server_options) const proto::MapBuilderServerOptions& map_builder_server_options,
: map_builder_(map_builder_server_options.map_builder_options()) { std::unique_ptr<cartographer::mapping::MapBuilder> map_builder)
: map_builder_(std::move(map_builder)) {
framework::Server::Builder server_builder; framework::Server::Builder server_builder;
server_builder.SetServerAddress(map_builder_server_options.server_address()); server_builder.SetServerAddress(map_builder_server_options.server_address());
server_builder.SetNumGrpcThreads( server_builder.SetNumGrpcThreads(

View File

@ -74,7 +74,8 @@ class MapBuilderServer {
friend MapBuilderContext; friend MapBuilderContext;
MapBuilderServer( MapBuilderServer(
const proto::MapBuilderServerOptions& map_builder_server_options); const proto::MapBuilderServerOptions& map_builder_server_options,
std::unique_ptr<cartographer::mapping::MapBuilder> map_builder);
// Starts the gRPC server and the SLAM thread. // Starts the gRPC server and the SLAM thread.
void Start(); void Start();
@ -105,7 +106,7 @@ class MapBuilderServer {
bool shutting_down_ = false; bool shutting_down_ = false;
std::unique_ptr<std::thread> slam_thread_; std::unique_ptr<std::thread> slam_thread_;
std::unique_ptr<framework::Server> grpc_server_; std::unique_ptr<framework::Server> grpc_server_;
cartographer::mapping::MapBuilder map_builder_; std::unique_ptr<cartographer::mapping::MapBuilder> map_builder_;
cartographer::common::BlockingQueue<std::unique_ptr<SensorData>> cartographer::common::BlockingQueue<std::unique_ptr<SensorData>>
sensor_data_queue_; sensor_data_queue_;
cartographer::common::Mutex local_slam_subscriptions_lock_; cartographer::common::Mutex local_slam_subscriptions_lock_;

View File

@ -34,7 +34,11 @@ void Run(const std::string& configuration_directory,
proto::MapBuilderServerOptions map_builder_server_options = proto::MapBuilderServerOptions map_builder_server_options =
LoadMapBuilderServerOptions(configuration_directory, LoadMapBuilderServerOptions(configuration_directory,
configuration_basename); configuration_basename);
MapBuilderServer map_builder_server(map_builder_server_options); auto map_builder =
cartographer::common::make_unique<cartographer::mapping::MapBuilder>(
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.Start();
map_builder_server.WaitForShutdown(); map_builder_server.WaitForShutdown();
} }