aboutsummaryrefslogtreecommitdiff
path: root/tests/progress/driver.cxx
diff options
context:
space:
mode:
authorKaren Arutyunov <karen@codesynthesis.com>2017-07-27 14:43:55 +0300
committerKaren Arutyunov <karen@codesynthesis.com>2017-07-27 14:45:49 +0300
commit40426fe405dc795668351cb41bd50d8d08e5b094 (patch)
tree822509effcf30018b366f33560b42fb0d6dc85a9 /tests/progress/driver.cxx
parent72450e72e400eb03325ca182f0e118fde8cd1705 (diff)
Complete progress test
Diffstat (limited to 'tests/progress/driver.cxx')
-rw-r--r--tests/progress/driver.cxx67
1 files changed, 66 insertions, 1 deletions
diff --git a/tests/progress/driver.cxx b/tests/progress/driver.cxx
index 702319a..64e84fd 100644
--- a/tests/progress/driver.cxx
+++ b/tests/progress/driver.cxx
@@ -3,22 +3,57 @@
// license : MIT; see accompanying LICENSE file
#ifndef _WIN32
+# include <unistd.h> // write()
+
# include <thread> // this_thread::sleep_for()
#else
# include <libbutl/win32-utility.hxx>
+
+# include <io.h> //_write()
#endif
#include <cstddef> // size_t
#include <iostream>
+#include <libbutl/process.hxx>
+#include <libbutl/fdstream.hxx> // fdnull(), stderr_fd()
#include <libbutl/diagnostics.hxx>
using namespace std;
using namespace butl;
+// Usage:
+//
+// argv[0] [-n] [-c]
+//
+// -n
+// Do not run child process. By default the program runs itself with -c
+// option (see below).
+//
+// -c
+// Run as a child process that just prints lines with a small but varying
+// delay.
+//
int
-main ()
+main (int argc, const char* argv[])
{
+ bool child (false);
+ bool no_child (false);
+
+ assert (argc > 0);
+
+ for (int i (1); i != argc; ++i)
+ {
+ string v (argv[i]);
+
+ if (v == "-c")
+ child = true;
+ else if (v == "-n")
+ no_child = true;
+ else
+ assert (false);
+ }
+
auto sleep = [] (size_t ms = 100)
{
// MINGW GCC 4.9 doesn't implement this_thread so use Win32 Sleep().
@@ -30,6 +65,34 @@ main ()
#endif
};
+ if (child)
+ {
+ auto print = [] (const string& s)
+ {
+#ifndef _WIN32
+ write (stderr_fd(), s.c_str (), s.size ());
+#else
+ _write (stderr_fd(), s.c_str (), static_cast<unsigned int> (s.size ()));
+#endif
+ };
+
+ for (size_t i (50); i != 0; --i)
+ {
+ print ("Child line " + to_string (i) + '\n');
+ sleep (200 - i);
+ }
+
+ return 0;
+ }
+
+ // @@ Can't compile unless convert to process_env() explicitly (GCC still
+ // warns about calls ambiguity).
+ //
+ process pr (!no_child
+ ? process_start (fdnull (), fdnull (), stderr_fd (),
+ process_env (argv[0]), "-c")
+ : process (process_exit (0))); // Exited normally.
+
for (size_t i (100); i != 0; --i)
{
if (i % 10 == 0)
@@ -59,4 +122,6 @@ main ()
}
sleep (1000);
+
+ assert (pr.wait ());
}