-
-
Notifications
You must be signed in to change notification settings - Fork 504
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
backend: add connection_initiator and corresponding tests
- Loading branch information
1 parent
f27110e
commit d693b1d
Showing
9 changed files
with
236 additions
and
79 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
#include <gmock/gmock.h> | ||
|
||
#include "connection_result.h" | ||
|
||
namespace dronecore { | ||
namespace testing { | ||
|
||
typedef std::function<void(uint64_t uuid)> event_callback_t; | ||
|
||
class MockDroneCore | ||
{ | ||
public: | ||
MOCK_CONST_METHOD1(add_udp_connection, ConnectionResult(int local_port_number)); | ||
MOCK_CONST_METHOD1(register_on_discover, void(event_callback_t)); | ||
MOCK_CONST_METHOD1(register_on_timeout, void(event_callback_t)); | ||
}; | ||
|
||
} // testing | ||
} // dronecore |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
#include <future> | ||
|
||
#include "connection_result.h" | ||
#include "log.h" | ||
|
||
namespace dronecore { | ||
namespace backend { | ||
|
||
template <typename DroneCore> | ||
class ConnectionInitiator | ||
{ | ||
public: | ||
ConnectionInitiator() {} | ||
~ConnectionInitiator() {} | ||
|
||
bool start(DroneCore &dc, const int port) | ||
{ | ||
init_mutex(); | ||
init_timeout_logging(dc); | ||
|
||
_discovery_future = wrapped_register_on_discover(dc); | ||
|
||
if (!add_udp_connection(dc, port)) { | ||
return false; | ||
} | ||
|
||
return true; | ||
} | ||
|
||
void wait() | ||
{ | ||
_discovery_future.wait(); | ||
} | ||
|
||
private: | ||
void init_mutex() | ||
{ | ||
_discovery_promise = std::make_shared<std::promise<uint64_t>>(); | ||
} | ||
|
||
void init_timeout_logging(DroneCore &dc) const | ||
{ | ||
dc.register_on_timeout([](uint64_t uuid) { | ||
LogInfo() << "Device timed out [UUID: " << uuid << "]"; | ||
}); | ||
} | ||
|
||
bool add_udp_connection(DroneCore &dc, const int port) | ||
{ | ||
dronecore::ConnectionResult connection_result = dc.add_udp_connection(port); | ||
|
||
if (connection_result != ConnectionResult::SUCCESS) { | ||
LogErr() << "Connection failed: " << connection_result_str(connection_result); | ||
return false; | ||
} | ||
|
||
return true; | ||
} | ||
|
||
std::future<uint64_t> wrapped_register_on_discover(DroneCore &dc) | ||
{ | ||
auto future = _discovery_promise->get_future(); | ||
|
||
LogInfo() << "Waiting to discover device..."; | ||
|
||
dc.register_on_discover([this](uint64_t uuid) { | ||
std::call_once(_discovery_flag, [this, uuid]() { | ||
LogInfo() << "Device discovered [UUID: " << uuid << "]"; | ||
_discovery_promise->set_value(uuid); | ||
}); | ||
}); | ||
|
||
return future; | ||
} | ||
|
||
std::once_flag _discovery_flag; | ||
std::shared_ptr<std::promise<uint64_t>> _discovery_promise; | ||
std::future<uint64_t> _discovery_future; | ||
}; | ||
|
||
} // backend | ||
} // dronecore |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
cmake_minimum_required(VERSION 3.1) | ||
|
||
add_executable(unit_tests_backend | ||
connection_initiator_test.cpp | ||
) | ||
|
||
set_target_properties(unit_tests_backend PROPERTIES COMPILE_FLAGS ${warnings}) | ||
|
||
target_include_directories(unit_tests_backend | ||
PRIVATE | ||
${CMAKE_CURRENT_SOURCE_DIR}/../src | ||
) | ||
|
||
target_link_libraries(unit_tests_backend | ||
backend | ||
gtest | ||
gmock | ||
gmock_main | ||
) | ||
|
||
add_test(unit_tests unit_tests_backend) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
#include <future> | ||
#include <gtest/gtest.h> | ||
|
||
#include "connection_initiator.h" | ||
#include "mocks/dronecore_mock.h" | ||
|
||
using testing::_; | ||
using testing::NiceMock; | ||
|
||
using event_callback_t = dronecore::testing::event_callback_t; | ||
using MockDroneCore = NiceMock<dronecore::testing::MockDroneCore>; | ||
using ConnectionInitiator = dronecore::backend::ConnectionInitiator<MockDroneCore>; | ||
|
||
static constexpr auto ARBITRARY_PORT = 1291; | ||
static constexpr auto ARBITRARY_UUID = 1492; | ||
|
||
ACTION_P(SaveCallback, event_callback) | ||
{ | ||
*event_callback = arg0; | ||
} | ||
|
||
TEST(ConnectionInitiator, registerDiscoverIsCalledExactlyOnce) | ||
{ | ||
ConnectionInitiator initiator; | ||
MockDroneCore dc; | ||
EXPECT_CALL(dc, register_on_discover(_)) | ||
.Times(1); | ||
|
||
initiator.start(dc, ARBITRARY_PORT); | ||
} | ||
|
||
TEST(ConnectionInitiator, startAddsUDPConnection) | ||
{ | ||
ConnectionInitiator initiator; | ||
MockDroneCore dc; | ||
|
||
EXPECT_CALL(dc, add_udp_connection(_)); | ||
|
||
initiator.start(dc, ARBITRARY_PORT); | ||
} | ||
|
||
TEST(ConnectionInitiator, startHangsUntilDeviceDiscovered) | ||
{ | ||
ConnectionInitiator initiator; | ||
MockDroneCore dc; | ||
event_callback_t discover_callback; | ||
EXPECT_CALL(dc, register_on_discover(_)) | ||
.WillOnce(SaveCallback(&discover_callback)); | ||
|
||
auto async_future = std::async(std::launch::async, [&initiator, &dc]() { | ||
initiator.start(dc, ARBITRARY_PORT); | ||
initiator.wait(); | ||
}); | ||
|
||
EXPECT_TRUE(async_future.wait_for(std::chrono::milliseconds(100)) == std::future_status::timeout) << | ||
"Call to 'start(...)' should hang until 'discover_callback(...)' is actually called!"; | ||
discover_callback(ARBITRARY_UUID); | ||
} | ||
|
||
TEST(ConnectionInitiator, connectionDetectedIfDiscoverCallbackCalledBeforeWait) | ||
{ | ||
ConnectionInitiator initiator; | ||
MockDroneCore dc; | ||
event_callback_t discover_callback; | ||
EXPECT_CALL(dc, register_on_discover(_)) | ||
.WillOnce(SaveCallback(&discover_callback)); | ||
|
||
initiator.start(dc, ARBITRARY_PORT); | ||
discover_callback(ARBITRARY_UUID); | ||
initiator.wait(); | ||
} | ||
|
||
TEST(ConnectionInitiator, doesNotCrashIfDiscoverCallbackCalledMoreThanOnce) | ||
{ | ||
ConnectionInitiator initiator; | ||
MockDroneCore dc; | ||
event_callback_t discover_callback; | ||
EXPECT_CALL(dc, register_on_discover(_)) | ||
.WillOnce(SaveCallback(&discover_callback)); | ||
|
||
initiator.start(dc, ARBITRARY_PORT); | ||
discover_callback(ARBITRARY_UUID); | ||
discover_callback(ARBITRARY_UUID); | ||
} |