tests_data.h 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. #pragma once
  2. #include <library/cpp/testing/common/env.h>
  3. #include <util/generic/noncopyable.h>
  4. #include <util/generic/ptr.h>
  5. #include <util/generic/string.h>
  6. #include <util/network/sock.h>
  7. class TInet6StreamSocket;
  8. // set two options: SO_REUSEADDR and SO_REUSEPORT, both are required for
  9. // correct implementation of TPortManager because of different operating systems
  10. // incompatibility: singe SO_REUSEADDR is enough for Linux, but not enough for Darwin
  11. template <class TSocketType>
  12. void SetReuseAddressAndPort(const TSocketType& sock) {
  13. const int retAddr = SetSockOpt(sock, SOL_SOCKET, SO_REUSEADDR, 1);
  14. if (retAddr < 0) {
  15. ythrow yexception() << "can't set SO_REUSEADDR: " << LastSystemErrorText(-retAddr);
  16. }
  17. #ifdef SO_REUSEPORT
  18. const int retPort = SetSockOpt(sock, SOL_SOCKET, SO_REUSEPORT, 1);
  19. if (retPort < 0) {
  20. ythrow yexception() << "can't set SO_REUSEPORT: " << LastSystemErrorText(-retPort);
  21. }
  22. #endif
  23. }
  24. class TPortManager: public TNonCopyable {
  25. public:
  26. TPortManager(bool reservePortsForCurrentTest = true);
  27. ~TPortManager();
  28. // Gets free TCP port
  29. ui16 GetPort(ui16 port = 0);
  30. // Gets free TCP port
  31. ui16 GetTcpPort(ui16 port = 0);
  32. // Gets free UDP port
  33. ui16 GetUdpPort(ui16 port = 0);
  34. // Gets one free port for use in both TCP and UDP protocols
  35. ui16 GetTcpAndUdpPort(ui16 port = 0);
  36. ui16 GetPortsRange(const ui16 startPort, const ui16 range);
  37. private:
  38. class TPortManagerImpl;
  39. THolder<TPortManagerImpl> Impl_;
  40. };
  41. ui16 GetRandomPort();