1#!/usr/bin/env bash
2# This script is used to deflake inherently flaky tsan tests.
3# It is invoked from lit tests as:
4# %deflake $THRESHOLD  mybinary
5# which is then substituted by lit to:
6# $(dirname %s)/deflake.bash $THRESHOLD mybinary
7# - When TSAN_TEST_DEFLAKE_THRESHOLD is defined to a positive integer value,
8#   THRESHOLD will be the defined value.
9# - When TSAN_TEST_DEFLAKE_THRESHOLD is not defined, THRESHOLD will be 10.
10# The script runs the target program up to $THRESHOLD times,
11# until it fails (i.e. produces a race report).
12
13THRESHOLD="${1}"
14shift
15
16# Early exit if $THRESHOLD is not a non-negative integer
17[[  "${THRESHOLD}" =~ ^[0-9]+$ ]] || exit 1
18
19while (( THRESHOLD-- )); do
20	OUT=`$@ 2>&1`
21	if [[ $? != 0 ]]; then
22		echo "$OUT"
23		exit 0
24	fi
25done
26exit 1
27