Merge pull request #467 from erikaldsund/fix_failing_unittests_in_master_and_1.0

Fix failing unittests in master and 1.0
This commit is contained in:
Farook Al-Sammarraie 2022-06-13 23:35:16 +03:00 committed by GitHub
commit 68452a3995
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 68 additions and 30 deletions

View File

@ -293,14 +293,6 @@ namespace crow
}
}
/// Notify anything using `wait_for_server_start()` to proceed
void notify_server_start()
{
std::unique_lock<std::mutex> lock(start_mutex_);
server_started_ = true;
cv_started_.notify_all();
}
/// Run the server
void run()
{
@ -492,10 +484,17 @@ namespace crow
/// Wait until the server has properly started
void wait_for_server_start()
{
std::unique_lock<std::mutex> lock(start_mutex_);
if (server_started_)
return;
cv_started_.wait(lock);
{
std::unique_lock<std::mutex> lock(start_mutex_);
if (!server_started_)
cv_started_.wait(lock);
}
if (server_)
server_->wait_for_start();
#ifdef CROW_ENABLE_SSL
else if (ssl_server_)
ssl_server_->wait_for_start();
#endif
}
private:
@ -508,6 +507,14 @@ namespace crow
black_magic::tuple_extract<Middlewares, decltype(fwd)>(fwd))...);
}
/// Notify anything using `wait_for_server_start()` to proceed
void notify_server_start()
{
std::unique_lock<std::mutex> lock(start_mutex_);
server_started_ = true;
cv_started_.notify_all();
}
private:
std::uint8_t timeout_{5};

View File

@ -153,6 +153,7 @@ namespace crow
std::thread(
[this] {
notify_start();
io_service_.run();
CROW_LOG_INFO << "Exiting.";
})
@ -175,6 +176,14 @@ namespace crow
io_service_.stop(); // Close main io_service
}
/// Wait until the server has properly started
void wait_for_start()
{
std::unique_lock<std::mutex> lock(start_mutex_);
if (!server_started_)
cv_started_.wait(lock);
}
void signal_clear()
{
signals_.clear();
@ -236,6 +245,14 @@ namespace crow
}
}
/// Notify anything using `wait_for_start()` to proceed
void notify_start()
{
std::unique_lock<std::mutex> lock(start_mutex_);
server_started_ = true;
cv_started_.notify_all();
}
private:
asio::io_service io_service_;
std::vector<std::unique_ptr<asio::io_service>> io_service_pool_;
@ -243,6 +260,9 @@ namespace crow
std::vector<std::function<std::string()>> get_cached_date_str_pool_;
tcp::acceptor acceptor_;
bool shutting_down_ = false;
bool server_started_{false};
std::condition_variable cv_started_;
std::mutex start_mutex_;
boost::asio::signal_set signals_;
boost::asio::deadline_timer tick_timer_;

View File

