tile/tests/http_client_test.cc
tqcq 8238c310f6
Some checks failed
linux-arm-gcc / linux-gcc-armhf (Release) (push) Waiting to run
linux-mips-gcc / linux-gcc-mipsel (Debug) (push) Waiting to run
linux-mips-gcc / linux-gcc-mipsel (Release) (push) Waiting to run
linux-mips64-gcc / linux-gcc-mips64el (Debug) (push) Waiting to run
linux-mips64-gcc / linux-gcc-mips64el (Release) (push) Waiting to run
linux-riscv64-gcc / linux-gcc-riscv64 (Debug) (push) Waiting to run
linux-riscv64-gcc / linux-gcc-riscv64 (Release) (push) Waiting to run
linux-x64-gcc / linux-gcc (Debug) (push) Waiting to run
linux-x64-gcc / linux-gcc (Release) (push) Waiting to run
linux-x86-gcc / linux-gcc (Debug) (push) Waiting to run
linux-x86-gcc / linux-gcc (Release) (push) Waiting to run
linux-aarch64-cpu-gcc / linux-gcc-aarch64 (Debug) (push) Failing after 1m22s
linux-aarch64-cpu-gcc / linux-gcc-aarch64 (Release) (push) Failing after 1m47s
linux-arm-gcc / linux-gcc-arm (Debug) (push) Failing after 1m38s
linux-arm-gcc / linux-gcc-arm (Release) (push) Failing after 2m3s
linux-arm-gcc / linux-gcc-armhf (Debug) (push) Has been cancelled
linux-aarch64-cpu-gcc / linux-gcc-aarch64 (Debug) (pull_request) Failing after 1m23s
linux-aarch64-cpu-gcc / linux-gcc-aarch64 (Release) (pull_request) Failing after 1m48s
linux-arm-gcc / linux-gcc-arm (Debug) (pull_request) Failing after 1m40s
linux-arm-gcc / linux-gcc-arm (Release) (pull_request) Failing after 1m36s
linux-arm-gcc / linux-gcc-armhf (Debug) (pull_request) Failing after 2m9s
linux-arm-gcc / linux-gcc-armhf (Release) (pull_request) Successful in 1m53s
linux-mips-gcc / linux-gcc-mipsel (Debug) (pull_request) Successful in 1m30s
linux-mips-gcc / linux-gcc-mipsel (Release) (pull_request) Failing after 1m41s
linux-mips64-gcc / linux-gcc-mips64el (Debug) (pull_request) Failing after 1m25s
linux-mips64-gcc / linux-gcc-mips64el (Release) (pull_request) Successful in 1m48s
linux-riscv64-gcc / linux-gcc-riscv64 (Debug) (pull_request) Successful in 3m5s
linux-riscv64-gcc / linux-gcc-riscv64 (Release) (pull_request) Successful in 1m47s
linux-x64-gcc / linux-gcc (Debug) (pull_request) Failing after 1m0s
linux-x64-gcc / linux-gcc (Release) (pull_request) Failing after 1m27s
linux-x86-gcc / linux-gcc (Debug) (pull_request) Failing after 1m12s
linux-x86-gcc / linux-gcc (Release) (pull_request) Failing after 1m38s
feat add test for future
2024-06-29 16:13:22 +08: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();
}