aboutsummaryrefslogtreecommitdiffstats
path: root/yt/cpp/mapreduce/http/ut/connection_pool_ut.cpp
blob: 90196246c5621c8e3165c7465b3a53956e1c9186 (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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
#include "simple_server.h"

#include <yt/cpp/mapreduce/http/http.h>

#include <yt/cpp/mapreduce/interface/config.h>

#include <library/cpp/threading/future/async.h>

#include <library/cpp/http/io/stream.h>

#include <library/cpp/testing/gtest/gtest.h>

#include <library/cpp/testing/common/network.h>

#include <util/string/builder.h>
#include <util/stream/tee.h>
#include <util/system/thread.h>

using namespace NYT;

namespace {
    void ParseFirstLine(const TString firstLine, TString& method, TString& host , ui64& port, TString& command)
    {
        size_t idx = firstLine.find_first_of(' ');
        method = firstLine.substr(0, idx);
        size_t idx2 = firstLine.find_first_of(':', idx + 1);
        host = firstLine.substr(idx + 1, idx2 - idx - 1);
        idx = firstLine.find_first_of('/', idx2 + 1);
        port = std::atoi(firstLine.substr(idx2 + 1, idx - idx2 - 1).c_str());
        idx2 = firstLine.find_first_of(' ', idx + 1);
        command = firstLine.substr(idx, idx2 - idx);
    }
} // namespace

THolder<TSimpleServer> CreateSimpleHttpServer()
{
    auto port = NTesting::GetFreePort();
    return MakeHolder<TSimpleServer>(
        port,
        [] (IInputStream* input, IOutputStream* output) {
            try {
                while (true) {
                    THttpInput httpInput(input);
                    httpInput.ReadAll();

                    THttpOutput httpOutput(output);
                    httpOutput.EnableKeepAlive(true);
                    httpOutput << "HTTP/1.1 200 OK\r\n";
                    httpOutput << "\r\n";
                    for (size_t i = 0; i != 10000; ++i) {
                        httpOutput << "The grass was greener";
                    }
                    httpOutput.Flush();
                }
            } catch (const std::exception&) {
            }
        });
}

THolder<TSimpleServer> CreateProxyHttpServer()
{
    auto port = NTesting::GetFreePort();
    return MakeHolder<TSimpleServer>(
        port,
        [] (IInputStream* input, IOutputStream* output) {
            try {
                while (true) {
                    THttpInput httpInput(input);
                    const TString inputStr = httpInput.FirstLine();
                    auto headers = httpInput.Headers();
                    TString method, command, host;
                    ui64 port;
                    ParseFirstLine(inputStr, method, host, port, command);

                    THttpRequest request;
                    const TString hostName = ::TStringBuilder() << host << ":" << port;
                    request.Connect(hostName);
                    auto header = THttpHeader(method, command);
                    request.StartRequest(header);
                    request.FinishRequest();
                    auto res = request.GetResponseStream();
                    THttpOutput httpOutput(output);
                    httpOutput.EnableKeepAlive(true);
                    auto strRes = res->ReadAll();
                    httpOutput << "HTTP/1.1 200 OK\r\n";
                    httpOutput << "\r\n";
                    httpOutput << strRes;
                    httpOutput.Flush();
                }
            } catch (const std::exception&) {
            }
        });
}


class TConnectionPoolConfigGuard
{
public:
    TConnectionPoolConfigGuard(int newSize)
    {
        OldValue_ = TConfig::Get()->ConnectionPoolSize;
        TConfig::Get()->ConnectionPoolSize = newSize;
    }

    ~TConnectionPoolConfigGuard()
    {
        TConfig::Get()->ConnectionPoolSize = OldValue_;
    }

private:
    int OldValue_;
};

class TFuncThread
    : public ISimpleThread
{
public:
    using TFunc = std::function<void()>;

public:
    TFuncThread(const TFunc& func)
        : Func_(func)
    { }

    void* ThreadProc() noexcept override {
        Func_();
        return nullptr;
    }

private:
    TFunc Func_;
};

TEST(TConnectionPool, TestReleaseUnread)
{
    auto simpleServer = CreateSimpleHttpServer();

    const TString hostName = ::TStringBuilder() << "localhost:" << simpleServer->GetPort();

    for (size_t i = 0; i != 10; ++i) {
        THttpRequest request;
        request.Connect(hostName);
        request.StartRequest(THttpHeader("GET", "foo"));
        request.FinishRequest();
        request.GetResponseStream();
    }
}

TEST(TConnectionPool, TestProxy)
{
    auto simpleServer = CreateSimpleHttpServer();
    auto simpleServer2 = CreateProxyHttpServer();

    const TString hostName = ::TStringBuilder() << "localhost:" << simpleServer->GetPort();
    const TString hostName2 = ::TStringBuilder() << "localhost:" << simpleServer2->GetPort();

    for (size_t i = 0; i != 10; ++i) {
        THttpRequest request;
        request.Connect(hostName2);
        auto header = THttpHeader("GET", "foo");
        header.SetProxyAddress(hostName2);
        header.SetHostPort(hostName);
        request.StartRequest(header);
        request.FinishRequest();
        request.GetResponseStream();
    }
}

TEST(TConnectionPool, TestConcurrency)
{
    TConnectionPoolConfigGuard g(1);

    auto simpleServer = CreateSimpleHttpServer();
    const TString hostName = ::TStringBuilder() << "localhost:" << simpleServer->GetPort();
    auto threadPool = CreateThreadPool(20);

    const auto func = [&] {
        for (int i = 0; i != 100; ++i) {
            THttpRequest request;
            request.Connect(hostName);
            request.StartRequest(THttpHeader("GET", "foo"));
            request.FinishRequest();
            auto res = request.GetResponseStream();
            res->ReadAll();
        }
    };

    TVector<THolder<TFuncThread>> threads;
    for (int i = 0; i != 10; ++i) {
        threads.push_back(MakeHolder<TFuncThread>(func));
    };

    for (auto& t : threads) {
        t->Start();
    }
    for (auto& t : threads) {
        t->Join();
    }
}