Newer
Older

vito.he
committed
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

vito.he
committed
"context"
"fmt"
"reflect"

vito.he
committed
import (
"github.com/stretchr/testify/assert"
)
import (
"github.com/apache/dubbo-go/common/config"
"github.com/apache/dubbo-go/common/extension"
"github.com/apache/dubbo-go/config_center"
)

vito.he
committed
c := &BaseConfig{}

vito.he
committed
mockMap["dubbo.registries.shanghai_reg1.protocol"] = "mock100"
mockMap["dubbo.reference.com.MockService.MockService.retries"] = "10"
mockMap["dubbo.com.MockService.MockService.GetUser.retries"] = "10"

vito.he
committed
mockMap["dubbo.consumer.check"] = "false"
mockMap["dubbo.application.name"] = "dubbo"
config.GetEnvInstance().UpdateExternalConfigMap(mockMap)

vito.he
committed
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
father := &ConsumerConfig{
Check: &[]bool{true}[0],
ApplicationConfig: &ApplicationConfig{
Organization: "dubbo_org",
Name: "dubbo",
Module: "module",
Version: "2.6.0",
Owner: "dubbo",
Environment: "test"},
Registries: map[string]*RegistryConfig{
//"shanghai_reg1": {
// id: "shanghai_reg1",
// Protocol: "mock",
// TimeoutStr: "2s",
// Group: "shanghai_idc",
// Address: "127.0.0.1:2181",
// Username: "user1",
// Password: "pwd1",
//},
"shanghai_reg2": {
Protocol: "mock",
TimeoutStr: "2s",
Group: "shanghai_idc",
Address: "127.0.0.2:2181",
Username: "user1",
Password: "pwd1",
},
"hangzhou_reg1": {
Protocol: "mock",
TimeoutStr: "2s",
Group: "hangzhou_idc",
Address: "127.0.0.3:2181",
Username: "user1",
Password: "pwd1",
},
"hangzhou_reg2": {
Protocol: "mock",
TimeoutStr: "2s",
Group: "hangzhou_idc",
Address: "127.0.0.4:2181",
Username: "user1",
Password: "pwd1",
},
},
References: map[string]*ReferenceConfig{
"MockService": {
InterfaceName: "com.MockService",

vito.he
committed
Protocol: "mock",
Cluster: "failover",
Loadbalance: "random",
Retries: 3,
Group: "huadong_idc",
Version: "1.0.0",
Methods: []*MethodConfig{
{
InterfaceId: "MockService",
InterfaceName: "com.MockService",

vito.he
committed
Name: "GetUser",
Retries: 2,
Loadbalance: "random",
},
{
InterfaceId: "MockService",
InterfaceName: "com.MockService",

vito.he
committed
Name: "GetUser1",
Retries: 2,
Loadbalance: "random",
},
},
},
},
}

vito.he
committed
assert.Equal(t, "mock100", father.Registries["shanghai_reg1"].Protocol)
assert.Equal(t, int64(10), father.References["MockService"].Retries)
assert.Equal(t, int64(10), father.References["MockService"].Methods[0].Retries)
assert.Equal(t, &[]bool{false}[0], father.Check)
assert.Equal(t, "dubbo", father.ApplicationConfig.Name)

