blob: 100daab080c25ffac88c8b4cd996022573a00fcf (
plain) (
blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
|
# Test zsh/system module
%prep
if zmodload -s zsh/system && zmodload -s zsh/zselect; then
tst_dir=V14.tmp
mkdir -p -- $tst_dir
else
ZTST_unimplemented='the zsh/system and zsh/zselect modules are not available'
fi
: > $tst_dir/file # File on which to acquire flock.
%test
(
zsystem flock -t 0 -i 0.000001 $tst_dir/file &&
zsystem flock -t 0.1 -i 0.000001 $tst_dir/file &&
zsystem flock -t 0.1 -i 0.0000001 $tst_dir/file &&
zsystem flock -t 1 -i 0.000001 $tst_dir/file
)
0:zsystem flock valid time arguments
(
zsystem flock -t 1073741824 $tst_dir/file ||
zsystem flock -t 1e100 $tst_dir/file ||
zsystem flock -i -1 $tst_dir/file ||
zsystem flock -i 0 $tst_dir/file ||
zsystem flock -i 1e100 $tst_dir/file
)
1:zsystem flock invalid time arguments
?(eval):zsystem:2: flock: invalid timeout value: '1073741824'
?(eval):zsystem:3: flock: invalid timeout value: '1e100'
?(eval):zsystem:4: flock: invalid interval value: '-1'
?(eval):zsystem:5: flock: invalid interval value: '0'
?(eval):zsystem:6: flock: invalid interval value: '1e100'
(
# Lock file for 1 second in the background.
lock_flag=$tst_dir/locked1
(zsystem flock $tst_dir/file \
&& touch $lock_flag \
&& zselect -t 100
mv $lock_flag $lock_flag.done) &
# Wait until sub-shell above has started.
while ! [[ -f $lock_flag || -f $lock_flag.done ]]; do
zselect -t 1
done
if [[ -f $lock_flag.done ]]; then
echo "Background shell should not have completed already." 1>&2
else
# Attempt to lock file with 0.5 second timeout: must fail.
zsystem flock -t 0.5 $tst_dir/file
fi
)
2:zsystem flock unsuccessful wait test
F:This timing test might fail due to process scheduling issues unrelated to zsh.
(
# Lock file for 0.5 second in the background.
lock_flag=$tst_dir/locked2
(zsystem flock $tst_dir/file \
&& touch $lock_flag \
&& zselect -t 50
mv $lock_flag $lock_flag.done) &
# Wait until sub-shell above has started.
while ! [[ -f $lock_flag || -f $lock_flag.done ]]; do
zselect -t 1
done
if [[ -f $lock_flag.done ]]; then
echo "Background shell should not have completed already." 1>&2
fi
typeset -F SECONDS
start=$SECONDS
# Attempt to lock file without a timeout:
# must succeed after sub-shell above releases it (0.5 second).
if zsystem flock $tst_dir/file; then
elapsed=$[ $SECONDS - $start ]
if [[ $elapsed -ge 0.3 && $elapsed -le 0.7 ]]; then
echo "elapsed time seems OK" 1>&2
else
echo "elapsed time $elapsed should be ~ 0.5 second" 1>&2
fi
fi
)
0:zsystem flock successful wait test, no timeout
?elapsed time seems OK
F:This timing test might fail due to process scheduling issues unrelated to zsh.
(
# Lock file for 0.5 second in the background.
lock_flag=$tst_dir/locked3
(zsystem flock $tst_dir/file \
&& touch $lock_flag \
&& zselect -t 50
mv $lock_flag $lock_flag.done) &
# Wait until sub-shell above has started.
while ! [[ -f $lock_flag || -f $lock_flag.done ]]; do
zselect -t 1
done
if [[ -f $lock_flag.done ]]; then
echo "Background shell should not have completed already." 1>&2
fi
typeset -F SECONDS
start=$SECONDS
# Attempt to lock file with 1-second timeout:
# must succeed 1 second after start because we retry every 1 second.
if zsystem flock -t 1 $tst_dir/file; then
elapsed=$[ $SECONDS - $start ]
if [[ $elapsed -ge 0.8 && $elapsed -le 1.2 ]]; then
echo "elapsed time seems OK" 1>&2
else
echo "elapsed time $elapsed should be ~ 1 second" 1>&2
fi
fi
)
0:zsystem flock successful wait test, integral seconds
?elapsed time seems OK
F:This timing test might fail due to process scheduling issues unrelated to zsh.
(
# Lock file for 0.25 second in the background.
lock_flag=$tst_dir/locked4
(zsystem flock $tst_dir/file \
&& touch $lock_flag \
&& zselect -t 25
mv $lock_flag $lock_flag.done) &
# Wait until sub-shell above has started.
while ! [[ -f $lock_flag || -f $lock_flag.done ]]; do
zselect -t 1
done
if [[ -f $lock_flag.done ]]; then
echo "Background shell should not have completed already." 1>&2
fi
typeset -F SECONDS
start=$SECONDS
# Attempt to lock file with 0.4-second timeout, retrying every 0.1 second:
# must succeed 0.3 second after start.
if zsystem flock -t 0.4 -i 0.1 $tst_dir/file; then
elapsed=$[ $SECONDS - $start ]
if [[ $elapsed -ge 0.2 && $elapsed -le 0.5 ]]; then
echo "elapsed time seems OK" 1>&2
else
echo "elapsed time $elapsed should be ~ 0.3 second" 1>&2
fi
fi
)
0:zsystem flock successful wait test, fractional seconds
?elapsed time seems OK
F:This timing test might fail due to process scheduling issues unrelated to zsh.
|