diff options
author | alexv-smirnov <alex@ydb.tech> | 2023-03-10 15:37:02 +0300 |
---|---|---|
committer | alexv-smirnov <alex@ydb.tech> | 2023-03-10 15:37:02 +0300 |
commit | 394219e982f65dd1ab4e4511051f4c97011c8712 (patch) | |
tree | d6a9c3984d7bac6e9564fa451bd28d6edcc063e3 /library/cpp/testing/common/ut/network_ut.cpp | |
parent | 7d576663c816bfaa02dcce5b6dfb8cfc3c7dec67 (diff) | |
download | ydb-394219e982f65dd1ab4e4511051f4c97011c8712.tar.gz |
Add GetPort(port) support to maintain compatibility with unittest TPortManager
Singleton did not allow reinitialisation, so in existing tests this line https://a.yandex-team.ru/arcadia/library/cpp/testing/common/ut/network_ut.cpp?rev=rXXXXXX#L45 did not have any effect. The tests worked just because in both tests the env var PORT_SYNC_PATH was the same and its changes did not affect the tests anyway.
As we need to change the env var NO_RANDOM_PORTS to run GetPort( port ) test, I had to make a reinitialisation method, which is now being called from inside the constructor, causing its double invocation during the tests. I could not find a better solution for Singleton(
Diffstat (limited to 'library/cpp/testing/common/ut/network_ut.cpp')
-rw-r--r-- | library/cpp/testing/common/ut/network_ut.cpp | 54 |
1 files changed, 53 insertions, 1 deletions
diff --git a/library/cpp/testing/common/ut/network_ut.cpp b/library/cpp/testing/common/ut/network_ut.cpp index 6a40775fd9..2016e26b09 100644 --- a/library/cpp/testing/common/ut/network_ut.cpp +++ b/library/cpp/testing/common/ut/network_ut.cpp @@ -15,7 +15,7 @@ static TTempDir TmpDir; TEST(NetworkTest, FreePort) { NTesting::TScopedEnvironment envGuard("PORT_SYNC_PATH", TmpDir.Name()); - + NTesting::InitPortManagerFromEnv(); TVector<NTesting::TPortHolder> ports(Reserve(100)); for (size_t i = 0; i < 100; ++i) { @@ -40,9 +40,61 @@ TEST(NetworkTest, FreePort) { } } +TEST(NetworkTest, FreePortWithinRanges) { + NTesting::TScopedEnvironment envGuard{{ + {"PORT_SYNC_PATH", TmpDir.Name()}, + {"VALID_PORT_RANGE", "3456:7654"}, + }}; + NTesting::InitPortManagerFromEnv(); + + for (size_t i = 0; i < 100; ++i) { + auto holder = NTesting::GetFreePort(); + ui16 port = holder; + ASSERT_GE(port, 3456u); + ASSERT_LE(port, 7654u); + } +} + +TEST(NetworkTest, GetPortRandom) { + NTesting::TScopedEnvironment envGuard{{ + {"PORT_SYNC_PATH", TmpDir.Name()}, + {"NO_RANDOM_PORTS", ""}, + }}; + NTesting::InitPortManagerFromEnv(); + + ui16 testPort = 80; // value just must be outside the assignable range + for (size_t i = 0; i < 10; ++i) { + NTesting::TPortHolder assigned = NTesting::NLegacy::GetPort(testPort); + ui16 assignedInt = assigned; + ASSERT_NE(testPort, assignedInt); + } +} + +TEST(NetworkTest, GetPortNonRandom) { + NTesting::TScopedEnvironment envGuard{{ + {"PORT_SYNC_PATH", TmpDir.Name()}, + {"NO_RANDOM_PORTS", "1"}, + }}; + NTesting::InitPortManagerFromEnv(); + + TVector<ui16> ports(Reserve(100)); // keep integers, we don't need the ports to remain allocated + + for (size_t i = 0; i < 10; ++i) { + auto portHolder = NTesting::GetFreePort(); + ports.push_back(portHolder); + } + + for (auto& testPort : ports) { + NTesting::TPortHolder assigned = NTesting::NLegacy::GetPort(testPort); + ui16 assignedInt = assigned; + ASSERT_EQ(testPort, assignedInt); + } +} + TEST(FreePortTest, FreePortsRange) { NTesting::TScopedEnvironment envGuard("PORT_SYNC_PATH", TmpDir.Name()); + NTesting::InitPortManagerFromEnv(); for (ui16 i = 2; i < 10; ++i) { TVector<NTesting::TPortHolder> ports = NTesting::NLegacy::GetFreePortsRange(i); |