vito.he
committed
func Test_refreshProvider(t *testing.T) {
c := &BaseConfig{}

vito.he
committed
mockMap["dubbo.registries.shanghai_reg1.protocol"] = "mock100"
mockMap["dubbo.service.com.MockService.MockService.retries"] = "10"
mockMap["dubbo.com.MockService.MockService.GetUser.retries"] = "10"

vito.he
committed
mockMap["dubbo.consumer.check"] = "false"
mockMap["dubbo.application.name"] = "dubbo"
mockMap["dubbo.protocols.jsonrpc1.name"] = "jsonrpc"
mockMap["dubbo.protocols.jsonrpc1.ip"] = "127.0.0.1"
mockMap["dubbo.protocols.jsonrpc1.port"] = "20001"
config.GetEnvInstance().UpdateExternalConfigMap(mockMap)

vito.he
committed
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
father := &ProviderConfig{
ApplicationConfig: &ApplicationConfig{
Organization: "dubbo_org",
Name: "dubbo",
Module: "module",
Version: "2.6.0",
Owner: "dubbo",
Environment: "test"},
Registries: map[string]*RegistryConfig{
//"shanghai_reg1": {
// id: "shanghai_reg1",
// Protocol: "mock",
// TimeoutStr: "2s",
// Group: "shanghai_idc",
// Address: "127.0.0.1:2181",
// Username: "user1",
// Password: "pwd1",
//},
"shanghai_reg2": {
Protocol: "mock",
TimeoutStr: "2s",
Group: "shanghai_idc",
Address: "127.0.0.2:2181",
Username: "user1",
Password: "pwd1",
},
"hangzhou_reg1": {
Protocol: "mock",
TimeoutStr: "2s",
Group: "hangzhou_idc",
Address: "127.0.0.3:2181",
Username: "user1",
Password: "pwd1",
},
"hangzhou_reg2": {
Protocol: "mock",
TimeoutStr: "2s",
Group: "hangzhou_idc",
Address: "127.0.0.4:2181",
Username: "user1",
Password: "pwd1",
},
},
Services: map[string]*ServiceConfig{
"MockService": {
InterfaceName: "com.MockService",

vito.he
committed
Protocol: "mock",
Cluster: "failover",
Loadbalance: "random",
Retries: 3,
Group: "huadong_idc",
Version: "1.0.0",
Methods: []*MethodConfig{
{
InterfaceId: "MockService",
InterfaceName: "com.MockService",

vito.he
committed
Name: "GetUser",
Retries: 2,
Loadbalance: "random",
},
{InterfaceId: "MockService",
InterfaceName: "com.MockService",

vito.he
committed
Name: "GetUser1",
Retries: 2,
Loadbalance: "random",
},
},
},
},
}

vito.he
committed
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
assert.Equal(t, "mock100", father.Registries["shanghai_reg1"].Protocol)
assert.Equal(t, int64(10), father.Services["MockService"].Retries)
assert.Equal(t, int64(10), father.Services["MockService"].Methods[0].Retries)
assert.Equal(t, "dubbo", father.ApplicationConfig.Name)
assert.Equal(t, "20001", father.Protocols["jsonrpc1"].Port)
}
func Test_startConfigCenter(t *testing.T) {
extension.SetConfigCenterFactory("mock", func() config_center.DynamicConfigurationFactory {
return &config_center.MockDynamicConfigurationFactory{}
})
c := &BaseConfig{ConfigCenterConfig: &ConfigCenterConfig{
Protocol: "mock",
Address: "172.0.0.1",
Group: "dubbo",
ConfigFile: "mockDubbo.properties",
}}
err := c.startConfigCenter(context.Background())
assert.NoError(t, err)
b, v := config.GetEnvInstance().Configuration().Front().Value.(*config.InmemoryConfiguration).GetProperty("dubbo.application.organization")
assert.True(t, b)
assert.Equal(t, "ikurento.com", v)
}
func Test_initializeStruct(t *testing.T) {
consumerConfig := &ConsumerConfig{}
tp := reflect.TypeOf(ConsumerConfig{})
v := reflect.New(tp)
initializeStruct(tp, v.Elem())
fmt.Println(reflect.ValueOf(consumerConfig).Elem().Type().String())
fmt.Println(v.Elem().Type().String())
reflect.ValueOf(consumerConfig).Elem().Set(v.Elem())
assert.Condition(t, func() (success bool) {
return consumerConfig.ApplicationConfig != nil
})
assert.Condition(t, func() (success bool) {
return consumerConfig.Registries != nil
})
assert.Condition(t, func() (success bool) {
return consumerConfig.References != nil
})