forked from robustirc/robustirc
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlock_test.go
More file actions
74 lines (68 loc) · 1.67 KB
/
lock_test.go
File metadata and controls
74 lines (68 loc) · 1.67 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
package main
import (
"bufio"
"os"
"os/exec"
"strings"
"testing"
)
func verifyLockDefer(path string, t *testing.T) {
t.Parallel()
f, err := os.Open(path)
if err != nil {
t.Fatal(err.Error())
}
defer f.Close()
// XXX: Ideally, we would analyze the Go source code and find all
// Lock() calls on sync.{RW,}Mutex and their deferred Unlock()
// counterpart. In practice, analyzing the text is good enough for
// now.
scanner := bufio.NewScanner(f)
var prev string
for scanner.Scan() {
if strings.Contains(prev, "Lock()") {
if !strings.Contains(scanner.Text(), "Unlock()") {
t.Fatalf("Lock() without Unlock() in next line: %q %q", prev, scanner.Text())
}
}
prev = scanner.Text()
}
if err := scanner.Err(); err != nil {
t.Fatalf("Reading %q: %v", path, err.Error())
}
}
var (
whitelistedFiles = []string{
// TODO: verify that refactoring outputstream.go to use
// functions for every lock actually results in a measurable
// performance decrease.
"outputstream.go",
}
)
func TestLockDefer(t *testing.T) {
// List all .go files that make up RobustIRC
output, err := exec.Command("go",
"list",
"-f",
`{{ range $f := .GoFiles }}{{ $.Dir }}/{{ $f }}{{ "\n" }}{{ end }}`,
"github.com/robustirc/robustirc/...").Output()
if err != nil {
t.Fatalf("Could not list Go files: %v", err)
}
for _, path := range strings.Split(strings.TrimSpace(string(output)), "\n") {
var skip bool
for _, whitelistedFile := range whitelistedFiles {
if strings.HasSuffix(path, whitelistedFile) {
skip = true
break
}
}
if skip {
continue
}
path := path // capture range variable
t.Run(path, func(t *testing.T) {
verifyLockDefer(path, t)
})
}
}