Source-Changes-HG archive

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index][Old Index]

[src/trunk]: src/tests/usr.bin/mixerctl Make sure that the open of /dev/pad0 ...



details:   https://anonhg.NetBSD.org/src/rev/312d0f1627b8
branches:  trunk
changeset: 825500:312d0f1627b8
user:      kre <kre%NetBSD.org@localhost>
date:      Tue Jul 18 12:40:57 2017 +0000

description:
Make sure that the open of /dev/pad0 has succeeded (or at the very
least been attempted) before attempting to open /dev/mixer to determine
if the system being tested has audio or not.  Leaving this for the background
cat command to do causes a race between that command and the parent sh.
Move this code to a helper function to avoid duplicating it.

Also avoid attempting to kill the background cat if it was never created.
The kill is likely unnecessary anyway, ATF seems to clean up processes
like that one without assistance.   Which is a good thing, as the kill
does not happen if the test is skipped, or fails.

diffstat:

 tests/usr.bin/mixerctl/t_mixerctl.sh |  50 ++++++++++++++++++++++++-----------
 1 files changed, 34 insertions(+), 16 deletions(-)

diffs (94 lines):

diff -r 0d5c0fa3f869 -r 312d0f1627b8 tests/usr.bin/mixerctl/t_mixerctl.sh
--- a/tests/usr.bin/mixerctl/t_mixerctl.sh      Tue Jul 18 09:00:55 2017 +0000
+++ b/tests/usr.bin/mixerctl/t_mixerctl.sh      Tue Jul 18 12:40:57 2017 +0000
@@ -1,17 +1,43 @@
-# $NetBSD: t_mixerctl.sh,v 1.6 2017/07/03 09:08:35 nat Exp $
+# $NetBSD: t_mixerctl.sh,v 1.7 2017/07/18 12:40:57 kre Exp $
+
+audio_setup() {
+       # Open /dev/pad0 so we have a configured audio device.
+       # Do it in a way that guarantees the open happens before
+       # we proceed to the (</dev/mixer) below (do not expect
+       # cat to be running in time to open the device.)
+
+       # Note: it is not important that the open of pad0 succeeds,
+       # if there is real audio hardware on the system, that can (will)
+       # be used instead, and having pad0 open is irrelevant.
+       # So, no errors reported if pad0 open fails.  If there turns
+       # out to be no audio device of any kind, then the open of the
+       # mixer will fail, causing the test to be skipped.
+
+       # Saving padpid and later killing it seems to be unnecessary,
+       # ATF appears to killpg() the process after the test finishes
+       # which is a good thing, otherwise a test that is skipped/fails
+       # would not kill the cat (doing it in a cleanup function is not
+       # convenient as it is a different execution environment, so shared
+       # variables, we would need to put $padpid in a file.)
+
+       unset padpid
+       { { cat >/dev/null & } < /dev/pad0 ; } 2>/dev/null && padpid=$!
+
+       (</dev/mixer) >/dev/null 2>&1 ||
+           atf_skip "no audio mixer available in kernel"
+}
 
 atf_test_case noargs_usage
 noargs_usage_head() {
        atf_set "descr" "Ensure mixerctl(1) with no args prints a usage message"
 }
 noargs_usage_body() {
-       cat /dev/pad0 > /dev/null 2>&1 &
-       padpid=$!
+       audio_setup
 
        atf_check -s exit:0 -o not-empty -e ignore \
                mixerctl
 
-       kill -HUP ${padpid} 2>/dev/null
+       ${padpid+kill -HUP ${padpid}} 2>/dev/null || :
 }
 
 atf_test_case showvalue
@@ -19,18 +45,14 @@
        atf_set "descr" "Ensure mixerctl(1) can print the value for all variables"
 }
 showvalue_body() {
-       cat /dev/pad0 > /dev/null 2>&1 &
-       padpid=$!
-
-       (</dev/mixer) >/dev/null 2>&1 ||
-           atf_skip "no audio mixer available in kernel"
+       audio_setup
 
        for var in $(mixerctl -a | awk -F= '{print $1}'); do
                atf_check -s exit:0 -e ignore -o match:"^${var}=" \
                        mixerctl ${var}
        done
 
-       kill -HUP ${padpid} 2>/dev/null         # may have exited already
+       ${padpid+kill -HUP ${padpid}} 2>/dev/null || :
 }
 
 atf_test_case nflag
@@ -38,11 +60,7 @@
        atf_set "descr" "Ensure 'mixerctl -n' actually suppresses some output"
 }
 nflag_body() {
-       cat /dev/pad0 > /dev/null 2>&1 &
-       padpid=$!
-
-       (</dev/mixer) >/dev/null 2>&1 ||
-           atf_skip "no audio mixer available in kernel"
+       audio_setup
 
        varname="$(mixerctl -a | sed -e 's/=.*//' -e q)"
 
@@ -52,7 +70,7 @@
        atf_check -s exit:0 -o not-match:"${varname}" -e ignore \
                mixerctl -n ${varname}
 
-       kill -HUP ${padpid} 2>/dev/null
+       ${padpid+kill -HUP ${padpid}} 2>/dev/null || :
 }
 
 atf_test_case nonexistant_device



Home | Main Index | Thread Index | Old Index