-
Notifications
You must be signed in to change notification settings - Fork 45
/
verify.go
64 lines (56 loc) · 1.52 KB
/
verify.go
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
// Package testutilsext is purely for test utilities that may access other packages
// in the codebase that tend to use testutils.
package testutilsext
import (
"context"
"fmt"
"net/http"
"os"
"runtime"
"time"
"github.com/edaniels/golog"
"go.uber.org/goleak"
"google.golang.org/api/option"
"google.golang.org/api/transport"
"go.viam.com/utils"
"go.viam.com/utils/artifact"
"go.viam.com/utils/testutils"
)
// VerifyTestMain preforms various runtime checks on code that tests run.
func VerifyTestMain(m goleak.TestingM) {
func() {
// workaround https://github.com/googleapis/google-cloud-go/issues/5430
httpClient := &http.Client{Transport: http.DefaultTransport.(*http.Transport).Clone()}
defer httpClient.CloseIdleConnections()
//nolint:errcheck
_, _ = transport.Creds(context.Background(), option.WithHTTPClient(httpClient))
t := time.NewTimer(100 * time.Millisecond)
defer t.Stop()
for {
select {
case <-t.C:
return
default:
runtime.Gosched()
}
}
}()
currentGoroutines := goleak.IgnoreCurrent()
cache, err := artifact.GlobalCache()
if err != nil {
golog.Global().Fatalw("error opening artifact", "error", err)
}
//nolint:ifshort
exitCode := m.Run()
testutils.Teardown()
if err := cache.Close(); err != nil {
golog.Global().Errorw("error closing artifact", "error", err)
}
if exitCode != 0 {
os.Exit(exitCode)
}
if err := utils.FindGoroutineLeaks(currentGoroutines); err != nil {
fmt.Fprintf(os.Stderr, "goleak: Errors on successful test run: %v\n", err)
os.Exit(1)
}
}