diff options
author | Devtools Arcadia <arcadia-devtools@yandex-team.ru> | 2022-02-07 18:08:42 +0300 |
---|---|---|
committer | Devtools Arcadia <arcadia-devtools@mous.vla.yp-c.yandex.net> | 2022-02-07 18:08:42 +0300 |
commit | 1110808a9d39d4b808aef724c861a2e1a38d2a69 (patch) | |
tree | e26c9fed0de5d9873cce7e00bc214573dc2195b7 /library/cpp/testing/unittest/tests_data.h | |
download | ydb-1110808a9d39d4b808aef724c861a2e1a38d2a69.tar.gz |
intermediate changes
ref:cde9a383711a11544ce7e107a78147fb96cc4029
Diffstat (limited to 'library/cpp/testing/unittest/tests_data.h')
-rw-r--r-- | library/cpp/testing/unittest/tests_data.h | 54 |
1 files changed, 54 insertions, 0 deletions
diff --git a/library/cpp/testing/unittest/tests_data.h b/library/cpp/testing/unittest/tests_data.h new file mode 100644 index 0000000000..6536bc1ae6 --- /dev/null +++ b/library/cpp/testing/unittest/tests_data.h @@ -0,0 +1,54 @@ +#pragma once + +#include <library/cpp/testing/common/env.h> + +#include <util/generic/noncopyable.h> +#include <util/generic/ptr.h> +#include <util/generic/string.h> +#include <util/network/sock.h> + +class TInet6StreamSocket; + +// set two options: SO_REUSEADDR and SO_REUSEPORT, both are required for +// correct implementation of TPortManager because of different operating systems +// incompatibility: singe SO_REUSEADDR is enough for Linux, but not enough for Darwin +template <class TSocketType> +void SetReuseAddressAndPort(const TSocketType& sock) { + const int retAddr = SetSockOpt(sock, SOL_SOCKET, SO_REUSEADDR, 1); + if (retAddr < 0) { + ythrow yexception() << "can't set SO_REUSEADDR: " << LastSystemErrorText(-retAddr); + } + +#ifdef SO_REUSEPORT + const int retPort = SetSockOpt(sock, SOL_SOCKET, SO_REUSEPORT, 1); + if (retPort < 0) { + ythrow yexception() << "can't set SO_REUSEPORT: " << LastSystemErrorText(-retPort); + } +#endif +} + +class TPortManager: public TNonCopyable { +public: + TPortManager(bool reservePortsForCurrentTest = true); + ~TPortManager(); + + // Gets free TCP port + ui16 GetPort(ui16 port = 0); + + // Gets free TCP port + ui16 GetTcpPort(ui16 port = 0); + + // Gets free UDP port + ui16 GetUdpPort(ui16 port = 0); + + // Gets one free port for use in both TCP and UDP protocols + ui16 GetTcpAndUdpPort(ui16 port = 0); + + ui16 GetPortsRange(const ui16 startPort, const ui16 range); + +private: + class TPortManagerImpl; + THolder<TPortManagerImpl> Impl_; +}; + +ui16 GetRandomPort(); |