-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathglobal_test.go
56 lines (44 loc) · 1.4 KB
/
global_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
package appctx_test
import (
"reflect"
"syscall"
"testing"
"time"
"github.com/srvc/appctx"
)
func TestGlobal(t *testing.T) {
var printCnt, fatalCnt int
logger := &fakeLogger{
PrintFunc: func(v ...interface{}) { printCnt++ },
FatalFunc: func(v ...interface{}) { fatalCnt++ },
}
defer func(l appctx.Logger) { appctx.ErrorLog = l }(appctx.ErrorLog)
appctx.ErrorLog = logger
ctx := appctx.Global()
defer appctx.ResetGlobal()
if got, want := ctx, appctx.Global(); got != want {
t.Error("appctx.Global() should return the same object anytime")
}
if got, want := ctx.Err(), error(nil); !reflect.DeepEqual(got, want) {
t.Errorf("ctx.Err() returns %v, want %v", got, want)
}
time.Sleep(5 * time.Millisecond)
syscall.Kill(syscall.Getpid(), syscall.SIGTERM)
time.Sleep(5 * time.Millisecond)
syscall.Kill(syscall.Getpid(), syscall.SIGTERM)
time.Sleep(5 * time.Millisecond)
if got, want := printCnt, 2; got != want {
t.Errorf("ErrorLog.Print called %d times, want %d", got, want)
}
if got, want := fatalCnt, 0; got != want {
t.Errorf("ErrorLog.Fatal called %d times, want %d", got, want)
}
syscall.Kill(syscall.Getpid(), syscall.SIGTERM)
time.Sleep(5 * time.Millisecond)
if got, want := printCnt, 2; got != want {
t.Errorf("ErrorLog.Print called %d times, want %d", got, want)
}
if got, want := fatalCnt, 1; got != want {
t.Errorf("ErrorLog.Fatal called %d times, want %d", got, want)
}
}