From ae43c5780651d594b1ec76e99330cd6ef082b0c5 Mon Sep 17 00:00:00 2001 From: Karen Arutyunov Date: Fri, 16 Oct 2020 22:18:45 +0300 Subject: Add fdselect() overload that also takes timeout --- tests/fdstream/driver.cxx | 37 +++++++++++++++++++++++++++++++------ 1 file changed, 31 insertions(+), 6 deletions(-) (limited to 'tests/fdstream/driver.cxx') diff --git a/tests/fdstream/driver.cxx b/tests/fdstream/driver.cxx index 3215e02..76fc64a 100644 --- a/tests/fdstream/driver.cxx +++ b/tests/fdstream/driver.cxx @@ -470,12 +470,12 @@ main (int argc, const char* argv[]) // { string s; - for (size_t i (0); i < 100; ++i) + for (size_t i (0); i < 300; ++i) s += "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz\n"; const char* args[] = {argv[0], "-c", nullptr}; - auto test_read = [&args, &s] () + auto test_read = [&args, &s] (bool timeout) { try { @@ -491,11 +491,29 @@ main (int argc, const char* argv[]) string r; char buf[300]; + bool timedout (false); while (!is.eof ()) { - pair nd (fdselect (rds, wds)); - - assert (nd.first == 1 && nd.second == 0 && rds[0].ready); + if (timeout) + { + pair nd ( + fdselect (rds, wds, chrono::milliseconds (3))); + + assert (((nd.first == 0 && !rds[0].ready) || + (nd.first == 1 && rds[0].ready)) && + nd.second == 0); + + if (nd.first == 0) + { + timedout = true; + continue; + } + } + else + { + pair nd (fdselect (rds, wds)); + assert (nd.first == 1 && nd.second == 0 && rds[0].ready); + } for (streamsize n; (n = is.readsome (buf, sizeof (buf))) != 0; ) r.append (buf, static_cast (n)); @@ -504,6 +522,10 @@ main (int argc, const char* argv[]) is.close (); assert (r == s); + + // If timeout is used, then it most likely timedout, at least once. + // + assert (timedout == timeout); } catch (const ios::failure&) { @@ -517,7 +539,10 @@ main (int argc, const char* argv[]) vector threads; for (size_t i (0); i < 10; ++i) - threads.emplace_back (test_read); + { + threads.emplace_back ([&test_read] {test_read (true /* timeout */);}); + threads.emplace_back ([&test_read] {test_read (false /* timeout */);}); + } // While the threads are busy, let's test the skip/non_blocking modes // combination. -- cgit v1.1