File: timeout.sh

package info (click to toggle)
saunafs 5.1.2-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 12,820 kB
  • sloc: cpp: 109,287; sh: 18,755; python: 4,737; ansic: 4,023; makefile: 60; awk: 17
file content (97 lines) | stat: -rw-r--r-- 3,047 bytes parent folder | download
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
timeout_killer_thread() {
	# Python3 is used here to perform floating-point multiplication in timeout computation
	assert_program_installed python3

	local machine_environment_multiplier=1
	if [[ ! -z "${SAUNAFS_TEST_TIMEOUT_MULTIPLIER:-}" ]]; then
		machine_environment_multiplier="$SAUNAFS_TEST_TIMEOUT_MULTIPLIER"
	fi

	while ! test_frozen; do
		sleep 1
		if test_frozen; then
			return
		fi

		local multiplier=$(cat "$test_timeout_multiplier_file")
		local begin_ts=$(cat "$test_timeout_begin_ts_file") || true
		local value_string=$(cat "$test_timeout_value_file") || true
		local value=$(($(date +%s -d "$value_string") - $(date +%s)))
		local now_ts=$(date +%s)

		# Increase test time in Windows environment
		if is_windows_system; then
			multiplier=$((5 * $multiplier))
		fi

		if [[ -z $begin_ts || -z $value_string || -z $multiplier ]]; then
			# A race with timeout_set occurred (it truncates the endTS file and then writes it)
			# or a race with test_cleanup (test_timeout_end_ts_file has just been removed)
			continue
		fi

		local total_timeout=$(($value * $multiplier * $machine_environment_multiplier))
		local end_ts=$(($begin_ts + $total_timeout))

		if (( now_ts >= end_ts )); then
			local format=$(cat <<-END
				Test timed out (Timeout: %d seconds)
				   Timeout components: TEST TIMEOUT VALUE | MACHINE'S MULTIPLIER | FRAMEWORK'S MULTIPLIER (e.g. for running under Valgrind)
				         [in seconds]  %18s | %20s | %22s
				 timeout_set argument: %18s |
			END
			)

			local test_timeout_message=$( printf "$format" \
				"$total_timeout" \
				"$value" "$machine_environment_multiplier" "$multiplier" \
				"$value_string" )

			test_add_failure "$test_timeout_message"
			test_freeze_result
			killall -9 -u $(whoami)
		fi
	done
}

timeout_init() {
	test_timeout_begin_ts_file="$TEMP_DIR/$(unique_file)_timeout_beginTS.txt"
	test_timeout_multiplier_file="$TEMP_DIR/$(unique_file)_timeout_multiplier.txt"
	test_timeout_value_file="$TEMP_DIR/$(unique_file)_timeout_value.txt"

	# default timeout values
	timeout_set "30 seconds"
	timeout_set_multiplier 1

	# Parentheses below are needed to make 'wait' command work in tests.
	# They make the killer thread to be a job owned by a temporary subshell, not ours
	( timeout_killer_thread & )
}

timeout_set() {
	echo "$*" > "$test_timeout_value_file"
	date +%s > "$test_timeout_begin_ts_file"
}

timeout_set_multiplier() {
	echo "$1" > "$test_timeout_multiplier_file"
}

timeout_get_multiplier() {
	cat "$test_timeout_multiplier_file"
}

# takes as parameter timeout (e.g. '1 minute') and rescales it by multiplier
# prints result in seconds to standard output (e.g. '900 seconds')
timeout_rescale() {
	local multiplier=$(timeout_get_multiplier)
	local value=$(($(date +%s -d "$1") - $(date +%s)))
	echo $((value * multiplier)) seconds
}

# takes as parameter timeout in seconds (e.g. '60') and rescales it by multiplier
# prints result to standard output (e.g. '900')
timeout_rescale_seconds() {
	local multiplier=$(timeout_get_multiplier)
	echo $(($1 * multiplier))
}