tile/tests/http_client_test.cc
tqcq 9453a7cc55
All checks were successful
linux-aarch64-cpu-gcc / linux-gcc-aarch64 (Debug) (push) Successful in 1m28s
linux-aarch64-cpu-gcc / linux-gcc-aarch64 (Release) (push) Successful in 1m56s
linux-arm-gcc / linux-gcc-arm (Debug) (push) Successful in 1m43s
linux-arm-gcc / linux-gcc-arm (Release) (push) Successful in 1m40s
linux-arm-gcc / linux-gcc-armhf (Debug) (push) Successful in 1m59s
linux-arm-gcc / linux-gcc-armhf (Release) (push) Successful in 1m51s
linux-mips-gcc / linux-gcc-mipsel (Debug) (push) Successful in 1m32s
linux-mips-gcc / linux-gcc-mipsel (Release) (push) Successful in 1m38s
linux-mips64-gcc / linux-gcc-mips64el (Debug) (push) Successful in 1m27s
linux-mips64-gcc / linux-gcc-mips64el (Release) (push) Successful in 1m41s
linux-riscv64-gcc / linux-gcc-riscv64 (Debug) (push) Successful in 3m17s
linux-riscv64-gcc / linux-gcc-riscv64 (Release) (push) Successful in 1m45s
linux-x64-gcc / linux-gcc (Debug) (push) Successful in 2m10s
linux-x64-gcc / linux-gcc (Release) (push) Successful in 2m26s
linux-x86-gcc / linux-gcc (Debug) (push) Successful in 2m35s
linux-x86-gcc / linux-gcc (Release) (push) Successful in 2m29s
fix on_init crash (#3)
Reviewed-on: #3
2024-07-01 01:47:54 +00:00

86 lines
2.2 KiB
C++

#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();
}