From 4664af12fa19b016ac21ae1b7ce8b8b6440fc1e3 Mon Sep 17 00:00:00 2001 From: Torsten Dreyer Date: Fri, 2 Dec 2016 14:43:52 +0100 Subject: [PATCH] Fix lockup on Windows when polling DNS, add test --- simgear/io/DNSClient.cxx | 2 +- simgear/io/test_DNS.cxx | 77 ++++++++++++++++++++++++++++++++++++---- 2 files changed, 72 insertions(+), 7 deletions(-) diff --git a/simgear/io/DNSClient.cxx b/simgear/io/DNSClient.cxx index 3de7b934..ff69d2ce 100644 --- a/simgear/io/DNSClient.cxx +++ b/simgear/io/DNSClient.cxx @@ -238,7 +238,7 @@ void Client::makeRequest(const Request_ptr& r) void Client::update(int waitTimeout) { time_t now = time(NULL); - if( dns_timeouts( d->ctx, waitTimeout, now ) < 0 ) + if( dns_timeouts( d->ctx, -1, now ) < 0 ) return; dns_ioevent(d->ctx, now); diff --git a/simgear/io/test_DNS.cxx b/simgear/io/test_DNS.cxx index d6c1a4d0..63d7d117 100644 --- a/simgear/io/test_DNS.cxx +++ b/simgear/io/test_DNS.cxx @@ -4,6 +4,8 @@ #include #include #include +#include +#include #include @@ -30,16 +32,79 @@ using namespace simgear; exit(1); \ } +class Watchdog +{ +public: + Watchdog() : _interval(0), _timer(0), _running(false) {} + ~Watchdog() { + stop(); + } + + void start(unsigned int milliseconds) + { + _interval = milliseconds; + _timer = 0; + _running = true; + _thread = std::thread(&Watchdog::loop, this); + } + + void stop() + { + _running = false; + _thread.join(); + } + +private: + unsigned int _interval; + std::atomic _timer; + std::atomic _running; + std::thread _thread; + + void loop() + { + while (_running) + { + _timer++; + + if (_timer >= _interval) + { + _running = false; + std::cerr << "Failure: timeout." << endl; + exit(EXIT_FAILURE); + } + + std::this_thread::sleep_for(std::chrono::milliseconds(100)); + } + } +}; + int main(int argc, char* argv[]) { sglog().setLogLevels( SG_ALL, SG_DEBUG ); - DNS::Client cl; -#define EXISTING_RECORD "terrasync.flightgear.org" + Watchdog watchdog; + watchdog.start(100); - // test existing NAPTR - // fgtest.t3r.de. 600 IN NAPTR 999 99 "U" "test" "!^.*$!http://dnstest.flightgear.org/!" . + simgear::Socket::initSockets(); + + + DNS::Client cl; + + cout << "test update without prior pending request" << endl; + { + cout << "polling."; + for( int i = 0; i < 20; i++ ) { + SGTimeStamp::sleepForMSec(200); + cl.update(0); + cout << "."; + cout.flush(); + } + cout << "done" << endl; + } + +#define EXISTING_RECORD "terrasync.flightgear.org" + cout << "test existing NAPTR: " EXISTING_RECORD << endl; { DNS::NAPTRRequest * naptrRequest = new DNS::NAPTRRequest(EXISTING_RECORD); DNS::Request_ptr r(naptrRequest); @@ -58,7 +123,7 @@ int main(int argc, char* argv[]) return EXIT_FAILURE; } - // test for ascending preference/order + cout << "test for ascending preference/order" << endl; int order = -1, preference = -1; for( DNS::NAPTRRequest::NAPTR_list::const_iterator it = naptrRequest->entries.begin(); it != naptrRequest->entries.end(); ++it ) { // currently only support "U" which implies empty replacement @@ -95,7 +160,7 @@ int main(int argc, char* argv[]) } } - // test non-existing NAPTR + cout << "test non-existing NAPTR" << endl; { DNS::NAPTRRequest * naptrRequest = new DNS::NAPTRRequest("jurkxkqdiufqzpfvzqok.prozhqrlcaavbxifkkhf"); DNS::Request_ptr r(naptrRequest);