forked from minotar/imgd
-
Notifications
You must be signed in to change notification settings - Fork 0
/
signals_test.go
60 lines (53 loc) · 1.5 KB
/
signals_test.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
package main
import (
"bytes"
"github.com/op/go-logging"
"os"
"strings"
"syscall"
"testing"
)
type StashingWriter struct {
buf bytes.Buffer
}
func (w *StashingWriter) Write(p []byte) (n int, err error) {
return w.buf.Write(p)
}
func (w *StashingWriter) Unstash() string {
return w.buf.String()
}
func testSetupSignals() *StashingWriter {
sw := new(StashingWriter)
logBackend := logging.NewLogBackend(sw, "", 0)
stats = MakeStatsCollector()
setupConfig()
setupLog(logBackend)
setupCache()
return sw
}
func testDumpsProfile(t *testing.T, prefixText string, signal os.Signal) {
sw := testSetupSignals()
sh := new(SignalHandler)
sh.handleSignal(signal)
logOutput := sw.Unstash()
t.Logf("Actual log output\n%s\nEnd actual log output", logOutput)
if !strings.Contains(logOutput, prefixText) {
t.Fatalf("Did not output '%s' in log", prefixText)
}
if strings.Count(logOutput, prefixText) > 1 {
t.Fatalf("'%s' occurred multiple times in log", prefixText)
}
pprofLocation := logOutput[strings.Index(logOutput, prefixText)+len(prefixText):]
pprofLocation = pprofLocation[:strings.Index(pprofLocation, "\n")]
if _, err := os.Stat(pprofLocation); os.IsNotExist(err) {
t.Fatal("Pprof output path " + pprofLocation + " does not exist!")
} else {
os.Remove(pprofLocation)
}
}
func TestDumpsBlockProfile(t *testing.T) {
testDumpsProfile(t, "Dumped block pprof to ", syscall.SIGUSR1)
}
func TestDumpsGoroutineProfile(t *testing.T) {
testDumpsProfile(t, "Dumped goroutine pprof to ", syscall.SIGUSR2)
}