test_port_manager.cpp 1.1 KB

123456789101112131415161718192021222324252627282930313233343536
  1. #include <library/cpp/testing/unittest/registar.h>
  2. #include <library/cpp/testing/unittest/tests_data.h>
  3. bool IsFreePort(ui16 port) {
  4. TInet6StreamSocket sock;
  5. TSockAddrInet6 addr("::", port);
  6. Y_ENSURE(SetSockOpt(sock, SOL_SOCKET, SO_REUSEADDR, 1) == 0);
  7. SetReuseAddressAndPort(sock);
  8. if (sock.Bind(&addr) == 0) {
  9. return true;
  10. }
  11. return false;
  12. }
  13. void get_port_ranges() {
  14. for (int i = 1; i < 10; ++i) {
  15. TPortManager pm;
  16. ui16 port = pm.GetPortsRange(1024, i);
  17. for (int p = port; p < port + i; ++p) {
  18. UNIT_ASSERT(IsFreePort(p));
  19. }
  20. }
  21. }
  22. Y_UNIT_TEST_SUITE(TestTPortManager) {
  23. Y_UNIT_TEST(ParallelRun0) {get_port_ranges();}
  24. Y_UNIT_TEST(ParallelRun1) {get_port_ranges();}
  25. Y_UNIT_TEST(ParallelRun2) {get_port_ranges();}
  26. Y_UNIT_TEST(ParallelRun3) {get_port_ranges();}
  27. Y_UNIT_TEST(ParallelRun4) {get_port_ranges();}
  28. Y_UNIT_TEST(ParallelRun5) {get_port_ranges();}
  29. Y_UNIT_TEST(ParallelRun6) {get_port_ranges();}
  30. Y_UNIT_TEST(ParallelRun7) {get_port_ranges();}
  31. Y_UNIT_TEST(ParallelRun8) {get_port_ranges();}
  32. Y_UNIT_TEST(ParallelRun9) {get_port_ranges();}
  33. }