Welcome to mirror list, hosted at ThFree Co, Russian Federation.

locked_mkdir_waiters.sh « test - github.com/freebsd/poudriere.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: d76dfa4c88a72fb8487c344f7dd5f6c07964f730 (plain)
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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
set -e
. common.locked_mkdir.sh
set +e

LOCK1="${LOCKBASE}/lock_waiters"

# Multiple waiters - one should win
max=1
n=0
[ -d "${LOCK1}" ]
assert_not 0 $? "$0:$LINENO: Lock dir should not exist"
until [ "${n}" -eq "${max}" ]; do
	[ -d "${LOCK1}" ]
	assert_not 0 $? "$0:$LINENO: Lock dir should not exist"

	echo "$0:$LINENO: Parent pid $$ has lock ${n}"
	time=$(date +%s)
	locked_mkdir 0 ${LOCK1} $$
	assert 0 $? "$0:$LINENO: Lock should succeed"
	assert_pid "$0:$LINENO" "${LOCK1}" "$$"
	nowtime=$(date +%s)
	elapsed=$((${nowtime} - ${time}))
	[ "${elapsed}" -le 3 ]
	assert 0 $? "$0:$LINENO: Lock shouldn't sleep elapsed=${elapsed} ${n}"

	# Background waiters
	(
		mypid=$(sh -c 'echo $PPID')
		time=$(date +%s)
		locked_mkdir 5 "${LOCK1}" "${mypid}"
		got_lock=$?
		echo "$0:$LINENO: Pid ${mypid} got_lock=${got_lock} ${n}"
		nowtime=$(date +%s)
		elapsed=$((${nowtime} - ${time}))
		[ "${elapsed}" -le 7 ]
		assert 0 $? "$0:$LINENO: Lock slept too long elapsed=${elapsed} ${n}"
		if [ "${got_lock}" -eq 0 ]; then
			assert_pid "$0:$LINENO" "${LOCK1}" "${mypid}" "${n}"
		fi
		if [ "${got_lock}" -eq 0 ]; then
			# Wait a bit to not allow other waiters to win
			sleep 7
		fi
		exit ${got_lock}
	) &
	pid_unlock1=$!
	(
		mypid=$(sh -c 'echo $PPID')
		time=$(date +%s)
		locked_mkdir 5 "${LOCK1}" "${mypid}"
		got_lock=$?
		echo "$0:$LINENO: Pid ${mypid} got_lock=${got_lock} ${n}"
		nowtime=$(date +%s)
		elapsed=$((${nowtime} - ${time}))
		[ "${elapsed}" -le 7 ]
		assert 0 $? "$0:$LINENO: Lock slept too long elapsed=${elapsed} ${n}"
		if [ "${got_lock}" -eq 0 ]; then
			assert_pid "$0:$LINENO" "${LOCK1}" "${mypid}" "${n}"
		fi
		if [ "${got_lock}" -eq 0 ]; then
			# Wait a bit to not allow other waiters to win
			sleep 7
		fi
		exit ${got_lock}
	) &
	pid_unlock2=$!
	(
		mypid=$(sh -c 'echo $PPID')
		time=$(date +%s)
		locked_mkdir 5 "${LOCK1}" "${mypid}"
		got_lock=$?
		echo "$0:$LINENO: Pid ${mypid} got_lock=${got_lock} ${n}"
		nowtime=$(date +%s)
		elapsed=$((${nowtime} - ${time}))
		[ "${elapsed}" -le 7 ]
		assert 0 $? "$0:$LINENO: Lock slept too long elapsed=${elapsed} ${n}"
		if [ "${got_lock}" -eq 0 ]; then
			assert_pid "$0:$LINENO" "${LOCK1}" "${mypid}" "${n}"
		fi
		if [ "${got_lock}" -eq 0 ]; then
			# Wait a bit to not allow other waiters to win
			sleep 7
		fi
		exit ${got_lock}
	) &
	pid_unlock3=$!
	(
		mypid=$(sh -c 'echo $PPID')
		time=$(date +%s)
		locked_mkdir 5 "${LOCK1}" "${mypid}"
		got_lock=$?
		echo "$0:$LINENO: Pid ${mypid} got_lock=${got_lock} ${n}"
		nowtime=$(date +%s)
		elapsed=$((${nowtime} - ${time}))
		[ "${elapsed}" -le 7 ]
		assert 0 $? "$0:$LINENO: Lock slept too long elapsed=${elapsed} ${n}"
		if [ "${got_lock}" -eq 0 ]; then
			assert_pid "$0:$LINENO" "${LOCK1}" "${mypid}" "${n}"
		fi
		if [ "${got_lock}" -eq 0 ]; then
			# Wait a bit to not allow other waiters to win
			sleep 7
		fi
		exit ${got_lock}
	) &
	pid_unlock4=$!

	# Drop the lock for a child to pickup
	sleep 2
	echo "$0:$LINENO: Parent pid $$ dropping lock ${n}"
	rmdir "${LOCK1}"
	assert 0 $? "$0:$LINENO: rmdir should succeed ${n}"

	# XXX: Not sleeping should serialize and all should pass
	# Wait for each to finish its wait task
	sleep 6
	# Now a child should own the lock but only *1* should own it. Failed
	# waiters return 75 (EX_TEMPFAIL). Any other non-zero is fatal.
	_wait "${pid_unlock1}"
	status_unlock1=$?
	_wait "${pid_unlock2}"
	status_unlock2=$?
	_wait "${pid_unlock3}"
	status_unlock3=$?
	_wait "${pid_unlock4}"
	status_unlock4=$?

	nowtime=$(date +%s)

	assert $((75 * 3)) $((status_unlock1 + status_unlock2 + status_unlock3 + \
		status_unlock4)) "$0:$LINENO: 3 waiters should timeout on lock and 1 should win ${n}"
	[ -d "${LOCK1}" ]
	assert 0 $? "$0:$LINENO: Lock dir should exist ${n}"

	# Make sure the one who thinks it won actually did win in the end
	[ "${status_unlock1}" -eq 0 ] && assert_pid "$0:$LINENO" "${LOCK1}" "${pid_unlock1}"
	[ "${status_unlock2}" -eq 0 ] && assert_pid "$0:$LINENO" "${LOCK1}" "${pid_unlock2}"
	[ "${status_unlock3}" -eq 0 ] && assert_pid "$0:$LINENO" "${LOCK1}" "${pid_unlock3}"
	[ "${status_unlock4}" -eq 0 ] && assert_pid "$0:$LINENO" "${LOCK1}" "${pid_unlock4}"

	elapsed=$((${nowtime} - ${time}))
	# This is hard to properly test due to the extra sleeps in the children
	[ "${elapsed}" -le 20 ]
	assert 0 $? "$0:$LINENO: Children locks took too long elapsed=${elapsed} ${n}"

	# Should be able to take the lock immediately now without removing the
	# dir since children are all dead.
	[ -d "${LOCK1}" ]
	assert 0 $? "$0:$LINENO: Lock dir should exist ${n}"
	time=$(date +%s)
	locked_mkdir 2 ${LOCK1} $$
	assert 0 $? "$0:$LINENO: Unlocked dir should succeed to lock ${n}"
	[ -d "${LOCK1}" ]
	assert 0 $? "$0:$LINENO: Lock dir should exist ${n}"
	assert_pid "$0:$LINENO" "${LOCK1}" "$$"
	nowtime=$(date +%s)
	elapsed=$((${nowtime} - ${time}))
	[ "${elapsed}" -lt 4 ]
	assert 0 $? "$0:$LINENO: Unlocked dir should not wait to lock ${n}"

	rmdir "${LOCK1}"
	assert 0 $? "$0:$LINENO: rmdir should succeed ${n}"

	n=$((n + 1))
done

rm -rf "${LOCKBASE}"