]> git.localhorst.tv Git - blank.git/blobdiff - tst/app/ProcessTest.cpp
timeouts reading from spawned processes
[blank.git] / tst / app / ProcessTest.cpp
index 778b7f6d63e5d486725e821dcf37c5a4ba4a4268..561a3dd3573f0a4f796fdc24f88d2f0dba82890f 100644 (file)
@@ -21,11 +21,11 @@ void ProcessTest::tearDown() {
 
 void ProcessTest::testExit() {
 #ifdef __WIN32
-#  error "TODO: implemente Process tests for windows"
+#  error "TODO: implement Process tests for windows"
 #else
 
        {
-               Process proc("/usr/bin/env", { "env", "true" }, { });
+               Process proc("/usr/bin/env", { "env", "true" });
                int status = proc.Join();
                CPPUNIT_ASSERT_EQUAL_MESSAGE(
                        "exit status of true assumed 0",
@@ -33,7 +33,7 @@ void ProcessTest::testExit() {
        }
 
        {
-               Process proc("/usr/bin/env", { "env", "false" }, { });
+               Process proc("/usr/bin/env", { "env", "false" });
                int status = proc.Join();
                CPPUNIT_ASSERT_EQUAL_MESSAGE(
                        "exit status of false assumed 1",
@@ -45,15 +45,15 @@ void ProcessTest::testExit() {
 
 void ProcessTest::testStream() {
 #ifdef __WIN32
-#  error "TODO: implemente Process tests for windows"
+#  error "TODO: implement Process tests for windows"
 #else
 
        {
                const string test_input("hello, world");
                const string expected_output("hello, world\n");
-               Process proc("/usr/bin/env", { "env", "echo", test_input.c_str() }, { });
+               Process proc("/usr/bin/env", { "env", "echo", test_input.c_str() });
                char buffer[expected_output.length() + 1];
-               size_t len = proc.ReadOut(buffer, sizeof(buffer));
+               size_t len = proc.ReadOut(buffer, sizeof(buffer), 1000);
                const string output(buffer, len);
                int status = proc.Join();
                CPPUNIT_ASSERT_EQUAL_MESSAGE(
@@ -72,7 +72,7 @@ void ProcessTest::testStream() {
                const string expected_output("hello, error\n");
                Process proc("/usr/bin/env", { "env", "sh", "-c", "echo $1 >&2", "echo", test_input.c_str() }, { });
                char buffer[expected_output.length() + 1];
-               size_t len = proc.ReadErr(buffer, sizeof(buffer));
+               size_t len = proc.ReadErr(buffer, sizeof(buffer), 1000);
                const string output(buffer, len);
                int status = proc.Join();
                CPPUNIT_ASSERT_EQUAL_MESSAGE(
@@ -90,14 +90,16 @@ void ProcessTest::testStream() {
        {
                const string test_input("dog");
                const string expected_output("dog");
-               Process proc("/usr/bin/env", { "env", "cat" }, { });
+               Process proc("/usr/bin/env", { "env", "cat" });
                size_t len = proc.WriteIn(test_input.c_str(), test_input.size());
                CPPUNIT_ASSERT_EQUAL_MESSAGE(
                        "unexpected length of input to cat",
                        test_input.size(), len);
+               // close input stream so cat knows we're done
+               proc.CloseIn();
 
                char buffer[expected_output.length() + 1];
-               len = proc.ReadOut(buffer, sizeof(buffer));
+               len = proc.ReadOut(buffer, sizeof(buffer), 1000);
                const string output(buffer, len);
                int status = proc.Join();
                CPPUNIT_ASSERT_EQUAL_MESSAGE(
@@ -114,5 +116,48 @@ void ProcessTest::testStream() {
 #endif
 }
 
+
+void ProcessTest::testEnv() {
+#ifdef __WIN32
+#  error "TODO: implement Process tests for windows"
+#else
+       {
+               const string test_input("Hello, environment");
+               const string expected_output("Hello, environment\n");
+               Process proc("/usr/bin/env", { "env", "sh", "-c", "echo $BLANK_ENV_TEST" }, { "BLANK_ENV_TEST=" + test_input });
+               char buffer[expected_output.length() + 1];
+               size_t len = proc.ReadOut(buffer, sizeof(buffer), 1000);
+               const string output(buffer, len);
+               int status = proc.Join();
+               CPPUNIT_ASSERT_EQUAL_MESSAGE(
+                       "exit status of echo assumed 0",
+                       0, status);
+               CPPUNIT_ASSERT_EQUAL_MESSAGE(
+                       "unexpected length of echo output",
+                       expected_output.size(), len);
+               CPPUNIT_ASSERT_EQUAL_MESSAGE(
+                       "unexpected error output of echo",
+                       expected_output, output);
+       }
+
+#endif
+}
+
+void ProcessTest::testTimeout() {
+#ifdef __WIN32
+#  error "TODO: implement Process tests for windows"
+#else
+       Process proc("/usr/bin/env", { "env", "cat" });
+       char buffer;
+       CPPUNIT_ASSERT_THROW_MESSAGE(
+               "read timeout on child process' stdout should throw",
+               proc.ReadOut(&buffer, 1, 1), std::runtime_error);
+       CPPUNIT_ASSERT_THROW_MESSAGE(
+               "read timeout on child process' stderr should throw",
+               proc.ReadErr(&buffer, 1, 1), std::runtime_error);
+       proc.Terminate();
+#endif
+}
+
 }
 }