tests: use std::chrono instead of pulling in ares__tvnow and ares__timeval_remaining (#809)

This will allow more tests to run even when internal symbols aren't
accessible.

Fix By: Brad House (@bradh352)
pull/812/head
Brad House 5 months ago committed by GitHub
parent 8a50fc6c67
commit f90a81ed81
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
  1. 1
      .github/workflows/msys2.yml
  2. 2
      test/ares-test-mock-ai.cc
  3. 3
      test/ares-test-mock-et.cc
  4. 2
      test/ares-test-mock.cc
  5. 88
      test/ares-test.cc

@ -18,6 +18,7 @@ jobs:
matrix: matrix:
include: include:
- { msystem: CLANG64, env: clang-x86_64, extra_packages: "mingw-w64-clang-x86_64-clang mingw-w64-clang-x86_64-clang-analyzer" } - { msystem: CLANG64, env: clang-x86_64, extra_packages: "mingw-w64-clang-x86_64-clang mingw-w64-clang-x86_64-clang-analyzer" }
- { msystem: CLANG32, env: clang-i686, extra_packages: "mingw-w64-clang-i686-clang mingw-w64-clang-i686-clang-analyzer" }
- { msystem: MINGW64, env: x86_64, extra_packages: "mingw-w64-x86_64-gcc" } - { msystem: MINGW64, env: x86_64, extra_packages: "mingw-w64-x86_64-gcc" }
- { msystem: MINGW32, env: i686, extra_packages: "mingw-w64-i686-gcc" } - { msystem: MINGW32, env: i686, extra_packages: "mingw-w64-i686-gcc" }
# No need to test UCRT64 since clang64 uses UCRT # No need to test UCRT64 since clang64 uses UCRT

@ -500,7 +500,6 @@ TEST_P(MockChannelTestAI, FamilyV6) {
EXPECT_THAT(result.ai_, IncludesV6Address("2121:0000:0000:0000:0000:0000:0000:0303")); EXPECT_THAT(result.ai_, IncludesV6Address("2121:0000:0000:0000:0000:0000:0000:0303"));
} }
#ifndef CARES_SYMBOL_HIDING
// Test case for Issue #662 // Test case for Issue #662
TEST_P(MockChannelTestAI, PartialQueryCancel) { TEST_P(MockChannelTestAI, PartialQueryCancel) {
std::vector<byte> nothing; std::vector<byte> nothing;
@ -528,7 +527,6 @@ TEST_P(MockChannelTestAI, PartialQueryCancel) {
EXPECT_TRUE(result.done_); EXPECT_TRUE(result.done_);
EXPECT_EQ(ARES_ECANCELLED, result.status_); EXPECT_EQ(ARES_ECANCELLED, result.status_);
} }
#endif
TEST_P(MockChannelTestAI, FamilyV4) { TEST_P(MockChannelTestAI, FamilyV4) {
DNSPacket rsp4; DNSPacket rsp4;

@ -766,7 +766,6 @@ TEST_P(MockEventThreadTest, DestroyQuick) {
EXPECT_TRUE(result.done_); EXPECT_TRUE(result.done_);
} }
#ifndef CARES_SYMBOL_HIDING
// Test case for Issue #662 // Test case for Issue #662
TEST_P(MockEventThreadTest, PartialQueryCancel) { TEST_P(MockEventThreadTest, PartialQueryCancel) {
std::vector<byte> nothing; std::vector<byte> nothing;
@ -826,8 +825,6 @@ TEST_P(MockEventThreadTest, BulkCancel) {
if (verbose) std::cerr << "success: " << success_cnt << ", cancel: " << cancel_cnt << std::endl; if (verbose) std::cerr << "success: " << success_cnt << ", cancel: " << cancel_cnt << std::endl;
} }
} }
#endif
TEST_P(MockEventThreadTest, UnspecifiedFamilyV6) { TEST_P(MockEventThreadTest, UnspecifiedFamilyV6) {
DNSPacket rsp6; DNSPacket rsp6;

@ -1109,7 +1109,6 @@ TEST_P(MockChannelTest, V4WorksV6Timeout) {
EXPECT_EQ("{'www.google.com' aliases=[] addrs=[1.2.3.4]}", ss.str()); EXPECT_EQ("{'www.google.com' aliases=[] addrs=[1.2.3.4]}", ss.str());
} }
#ifndef CARES_SYMBOL_HIDING
// Test case for Issue #662 // Test case for Issue #662
TEST_P(MockChannelTest, PartialQueryCancel) { TEST_P(MockChannelTest, PartialQueryCancel) {
std::vector<byte> nothing; std::vector<byte> nothing;
@ -1132,7 +1131,6 @@ TEST_P(MockChannelTest, PartialQueryCancel) {
EXPECT_TRUE(result.done_); EXPECT_TRUE(result.done_);
EXPECT_EQ(ARES_ECANCELLED, result.status_); EXPECT_EQ(ARES_ECANCELLED, result.status_);
} }
#endif
TEST_P(MockChannelTest, UnspecifiedFamilyV6) { TEST_P(MockChannelTest, UnspecifiedFamilyV6) {
DNSPacket rsp6; DNSPacket rsp6;

88
test/ares-test.cc vendored

@ -59,6 +59,7 @@ extern "C" {
#include <functional> #include <functional>
#include <sstream> #include <sstream>
#include <algorithm> #include <algorithm>
#include <chrono>
#ifdef WIN32 #ifdef WIN32
#define BYTE_CAST (char *) #define BYTE_CAST (char *)
@ -257,32 +258,15 @@ void ProcessWork(ares_channel_t *channel,
int nfds, count; int nfds, count;
fd_set readers, writers; fd_set readers, writers;
#ifndef CARES_SYMBOL_HIDING auto tv_begin = std::chrono::high_resolution_clock::now();
ares_timeval_t tv_begin; auto tv_cancel = tv_begin;
ares_timeval_t tv_cancel;
ares__tvnow(&tv_begin);
memcpy(&tv_cancel, &tv_begin, sizeof(tv_cancel));
if (cancel_ms) { if (cancel_ms) {
if (verbose) std::cerr << "ares_cancel will be called after " << cancel_ms << "ms" << std::endl; if (verbose) std::cerr << "ares_cancel will be called after " << cancel_ms << "ms" << std::endl;
tv_cancel.sec += (cancel_ms / 1000); tv_cancel += std::chrono::milliseconds(cancel_ms);
tv_cancel.usec += ((cancel_ms % 1000) * 1000);
}
#else
if (cancel_ms) {
std::cerr << "library built with symbol hiding, can't test with cancel support" << std::endl;
return;
} }
#endif
while (true) { while (true) {
#ifndef CARES_SYMBOL_HIDING
ares_timeval_t tv_now;
ares_timeval_t atv_remaining;
ares__tvnow(&tv_now);
#endif
struct timeval tv; struct timeval tv;
struct timeval *tv_select; struct timeval *tv_select;
@ -308,29 +292,24 @@ void ProcessWork(ares_channel_t *channel,
if (tv_select == NULL) if (tv_select == NULL)
return; return;
#ifndef CARES_SYMBOL_HIDING
if (cancel_ms) { if (cancel_ms) {
unsigned int remaining_ms; auto tv_now = std::chrono::high_resolution_clock::now();
ares__timeval_remaining(&atv_remaining, auto remaining_ms = std::chrono::duration_cast<std::chrono::milliseconds>(tv_cancel - tv_now).count();
&tv_now,
&tv_cancel);
remaining_ms = (unsigned int)((atv_remaining.sec * 1000) + (atv_remaining.usec / 1000)); if (remaining_ms <= 0) {
if (remaining_ms == 0) {
if (verbose) std::cerr << "Issuing ares_cancel()" << std::endl; if (verbose) std::cerr << "Issuing ares_cancel()" << std::endl;
ares_cancel(channel); ares_cancel(channel);
cancel_ms = 0; /* Disable issuing cancel again */ cancel_ms = 0; /* Disable issuing cancel again */
} else { } else {
struct timeval tv_remaining; struct timeval tv_remaining;
tv_remaining.tv_sec = atv_remaining.sec; tv_remaining.tv_sec = remaining_ms / 1000;
tv_remaining.tv_usec = (int)atv_remaining.usec; tv_remaining.tv_usec = (int)(remaining_ms % 1000);
/* Recalculate proper timeout since we also have a cancel to wait on */ /* Recalculate proper timeout since we also have a cancel to wait on */
tv_select = ares_timeout(channel, &tv_remaining, &tv); tv_select = ares_timeout(channel, &tv_remaining, &tv);
} }
} }
#endif
count = select(nfds, &readers, &writers, nullptr, tv_select); count = select(nfds, &readers, &writers, nullptr, tv_select);
if (count < 0) { if (count < 0) {
@ -877,29 +856,13 @@ void MockChannelOptsTest::Process(unsigned int cancel_ms) {
void MockEventThreadOptsTest::Process(unsigned int cancel_ms) { void MockEventThreadOptsTest::Process(unsigned int cancel_ms) {
std::set<ares_socket_t> fds; std::set<ares_socket_t> fds;
#ifndef CARES_SYMBOL_HIDING auto tv_begin = std::chrono::high_resolution_clock::now();
bool has_cancel_ms = (cancel_ms > 0)?true:false; auto tv_cancel = tv_begin;
ares_timeval_t tv_begin;
ares_timeval_t tv_cancel;
#endif
#ifndef CARES_SYMBOL_HIDING
ares_timeval_t tv_now;
ares_timeval_t atv_remaining;
if (has_cancel_ms) {
ares__tvnow(&tv_begin);
memcpy(&tv_cancel, &tv_begin, sizeof(tv_cancel));
if (verbose) std::cerr << "ares_cancel will be called after " << cancel_ms << "ms" << std::endl;
tv_cancel.sec += (cancel_ms / 1000);
tv_cancel.usec += ((cancel_ms % 1000) * 1000);
}
#else
if (cancel_ms) { if (cancel_ms) {
std::cerr << "library built with symbol hiding, can't test with cancel support" << std::endl; if (verbose) std::cerr << "ares_cancel will be called after " << cancel_ms << "ms" << std::endl;
return; tv_cancel += std::chrono::milliseconds(cancel_ms);
} }
#endif
while (ares_queue_active_queries(channel_)) { while (ares_queue_active_queries(channel_)) {
int nfds = 0; int nfds = 0;
@ -918,32 +881,25 @@ void MockEventThreadOptsTest::Process(unsigned int cancel_ms) {
} }
} }
/* We just always wait 20ms then recheck. Not doing any complex signaling. */ /* We just always wait 20ms then recheck if we're done. Not doing any
* complex signaling. */
tv.tv_sec = 0; tv.tv_sec = 0;
tv.tv_usec = 20000; tv.tv_usec = 20000;
#ifndef CARES_SYMBOL_HIDING if (cancel_ms) {
ares__tvnow(&tv_now); auto tv_now = std::chrono::high_resolution_clock::now();
auto remaining_ms = std::chrono::duration_cast<std::chrono::milliseconds>(tv_cancel - tv_now).count();
unsigned int remaining_ms = 0; if (remaining_ms <= 0) {
if (has_cancel_ms) {
ares__timeval_remaining(&atv_remaining,
&tv_now,
&tv_cancel);
remaining_ms = (unsigned int)((atv_remaining.sec * 1000) + (atv_remaining.usec / 1000));
if (remaining_ms == 0) {
if (verbose) std::cerr << "Issuing ares_cancel()" << std::endl; if (verbose) std::cerr << "Issuing ares_cancel()" << std::endl;
ares_cancel(channel_); ares_cancel(channel_);
cancel_ms = 0; /* Disable issuing cancel again */ cancel_ms = 0; /* Disable issuing cancel again */
has_cancel_ms = false; } else {
tv.tv_sec = remaining_ms / 1000;
tv.tv_usec = (int)(remaining_ms % 1000);
} }
} }
if (has_cancel_ms && remaining_ms < 20) {
tv.tv_usec = (int)remaining_ms * 1000;
}
#endif
if (select(nfds, &readers, nullptr, nullptr, &tv) < 0) { if (select(nfds, &readers, nullptr, nullptr, &tv) < 0) {
fprintf(stderr, "select() failed, errno %d\n", errno); fprintf(stderr, "select() failed, errno %d\n", errno);
return; return;

Loading…
Cancel
Save