aboutsummaryrefslogtreecommitdiffstats
path: root/library/cpp/testing/unittest/fat/test_port_manager.cpp
blob: f77d2e3a25c7352065bb05ed967130f76a377f23 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
#include <library/cpp/testing/unittest/registar.h>
#include <library/cpp/testing/unittest/tests_data.h>

bool IsFreePort(ui16 port) {
    TInet6StreamSocket sock;
    TSockAddrInet6 addr("::", port);
    Y_ENSURE(SetSockOpt(sock, SOL_SOCKET, SO_REUSEADDR, 1) == 0);
    SetReuseAddressAndPort(sock);
    if (sock.Bind(&addr) == 0) {
        return true;
    }
    return false;
}

void get_port_ranges() {
    for (int i = 1; i < 10; ++i) {
        TPortManager pm;
        ui16 port = pm.GetPortsRange(1024, i);
        for (int p = port; p < port + i; ++p) {
            UNIT_ASSERT(IsFreePort(p));
        }
    }
}

Y_UNIT_TEST_SUITE(TestTPortManager) {
    Y_UNIT_TEST(ParallelRun0) {get_port_ranges();}
    Y_UNIT_TEST(ParallelRun1) {get_port_ranges();}
    Y_UNIT_TEST(ParallelRun2) {get_port_ranges();}
    Y_UNIT_TEST(ParallelRun3) {get_port_ranges();}
    Y_UNIT_TEST(ParallelRun4) {get_port_ranges();}
    Y_UNIT_TEST(ParallelRun5) {get_port_ranges();}
    Y_UNIT_TEST(ParallelRun6) {get_port_ranges();}
    Y_UNIT_TEST(ParallelRun7) {get_port_ranges();}
    Y_UNIT_TEST(ParallelRun8) {get_port_ranges();}
    Y_UNIT_TEST(ParallelRun9) {get_port_ranges();}
}