init repo.
This commit is contained in:
85
tests/http_client_test.cc
Normal file
85
tests/http_client_test.cc
Normal file
@ -0,0 +1,85 @@
|
||||
#include "tile/net/http/http_client.h"
|
||||
|
||||
#include "gtest/gtest.h"
|
||||
|
||||
#include "tile/base/thread/latch.h"
|
||||
|
||||
const std::string url = "http://127.0.0.1:8000/";
|
||||
|
||||
TEST(HttpClient, Request) {
|
||||
tile::HttpClient client;
|
||||
std::vector<tile::Future<
|
||||
std::expected<tile::HttpResponse, tile::HttpClient::ErrorCode>>>
|
||||
v;
|
||||
tile::HttpClient::RequestOptions options;
|
||||
options.timeout = std::chrono::seconds(20);
|
||||
|
||||
std::atomic<int> count{0};
|
||||
|
||||
for (int i = 0; i != 1000; ++i) {
|
||||
for (auto code : tile::AllHttpStatus) {
|
||||
if (static_cast<int>(code) < 200) {
|
||||
continue;
|
||||
}
|
||||
|
||||
v.emplace_back(
|
||||
client.AsyncGet(url + std::to_string(static_cast<int>(code)), options)
|
||||
.Then([&](std::expected<tile::HttpResponse,
|
||||
tile::HttpClient::ErrorCode> &&res) {
|
||||
int cnt = count.fetch_add(1) + 1;
|
||||
TILE_LOG_INFO_EVERY_SECOND("complete {}", cnt);
|
||||
return res;
|
||||
}));
|
||||
}
|
||||
}
|
||||
|
||||
int j = 0;
|
||||
for (int i = 0; i != 1000; ++i) {
|
||||
for (auto &code : tile::AllHttpStatus) {
|
||||
if (static_cast<int>(code) < 200) {
|
||||
continue;
|
||||
}
|
||||
|
||||
auto resp = tile::future::BlockingGet(std::move(v[j++]));
|
||||
|
||||
EXPECT_TRUE(resp) << tile::HttpClient::ErrorCodeToString(resp.error());
|
||||
EXPECT_EQ(resp->status(), code);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
TEST(HttpClient, RequestNoWait) {
|
||||
tile::HttpClient client;
|
||||
tile::HttpClient::RequestOptions options;
|
||||
options.timeout = std::chrono::seconds(20);
|
||||
int cnt = 0;
|
||||
|
||||
for (int i = 0; i != 1000; ++i) {
|
||||
for (auto code : tile::AllHttpStatus) {
|
||||
if (static_cast<int>(code) < 200) {
|
||||
continue;
|
||||
}
|
||||
++cnt;
|
||||
}
|
||||
}
|
||||
|
||||
tile::Latch latch(cnt);
|
||||
std::atomic<int> count{0};
|
||||
|
||||
for (int i = 0; i != 1000; ++i) {
|
||||
for (auto code : tile::AllHttpStatus) {
|
||||
if (static_cast<int>(code) < 200) {
|
||||
continue;
|
||||
}
|
||||
|
||||
client.AsyncGet(url + std::to_string(static_cast<int>(code)), options)
|
||||
.Then([&](std::expected<tile::HttpResponse,
|
||||
tile::HttpClient::ErrorCode> &&) {
|
||||
int cnt = count.fetch_add(1) + 1;
|
||||
TILE_LOG_INFO_EVERY_SECOND("complete {}", cnt);
|
||||
latch.CountDown();
|
||||
});
|
||||
}
|
||||
}
|
||||
latch.Wait();
|
||||
}
|
24
tests/http_server.py
Normal file
24
tests/http_server.py
Normal file
@ -0,0 +1,24 @@
|
||||
#!/usr/bin/python3
|
||||
import http.server
|
||||
import socket
|
||||
import socketserver
|
||||
from http.server import HTTPServer, BaseHTTPRequestHandler
|
||||
|
||||
PORT = 8000
|
||||
|
||||
class Request(BaseHTTPRequestHandler):
|
||||
def do_GET(self):
|
||||
code = int(self.path.split("/")[1])
|
||||
|
||||
self.send_response(code)
|
||||
self.send_header('Content-type', 'text/plain')
|
||||
self.end_headers()
|
||||
|
||||
class HTTPServer(socketserver.TCPServer):
|
||||
def server_bind(self):
|
||||
self.socket.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
|
||||
self.socket.bind(self.server_address)
|
||||
|
||||
Handler = http.server.SimpleHTTPRequestHandler
|
||||
with HTTPServer(("", PORT), Request) as httpd:
|
||||
httpd.serve_forever()
|
Reference in New Issue
Block a user