Files
kopia/internal/testutil/testutil.go
Jarek Kowalski 9a756c719f Enabled race detector in CI, fixed a few data races (#919)
* content: fixed data race in IterateUnreferencedBlobs

* upload: fixed data race between uploader and estimator

* testing: fixed data race in repo/blob/logging test

* makefile: run tests on CI/linux/amd64 with -race

* robustness: fixed test race

* content: fixed data race getContentDataUnlocked that triggers TestParallelWrites - looks scary but in practice very hard to trigger in real life and does not cause data corruption

* testing: reduce test complexity under race detector

* server: fixed minor race in refreshStatus()

* testing: reduced depth of sharedTestDataDir2

* ci: run race detector in separate job

* ci: run unit test race detector in parallel to integration tests
2021-04-02 18:21:04 -07:00

61 lines
1.3 KiB
Go

package testutil
import (
"fmt"
"os"
"runtime"
"strings"
"testing"
"github.com/kopia/kopia/internal/buf"
)
// TestSkipUnlessCI skips the current test with a provided message, except when running
// in CI environment, in which case it causes hard failure.
func TestSkipUnlessCI(t *testing.T, msg string, args ...interface{}) {
t.Helper()
if len(args) > 0 {
msg = fmt.Sprintf(msg, args...)
}
if os.Getenv("CI") != "" {
t.Fatal(msg)
} else {
t.Skip(msg)
}
}
// TestSkipOnCIUnlessLinuxAMD64 skips the current test if running on CI unless the environment is Linux/AMD64.
func TestSkipOnCIUnlessLinuxAMD64(t *testing.T) {
t.Helper()
if os.Getenv("CI") != "" && runtime.GOOS+"/"+runtime.GOARCH != "linux/amd64" {
t.Skip("test not supported in this environment.")
}
}
// ShouldReduceTestComplexity returns true if test complexity should be reduced on the current machine.
func ShouldReduceTestComplexity() bool {
if isRaceDetector {
return true
}
return strings.Contains(runtime.GOARCH, "arm")
}
// MyTestMain runs tests and verifies some post-run invariants.
func MyTestMain(m *testing.M) {
v := m.Run()
if ap := buf.ActivePools(); len(ap) != 0 {
for _, v := range ap {
fmt.Fprintf(os.Stderr, "test did not release pool allocated from: %v\n", v)
}
os.Exit(1)
}
os.Exit(v)
}