aboutsummaryrefslogtreecommitdiffstats
path: root/library/cpp/testing/common/network.h
blob: 66fb2b0c403719bf989b71e492b03987ac194158 (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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
#pragma once 
 
#include <util/generic/ptr.h> 
#include <util/generic/vector.h> 
 
namespace NTesting { 
 
    //@brief network port holder interface 
    class IPort { 
    public: 
        virtual ~IPort() {} 
 
        virtual ui16 Get() = 0; 
    }; 
 
    class TPortHolder : private THolder<IPort> { 
        using TBase = THolder<IPort>; 
    public: 
        using TBase::TBase; 
        using TBase::Release; 
        using TBase::Reset; 
 
        operator ui16() const& { 
            return (*this)->Get(); 
        } 
 
        operator ui16() const&& = delete; 
    }; 
 
    IOutputStream& operator<<(IOutputStream& out, const TPortHolder& port);

    //@brief Get first free port. 
    [[nodiscard]] TPortHolder GetFreePort(); 
 
    namespace NLegacy { 
        // Do not use this method, it needs only for TPortManager from unittests. 
        // Returns continuous sequence of the specified number of ports. 
        [[nodiscard]] TVector<TPortHolder> GetFreePortsRange(size_t count); 
    } 
 
    //@brief helper class for inheritance 
    struct TFreePortOwner { 
        TFreePortOwner() : Port_(GetFreePort()) {} 
 
        ui16 GetPort() const { 
            return Port_; 
        } 
 
    private: 
        TPortHolder Port_; 
    }; 
}