Skip to content

Commit 62fd4b1

Browse files
committed
Add test to verify than read stdout is async when no data are available
1 parent 089feaa commit 62fd4b1

File tree

3 files changed

+70
-0
lines changed

3 files changed

+70
-0
lines changed

test/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ add_executable(process_fail_stackoverflow process_fail_stackoverflow.c)
4747
add_executable(process_hung process_hung.c)
4848
add_executable(process_stdout_data process_stdout_data.c)
4949
add_executable(process_stdout_poll process_stdout_poll.c)
50+
add_executable(process_stdout_poll_nodata process_stdout_poll_nodata.c)
5051
add_executable(process_stderr_poll process_stderr_poll.c)
5152
add_executable(process_stdout_large process_stdout_large.c)
5253
add_executable(process_call_return_argc process_call_return_argc.c)

test/main.c

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -643,6 +643,38 @@ UTEST(subprocess, read_stdout_async) {
643643
ASSERT_EQ(ret, 0);
644644
}
645645

646+
UTEST(subprocess, read_stdout_async_nodata) {
647+
const char *const commandLine[] = {"./process_stdout_poll_nodata", 0};
648+
struct subprocess_s process;
649+
int ret = -1;
650+
static char data[14 + 1] = {0};
651+
unsigned index = 0;
652+
unsigned bytes_read = 0;
653+
654+
ASSERT_EQ(0, subprocess_create(commandLine, subprocess_option_enable_async,
655+
&process));
656+
657+
while(subprocess_alive(&process)) {
658+
bytes_read = subprocess_read_stdout(&process, data + index,
659+
sizeof(data) - 1 - index);
660+
index += bytes_read;
661+
662+
// Send any character to the subprocess to tell it to pursuit
663+
if (bytes_read == 0) {
664+
fputc('s', subprocess_stdin(&process));
665+
fflush(subprocess_stdin(&process));
666+
}
667+
}
668+
669+
ASSERT_EQ(15u, bytes_read);
670+
ASSERT_EQ(15u, index);
671+
ASSERT_TRUE(0 == memcmp("Hello, world!", data, 15));
672+
673+
ASSERT_EQ(0, subprocess_join(&process, &ret));
674+
ASSERT_EQ(0, subprocess_destroy(&process));
675+
ASSERT_EQ(ret, 0);
676+
}
677+
646678
UTEST(subprocess, poll_stdout_async) {
647679
const char *const commandLine[] = {"./process_stdout_poll", "16384", 0};
648680
struct subprocess_s process;

test/process_stdout_poll_nodata.c

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
// This is free and unencumbered software released into the public domain.
2+
//
3+
// Anyone is free to copy, modify, publish, use, compile, sell, or
4+
// distribute this software, either in source code form or as a compiled
5+
// binary, for any purpose, commercial or non-commercial, and by any
6+
// means.
7+
//
8+
// In jurisdictions that recognize copyright laws, the author or authors
9+
// of this software dedicate any and all copyright interest in the
10+
// software to the public domain. We make this dedication for the benefit
11+
// of the public at large and to the detriment of our heirs and
12+
// successors. We intend this dedication to be an overt act of
13+
// relinquishment in perpetuity of all present and future rights to this
14+
// software under copyright law.
15+
//
16+
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17+
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18+
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
19+
// IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR
20+
// OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
21+
// ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
22+
// OTHER DEALINGS IN THE SOFTWARE.
23+
//
24+
// For more information, please refer to <http://unlicense.org/>
25+
26+
#include <stdio.h>
27+
#include <string.h>
28+
29+
int main(int argc, const char *const argv[]) {
30+
// Do nothing, wait for an event on stdin
31+
while(fgetc(stdin) == EOF){}
32+
33+
// Event received, write something on stdout
34+
printf("Hello, world!");
35+
36+
return 0;
37+
}

0 commit comments

Comments
 (0)