Skip to content

Commit

Permalink
Merge pull request #1873 from AntelopeIO/GH-1858-test-main
Browse files Browse the repository at this point in the history
[5.0 -> main] PH: Improve error handling and use one strand
  • Loading branch information
heifner authored Nov 7, 2023
2 parents 4824d8b + 3e174cb commit 30eebb6
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 10 deletions.
9 changes: 7 additions & 2 deletions tests/trx_generator/http_client_async.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@
// libs/beast/example/http/client/async/http_client_async.cpp
// with minimum modification and yet reusable.
//
// Updated to use strand like in the boost example
// libs/beast/example/http/client/crawl/http_crawl.cpp
//
//
// Copyright (c) 2016-2019 Vinnie Falco (vinnie dot falco at gmail dot com)
//
Expand Down Expand Up @@ -48,6 +51,7 @@ inline void fail(beast::error_code ec, char const* what) { std::cerr << what <<

// Performs an HTTP GET and prints the response
class session : public std::enable_shared_from_this<session> {
net::strand<net::io_context::executor_type> ex_;
tcp::resolver resolver_;
beast::tcp_stream stream_;
beast::flat_buffer buffer_; // (Must persist between reads)
Expand All @@ -59,8 +63,9 @@ class session : public std::enable_shared_from_this<session> {
// Objects are constructed with a strand to
// ensure that handlers do not execute concurrently.
explicit session(net::io_context& ioc, const response_callback_t& response_callback)
: resolver_(net::make_strand(ioc))
, stream_(net::make_strand(ioc))
: ex_(net::make_strand(ioc.get_executor()))
, resolver_(ex_)
, stream_(ex_)
, response_callback_(response_callback) {}

// Start the asynchronous operation
Expand Down
28 changes: 20 additions & 8 deletions tests/trx_generator/trx_provider.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -109,16 +109,21 @@ namespace eosio::testing {
void http_connection::disconnect() {
int max = 30;
int waited = 0;
while (_sent.load() != _acknowledged.load() && waited < max) {
ilog("http_connection::disconnect waiting on ack - sent ${s} | acked ${a} | waited ${w}",
("s", _sent.load())("a", _acknowledged.load())("w", waited));
for (uint64_t sent = _sent.load(), acknowledged = _acknowledged.load();
sent != acknowledged && waited < max;
sent = _sent.load(), acknowledged = _acknowledged.load()) {
ilog("disconnect waiting on ack - sent ${s} | acked ${a} | waited ${w}",
("s", sent)("a", acknowledged)("w", waited));
sleep(1);
++waited;
}
if (waited == max) {
elog("http_connection::disconnect failed to receive all acks in time - sent ${s} | acked ${a} | waited ${w}",
elog("disconnect failed to receive all acks in time - sent ${s} | acked ${a} | waited ${w}",
("s", _sent.load())("a", _acknowledged.load())("w", waited));
}
if (_errors.load()) {
elog("${n} errors reported during http calls, see logs", ("n", _errors.load()));
}
}

bool http_connection::needs_response_trace_info() {
Expand Down Expand Up @@ -151,10 +156,17 @@ namespace eosio::testing {
trx_acknowledged(trx_id, fc::time_point::now());
if (ec) {
elog("http error: ${c}: ${m}", ("c", ec.value())("m", ec.message()));
throw std::runtime_error(ec.message());
++_errors;
return;
}

if (this->needs_response_trace_info() && response.result() == boost::beast::http::status::ok) {
bool exception = false;
auto exception_handler = [this, &response, &exception](const fc::exception_ptr& ex) {
elog("Fail to parse JSON from string: ${string}", ("string", response.body()));
++_errors;
exception = true;
};
try {
fc::variant resp_json = fc::json::from_string(response.body());
if (resp_json.is_object() && resp_json.get_object().contains("processed")) {
Expand Down Expand Up @@ -186,9 +198,9 @@ namespace eosio::testing {
elog("async_http_request Transaction failed, transaction not processed: ${string}",
("string", response.body()));
}
}
EOS_RETHROW_EXCEPTIONS(chain::json_parse_exception, "Fail to parse JSON from string: ${string}",
("string", response.body()));
} CATCH_AND_CALL(exception_handler)
if (exception)
return;
}

if (!(response.result() == boost::beast::http::status::accepted ||
Expand Down
1 change: 1 addition & 0 deletions tests/trx_generator/trx_provider.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,7 @@ namespace eosio::testing {

std::atomic<uint64_t> _acknowledged{0};
std::atomic<uint64_t> _sent{0};
std::atomic<uint64_t> _errors{0};

explicit http_connection(const provider_base_config& provider_config)
: provider_connection(provider_config) {}
Expand Down

0 comments on commit 30eebb6

Please sign in to comment.