forked from go-kratos/kratos
-
Notifications
You must be signed in to change notification settings - Fork 0
/
options_test.go
121 lines (102 loc) · 2.26 KB
/
options_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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
package kratos
import (
"context"
"log"
"net/url"
"os"
"testing"
"time"
xlog "github.com/go-kratos/kratos/v2/log"
"github.com/go-kratos/kratos/v2/registry"
"github.com/go-kratos/kratos/v2/transport"
"github.com/stretchr/testify/assert"
)
func TestID(t *testing.T) {
o := &options{}
v := "123"
ID(v)(o)
assert.Equal(t, v, o.id)
}
func TestName(t *testing.T) {
o := &options{}
v := "abc"
Name(v)(o)
assert.Equal(t, v, o.name)
}
func TestVersion(t *testing.T) {
o := &options{}
v := "123"
Version(v)(o)
assert.Equal(t, v, o.version)
}
func TestMetadata(t *testing.T) {
o := &options{}
v := map[string]string{
"a": "1",
"b": "2",
}
Metadata(v)(o)
assert.Equal(t, v, o.metadata)
}
func TestEndpoint(t *testing.T) {
o := &options{}
v := []*url.URL{
{Host: "example.com"},
{Host: "foo.com"},
}
Endpoint(v...)(o)
assert.Equal(t, v, o.endpoints)
}
func TestContext(t *testing.T) {
o := &options{}
v := context.WithValue(context.TODO(), "a", "b")
Context(v)(o)
assert.Equal(t, v, o.ctx)
}
func TestLogger(t *testing.T) {
o := &options{}
v := xlog.NewStdLogger(log.Writer())
Logger(v)(o)
assert.Equal(t, xlog.NewHelper(v), o.logger)
}
type mockServer struct{}
func (m *mockServer) Start(ctx context.Context) error { return nil }
func (m *mockServer) Stop(ctx context.Context) error { return nil }
func TestServer(t *testing.T) {
o := &options{}
v := []transport.Server{
&mockServer{}, &mockServer{},
}
Server(v...)(o)
assert.Equal(t, v, o.servers)
}
type mockSignal struct{}
func (m *mockSignal) String() string { return "sig" }
func (m *mockSignal) Signal() {}
func TestSignal(t *testing.T) {
o := &options{}
v := []os.Signal{
&mockSignal{}, &mockSignal{},
}
Signal(v...)(o)
assert.Equal(t, v, o.sigs)
}
type mockRegistrar struct{}
func (m *mockRegistrar) Register(ctx context.Context, service *registry.ServiceInstance) error {
return nil
}
func (m *mockRegistrar) Deregister(ctx context.Context, service *registry.ServiceInstance) error {
return nil
}
func TestRegistrar(t *testing.T) {
o := &options{}
v := &mockRegistrar{}
Registrar(v)(o)
assert.Equal(t, v, o.registrar)
}
func TestRegistrarTimeout(t *testing.T) {
o := &options{}
v := time.Duration(123)
RegistrarTimeout(v)(o)
assert.Equal(t, v, o.registrarTimeout)
}