-
Notifications
You must be signed in to change notification settings - Fork 16
/
event_test.go
86 lines (72 loc) · 1.67 KB
/
event_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
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
package events
import (
"reflect"
"sync"
"testing"
"time"
)
func TestEvent(t *testing.T) {
t.Run("Clone", func(t *testing.T) {
e1 := &Event{
Message: "Hello World",
Source: "file.go:42",
Args: Args{{"hello", "world"}},
Time: time.Now(),
}
e2 := e1.Clone()
if e1 == e2 {
t.Error("Clone cannot return a value with the same address as the original")
}
if !reflect.DeepEqual(e1, e2) {
t.Errorf("%#v", e2)
}
})
}
func TestArgs(t *testing.T) {
t.Run("Get", func(t *testing.T) {
args := Args{{"hello", "world"}, {"answer", 42}}
if v, ok := args.Get("answer"); !ok {
t.Error("expected answer but got nothing")
} else if !reflect.DeepEqual(v, 42) {
t.Error("expected answer=42 but got", v)
}
if v, ok := args.Get("question"); ok {
t.Error("expected no question but got", v)
}
})
t.Run("Map", func(t *testing.T) {
a1 := Args{{"hello", "world"}, {"answer", 42}}
SortArgs(a1)
m1 := a1.Map()
a2 := A(m1)
SortArgs(a2)
if !reflect.DeepEqual(a1, a2) {
t.Errorf("%#v != %#v", a1, a2)
}
})
}
// This test is crafted to crash the program if some of the unsafe operations
// perform illegal memory changes that mess up the GC state.
//
// Run it with CGO_ENABLED=0 GODEBUG=gccheckmark=1 GOTRACEBACK=crash GOGC=1
func TestUnsafe(t *testing.T) {
logger := Logger{
Handler: Discard,
EnableDebug: true,
}
wg := sync.WaitGroup{}
for i := 0; i != 10; i++ {
wg.Add(1)
go func() {
defer wg.Done()
for i := 0; i != 2000; i++ {
_ = make([]byte, 1024*1024)
from := "Luke"
to := "Han"
logger.Log("hello world: from=%{from}s, to=%{to}s", from, to)
time.Sleep(time.Millisecond)
}
}()
}
wg.Wait()
}