vitess.io/vitess@v0.16.2/tools/unit_test_race.sh (about) 1 #!/bin/bash 2 3 # Copyright 2019 The Vitess Authors. 4 # 5 # Licensed under the Apache License, Version 2.0 (the "License"); 6 # you may not use this file except in compliance with the License. 7 # You may obtain a copy of the License at 8 # 9 # http://www.apache.org/licenses/LICENSE-2.0 10 # 11 # Unless required by applicable law or agreed to in writing, software 12 # distributed under the License is distributed on an "AS IS" BASIS, 13 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 # See the License for the specific language governing permissions and 15 # limitations under the License. 16 17 source build.env 18 19 if [[ -z $VT_GO_PARALLEL && -n $VT_GO_PARALLEL_VALUE ]]; then 20 VT_GO_PARALLEL="-p $VT_GO_PARALLEL_VALUE" 21 fi 22 23 # All Go packages with test files. 24 # Output per line: <full Go package name> <all _test.go files in the package>* 25 26 packages_with_tests=$(go list -f '{{if len .TestGoFiles}}{{.ImportPath}} {{join .TestGoFiles " "}}{{end}}{{if len .XTestGoFiles}}{{.ImportPath}} {{join .XTestGoFiles " "}}{{end}}' ./go/... | sort) 27 28 # exclude end to end tests 29 packages_to_test=$(echo "$packages_with_tests" | cut -d" " -f1 | grep -v "endtoend") 30 all_except_flaky_tests=$(echo "$packages_to_test" | grep -vE ".+ .+_flaky_test\.go" | cut -d" " -f1 | grep -v "endtoend") 31 flaky_tests=$(echo "$packages_to_test" | grep -E ".+ .+_flaky_test\.go" | cut -d" " -f1) 32 33 # Flaky tests have the suffix "_flaky_test.go". 34 # Exclude endtoend tests 35 all_except_flaky_tests=$(echo "$packages_with_tests" | grep -vE ".+ .+_flaky_test\.go" | cut -d" " -f1 | grep -v "endtoend") 36 flaky_tests=$(echo "$packages_with_tests" | grep -E ".+ .+_flaky_test\.go" | cut -d" " -f1) 37 38 # Run non-flaky tests. 39 echo "$all_except_flaky_tests" | xargs go test $VT_GO_PARALLEL -race -count=1 40 if [ $? -ne 0 ]; then 41 echo "ERROR: Go unit tests failed. See above for errors." 42 echo 43 echo "This should NOT happen. Did you introduce a flaky unit test?" 44 echo "If so, please rename it to the suffix _flaky_test.go." 45 exit 1 46 fi 47 48 echo '# Flaky tests (3 attempts permitted)' 49 50 # Run flaky tests sequentially. Retry when necessary. 51 for pkg in $flaky_tests; do 52 max_attempts=3 53 attempt=1 54 # Set a timeout because some tests may deadlock when they flake. 55 until go test -timeout 2m $VT_GO_PARALLEL $pkg -race -count=1; do 56 echo "FAILED (try $attempt/$max_attempts) in $pkg (return code $?). See above for errors." 57 if [ $((++attempt)) -gt $max_attempts ]; then 58 echo "ERROR: Flaky Go unit tests in package $pkg failed too often (after $max_attempts retries). Please reduce the flakiness." 59 exit 1 60 fi 61 done 62 done 63