forked from cockroachdb/cockroach
/
testing.go
167 lines (153 loc) · 4.83 KB
/
testing.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
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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
// Copyright 2014 The Cockroach Authors.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
// implied. See the License for the specific language governing
// permissions and limitations under the License. See the AUTHORS file
// for names of contributors.
//
// Author: Spencer Kimball (spencer.kimball@gmail.com)
package util
import (
"fmt"
"io/ioutil"
"net"
"os"
"time"
)
// Tester is a proxy for e.g. testing.T which does not introduce a dependency
// on "testing".
type Tester interface {
Failed() bool
Fatal(args ...interface{})
Fatalf(format string, args ...interface{})
}
// tempUnixFile creates a temporary file for use with a unix domain socket.
// TODO(bdarnell): use TempDir instead to make this atomic.
func tempUnixFile() string {
f, err := ioutil.TempFile("", "unix-socket")
if err != nil {
panic(fmt.Sprintf("unable to create temp file: %s", err))
}
f.Close()
if err := os.Remove(f.Name()); err != nil {
panic(fmt.Sprintf("unable to remove temp file: %s", err))
}
return f.Name()
}
// tempLocalhostAddr creates an address to localhost using a monotonically
// increasing port number in the range [minLocalhostPort, ...].
func tempLocalhostAddr() string {
return "127.0.0.1:0"
}
// CreateTempDir creates a temporary directory and returns its path.
// You should usually call defer CleanupDir(dir) right after.
func CreateTempDir(t Tester, prefix string) string {
dir, err := ioutil.TempDir("", prefix)
if err != nil {
t.Fatal(err)
}
return dir
}
// CreateNTempDirs creates N temporary directories and returns a slice
// of paths.
// You should usually call defer CleanupDirs(dirs) right after.
func CreateNTempDirs(t Tester, prefix string, n int) []string {
dirs := make([]string, n)
var err error
for i := 0; i < n; i++ {
dirs[i], err = ioutil.TempDir("", prefix)
if err != nil {
t.Fatal(err)
}
}
return dirs
}
// CleanupDir removes the passed-in directory and all contents. Errors are ignored.
func CleanupDir(dir string) {
_ = os.RemoveAll(dir)
}
// CleanupDirs removes all passed-in directories and their contents. Errors are ignored.
func CleanupDirs(dirs []string) {
for _, dir := range dirs {
_ = os.RemoveAll(dir)
}
}
// CreateTestAddr creates an unused address for testing. The "network"
// parameter should be one of "tcp" or "unix".
func CreateTestAddr(network string) net.Addr {
switch network {
case "tcp":
addr, err := net.ResolveTCPAddr("tcp", tempLocalhostAddr())
if err != nil {
panic(err)
}
return addr
case "unix":
addr, err := net.ResolveUnixAddr("unix", tempUnixFile())
if err != nil {
panic(err)
}
return addr
}
panic(fmt.Sprintf("unknown network type: %s", network))
}
// IsTrueWithin returns an error if the supplied function fails to
// evaluate to true within the specified duration. The function is
// invoked immediately at first and then successively with an
// exponential backoff starting at 1ns and ending at the specified
// duration.
//
// This method is deprecated; use SucceedsWithin instead.
// TODO(bdarnell): convert existing uses of IsTrueWithin to SucceedsWithin.
func IsTrueWithin(trueFunc func() bool, duration time.Duration) error {
total := time.Duration(0)
for wait := time.Duration(1); total < duration; wait *= 2 {
if trueFunc() {
return nil
}
time.Sleep(wait)
total += wait
}
return ErrorfSkipFrames(1, "condition failed to evaluate true within %s", duration)
}
// SucceedsWithin fails the test (with t.Fatal) unless the supplied
// function runs without error within the specified duration. The
// function is invoked immediately at first and then successively with
// an exponential backoff starting at 1ns and ending at the specified
// duration.
func SucceedsWithin(t Tester, duration time.Duration, fn func() error) {
SucceedsWithinDepth(1, t, duration, fn)
}
// SucceedsWithinDepth is like SucceedsWithin() but with an additional
// stack depth offset.
func SucceedsWithinDepth(depth int, t Tester, duration time.Duration, fn func() error) {
deadline := time.Now().Add(duration)
var lastErr error
for wait := time.Duration(1); time.Now().Before(deadline); wait *= 2 {
lastErr = fn()
if lastErr == nil {
return
}
if wait > time.Second {
wait = time.Second
}
time.Sleep(wait)
}
t.Fatal(ErrorfSkipFrames(1+depth, "condition failed to evaluate within %s: %s", duration, lastErr))
}
// Panics calls the supplied function and returns true if and only if it panics.
func Panics(f func()) (panics bool) {
defer func() {
panics = recover() != nil
}()
f()
return
}