docs: Move release notes to their own section.
[nbdkit/ericb.git] / tests / test-parallel-sh.sh
blob15d8875d7dea53ef7e3424679ebf75795bb281be
1 #!/usr/bin/env bash
2 # nbdkit
3 # Copyright (C) 2017-2019 Red Hat Inc.
5 # Redistribution and use in source and binary forms, with or without
6 # modification, are permitted provided that the following conditions are
7 # met:
9 # * Redistributions of source code must retain the above copyright
10 # notice, this list of conditions and the following disclaimer.
12 # * Redistributions in binary form must reproduce the above copyright
13 # notice, this list of conditions and the following disclaimer in the
14 # documentation and/or other materials provided with the distribution.
16 # * Neither the name of Red Hat nor the names of its contributors may be
17 # used to endorse or promote products derived from this software without
18 # specific prior written permission.
20 # THIS SOFTWARE IS PROVIDED BY RED HAT AND CONTRIBUTORS ''AS IS'' AND
21 # ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
22 # THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
23 # PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL RED HAT OR
24 # CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
25 # SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
26 # LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
27 # USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
28 # ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
29 # OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
30 # OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31 # SUCH DAMAGE.
33 source ./functions.sh
35 # Check file-data was created by Makefile and qemu-io exists.
36 requires test -f file-data
37 requires qemu-io --version
39 nbdkit --dump-plugin sh | grep -q ^thread_model=parallel ||
40 { echo "nbdkit lacks support for parallel requests"; exit 77; }
42 cleanup_fn rm -f test-parallel-sh.data test-parallel-sh.out test-parallel-sh.script
44 # Populate file, and sanity check that qemu-io can issue parallel requests
45 printf '%1024s' . > test-parallel-sh.data
46 qemu-io -f raw -c "aio_write -P 1 0 512" -c "aio_write -P 2 512 512" \
47 -c aio_flush test-parallel-sh.data ||
48 { echo "'qemu-io' can't drive parallel requests"; exit 77; }
50 # Set up the sh plugin to delay both reads and writes (for a good
51 # chance that parallel requests are in flight), and with writes longer
52 # than reads (to more easily detect if out-of-order completion
53 # happens). This test may have spurious failures under heavy loads on
54 # the test machine, where tuning the delays may help.
56 # Also test for leaked fds when possible: nbdkit does not close fds it
57 # inherited, but other than 0, 1, 2, and the fd associated with the
58 # script, the child shell should not see any new fds not also present
59 # in nbdkit's parent environment. When testing for the count of open
60 # fds, use ls in a subshell (rather than a glob directly within the
61 # shell under test), to avoid yet another fd open on the /proc/self/fd
62 # directory.
64 curr_fds=
65 if test -d /proc/$$/fd; then
66 curr_fds=$(/usr/bin/env bash -c '(ls /proc/$$/fd)' | wc -w)
68 echo "using curr_fds=$curr_fds"
70 cat > test-parallel-sh.script <<EOF
71 #!/usr/bin/env bash
72 f=test-parallel-sh.data
73 if ! test -f \$f; then
74 echo "can't locate test-parallel-sh.data" >&2; exit 5
76 if test -d /proc/\$\$/fd; then
78 if test \$( ls /proc/\$\$/fd | wc -w ) -ne \$(($curr_fds + 1)); then
79 echo "there seem to be leaked fds, curr_fds=$curr_fds" >&2
80 ls -l /proc/\$\$/fd >&2
81 exit 1
83 ) || exit 5
85 case \$1 in
86 thread_model) echo parallel ;;
87 get_size) stat -L -c %s \$f || exit 1 ;;
88 pread) dd iflag=skip_bytes,count_bytes skip=\$4 count=\$3 if=\$f || exit 1 ;;
89 pwrite) dd oflag=seek_bytes conv=notrunc seek=\$4 of=\$f || exit 1 ;;
90 can_write) ;;
91 *) exit 2 ;;
92 esac
93 exit 0
94 EOF
95 chmod +x test-parallel-sh.script
97 # With --threads=1, the write should complete first because it was issued first
98 nbdkit -v -t 1 -U - --filter=delay sh test-parallel-sh.script \
99 wdelay=2 rdelay=1 --run 'qemu-io -f raw -c "aio_write -P 2 512 512" \
100 -c "aio_read -P 1 0 512" -c aio_flush $nbd' |
101 tee test-parallel-sh.out
102 if test "$(grep '512/512' test-parallel-sh.out)" != \
103 "wrote 512/512 bytes at offset 512
104 read 512/512 bytes at offset 0"; then
105 exit 1
108 # With default --threads, the faster read should complete first
109 nbdkit -v -U - --filter=delay sh test-parallel-sh.script \
110 wdelay=2 rdelay=1 --run 'qemu-io -f raw -c "aio_write -P 2 512 512" \
111 -c "aio_read -P 1 0 512" -c aio_flush $nbd' |
112 tee test-parallel-sh.out
113 if test "$(grep '512/512' test-parallel-sh.out)" != \
114 "read 512/512 bytes at offset 0
115 wrote 512/512 bytes at offset 512"; then
116 exit 1
119 # With --filter=noparallel, the write should complete first because it was
120 # issued first. Also test that the log filter doesn't leak an fd
121 nbdkit -v -U - --filter=noparallel --filter=log --filter=delay \
122 sh test-parallel-sh.script logfile=/dev/null \
123 wdelay=2 rdelay=1 --run 'qemu-io -f raw -c "aio_write -P 2 512 512" \
124 -c "aio_read -P 1 0 512" -c aio_flush $nbd' |
125 tee test-parallel-sh.out
126 if test "$(grep '512/512' test-parallel-sh.out)" != \
127 "wrote 512/512 bytes at offset 512
128 read 512/512 bytes at offset 0"; then
129 exit 1
132 exit 0