Skip to content

Commit

Permalink
Add test to verify than read stdout is async when no data are available
Browse files Browse the repository at this point in the history
  • Loading branch information
legerch committed Sep 27, 2023
1 parent 089feaa commit b65dbc0
Show file tree
Hide file tree
Showing 3 changed files with 70 additions and 0 deletions.
1 change: 1 addition & 0 deletions test/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ add_executable(process_fail_stackoverflow process_fail_stackoverflow.c)
add_executable(process_hung process_hung.c)
add_executable(process_stdout_data process_stdout_data.c)
add_executable(process_stdout_poll process_stdout_poll.c)
add_executable(process_stdout_poll_nodata process_stdout_poll_nodata.c)
add_executable(process_stderr_poll process_stderr_poll.c)
add_executable(process_stdout_large process_stdout_large.c)
add_executable(process_call_return_argc process_call_return_argc.c)
Expand Down
32 changes: 32 additions & 0 deletions test/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -643,6 +643,38 @@ UTEST(subprocess, read_stdout_async) {
ASSERT_EQ(ret, 0);
}

UTEST(subprocess, read_stdout_async_nodata) {
const char *const commandLine[] = {"./process_stdout_poll_nodata", 0};
struct subprocess_s process;
int ret = -1;
static char data[14 + 1] = {0};
unsigned index = 0;
unsigned bytes_read = 0;

ASSERT_EQ(0, subprocess_create(commandLine, subprocess_option_enable_async,
&process));

while(subprocess_alive(&process)) {
bytes_read = subprocess_read_stdout(&process, data + index,
sizeof(data) - 1 - index);
index += bytes_read;

// Send any character to the subprocess to tell it to pursuit
if (bytes_read == 0) {
fputc('s', subprocess_stdin(&process));
fflush(subprocess_stdin(&process));
}
}

ASSERT_EQ(15u, bytes_read);
ASSERT_EQ(15u, index);
ASSERT_TRUE(0 == memcmp("Hello, world!", data, 14));

ASSERT_EQ(0, subprocess_join(&process, &ret));
ASSERT_EQ(0, subprocess_destroy(&process));
ASSERT_EQ(ret, 0);
}

UTEST(subprocess, poll_stdout_async) {
const char *const commandLine[] = {"./process_stdout_poll", "16384", 0};
struct subprocess_s process;
Expand Down
37 changes: 37 additions & 0 deletions test/process_stdout_poll_nodata.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
// This is free and unencumbered software released into the public domain.
//
// Anyone is free to copy, modify, publish, use, compile, sell, or
// distribute this software, either in source code form or as a compiled
// binary, for any purpose, commercial or non-commercial, and by any
// means.
//
// In jurisdictions that recognize copyright laws, the author or authors
// of this software dedicate any and all copyright interest in the
// software to the public domain. We make this dedication for the benefit
// of the public at large and to the detriment of our heirs and
// successors. We intend this dedication to be an overt act of
// relinquishment in perpetuity of all present and future rights to this
// software under copyright law.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
// IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR
// OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
// ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
// OTHER DEALINGS IN THE SOFTWARE.
//
// For more information, please refer to <http://unlicense.org/>

#include <stdio.h>
#include <string.h>

int main(int argc, const char *const argv[]) {
// Do nothing, wait for an event on stdin
while(fgetc(stdin) == EOF){}

// Event received, write something on stdout
printf("Hello, world!");

return 0;
}

0 comments on commit b65dbc0

Please sign in to comment.