Skip to content

Commit c20edb6

Browse files
committed
Add client connection helper and add destruct tests with connections.
1 parent c59aaa1 commit c20edb6

File tree

4 files changed

+63
-26
lines changed

4 files changed

+63
-26
lines changed

tests/unit/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ add_executable(netremote-test-unit)
99

1010
target_sources(netremote-test-unit
1111
PRIVATE
12+
${CMAKE_CURRENT_LIST_DIR}/TestNetRemoteCommon.cxx
1213
${CMAKE_CURRENT_LIST_DIR}/TestNetRemoteServer.cxx
1314
${CMAKE_CURRENT_LIST_DIR}/TestNetRemoteServiceClient.cxx
1415
)

tests/unit/TestNetRemoteCommon.cxx

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
2+
#include <catch2/catch_test_macros.hpp>
3+
#include <grpcpp/create_channel.h>
4+
5+
#include "TestNetRemoteCommon.hxx"
6+
7+
using namespace Microsoft::Net::Remote;
8+
using namespace Microsoft::Net::Remote::Test;
9+
using namespace Microsoft::Net::Remote::Service;
10+
11+
std::vector<std::tuple<std::shared_ptr<grpc::Channel>, std::unique_ptr<NetRemote::Stub>>> Microsoft::Net::Remote::Test::EstablishClientConnections(std::size_t numConnectionsToEstablish)
12+
{
13+
std::vector<std::tuple<std::shared_ptr<grpc::Channel>, std::unique_ptr<NetRemote::Stub>>> clients(numConnectionsToEstablish);
14+
std::ranges::transform(clients, std::begin(clients), [&](auto&&)
15+
{
16+
auto channel = grpc::CreateChannel(RemoteServiceAddressHttp, grpc::InsecureChannelCredentials());
17+
auto client = NetRemote::NewStub(channel);
18+
REQUIRE(channel->WaitForConnected(std::chrono::system_clock::now() + RemoteServiceConnectionTimeout));
19+
20+
return std::make_tuple(std::move(channel), std::move(client));
21+
});
22+
23+
return clients;
24+
}

tests/unit/TestNetRemoteCommon.hxx

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,39 @@
22
#ifndef TEST_NET_REMOTE_COMMON_HXX
33
#define TEST_NET_REMOTE_COMMON_HXX
44

5+
#include <algorithm>
56
#include <chrono>
7+
#include <memory>
8+
#include <ranges>
69
#include <string_view>
10+
#include <tuple>
11+
#include <vector>
12+
13+
#include <NetRemoteService.grpc.pb.h>
714

815
namespace Microsoft::Net::Remote::Test
916
{
1017
using namespace std::chrono_literals;
1118

19+
/**
20+
* @brief Default server address (insecure).
21+
*/
1222
constexpr auto RemoteServiceAddressHttp = "localhost:5047";
23+
24+
/**
25+
* @brief Default client connection timeout.
26+
*/
1327
constexpr auto RemoteServiceConnectionTimeout = 3s;
28+
29+
/**
30+
* @brief Establish the specified number of connections to a netremote server
31+
* with default address and credentials.
32+
*
33+
* @param numConnectionsToEstablish The number of client connections to establish.
34+
* @return std::vector<std::tuple<std::shared_ptr<grpc::Channel>, std::unique_ptr<Microsoft::Net::Remote::Service::NetRemote::Stub>>>
35+
*/
36+
std::vector<std::tuple<std::shared_ptr<grpc::Channel>, std::unique_ptr<Microsoft::Net::Remote::Service::NetRemote::Stub>>> EstablishClientConnections(std::size_t numConnectionsToEstablish);
37+
1438
} // namespace Micosoft::Net::Remote::Test
1539

1640
#endif // TEST_NET_REMOTE_COMMON_HXX

tests/unit/TestNetRemoteServer.cxx

Lines changed: 14 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,9 @@
11

