-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstack_test.go
More file actions
81 lines (70 loc) · 2.74 KB
/
stack_test.go
File metadata and controls
81 lines (70 loc) · 2.74 KB
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
package testlib
import (
"fmt"
"regexp"
"strings"
"testing"
log "github.com/sirupsen/logrus"
)
var initErr error
// init is tested here as an anti-pattern.
func init() {
defer func() {
if r := recover(); r != nil {
recoveredMessage := fmt.Sprintf("%s", r)
if !strings.HasPrefix(recoveredMessage, "CurrentRunningTest() failed") {
initErr = fmt.Errorf("`CurrentRunningTest()' function is broken, the panic message didn't match the expected format, broken message=%s", recoveredMessage)
} else {
log.Info("Successfully recovered from panic generated by invoking `CurrentRunningTest()' from inside `init()' function")
}
} else {
initErr = fmt.Errorf("`CurrentRunningTest()' function is broken, I expected to recover from a panic, but `recover()' yielded nil")
}
}()
log.Infof("If you see this message, watch out! The test is going to fail! `CurrentRunningTest()'=%s", CurrentRunningTest())
}
// requireCleanInit checks the result of `init` to ensure it has behaved as
// expected.
func requireCleanInit(t *testing.T) {
if initErr != nil {
t.Errorf("`init()' left an unexpected error behind: %s", initErr)
}
}
func verifyRunningTestName(t *testing.T, testName string) {
requireCleanInit(t) // Placed here to ensure the check is executed at some point in this test suite.
if reportedName := CurrentRunningTest(); reportedName != testName {
t.Errorf(`CurrentRunningTest() should have produced "%s" but instead gave "%s"`, testName, reportedName)
}
}
func Test_CurrentRunningTest(t *testing.T) {
testName := "Test_CurrentRunningTest"
verifyRunningTestName(t, testName)
}
func TestNoUnderScoreJustLetters(t *testing.T) {
testName := "TestNoUnderScoreJustLetters"
verifyRunningTestName(t, testName)
}
func Test0IsTheNumberZer0(t *testing.T) {
testName := "Test0IsTheNumberZer0"
verifyRunningTestName(t, testName)
}
func TestExpr2(t *testing.T) {
st := []byte(`goroutine 1 [running, locked to thread]:
gigawatt-server/pkg/testlib.Stack(0x0, 0x0, 0x0)
/Users/jay/go/src/gigawatt-server/pkg/testlib/stack.go:24 +0xa5
gigawatt-server/pkg/testlib.IsRunningTests(0x6320e)
/Users/jay/go/src/gigawatt-server/pkg/testlib/stack.go:70 +0x27
gigawatt-server/config.validate()
/Users/jay/go/src/gigawatt-server/config/config.go:165 +0x1e4
gigawatt-server/config.init·1()
/Users/jay/go/src/gigawatt-server/config/config.go:142 +0xa28
gigawatt-server/config.init()
/Users/jay/go/src/gigawatt-server/config/resource_acquisition.go:1 +0x136
gigawatt-server/pkg/kafka.init()
/Users/jay/go/src/gigawatt-server/pkg/kafka/kafka_test.go:69 +0x42
main.init()
gigawatt-server/pkg/kafka/_test/_testmain.go:56 +0x4a`)
if !regexp.MustCompile(testExpr2).Match(st) {
t.Errorf("testExpr2 didn't match stack;\n\ntestExpr2=%s\n\nstack=%s", testExpr2, string(st))
}
}