Skip to content

Commit

Permalink
Add client connection helper and add destruct tests with connections.
Browse files Browse the repository at this point in the history
  • Loading branch information
abeltrano committed Dec 1, 2023
1 parent c59aaa1 commit c20edb6
Show file tree
Hide file tree
Showing 4 changed files with 63 additions and 26 deletions.
1 change: 1 addition & 0 deletions tests/unit/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ add_executable(netremote-test-unit)

target_sources(netremote-test-unit
PRIVATE
${CMAKE_CURRENT_LIST_DIR}/TestNetRemoteCommon.cxx
${CMAKE_CURRENT_LIST_DIR}/TestNetRemoteServer.cxx
${CMAKE_CURRENT_LIST_DIR}/TestNetRemoteServiceClient.cxx
)
Expand Down
24 changes: 24 additions & 0 deletions tests/unit/TestNetRemoteCommon.cxx
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@

#include <catch2/catch_test_macros.hpp>
#include <grpcpp/create_channel.h>

#include "TestNetRemoteCommon.hxx"

using namespace Microsoft::Net::Remote;
using namespace Microsoft::Net::Remote::Test;
using namespace Microsoft::Net::Remote::Service;

std::vector<std::tuple<std::shared_ptr<grpc::Channel>, std::unique_ptr<NetRemote::Stub>>> Microsoft::Net::Remote::Test::EstablishClientConnections(std::size_t numConnectionsToEstablish)
{
std::vector<std::tuple<std::shared_ptr<grpc::Channel>, std::unique_ptr<NetRemote::Stub>>> clients(numConnectionsToEstablish);
std::ranges::transform(clients, std::begin(clients), [&](auto&&)
{
auto channel = grpc::CreateChannel(RemoteServiceAddressHttp, grpc::InsecureChannelCredentials());
auto client = NetRemote::NewStub(channel);
REQUIRE(channel->WaitForConnected(std::chrono::system_clock::now() + RemoteServiceConnectionTimeout));

return std::make_tuple(std::move(channel), std::move(client));
});

return clients;
}
24 changes: 24 additions & 0 deletions tests/unit/TestNetRemoteCommon.hxx
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,39 @@
#ifndef TEST_NET_REMOTE_COMMON_HXX
#define TEST_NET_REMOTE_COMMON_HXX

#include <algorithm>
#include <chrono>
#include <memory>
#include <ranges>
#include <string_view>
#include <tuple>
#include <vector>

#include <NetRemoteService.grpc.pb.h>

namespace Microsoft::Net::Remote::Test
{
using namespace std::chrono_literals;

/**
* @brief Default server address (insecure).
*/
constexpr auto RemoteServiceAddressHttp = "localhost:5047";

/**
* @brief Default client connection timeout.
*/
constexpr auto RemoteServiceConnectionTimeout = 3s;

/**
* @brief Establish the specified number of connections to a netremote server
* with default address and credentials.
*
* @param numConnectionsToEstablish The number of client connections to establish.
* @return std::vector<std::tuple<std::shared_ptr<grpc::Channel>, std::unique_ptr<Microsoft::Net::Remote::Service::NetRemote::Stub>>>
*/
std::vector<std::tuple<std::shared_ptr<grpc::Channel>, std::unique_ptr<Microsoft::Net::Remote::Service::NetRemote::Stub>>> EstablishClientConnections(std::size_t numConnectionsToEstablish);

} // namespace Micosoft::Net::Remote::Test

#endif // TEST_NET_REMOTE_COMMON_HXX
40 changes: 14 additions & 26 deletions tests/unit/TestNetRemoteServer.cxx
Original file line number Diff line number Diff line change
@@ -1,14 +1,9 @@

#include <algorithm>
#include <optional>
#include <ranges>
#include <tuple>
#include <vector>

#include <catch2/catch_test_macros.hpp>
#include <catch2/generators/catch_generators_range.hpp>
#include <grpcpp/create_channel.h>
#include <magic_enum.hpp>
#include <microsoft/net/remote/NetRemoteServer.hxx>
#include <NetRemoteService.grpc.pb.h>

Expand All @@ -27,16 +22,24 @@ TEST_CASE("Create a NetRemoteServer instance", "[basic][rpc][remote]")
TEST_CASE("Destroy a NetRemoteServer instance", "[basic][rpc][remote]")
{
using namespace Microsoft::Net::Remote;
using namespace Microsoft::Net::Remote::Service;

std::optional<NetRemoteServer> server{ Test::RemoteServiceAddressHttp };
server->Run();

SECTION("Destroy doesn't cause a crash")
{
std::optional<NetRemoteServer> server{ Test::RemoteServiceAddressHttp };
REQUIRE_NOTHROW(server.reset());
}

SECTION("Destroy doesn't cause a crash with no connected clients")
SECTION("Destroy doesn't cause a crash with connected clients")
{
std::optional<NetRemoteServer> server{ Test::RemoteServiceAddressHttp };
// Vary the number of connected clients and validate test section for each.
const auto numClientsToCreate = Catch::Generators::range(1, 2);

// Establish client connections to the server.
const auto clients = Test::EstablishClientConnections(static_cast<std::size_t>(numClientsToCreate.get()));

REQUIRE_NOTHROW(server.reset());
}
}
Expand Down Expand Up @@ -77,15 +80,7 @@ TEST_CASE("NetRemoteServer shuts down correctly", "[basic][rpc][remote]")
const auto numClientsToCreate = Catch::Generators::range(1, 3);

// Establish client connections to the server.
std::vector<std::tuple<std::shared_ptr<grpc::Channel>, std::unique_ptr<NetRemote::Stub>>> clients(static_cast<std::size_t>(numClientsToCreate.get()));
std::ranges::transform(clients, std::begin(clients), [&](auto&&)
{
auto channel = grpc::CreateChannel(Test::RemoteServiceAddressHttp, grpc::InsecureChannelCredentials());
auto client = NetRemote::NewStub(channel);
REQUIRE(channel->WaitForConnected(std::chrono::system_clock::now() + Test::RemoteServiceConnectionTimeout));

return std::make_tuple(channel, std::move(client));
});
const auto clients = Test::EstablishClientConnections(static_cast<std::size_t>(numClientsToCreate.get()));

// Stop the server.
REQUIRE_NOTHROW(server.Stop());
Expand All @@ -108,15 +103,8 @@ TEST_CASE("NetRemoteServer shuts down correctly", "[basic][rpc][remote]")
// Vary the number of connected clients and validate test section for each.
const auto numClientsToCreate = Catch::Generators::range(1, 3);

std::vector<std::tuple<std::shared_ptr<grpc::Channel>, std::unique_ptr<NetRemote::Stub>>> clients(static_cast<std::size_t>(numClientsToCreate.get()));
std::ranges::transform(clients, std::begin(clients), [&](auto&&)
{
auto channel = grpc::CreateChannel(Test::RemoteServiceAddressHttp, grpc::InsecureChannelCredentials());
auto client = NetRemote::NewStub(channel);
REQUIRE(channel->WaitForConnected(std::chrono::system_clock::now() + Test::RemoteServiceConnectionTimeout));

return std::make_tuple(channel, std::move(client));
});
// Establish client connections to the server.
const auto clients = Test::EstablishClientConnections(static_cast<std::size_t>(numClientsToCreate.get()));

REQUIRE_NOTHROW(server.Stop());
REQUIRE(server.GetGrpcServer() == nullptr);
Expand Down

0 comments on commit c20edb6

Please sign in to comment.