File: threadname_test.cc

package info (click to toggle)
pistache 0.0.5%2Bds-3
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 1,420 kB
  • sloc: cpp: 19,169; ansic: 556; sh: 86; makefile: 15
file content (204 lines) | stat: -rw-r--r-- 6,382 bytes parent folder | download | duplicates (2)
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
200
201
202
203
204
/*
 * SPDX-FileCopyrightText: 2019 mohsenomidi
 *
 * SPDX-License-Identifier: Apache-2.0
 */

#include <pistache/async.h>
#include <pistache/client.h>
#include <pistache/common.h>
#include <pistache/endpoint.h>
#include <pistache/http.h>

#include <gtest/gtest.h>

#include <chrono>
#include <fstream>
#include <future>
#include <string>

using namespace Pistache;

struct HelloHandlerWithDelay : public Http::Handler
{
    HTTP_PROTOTYPE(HelloHandlerWithDelay)

    explicit HelloHandlerWithDelay(int delay = 0)
        : delay_(delay)
    { }

    void onRequest(const Http::Request& /*request*/,
                   Http::ResponseWriter writer) override
    {
        std::this_thread::sleep_for(std::chrono::seconds(delay_));
        writer.send(Http::Code::Ok, "Hello, World!");
    }

    int delay_;
};

int clientLogicFunc(int response_size, const std::string& server_page,
                    int wait_seconds)
{
    Http::Experimental::Client client;
    client.init();

    std::vector<Async::Promise<Http::Response>> responses;
    auto rb              = client.get(server_page);
    int resolver_counter = 0;
    int reject_counter   = 0;
    for (int i = 0; i < response_size; ++i)
    {
        auto response = rb.send();
        response.then(
            [&resolver_counter](Http::Response resp) {
                std::cout << "Response code is " << resp.code() << std::endl;
                if (resp.code() == Http::Code::Ok)
                {
                    ++resolver_counter;
                }
            },
            [&reject_counter](std::exception_ptr exc) {
                PrintException excPrinter;
                std::cout << "Reject with reason: ";
                excPrinter(exc);
                ++reject_counter;
            });
        responses.push_back(std::move(response));
    }

    auto sync = Async::whenAll(responses.begin(), responses.end());
    Async::Barrier<std::vector<Http::Response>> barrier(sync);
    barrier.wait_for(std::chrono::seconds(wait_seconds));

    client.shutdown();

    std::cout << "resolves: " << resolver_counter
              << ", rejects: " << reject_counter << "\n";

    return resolver_counter;
}

TEST(
    http_server_test,
    multiple_client_with_requests_to_multithreaded_server_threadName_null_str)
{
    const Pistache::Address address("localhost", Pistache::Port(0));

    const std::string threadName_null_str = "";

    Http::Endpoint server(address);
    auto flags       = Tcp::Options::ReuseAddr;
    auto server_opts = Http::Endpoint::options().flags(flags).threads(2).threadsName(
        threadName_null_str);
    server.init(server_opts);
    server.setHandler(Http::make_handler<HelloHandlerWithDelay>());
    server.serveThreaded();

    const std::string server_address = "localhost:" + server.getPort().toString();
    std::cout << "Server address: " << server_address << "\n";

    const int CLIENT_REQUEST_SIZE = 2;
    const int SIX_SECONDS_TIMOUT  = 6;
    std::future<int> result(std::async(clientLogicFunc, CLIENT_REQUEST_SIZE,
                                       server_address, SIX_SECONDS_TIMOUT));

    int res1 = result.get();

    server.shutdown();

    ASSERT_EQ(res1, CLIENT_REQUEST_SIZE);
}

TEST(
    http_server_test,
    multiple_client_with_requests_to_multithreaded_server_threadName_single_char)
{
    const Pistache::Address address("localhost", Pistache::Port(0));

    const std::string threadName_single_char = "a";

    Http::Endpoint server(address);
    auto flags       = Tcp::Options::ReuseAddr;
    auto server_opts = Http::Endpoint::options().flags(flags).threads(2).threadsName(
        threadName_single_char);
    server.init(server_opts);
    server.setHandler(Http::make_handler<HelloHandlerWithDelay>());
    server.serveThreaded();

    const std::string server_address = "localhost:" + server.getPort().toString();
    std::cout << "Server address: " << server_address << "\n";

    const int CLIENT_REQUEST_SIZE = 2;
    const int SIX_SECONDS_TIMOUT  = 6;
    std::future<int> result(std::async(clientLogicFunc, CLIENT_REQUEST_SIZE,
                                       server_address, SIX_SECONDS_TIMOUT));

    int res1 = result.get();

    server.shutdown();

    ASSERT_EQ(res1, CLIENT_REQUEST_SIZE);
}

TEST(
    http_server_test,
    multiple_client_with_requests_to_multithreaded_server_threadName_max_length)
{
    const Pistache::Address address("localhost", Pistache::Port(0));

    const std::string threadName_max_length = "0123456789abcdef";

    Http::Endpoint server(address);
    auto flags       = Tcp::Options::ReuseAddr;
    auto server_opts = Http::Endpoint::options().flags(flags).threads(2).threadsName(
        threadName_max_length);
    server.init(server_opts);
    server.setHandler(Http::make_handler<HelloHandlerWithDelay>());
    server.serveThreaded();

    const std::string server_address = "localhost:" + server.getPort().toString();
    std::cout << "Server address: " << server_address << "\n";

    const int CLIENT_REQUEST_SIZE = 2;
    const int SIX_SECONDS_TIMOUT  = 6;
    std::future<int> result(std::async(clientLogicFunc, CLIENT_REQUEST_SIZE,
                                       server_address, SIX_SECONDS_TIMOUT));

    int res1 = result.get();

    server.shutdown();

    ASSERT_EQ(res1, CLIENT_REQUEST_SIZE);
}

TEST(
    http_server_test,
    multiple_client_with_requests_to_multithreaded_server_threadName_exceed_length)
{
    const Pistache::Address address("localhost", Pistache::Port(0));

    const std::string threadName_exceed_length = "0123456789abcdefghi";

    Http::Endpoint server(address);
    auto flags       = Tcp::Options::ReuseAddr;
    auto server_opts = Http::Endpoint::options().flags(flags).threads(2).threadsName(
        threadName_exceed_length);
    server.init(server_opts);
    server.setHandler(Http::make_handler<HelloHandlerWithDelay>());
    server.serveThreaded();

    const std::string server_address = "localhost:" + server.getPort().toString();
    std::cout << "Server address: " << server_address << "\n";

    const int CLIENT_REQUEST_SIZE = 2;
    const int SIX_SECONDS_TIMOUT  = 6;
    std::future<int> result(std::async(clientLogicFunc, CLIENT_REQUEST_SIZE,
                                       server_address, SIX_SECONDS_TIMOUT));

    int res1 = result.get();

    server.shutdown();

    ASSERT_EQ(res1, CLIENT_REQUEST_SIZE);
}