@ -2240,7 +2240,7 @@ TEST_CASE("stream_response")
app.validate();
// running the test on a separate thread to allow the client to sleep
std::thread runTest([&app, &key_response, key_response_size]() {
std::thread runTest([&app, &key_response, key_response_size, keyword_]() {
auto _ = app.bindaddr(LOCALHOST_ADDRESS).port(45451).run_async();
app.wait_for_server_start();
asio::io_service is;
@ -2257,15 +2257,21 @@ TEST_CASE("stream_response")
asio::ip::address::from_string(LOCALHOST_ADDRESS), 45451));
c.send(asio::buffer(sendmsg));
//consuming the headers, since we don't need those for the test
// consuming the headers, since we don't need those for the test
static char buf[2048];
size_t received_headers_bytes = 0;
// magic number is 102 (it's the size of the headers, which is how much this line below needs to read)
const size_t headers_bytes = 102;
while (received_headers_bytes < headers_bytes)
received_headers_bytes += c.receive(asio::buffer(buf, 102));
received += received_headers_bytes - headers_bytes; //add any extra that might have been received to the proper received count
// Magic number is 102. It's the size of the headers, which is at
// least how much we need to read. Since the header size may change
// and break the test, we read twice as much as the header and
// search in the received data for the first occurrence of keyword_.
const size_t headers_bytes_and_some = 102 * 2;
while (received_headers_bytes < headers_bytes_and_some)
received_headers_bytes += c.receive(asio::buffer(buf + received_headers_bytes,
sizeof(buf) / sizeof(buf[0]) - received_headers_bytes));
const std::string::size_type header_end_pos = std::string(buf, received_headers_bytes).find(keyword_);
received += received_headers_bytes - header_end_pos; // add any extra that might have been received to the proper received count
while (received < key_response_size)
{
@ -2915,12 +2921,18 @@ TEST_CASE("blueprint")
TEST_CASE("base64")
{
unsigned char sample_bin[] = {0x14, 0xfb, 0x9c, 0x03, 0xd9, 0x7e};
std::string sample_bin_str(reinterpret_cast<char const*>(sample_bin),
sizeof(sample_bin) / sizeof(sample_bin[0]));
std::string sample_bin_enc = "FPucA9l+";
std::string sample_bin_enc_url = "FPucA9l-";
unsigned char sample_bin1[] = {0x14, 0xfb, 0x9c, 0x03, 0xd9};
std::string sample_bin1_str(reinterpret_cast<char const*>(sample_bin1),
sizeof(sample_bin1) / sizeof(sample_bin1[0]));
std::string sample_bin1_enc = "FPucA9k=";
std::string sample_bin1_enc_np = "FPucA9k";
unsigned char sample_bin2[] = {0x14, 0xfb, 0x9c, 0x03};
std::string sample_bin2_str(reinterpret_cast<char const*>(sample_bin2),
sizeof(sample_bin2) / sizeof(sample_bin2[0]));
std::string sample_bin2_enc = "FPucAw==";
std::string sample_bin2_enc_np = "FPucAw";
std::string sample_text = "Crow Allows users to handle requests that may not come from the network. This is done by calling the handle(req, res) method and providing a request and response objects. Which causes crow to identify and run the appropriate handler, returning the resulting response.";
@ -2928,20 +2940,19 @@ TEST_CASE("base64")
CHECK(crow::utility::base64encode(sample_text, sample_text.length()) == sample_base64);
CHECK(crow::utility::base64encode((unsigned char*)sample_bin, 6) == sample_bin_enc);
CHECK(crow::utility::base64encode_urlsafe((unsigned char*)sample_bin, 6) == sample_bin_enc_url);
CHECK(crow::utility::base64encode((unsigned char*)sample_bin1, 5) == sample_bin1_enc);
CHECK(crow::utility::base64encode((unsigned char*)sample_bin2, 4) == sample_bin2_enc);
CHECK(crow::utility::base64encode(sample_bin, 6) == sample_bin_enc);
CHECK(crow::utility::base64encode_urlsafe(sample_bin, 6) == sample_bin_enc_url);
CHECK(crow::utility::base64encode(sample_bin1, 5) == sample_bin1_enc);
CHECK(crow::utility::base64encode(sample_bin2, 4) == sample_bin2_enc);
CHECK(crow::utility::base64decode(sample_base64) == sample_text);
CHECK(crow::utility::base64decode(sample_base64, sample_base64.length()) == sample_text);
CHECK(crow::utility::base64decode(sample_bin_enc, 8) == std::string(reinterpret_cast<char const*>(sample_bin)));
CHECK(crow::utility::base64decode(sample_bin_enc_url, 8) == std::string(reinterpret_cast<char const*>(sample_bin)));
CHECK(crow::utility::base64decode(sample_bin1_enc, 8) == std::string(reinterpret_cast<char const*>(sample_bin1)).substr(0, 5));
CHECK(crow::utility::base64decode(sample_bin1_enc_np, 7) == std::string(reinterpret_cast<char const*>(sample_bin1)).substr(0, 5));
CHECK(crow::utility::base64decode(sample_bin2_enc, 8) == std::string(reinterpret_cast<char const*>(sample_bin2)).substr(0, 4));
CHECK(crow::utility::base64decode(sample_bin2_enc_np, 6) == std::string(reinterpret_cast<char const*>(sample_bin2)).substr(0, 4));
CHECK(crow::utility::base64decode(sample_bin_enc, 8) == sample_bin_str);
CHECK(crow::utility::base64decode(sample_bin_enc_url, 8) == sample_bin_str);
CHECK(crow::utility::base64decode(sample_bin1_enc, 8) == sample_bin1_str);
CHECK(crow::utility::base64decode(sample_bin1_enc_np, 7) == sample_bin1_str);
CHECK(crow::utility::base64decode(sample_bin2_enc, 8) == sample_bin2_str);
CHECK(crow::utility::base64decode(sample_bin2_enc_np, 6) == sample_bin2_str);
} // base64
TEST_CASE("sanitize_filename")