From 232087fd99915abaa7d917fd181ad8477bb689f2 Mon Sep 17 00:00:00 2001 From: Jonathan Nieder Date: Sun, 2 Jan 2011 18:51:07 -0600 Subject: tests: give vcs-svn/line_buffer its own test script Split the line_buffer test into small pieces and move it to its own file as preparation for adding more tests. Signed-off-by: Jonathan Nieder --- t/t0081-line-buffer.sh | 67 ++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 67 insertions(+) create mode 100755 t/t0081-line-buffer.sh (limited to 't/t0081-line-buffer.sh') diff --git a/t/t0081-line-buffer.sh b/t/t0081-line-buffer.sh new file mode 100755 index 0000000000..13ac735b55 --- /dev/null +++ b/t/t0081-line-buffer.sh @@ -0,0 +1,67 @@ +#!/bin/sh + +test_description="Test the svn importer's input handling routines. +" +. ./test-lib.sh + +test_expect_success 'read greeting' ' + echo HELLO >expect && + test-line-buffer <<-\EOF >actual && + read 5 + HELLO + EOF + test_cmp expect actual +' + +test_expect_success '0-length read, send along greeting' ' + printf "%s\n" "" HELLO >expect && + test-line-buffer <<-\EOF >actual && + read 0 + + copy 5 + HELLO + EOF + test_cmp expect actual +' + +test_expect_success 'buffer_read_string copes with trailing null byte' ' + echo >expect && + q_to_nul <<-\EOF | test-line-buffer >actual && + read 1 + Q + EOF + test_cmp expect actual +' + +test_expect_success '0-length read, copy null byte' ' + printf "%s\n" "" Q | q_to_nul >expect && + q_to_nul <<-\EOF | test-line-buffer >actual && + read 0 + + copy 1 + Q + EOF + test_cmp expect actual +' + +test_expect_success 'long reads are truncated' ' + printf "%s\n" foo "" >expect && + test-line-buffer <<-\EOF >actual && + read 5 + foo + EOF + test_cmp expect actual +' + +test_expect_success 'long copies are truncated' ' + printf "%s\n" "" foo >expect && + test-line-buffer <<-\EOF >actual && + read 0 + + copy 5 + foo + EOF + test_cmp expect actual +' + +test_done -- cgit v1.2.3 From 7b990c90514b24097ee71edbc02cb3a497a9476b Mon Sep 17 00:00:00 2001 From: Jonathan Nieder Date: Sun, 2 Jan 2011 18:52:28 -0600 Subject: vcs-svn: tweak test-line-buffer to not assume line-oriented input Do not expect an implicit newline after each input record. Use a separate command to exercise buffer_skip_bytes. Signed-off-by: Jonathan Nieder --- t/t0081-line-buffer.sh | 27 +++++++++++++-------------- 1 file changed, 13 insertions(+), 14 deletions(-) (limited to 't/t0081-line-buffer.sh') diff --git a/t/t0081-line-buffer.sh b/t/t0081-line-buffer.sh index 13ac735b55..68d6163995 100755 --- a/t/t0081-line-buffer.sh +++ b/t/t0081-line-buffer.sh @@ -7,45 +7,44 @@ test_description="Test the svn importer's input handling routines. test_expect_success 'read greeting' ' echo HELLO >expect && test-line-buffer <<-\EOF >actual && - read 5 + read 6 HELLO EOF test_cmp expect actual ' test_expect_success '0-length read, send along greeting' ' - printf "%s\n" "" HELLO >expect && + echo HELLO >expect && test-line-buffer <<-\EOF >actual && read 0 - - copy 5 + copy 6 HELLO EOF test_cmp expect actual ' -test_expect_success 'buffer_read_string copes with trailing null byte' ' - echo >expect && +test_expect_success 'buffer_read_string copes with null byte' ' + >expect && q_to_nul <<-\EOF | test-line-buffer >actual && - read 1 + read 2 Q EOF test_cmp expect actual ' -test_expect_success '0-length read, copy null byte' ' - printf "%s\n" "" Q | q_to_nul >expect && +test_expect_success 'skip, copy null byte' ' + echo Q | q_to_nul >expect && q_to_nul <<-\EOF | test-line-buffer >actual && - read 0 - - copy 1 + skip 2 + Q + copy 2 Q EOF test_cmp expect actual ' test_expect_success 'long reads are truncated' ' - printf "%s\n" foo "" >expect && + echo foo >expect && test-line-buffer <<-\EOF >actual && read 5 foo @@ -56,7 +55,7 @@ test_expect_success 'long reads are truncated' ' test_expect_success 'long copies are truncated' ' printf "%s\n" "" foo >expect && test-line-buffer <<-\EOF >actual && - read 0 + read 1 copy 5 foo -- cgit v1.2.3 From d280f68313eecb8b3838c70641a246382d5e5343 Mon Sep 17 00:00:00 2001 From: Jonathan Nieder Date: Sun, 2 Jan 2011 19:07:16 -0600 Subject: t0081 (line-buffer): add buffering tests POSIX makes the behavior of read(2) from a pipe fairly clear: a read from an empty pipe will block until there is data available and any other read will not block, prefering to return a partial result. Likewise, fread(3) and fgets(3) are clearly specified to act as though implemented by calling fgetc(3) in a simple loop. But the buffering behavior of fgetc is less clear. Luckily, no sane platform is going to implement fgetc by calling the equivalent of read(2) more than once. fgetc has to be able to return without filling its buffer to preserve errno when errors are encountered anyway. So let's assume the simpler behavior (trust) but add some tests to catch insane platforms that violate that when they come (verify). First check that fread can handle a 0-length read from an empty fifo. Because open(O_RDONLY) blocks until the writing end is open, open the writing end of the fifo in advance in a subshell. Next try short inputs from a pipe that is not filled all the way. Lastly (two tests) try very large inputs from a pipe that will not fit in the relevant buffers. The first of these tests reads a little more than 8192 bytes, which is BUFSIZ (the size of stdio's buffers) on this Linux machine. The second reads a little over 64 KiB (the pipe capacity on Linux) and is not run unless requested by setting the GIT_REMOTE_SVN_TEST_BIG_FILES environment variable. Signed-off-by: Jonathan Nieder --- t/t0081-line-buffer.sh | 110 ++++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 109 insertions(+), 1 deletion(-) (limited to 't/t0081-line-buffer.sh') diff --git a/t/t0081-line-buffer.sh b/t/t0081-line-buffer.sh index 68d6163995..33a728ed98 100755 --- a/t/t0081-line-buffer.sh +++ b/t/t0081-line-buffer.sh @@ -1,10 +1,76 @@ #!/bin/sh test_description="Test the svn importer's input handling routines. + +These tests exercise the line_buffer library, but their real purpose +is to check the assumptions that library makes of the platform's input +routines. Processes engaged in bi-directional communication would +hang if fread or fgets is too greedy. + +While at it, check that input of newlines and null bytes are handled +correctly. " . ./test-lib.sh -test_expect_success 'read greeting' ' +test -n "$GIT_REMOTE_SVN_TEST_BIG_FILES" && test_set_prereq EXPENSIVE + +generate_tens_of_lines () { + tens=$1 && + line=$2 && + + i=0 && + while test $i -lt "$tens" + do + for j in a b c d e f g h i j + do + echo "$line" + done && + : $((i = $i + 1)) || + return + done +} + +long_read_test () { + : each line is 10 bytes, including newline && + line=abcdefghi && + echo "$line" >expect && + + if ! test_declared_prereq PIPE + then + echo >&4 "long_read_test: need to declare PIPE prerequisite" + return 127 + fi && + tens_of_lines=$(($1 / 100 + 1)) && + lines=$(($tens_of_lines * 10)) && + readsize=$((($lines - 1) * 10 + 3)) && + copysize=7 && + rm -f input && + mkfifo input && + { + { + generate_tens_of_lines $tens_of_lines "$line" && + sleep 100 + } >input & + } && + test-line-buffer input <<-EOF >output && + read $readsize + copy $copysize + EOF + kill $! && + test_line_count = $lines output && + tail -n 1 actual && + test_cmp expect actual +} + +test_expect_success 'setup: have pipes?' ' + rm -f frob && + if mkfifo frob + then + test_set_prereq PIPE + fi +' + +test_expect_success 'hello world' ' echo HELLO >expect && test-line-buffer <<-\EOF >actual && read 6 @@ -13,6 +79,21 @@ test_expect_success 'read greeting' ' test_cmp expect actual ' +test_expect_success PIPE '0-length read, no input available' ' + >expect && + rm -f input && + mkfifo input && + { + sleep 100 >input & + } && + test-line-buffer input <<-\EOF >actual && + read 0 + copy 0 + EOF + kill $! && + test_cmp expect actual +' + test_expect_success '0-length read, send along greeting' ' echo HELLO >expect && test-line-buffer <<-\EOF >actual && @@ -23,6 +104,33 @@ test_expect_success '0-length read, send along greeting' ' test_cmp expect actual ' +test_expect_success PIPE '1-byte read, no input available' ' + printf "%s" ab >expect && + rm -f input && + mkfifo input && + { + { + printf "%s" a && + printf "%s" b && + sleep 100 + } >input & + } && + test-line-buffer input <<-\EOF >actual && + read 1 + copy 1 + EOF + kill $! && + test_cmp expect actual +' + +test_expect_success PIPE 'long read (around 8192 bytes)' ' + long_read_test 8192 +' + +test_expect_success PIPE,EXPENSIVE 'longer read (around 65536 bytes)' ' + long_read_test 65536 +' + test_expect_success 'buffer_read_string copes with null byte' ' >expect && q_to_nul <<-\EOF | test-line-buffer >actual && -- cgit v1.2.3 From e832f43c1d26bf70611d98b62d95870a99292add Mon Sep 17 00:00:00 2001 From: Jonathan Nieder Date: Sun, 2 Jan 2011 21:05:46 -0600 Subject: vcs-svn: add binary-safe read function buffer_read_string works well for non line-oriented input except for one problem: it does not tell the caller how many bytes were actually written. This means that unless one is very careful about checking for errors (and eof) the calling program cannot tell the difference between the string "foo" followed by an early end of file and the string "foo\0bar\0baz". So introduce a variant that reports the length, too, a thinner wrapper around strbuf_fread. Its result is written to a strbuf so the caller does not need to keep track of the number of bytes read. Signed-off-by: Jonathan Nieder --- t/t0081-line-buffer.sh | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) (limited to 't/t0081-line-buffer.sh') diff --git a/t/t0081-line-buffer.sh b/t/t0081-line-buffer.sh index 33a728ed98..a8eeb20645 100755 --- a/t/t0081-line-buffer.sh +++ b/t/t0081-line-buffer.sh @@ -151,6 +151,15 @@ test_expect_success 'skip, copy null byte' ' test_cmp expect actual ' +test_expect_success 'read null byte' ' + echo ">QhelloQ" | q_to_nul >expect && + q_to_nul <<-\EOF | test-line-buffer >actual && + binary 8 + QhelloQ + EOF + test_cmp expect actual +' + test_expect_success 'long reads are truncated' ' echo foo >expect && test-line-buffer <<-\EOF >actual && @@ -171,4 +180,13 @@ test_expect_success 'long copies are truncated' ' test_cmp expect actual ' +test_expect_success 'long binary reads are truncated' ' + echo ">foo" >expect && + test-line-buffer <<-\EOF >actual && + binary 5 + foo + EOF + test_cmp expect actual +' + test_done -- cgit v1.2.3 From cb3f87cf1ba90373fdc240d65a4d65434099d9a3 Mon Sep 17 00:00:00 2001 From: Jonathan Nieder Date: Sun, 2 Jan 2011 21:09:38 -0600 Subject: vcs-svn: allow input from file descriptor Based-on-patch-by: David Barr Signed-off-by: Jonathan Nieder --- t/t0081-line-buffer.sh | 9 +++++++++ 1 file changed, 9 insertions(+) (limited to 't/t0081-line-buffer.sh') diff --git a/t/t0081-line-buffer.sh b/t/t0081-line-buffer.sh index a8eeb20645..550fad0823 100755 --- a/t/t0081-line-buffer.sh +++ b/t/t0081-line-buffer.sh @@ -131,6 +131,15 @@ test_expect_success PIPE,EXPENSIVE 'longer read (around 65536 bytes)' ' long_read_test 65536 ' +test_expect_success 'read from file descriptor' ' + rm -f input && + echo hello >expect && + echo hello >input && + echo copy 6 | + test-line-buffer "&4" 4actual && + test_cmp expect actual +' + test_expect_success 'buffer_read_string copes with null byte' ' >expect && q_to_nul <<-\EOF | test-line-buffer >actual && -- cgit v1.2.3 From 7e2fe3a9fc816391b322ad9b3f2adf9342631db6 Mon Sep 17 00:00:00 2001 From: Jonathan Nieder Date: Thu, 24 Mar 2011 23:09:19 -0500 Subject: vcs-svn: remove buffer_read_string All previous users of buffer_read_string have already been converted to use the more intuitive buffer_read_binary, so remove the old API to avoid some confusion. Signed-off-by: Jonathan Nieder --- t/t0081-line-buffer.sh | 35 +++++++++++++---------------------- 1 file changed, 13 insertions(+), 22 deletions(-) (limited to 't/t0081-line-buffer.sh') diff --git a/t/t0081-line-buffer.sh b/t/t0081-line-buffer.sh index 550fad0823..1dbe1c9b08 100755 --- a/t/t0081-line-buffer.sh +++ b/t/t0081-line-buffer.sh @@ -53,7 +53,7 @@ long_read_test () { } >input & } && test-line-buffer input <<-EOF >output && - read $readsize + binary $readsize copy $copysize EOF kill $! && @@ -71,23 +71,23 @@ test_expect_success 'setup: have pipes?' ' ' test_expect_success 'hello world' ' - echo HELLO >expect && + echo ">HELLO" >expect && test-line-buffer <<-\EOF >actual && - read 6 + binary 6 HELLO EOF test_cmp expect actual ' test_expect_success PIPE '0-length read, no input available' ' - >expect && + printf ">" >expect && rm -f input && mkfifo input && { sleep 100 >input & } && test-line-buffer input <<-\EOF >actual && - read 0 + binary 0 copy 0 EOF kill $! && @@ -95,9 +95,9 @@ test_expect_success PIPE '0-length read, no input available' ' ' test_expect_success '0-length read, send along greeting' ' - echo HELLO >expect && + echo ">HELLO" >expect && test-line-buffer <<-\EOF >actual && - read 0 + binary 0 copy 6 HELLO EOF @@ -105,7 +105,7 @@ test_expect_success '0-length read, send along greeting' ' ' test_expect_success PIPE '1-byte read, no input available' ' - printf "%s" ab >expect && + printf ">%s" ab >expect && rm -f input && mkfifo input && { @@ -116,7 +116,7 @@ test_expect_success PIPE '1-byte read, no input available' ' } >input & } && test-line-buffer input <<-\EOF >actual && - read 1 + binary 1 copy 1 EOF kill $! && @@ -140,15 +140,6 @@ test_expect_success 'read from file descriptor' ' test_cmp expect actual ' -test_expect_success 'buffer_read_string copes with null byte' ' - >expect && - q_to_nul <<-\EOF | test-line-buffer >actual && - read 2 - Q - EOF - test_cmp expect actual -' - test_expect_success 'skip, copy null byte' ' echo Q | q_to_nul >expect && q_to_nul <<-\EOF | test-line-buffer >actual && @@ -170,18 +161,18 @@ test_expect_success 'read null byte' ' ' test_expect_success 'long reads are truncated' ' - echo foo >expect && + echo ">foo" >expect && test-line-buffer <<-\EOF >actual && - read 5 + binary 5 foo EOF test_cmp expect actual ' test_expect_success 'long copies are truncated' ' - printf "%s\n" "" foo >expect && + printf "%s\n" ">" foo >expect && test-line-buffer <<-\EOF >actual && - read 1 + binary 1 copy 5 foo -- cgit v1.2.3 From a892a2ddfefca2e25d595bb685c8853d023fde0a Mon Sep 17 00:00:00 2001 From: Jeff King Date: Tue, 29 Mar 2011 23:30:17 -0400 Subject: tests: kill backgrounded processes more robustly t0081 creates several background processes that write to a fifo and then go to sleep for a while (so the reader of the fifo does not see EOF). Each background process is made in a curly-braced block in the shell, and after we are done reading from the fifo, we use "kill $!" to kill it off. For a simple, single-command process, this works reliably and kills the child sleep process. But for more complex commands like "make_some_output && sleep", the results are less predictable. When executing under bash, we end up with a subshell that gets killed by the $! but leaves the sleep process still alive. This is bad not only for process hygeine (we are leaving random sleep processes to expire after a while), but also interacts badly with the "prove" command. When prove executes a test, it does not realize the test is done when it sees SIGCHLD, but rather waits until the test's stdout pipe is closed. The orphaned sleep process may keep that pipe open via test-lib's file descriptor 5, causing prove to hang for 100 seconds. The solution is to explicitly use a subshell and to exec the final sleep process, so that when we "kill $!" we get the process id of the sleep process. [jn: original patch by Jeff had some additional bits: 1. Wrap the "kill" in a test_when_finished, since we want to clean up the process whether the test succeeds or not. 2. The "kill" is part of our && chain for test success. It probably won't fail, but it can if the process has expired before we manage to kill it. So let's mark it as OK to fail. I'm postponing that for now.] Reported-by: Junio C Hamano Signed-off-by: Jeff King Signed-off-by: Jonathan Nieder --- t/t0081-line-buffer.sh | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) (limited to 't/t0081-line-buffer.sh') diff --git a/t/t0081-line-buffer.sh b/t/t0081-line-buffer.sh index 1dbe1c9b08..5067d1e15b 100755 --- a/t/t0081-line-buffer.sh +++ b/t/t0081-line-buffer.sh @@ -47,10 +47,10 @@ long_read_test () { rm -f input && mkfifo input && { - { + ( generate_tens_of_lines $tens_of_lines "$line" && - sleep 100 - } >input & + exec sleep 100 + ) >input & } && test-line-buffer input <<-EOF >output && binary $readsize @@ -109,11 +109,11 @@ test_expect_success PIPE '1-byte read, no input available' ' rm -f input && mkfifo input && { - { + ( printf "%s" a && printf "%s" b && - sleep 100 - } >input & + exec sleep 100 + ) >input & } && test-line-buffer input <<-\EOF >actual && binary 1 -- cgit v1.2.3 From 6908e999468d7eb531a1609cee37673c5d3ca04f Mon Sep 17 00:00:00 2001 From: Jonathan Nieder Date: Wed, 30 Mar 2011 03:11:41 -0500 Subject: Revert "t0081 (line-buffer): add buffering tests" This (morally) reverts commit d280f68313eecb8b3838c70641a246382d5e5343, which added some tests that are a pain to maintain and are not likely to find bugs in git. Helped-by: Johannes Sixt Signed-off-by: Jonathan Nieder Acked-by: Jeff King --- t/t0081-line-buffer.sh | 106 +------------------------------------------------ 1 file changed, 2 insertions(+), 104 deletions(-) (limited to 't/t0081-line-buffer.sh') diff --git a/t/t0081-line-buffer.sh b/t/t0081-line-buffer.sh index 5067d1e15b..bd83ed371a 100755 --- a/t/t0081-line-buffer.sh +++ b/t/t0081-line-buffer.sh @@ -2,74 +2,14 @@ test_description="Test the svn importer's input handling routines. -These tests exercise the line_buffer library, but their real purpose -is to check the assumptions that library makes of the platform's input -routines. Processes engaged in bi-directional communication would -hang if fread or fgets is too greedy. +These tests provide some simple checks that the line_buffer API +behaves as advertised. While at it, check that input of newlines and null bytes are handled correctly. " . ./test-lib.sh -test -n "$GIT_REMOTE_SVN_TEST_BIG_FILES" && test_set_prereq EXPENSIVE - -generate_tens_of_lines () { - tens=$1 && - line=$2 && - - i=0 && - while test $i -lt "$tens" - do - for j in a b c d e f g h i j - do - echo "$line" - done && - : $((i = $i + 1)) || - return - done -} - -long_read_test () { - : each line is 10 bytes, including newline && - line=abcdefghi && - echo "$line" >expect && - - if ! test_declared_prereq PIPE - then - echo >&4 "long_read_test: need to declare PIPE prerequisite" - return 127 - fi && - tens_of_lines=$(($1 / 100 + 1)) && - lines=$(($tens_of_lines * 10)) && - readsize=$((($lines - 1) * 10 + 3)) && - copysize=7 && - rm -f input && - mkfifo input && - { - ( - generate_tens_of_lines $tens_of_lines "$line" && - exec sleep 100 - ) >input & - } && - test-line-buffer input <<-EOF >output && - binary $readsize - copy $copysize - EOF - kill $! && - test_line_count = $lines output && - tail -n 1 actual && - test_cmp expect actual -} - -test_expect_success 'setup: have pipes?' ' - rm -f frob && - if mkfifo frob - then - test_set_prereq PIPE - fi -' - test_expect_success 'hello world' ' echo ">HELLO" >expect && test-line-buffer <<-\EOF >actual && @@ -79,21 +19,6 @@ test_expect_success 'hello world' ' test_cmp expect actual ' -test_expect_success PIPE '0-length read, no input available' ' - printf ">" >expect && - rm -f input && - mkfifo input && - { - sleep 100 >input & - } && - test-line-buffer input <<-\EOF >actual && - binary 0 - copy 0 - EOF - kill $! && - test_cmp expect actual -' - test_expect_success '0-length read, send along greeting' ' echo ">HELLO" >expect && test-line-buffer <<-\EOF >actual && @@ -104,33 +29,6 @@ test_expect_success '0-length read, send along greeting' ' test_cmp expect actual ' -test_expect_success PIPE '1-byte read, no input available' ' - printf ">%s" ab >expect && - rm -f input && - mkfifo input && - { - ( - printf "%s" a && - printf "%s" b && - exec sleep 100 - ) >input & - } && - test-line-buffer input <<-\EOF >actual && - binary 1 - copy 1 - EOF - kill $! && - test_cmp expect actual -' - -test_expect_success PIPE 'long read (around 8192 bytes)' ' - long_read_test 8192 -' - -test_expect_success PIPE,EXPENSIVE 'longer read (around 65536 bytes)' ' - long_read_test 65536 -' - test_expect_success 'read from file descriptor' ' rm -f input && echo hello >expect && -- cgit v1.2.3