forked from go-kratos/kratos
-
Notifications
You must be signed in to change notification settings - Fork 0
/
options_test.go
145 lines (125 loc) · 3 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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
package kratos
import (
"context"
"log"
"net/url"
"os"
"reflect"
"testing"
"time"
xlog "github.com/go-kratos/kratos/v2/log"
"github.com/go-kratos/kratos/v2/registry"
"github.com/go-kratos/kratos/v2/transport"
)
func TestID(t *testing.T) {
o := &options{}
v := "123"
ID(v)(o)
if !reflect.DeepEqual(v, o.id) {
t.Fatalf("o.id:%s is not equal to v:%s", o.id, v)
}
}
func TestName(t *testing.T) {
o := &options{}
v := "abc"
Name(v)(o)
if !reflect.DeepEqual(v, o.name) {
t.Fatalf("o.name:%s is not equal to v:%s", o.name, v)
}
}
func TestVersion(t *testing.T) {
o := &options{}
v := "123"
Version(v)(o)
if !reflect.DeepEqual(v, o.version) {
t.Fatalf("o.version:%s is not equal to v:%s", o.version, v)
}
}
func TestMetadata(t *testing.T) {
o := &options{}
v := map[string]string{
"a": "1",
"b": "2",
}
Metadata(v)(o)
if !reflect.DeepEqual(v, o.metadata) {
t.Fatalf("o.metadata:%s is not equal to v:%s", o.metadata, v)
}
}
func TestEndpoint(t *testing.T) {
o := &options{}
v := []*url.URL{
{Host: "example.com"},
{Host: "foo.com"},
}
Endpoint(v...)(o)
if !reflect.DeepEqual(v, o.endpoints) {
t.Fatalf("o.endpoints:%s is not equal to v:%s", o.endpoints, v)
}
}
func TestContext(t *testing.T) {
type ctxKey = struct{}
o := &options{}
v := context.WithValue(context.TODO(), ctxKey{}, "b")
Context(v)(o)
if !reflect.DeepEqual(v, o.ctx) {
t.Fatalf("o.ctx:%s is not equal to v:%s", o.ctx, v)
}
}
func TestLogger(t *testing.T) {
o := &options{}
v := xlog.NewStdLogger(log.Writer())
Logger(v)(o)
if !reflect.DeepEqual(v, o.logger) {
t.Fatalf("o.logger:%v is not equal to xlog.NewHelper(v):%v", o.logger, xlog.NewHelper(v))
}
}
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)
if !reflect.DeepEqual(v, o.servers) {
t.Fatalf("o.servers:%s is not equal to xlog.NewHelper(v):%s", o.servers, v)
}
}
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)
if !reflect.DeepEqual(v, o.sigs) {
t.Fatal("o.sigs is not equal to v")
}
}
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)
if !reflect.DeepEqual(v, o.registrar) {
t.Fatal("o.registrar is not equal to v")
}
}
func TestRegistrarTimeout(t *testing.T) {
o := &options{}
v := time.Duration(123)
RegistrarTimeout(v)(o)
if !reflect.DeepEqual(v, o.registrarTimeout) {
t.Fatal("o.registrarTimeout is not equal to v")
}
}