Skip to content

Commit

Permalink
backend: add in_air to telemetry
Browse files Browse the repository at this point in the history
  • Loading branch information
JonasVautherin committed Apr 26, 2018
1 parent 3e54117 commit e1a34af
Show file tree
Hide file tree
Showing 3 changed files with 104 additions and 4 deletions.
14 changes: 14 additions & 0 deletions backend/src/plugins/telemetry/telemetry_service_impl.h
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,20 @@ class TelemetryServiceImpl final : public dronecore::rpc::telemetry::TelemetrySe
return grpc::Status::OK;
}

grpc::Status SubscribeInAir(grpc::ServerContext * /* context */,
const dronecore::rpc::telemetry::SubscribeInAirRequest * /* request */,
grpc::ServerWriter<rpc::telemetry::InAirResponse> *writer) override
{
_telemetry.in_air_async([&writer](bool is_in_air) {
dronecore::rpc::telemetry::InAirResponse rpc_in_air_response;
rpc_in_air_response.set_is_in_air(is_in_air);
writer->Write(rpc_in_air_response);
});

_stop_future.wait();
return grpc::Status::OK;
}

void stop()
{
_stop_promise.set_value();
Expand Down
93 changes: 89 additions & 4 deletions backend/test/telemetry_service_impl_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -60,8 +60,11 @@ class TelemetryServiceImplTest : public ::testing::Test
std::vector<Health> generateRandomHealthsVector(const int size);
bool generateRandomBool();

void checkSendsHomePositions(const std::vector<Position> &home_positions);
std::future<void> subscribeHomeAsync(std::vector<Position> &home_positions);
void checkSendsHomePositions(const std::vector<Position> &home_positions) const;
std::future<void> subscribeHomeAsync(std::vector<Position> &home_positions) const;

void checkSendsInAirEvents(const std::vector<bool> &in_air_events) const;
std::future<void> subscribeInAirAsync(std::vector<bool> &in_air_events) const;

std::unique_ptr<grpc::Server> _server;
std::unique_ptr<TelemetryService::Stub> _stub;
Expand Down Expand Up @@ -317,7 +320,7 @@ TEST_F(TelemetryServiceImplTest, registersToTelemetryHomeAsync)
}

std::future<void> TelemetryServiceImplTest::subscribeHomeAsync(std::vector<Position>
&home_positions)
&home_positions) const
{
return std::async(std::launch::async, [&]() {
grpc::ClientContext context;
Expand Down Expand Up @@ -360,7 +363,7 @@ TEST_F(TelemetryServiceImplTest, sendsOneHome)
checkSendsHomePositions(home_positions);
}

void TelemetryServiceImplTest::checkSendsHomePositions(const std::vector<Position> &home_positions)
void TelemetryServiceImplTest::checkSendsHomePositions(const std::vector<Position> &home_positions) const
{
std::promise<void> subscription_promise;
auto subscription_future = subscription_promise.get_future();
Expand Down Expand Up @@ -393,4 +396,86 @@ TEST_F(TelemetryServiceImplTest, sendsMultipleHomePositions)
checkSendsHomePositions(home_positions);
}

TEST_F(TelemetryServiceImplTest, registersToTelemetryInAirAsync)
{
EXPECT_CALL(*_telemetry, in_air_async(_))
.Times(1);

std::vector<bool> in_air_events;
auto in_air_stream_future = subscribeInAirAsync(in_air_events);

_telemetry_service->stop();
in_air_stream_future.wait();
}

std::future<void> TelemetryServiceImplTest::subscribeInAirAsync(std::vector<bool> &in_air_events) const
{
return std::async(std::launch::async, [&]() {
grpc::ClientContext context;
dronecore::rpc::telemetry::SubscribeInAirRequest request;
auto response_reader = _stub->SubscribeInAir(&context, request);

dronecore::rpc::telemetry::InAirResponse response;
while (response_reader->Read(&response)) {
auto is_in_air = response.is_in_air();
in_air_events.push_back(is_in_air);
}

response_reader->Finish();
});
}

TEST_F(TelemetryServiceImplTest, doesNotSendInAirIfCallbackNotCalled)
{
std::vector<bool> in_air_events;
auto in_air_stream_future = subscribeInAirAsync(in_air_events);

_telemetry_service->stop();
in_air_stream_future.wait();

EXPECT_EQ(0, in_air_events.size());
}

TEST_F(TelemetryServiceImplTest, sendsOneInAirEvent)
{
std::vector<bool> in_air_events;
in_air_events.push_back(generateRandomBool());

checkSendsInAirEvents(in_air_events);
}

void TelemetryServiceImplTest::checkSendsInAirEvents(const std::vector<bool> &in_air_events) const
{
std::promise<void> subscription_promise;
auto subscription_future = subscription_promise.get_future();
dronecore::Telemetry::in_air_callback_t in_air_callback;
EXPECT_CALL(*_telemetry, in_air_async(_))
.WillOnce(SaveCallback(&in_air_callback, &subscription_promise));

std::vector<bool> received_in_air_events;
auto in_air_stream_future = subscribeInAirAsync(received_in_air_events);
subscription_future.wait();
for (const auto is_in_air : in_air_events) {
in_air_callback(is_in_air);
}
_telemetry_service->stop();
in_air_stream_future.wait();

ASSERT_EQ(in_air_events.size(), received_in_air_events.size());
for (size_t i = 0; i < in_air_events.size(); i++) {
EXPECT_EQ(in_air_events.at(i), received_in_air_events.at(i));
}
}

TEST_F(TelemetryServiceImplTest, sendsMultipleInAirEvents)
{
std::vector<bool> in_air_events;

for (int i = 0; i < 10; i++) {
in_air_events.push_back(generateRandomBool());
}

checkSendsInAirEvents(in_air_events);
}

} // namespace
1 change: 1 addition & 0 deletions plugins/telemetry/mocks/telemetry_mock.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ class MockTelemetry
MOCK_CONST_METHOD1(position_async, void(Telemetry::position_callback_t));
MOCK_CONST_METHOD1(health_async, void(Telemetry::health_callback_t));
MOCK_CONST_METHOD1(home_position_async, void(Telemetry::position_callback_t));
MOCK_CONST_METHOD1(in_air_async, void(Telemetry::in_air_callback_t));
};

} // namespace testing
Expand Down

0 comments on commit e1a34af

Please sign in to comment.