2-
#include <algorithm>
32
#include <optional>
4-
#include <ranges>
5-
#include <tuple>
6-
#include <vector>
73

84
#include <catch2/catch_test_macros.hpp>
95
#include <catch2/generators/catch_generators_range.hpp>
106
#include <grpcpp/create_channel.h>
11-
#include <magic_enum.hpp>
127
#include <microsoft/net/remote/NetRemoteServer.hxx>
138
#include <NetRemoteService.grpc.pb.h>
149

@@ -27,16 +22,24 @@ TEST_CASE("Create a NetRemoteServer instance", "[basic][rpc][remote]")
2722
TEST_CASE("Destroy a NetRemoteServer instance", "[basic][rpc][remote]")
2823
{
2924
using namespace Microsoft::Net::Remote;
25+
using namespace Microsoft::Net::Remote::Service;
26+
27+
std::optional<NetRemoteServer> server{ Test::RemoteServiceAddressHttp };
28+
server->Run();
3029

3130
SECTION("Destroy doesn't cause a crash")
3231
{
33-
std::optional<NetRemoteServer> server{ Test::RemoteServiceAddressHttp };
3432
REQUIRE_NOTHROW(server.reset());
3533
}
3634

37-
SECTION("Destroy doesn't cause a crash with no connected clients")
35+
SECTION("Destroy doesn't cause a crash with connected clients")
3836
{
39-
std::optional<NetRemoteServer> server{ Test::RemoteServiceAddressHttp };
37+
// Vary the number of connected clients and validate test section for each.
38+
const auto numClientsToCreate = Catch::Generators::range(1, 2);
39+
40+
// Establish client connections to the server.
41+
const auto clients = Test::EstablishClientConnections(static_cast<std::size_t>(numClientsToCreate.get()));
42+
4043
REQUIRE_NOTHROW(server.reset());
4144
}
4245
}
@@ -77,15 +80,7 @@ TEST_CASE("NetRemoteServer shuts down correctly", "[basic][rpc][remote]")
7780
const auto numClientsToCreate = Catch::Generators::range(1, 3);
7881

7982
// Establish client connections to the server.
80-
std::vector<std::tuple<std::shared_ptr<grpc::Channel>, std::unique_ptr<NetRemote::Stub>>> clients(static_cast<std::size_t>(numClientsToCreate.get()));
81-
std::ranges::transform(clients, std::begin(clients), [&](auto&&)
82-
{
83-
auto channel = grpc::CreateChannel(Test::RemoteServiceAddressHttp, grpc::InsecureChannelCredentials());
84-
auto client = NetRemote::NewStub(channel);
85-
REQUIRE(channel->WaitForConnected(std::chrono::system_clock::now() + Test::RemoteServiceConnectionTimeout));
86-
87-
return std::make_tuple(channel, std::move(client));
88-
});
83+
const auto clients = Test::EstablishClientConnections(static_cast<std::size_t>(numClientsToCreate.get()));
8984

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

111-
std::vector<std::tuple<std::shared_ptr<grpc::Channel>, std::unique_ptr<NetRemote::Stub>>> clients(static_cast<std::size_t>(numClientsToCreate.get()));
112-
std::ranges::transform(clients, std::begin(clients), [&](auto&&)
113-
{
114-
auto channel = grpc::CreateChannel(Test::RemoteServiceAddressHttp, grpc::InsecureChannelCredentials());
115-
auto client = NetRemote::NewStub(channel);
116-
REQUIRE(channel->WaitForConnected(std::chrono::system_clock::now() + Test::RemoteServiceConnectionTimeout));
117-
118-
return std::make_tuple(channel, std::move(client));
119-
});
106+
// Establish client connections to the server.
107+
const auto clients = Test::EstablishClientConnections(static_cast<std::size_t>(numClientsToCreate.get()));
120108

121109
REQUIRE_NOTHROW(server.Stop());
122110
REQUIRE(server.GetGrpcServer() == nullptr);

0 commit comments

Comments
 (0)