-
Notifications
You must be signed in to change notification settings - Fork 1
/
mainthread_test.go
94 lines (85 loc) · 1.45 KB
/
mainthread_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
87
88
89
90
91
92
93
94
package mainthread_test
import (
"errors"
"io"
"testing"
"github.com/gopxl/mainthread/v2"
"github.com/stretchr/testify/require"
)
func BenchmarkCall(b *testing.B) {
run := func() {
f := func() {}
b.ResetTimer()
for i := 0; i < b.N; i++ {
mainthread.Call(f)
}
}
mainthread.Run(run)
}
func BenchmarkCallErr(b *testing.B) {
run := func() {
f := func() error {
return errors.New("foo")
}
b.ResetTimer()
for i := 0; i < b.N; i++ {
mainthread.CallErr(f)
}
}
mainthread.Run(run)
}
func BenchmarkCallVal(b *testing.B) {
run := func() {
f := func() int {
return 42
}
b.ResetTimer()
for i := 0; i < b.N; i++ {
mainthread.CallVal(f)
}
}
mainthread.Run(run)
}
func TestCallVal_Resp(t *testing.T) {
run := func() {
f := func() *int {
v := 1
return &v
}
v := mainthread.CallVal(f)
require.NotNil(t, v)
require.Equal(t, 1, *v)
}
mainthread.Run(run)
}
func TestCallVal_nilResp(t *testing.T) {
run := func() {
f := func() *int {
return nil
}
v := mainthread.CallVal(f)
require.Nil(t, v)
}
mainthread.Run(run)
}
func TestCallErr_Resp(t *testing.T) {
run := func() {
f := func() error {
return io.EOF
}
v := mainthread.CallErr(f)
require.NotNil(t, v)
require.EqualError(t, v, io.EOF.Error())
}
mainthread.Run(run)
}
func TestCallErr_nilResp(t *testing.T) {
run := func() {
f := func() error {
return nil
}
v := mainthread.CallErr(f)
require.NoError(t, v)
}
mainthread.Run(run)
}