tests_data.cpp 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. #include "tests_data.h"
  2. #include "registar.h"
  3. #include <library/cpp/testing/common/network.h>
  4. #include <util/system/env.h>
  5. #include <util/system/mutex.h>
  6. class TPortManager::TPortManagerImpl {
  7. public:
  8. TPortManagerImpl(bool reservePortsForCurrentTest)
  9. : EnableReservePortsForCurrentTest(reservePortsForCurrentTest)
  10. , DisableRandomPorts(!GetEnv("NO_RANDOM_PORTS").empty())
  11. {
  12. }
  13. ui16 GetPort(ui16 port) {
  14. if (port && DisableRandomPorts) {
  15. return port;
  16. }
  17. TAtomicSharedPtr<NTesting::IPort> holder(NTesting::GetFreePort().Release());
  18. ReservePortForCurrentTest(holder);
  19. TGuard<TMutex> g(Lock);
  20. ReservedPorts.push_back(holder);
  21. return holder->Get();
  22. }
  23. ui16 GetUdpPort(ui16 port) {
  24. return GetPort(port);
  25. }
  26. ui16 GetTcpPort(ui16 port) {
  27. return GetPort(port);
  28. }
  29. ui16 GetTcpAndUdpPort(ui16 port) {
  30. return GetPort(port);
  31. }
  32. ui16 GetPortsRange(const ui16 startPort, const ui16 range) {
  33. Y_UNUSED(startPort);
  34. auto ports = NTesting::NLegacy::GetFreePortsRange(range);
  35. ui16 first = ports[0];
  36. TGuard<TMutex> g(Lock);
  37. for (auto& port : ports) {
  38. ReservedPorts.emplace_back(port.Release());
  39. ReservePortForCurrentTest(ReservedPorts.back());
  40. }
  41. return first;
  42. }
  43. private:
  44. void ReservePortForCurrentTest(const TAtomicSharedPtr<NTesting::IPort>& portGuard) {
  45. if (EnableReservePortsForCurrentTest) {
  46. TTestBase* currentTest = NUnitTest::NPrivate::GetCurrentTest();
  47. if (currentTest != nullptr) {
  48. currentTest->RunAfterTest([guard = portGuard]() mutable {
  49. guard = nullptr; // remove reference for allocated port
  50. });
  51. }
  52. }
  53. }
  54. private:
  55. TMutex Lock;
  56. TVector<TAtomicSharedPtr<NTesting::IPort>> ReservedPorts;
  57. const bool EnableReservePortsForCurrentTest;
  58. const bool DisableRandomPorts;
  59. };
  60. TPortManager::TPortManager(bool reservePortsForCurrentTest)
  61. : Impl_(new TPortManagerImpl(reservePortsForCurrentTest))
  62. {
  63. }
  64. TPortManager::~TPortManager() {
  65. }
  66. ui16 TPortManager::GetPort(ui16 port) {
  67. return Impl_->GetTcpPort(port);
  68. }
  69. ui16 TPortManager::GetTcpPort(ui16 port) {
  70. return Impl_->GetTcpPort(port);
  71. }
  72. ui16 TPortManager::GetUdpPort(ui16 port) {
  73. return Impl_->GetUdpPort(port);
  74. }
  75. ui16 TPortManager::GetTcpAndUdpPort(ui16 port) {
  76. return Impl_->GetTcpAndUdpPort(port);
  77. }
  78. ui16 TPortManager::GetPortsRange(const ui16 startPort, const ui16 range) {
  79. return Impl_->GetPortsRange(startPort, range);
  80. }
  81. ui16 GetRandomPort() {
  82. TPortManager* pm = Singleton<TPortManager>(false);
  83. return pm->GetPort();
  84. }