Newer
Older
/*
* 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.
*/
package apollo
import (
"context"
"fmt"
"github.com/apache/dubbo-go/common"
"github.com/apache/dubbo-go/config"
"github.com/apache/dubbo-go/config_center"
"github.com/apache/dubbo-go/config_center/parser"
mockAppId = "testApplication_yang"
mockCluster = "dev"
mockNamespace = "mockDubbog.properties"
mockNotifyRes = `[{
"notificationId": 53050,
"messages": {
"details": {
"testApplication_yang+default+mockDubbog": 53050
}
}
}]`
mockServiceConfigRes = `[{
"appName": "APOLLO-CONFIGSERVICE",
"instanceId": "instance-300408ep:apollo-configservice:8080",
"homepageUrl": "http://localhost:8080"
}]`
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
mockConfigRes = `{
"appId": "testApplication_yang",
"cluster": "default",
"namespaceName": "mockDubbog.properties",
"configurations": {
"registries.hangzhouzk.username": "",
"application.owner": "ZX",
"registries.shanghaizk.username": "",
"protocols.dubbo.ip": "127.0.0.1",
"protocol_conf.dubbo.getty_session_param.tcp_write_timeout": "5s",
"services.UserProvider.cluster": "failover",
"application.module": "dubbogo user-info server",
"services.UserProvider.interface": "com.ikurento.user.UserProvider",
"protocol_conf.dubbo.getty_session_param.compress_encoding": "false",
"registries.shanghaizk.address": "127.0.0.1:2182",
"protocol_conf.dubbo.session_timeout": "20s",
"registries.shanghaizk.timeout": "3s",
"protocol_conf.dubbo.getty_session_param.keep_alive_period": "120s",
"services.UserProvider.warmup": "100",
"application.version": "0.0.1",
"registries.hangzhouzk.protocol": "zookeeper",
"registries.hangzhouzk.password": "",
"protocols.dubbo.name": "dubbo",
"protocol_conf.dubbo.getty_session_param.wait_timeout": "1s",
"protocols.dubbo.port": "20000",
"application_config.owner": "demo",
"application_config.name": "demo",
"application_config.version": "0.0.1",
"application_config.environment": "dev",
"protocol_conf.dubbo.getty_session_param.session_name": "server",
"application.name": "BDTService",
"registries.hangzhouzk.timeout": "3s",
"protocol_conf.dubbo.getty_session_param.tcp_read_timeout": "1s",
"services.UserProvider.loadbalance": "random",
"protocol_conf.dubbo.session_number": "700",
"protocol_conf.dubbo.getty_session_param.max_msg_len": "1024",
"services.UserProvider.registry": "hangzhouzk",
"application_config.module": "demo",
"services.UserProvider.methods[0].name": "GetUser",
"protocol_conf.dubbo.getty_session_param.tcp_no_delay": "true",
"services.UserProvider.methods[0].retries": "1",
"protocol_conf.dubbo.getty_session_param.tcp_w_buf_size": "65536",
"protocol_conf.dubbo.getty_session_param.tcp_r_buf_size": "262144",
"registries.shanghaizk.password": "",
"application_config.organization": "demo",
"registries.shanghaizk.protocol": "zookeeper",
"protocol_conf.dubbo.getty_session_param.tcp_keep_alive": "true",
"registries.hangzhouzk.address": "127.0.0.1:2181",
"application.environment": "dev",
"services.UserProvider.protocol": "dubbo",
"application.organization": "ikurento.com",
"protocol_conf.dubbo.getty_session_param.pkg_wq_size": "512",
"services.UserProvider.methods[0].loadbalance": "random"
},
"releaseKey": "20191104105242-0f13805d89f834a4"
}`
)
func initApollo() *httptest.Server {
handlerMap := make(map[string]func(http.ResponseWriter, *http.Request), 1)
return runMockConfigServer(handlerMap, notifyResponse)
}
func configResponse(rw http.ResponseWriter, req *http.Request) {
result := fmt.Sprintf(mockConfigRes)
fmt.Fprintf(rw, "%s", result)
}
func notifyResponse(rw http.ResponseWriter, req *http.Request) {
fmt.Fprintf(rw, "%s", result)
}
func serviceConfigResponse(rw http.ResponseWriter, req *http.Request) {
result := fmt.Sprintf(mockServiceConfigRes)
fmt.Fprintf(rw, "%s", result)
}
//run mock config server
func runMockConfigServer(handlerMap map[string]func(http.ResponseWriter, *http.Request),
notifyHandler func(http.ResponseWriter, *http.Request)) *httptest.Server {
uriHandlerMap := make(map[string]func(http.ResponseWriter, *http.Request), 0)
for namespace, handler := range handlerMap {
uri := fmt.Sprintf("/configs/%s/%s/%s", mockAppId, mockCluster, namespace)
uriHandlerMap["/notifications/v2"] = notifyHandler
uriHandlerMap["/services/config"] = serviceConfigResponse
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
uri := r.RequestURI
for path, handler := range uriHandlerMap {
if strings.HasPrefix(uri, path) {
handler(w, r)
break
}
}
}))
return ts
}
func Test_GetConfig(t *testing.T) {
configs, err := configuration.GetProperties(mockNamespace, config_center.WithGroup("dubbo"))
assert.NoError(t, err)
configuration.SetParser(&parser.DefaultConfigurationParser{})
mapContent, err := configuration.Parser().Parse(configs)
assert.NoError(t, err)
assert.Equal(t, "ikurento.com", mapContent["application.organization"])
func Test_GetConfigItem(t *testing.T) {
configuration := initMockApollo(t)
configs, err := configuration.GetInternalProperty("application.organization")
assert.NoError(t, err)
configuration.SetParser(&parser.DefaultConfigurationParser{})
assert.NoError(t, err)
assert.Equal(t, "ikurento.com", configs)
c := &config.BaseConfig{ConfigCenterConfig: &config.ConfigCenterConfig{
AppId: "testApplication_yang",
apollo := initApollo()
apolloUrl := strings.ReplaceAll(apollo.URL, "http", "apollo")
url, err := common.NewURL(context.TODO(), apolloUrl, common.WithParams(c.ConfigCenterConfig.GetUrlMap()))
return configuration
}
func TestAddListener(t *testing.T) {
listener := &apolloDataListener{}
listener.wg.Add(1)
apollo := initMockApollo(t)
mockConfigRes = `{
"appId": "testApplication_yang",
"cluster": "default",
"namespaceName": "mockDubbog.properties",
"configurations": {
"registries.hangzhouzk.username": "11111"
},
"releaseKey": "20191104105242-0f13805d89f834a4"
}`
apollo.AddListener(mockNamespace, listener)
listener.wg.Wait()
assert.Equal(t, "registries.hangzhouzk.username", listener.event)
assert.Greater(t, listener.count, 0)
}
func TestRemoveListener(t *testing.T) {
listener := &apolloDataListener{}
apollo := initMockApollo(t)
mockConfigRes = `{
"appId": "testApplication_yang",
"cluster": "default",
"namespaceName": "mockDubbog.properties",
"configurations": {
"registries.hangzhouzk.username": "11111"
},
"releaseKey": "20191104105242-0f13805d89f834a4"
}`
apollo.AddListener(mockNamespace, listener)
apollo.RemoveListener(mockNamespace, listener)
assert.Equal(t, "", listener.event)
listenerCount := 0
apollo.listeners.Range(func(key, value interface{}) bool {
apolloListener := value.(*apolloListener)
for e := range apolloListener.listeners {
fmt.Println(e)
listenerCount++
}
return true
})
assert.Equal(t, listenerCount, 0)
assert.Equal(t, listener.count, 0)
}
type apolloDataListener struct {
wg sync.WaitGroup
count int
event string
}
func (l *apolloDataListener) Process(configType *config_center.ConfigChangeEvent) {
if configType.ConfigType != remoting.EventTypeUpdate {
return
}
l.wg.Done()
l.count++
l.event = configType.Key
func deleteMockJson(t *testing.T) {
//because the file write in another goroutine,so have a break ...
time.Sleep(100 * time.Millisecond)
remove := os.Remove("mockDubbog.properties.json")
t.Log("remove result:", remove)
}