forked from go-kratos/kratos
-
Notifications
You must be signed in to change notification settings - Fork 0
/
app_test.go
194 lines (181 loc) · 4.6 KB
/
app_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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
package kratos
import (
"context"
"fmt"
"reflect"
"sync"
"testing"
"time"
"github.com/go-kratos/kratos/v2/registry"
"github.com/go-kratos/kratos/v2/transport/grpc"
"github.com/go-kratos/kratos/v2/transport/http"
)
type mockRegistry struct {
lk sync.Mutex
service map[string]*registry.ServiceInstance
}
func (r *mockRegistry) Register(ctx context.Context, service *registry.ServiceInstance) error {
if service == nil || service.ID == "" {
return fmt.Errorf("no service id")
}
r.lk.Lock()
defer r.lk.Unlock()
r.service[service.ID] = service
return nil
}
// Deregister the registration.
func (r *mockRegistry) Deregister(ctx context.Context, service *registry.ServiceInstance) error {
r.lk.Lock()
defer r.lk.Unlock()
if r.service[service.ID] == nil {
return fmt.Errorf("deregister service not found")
}
delete(r.service, service.ID)
return nil
}
func TestApp(t *testing.T) {
hs := http.NewServer()
gs := grpc.NewServer()
app := New(
Name("kratos"),
Version("v1.0.0"),
Server(hs, gs),
Registrar(&mockRegistry{service: make(map[string]*registry.ServiceInstance)}),
)
time.AfterFunc(time.Second, func() {
_ = app.Stop()
})
if err := app.Run(); err != nil {
t.Fatal(err)
}
}
func TestApp_ID(t *testing.T) {
v := "123"
o := New(ID(v))
if !reflect.DeepEqual(v, o.ID()) {
t.Fatalf("o.ID():%s is not equal to v:%s", o.ID(), v)
}
}
func TestApp_Name(t *testing.T) {
v := "123"
o := New(Name(v))
if !reflect.DeepEqual(v, o.Name()) {
t.Fatalf("o.Name():%s is not equal to v:%s", o.Name(), v)
}
}
func TestApp_Version(t *testing.T) {
v := "123"
o := New(Version(v))
if !reflect.DeepEqual(v, o.Version()) {
t.Fatalf("o.Version():%s is not equal to v:%s", o.Version(), v)
}
}
func TestApp_Metadata(t *testing.T) {
v := map[string]string{
"a": "1",
"b": "2",
}
o := New(Metadata(v))
if !reflect.DeepEqual(v, o.Metadata()) {
t.Fatalf("o.Metadata():%s is not equal to v:%s", o.Metadata(), v)
}
}
func TestApp_Endpoint(t *testing.T) {
type fields struct {
id string
version string
name string
instance *registry.ServiceInstance
metadata map[string]string
want struct {
id string
version string
name string
endpoint []string
metadata map[string]string
}
}
tests := []fields{
{
id: "1",
name: "kratos-v1",
instance: ®istry.ServiceInstance{Endpoints: []string{"https://go-kratos.dev", "localhost"}},
metadata: map[string]string{},
version: "v1",
want: struct {
id string
version string
name string
endpoint []string
metadata map[string]string
}{
id: "1", version: "v1", name: "kratos-v1", endpoint: []string{"https://go-kratos.dev", "localhost"},
metadata: map[string]string{},
},
},
{
id: "2",
name: "kratos-v2",
instance: ®istry.ServiceInstance{Endpoints: []string{"test"}},
metadata: map[string]string{"kratos": "https://github.com/go-kratos/kratos"},
version: "v2",
want: struct {
id string
version string
name string
endpoint []string
metadata map[string]string
}{
id: "2", version: "v2", name: "kratos-v2", endpoint: []string{"test"},
metadata: map[string]string{"kratos": "https://github.com/go-kratos/kratos"},
},
},
{
id: "3",
name: "kratos-v3",
instance: nil,
metadata: make(map[string]string),
version: "v3",
want: struct {
id string
version string
name string
endpoint []string
metadata map[string]string
}{
id: "3", version: "v3", name: "kratos-v3", endpoint: nil,
metadata: map[string]string{},
},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
a := &App{
opts: options{id: tt.id, name: tt.name, metadata: tt.metadata, version: tt.version},
ctx: context.Background(),
cancel: nil,
instance: tt.instance,
}
ctx := NewContext(context.Background(), a)
if got, ok := FromContext(ctx); ok {
if got.ID() != tt.want.id {
t.Errorf("ID() = %v, want %v", got.ID(), tt.want.id)
}
if got.Name() != tt.want.name {
t.Errorf("Name() = %v, want %v", got.Name(), tt.want.name)
}
if got.Version() != tt.want.version {
t.Errorf("Version() = %v, want %v", got.Version(), tt.want.version)
}
if !reflect.DeepEqual(got.Endpoint(), tt.want.endpoint) {
t.Errorf("Endpoint() = %v, want %v", got.Endpoint(), tt.want.endpoint)
}
if !reflect.DeepEqual(got.Metadata(), tt.want.metadata) {
t.Errorf("Metadata() = %v, want %v", got.Metadata(), tt.want.metadata)
}
} else {
t.Errorf("ok() = %v, want %v", ok, true)
}
})
}
}