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 zookeeper
import (
import (
"github.com/dubbogo/go-zookeeper/zk"
"github.com/stretchr/testify/assert"
)
import (
"github.com/apache/dubbo-go/common"
"github.com/apache/dubbo-go/common/constant"
"github.com/apache/dubbo-go/common/extension"
"github.com/apache/dubbo-go/common/observer"
"github.com/apache/dubbo-go/common/observer/dispatcher"
"github.com/apache/dubbo-go/metadata/mapping"
"github.com/apache/dubbo-go/protocol"
func prepareData(t *testing.T) *zk.TestCluster {
assert.NotNil(t, tc.Servers[0])
address := "127.0.0.1:" + strconv.Itoa(tc.Servers[0].Port)
config.GetBaseConfig().ServiceDiscoveries[testName] = &config.ServiceDiscoveryConfig{
Protocol: "zookeeper",
RemoteRef: "test",
}
config.GetBaseConfig().Remotes[testName] = &config.RemoteConfig{
Address: address,
TimeoutStr: "10s",
}
}
func TestNewZookeeperServiceDiscovery(t *testing.T) {
name := "zookeeper1"
_, err := newZookeeperServiceDiscovery(name)
// the ServiceDiscoveryConfig not found
assert.NotNil(t, err)
sdc := &config.ServiceDiscoveryConfig{
Protocol: "zookeeper",
RemoteRef: "mock",
}
config.GetBaseConfig().ServiceDiscoveries[name] = sdc
_, err = newZookeeperServiceDiscovery(name)
// RemoteConfig not found
assert.NotNil(t, err)
}
func TestCURDZookeeperServiceDiscovery(t *testing.T) {
extension.SetEventDispatcher("mock", func() observer.EventDispatcher {
return &dispatcher.MockEventDispatcher{}
})
extension.SetGlobalServiceNameMapping(func() mapping.ServiceNameMapping {
return &mockServiceNameMapping{}
})
extension.SetProtocol("mock", func() protocol.Protocol {
return &mockProtocol{}
})
sd, err := newZookeeperServiceDiscovery(testName)
assert.Nil(t, err)
ServiceName: testName,
Host: "127.0.0.1",
Port: 2233,
Enable: true,
Healthy: true,
Metadata: nil,
}
ins.Metadata = map[string]string{"t1": "test1", constant.METADATA_SERVICE_URL_PARAMS_PROPERTY_NAME: `{"protocol":"mock","timeout":"10000","version":"1.0.0","dubbo":"2.0.2","release":"2.7.6","port":"2233"}`}
err = sd.Register(ins)
testsPager := sd.GetHealthyInstancesByPage(testName, 0, 1, true)
assert.Equal(t, 1, testsPager.GetDataSize())
assert.Equal(t, 1, testsPager.GetTotalPages())
test := testsPager.GetData()[0].(registry.ServiceInstance)
ServiceName: testName,
Host: "127.0.0.1",
Port: 2233,
Enable: true,
Healthy: true,
}
ins.Metadata = map[string]string{"t1": "test12", constant.METADATA_SERVICE_URL_PARAMS_PROPERTY_NAME: `{"protocol":"mock","timeout":"10000","version":"1.0.0","dubbo":"2.0.2","release":"2.7.6","port":"2233"}`}
err = sd.Update(ins)
testsPager = sd.GetInstancesByPage(testName, 0, 1)
assert.Equal(t, 1, testsPager.GetDataSize())
test = testsPager.GetData()[0].(registry.ServiceInstance)
assert.Equal(t, "test12", test.GetMetadata()["t1"])
testsMap := sd.GetRequestInstances([]string{testName}, 0, 1)
assert.Equal(t, 1, len(testsMap))
assert.Equal(t, 1, testsMap[testName].GetDataSize())
test = testsMap[testName].GetData()[0].(registry.ServiceInstance)
assert.Equal(t, "test12", test.GetMetadata()["t1"])
names := sd.GetServices()
assert.Equal(t, 1, names.Size())
assert.Equal(t, testName, names.Values()[0])
err = sd.Unregister(®istry.DefaultServiceInstance{
ServiceName: testName,
Host: "127.0.0.1",
Port: 2233,
Enable: true,
Healthy: true,
Metadata: nil,
})
assert.Nil(t, err)
}
func TestAddListenerZookeeperServiceDiscovery(t *testing.T) {
sd, err := newZookeeperServiceDiscovery(testName)
assert.Nil(t, err)
ServiceName: testName,
Host: "127.0.0.1",
Port: 2233,
Enable: true,
Healthy: true,
Metadata: nil,
}
ins.Metadata = map[string]string{"t1": "test12", constant.METADATA_SERVICE_URL_PARAMS_PROPERTY_NAME: `{"protocol":"mock","timeout":"10000","version":"1.0.0","dubbo":"2.0.2","release":"2.7.6","port":"2233"}`}
err = sd.Register(ins)
assert.Nil(t, err)
wg := &sync.WaitGroup{}
wg.Add(1)
tn := &testNotify{
wg: wg,
t: t,
}
extension.SetAndInitGlobalDispatcher("direct")
extension.GetGlobalDispatcher().AddEventListener(sicl)
err = sd.AddListener(sicl)
ServiceName: testName,
Host: "127.0.0.1",
Port: 2233,
Enable: true,
Healthy: true,
Metadata: nil,
}
ins.Metadata = map[string]string{"t1": "test12", constant.METADATA_SERVICE_URL_PARAMS_PROPERTY_NAME: `{"protocol":"mock","timeout":"10000","version":"1.0.0","dubbo":"2.0.2","release":"2.7.6","port":"2233"}`}
err = sd.Update(ins)
tn.wg.Wait()
}
type testNotify struct {
wg *sync.WaitGroup
t *testing.T
}
func (tn *testNotify) Notify(e *registry.ServiceEvent) {
assert.Equal(tn.t, "2233", e.Service.Port)
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
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
func (tn *testNotify) NotifyAll([]*registry.ServiceEvent, func()) {
}
type mockServiceNameMapping struct{}
func (m *mockServiceNameMapping) Map(string, string, string, string) error {
return nil
}
func (m *mockServiceNameMapping) Get(string, string, string, string) (*gxset.HashSet, error) {
return gxset.NewSet(config.GetApplicationConfig().Name), nil
}
type mockProtocol struct{}
func (m mockProtocol) Export(protocol.Invoker) protocol.Exporter {
panic("implement me")
}
func (m mockProtocol) Refer(*common.URL) protocol.Invoker {
return &mockInvoker{}
}
func (m mockProtocol) Destroy() {
panic("implement me")
}
type mockInvoker struct{}
func (m *mockInvoker) GetURL() *common.URL {
panic("implement me")
}
func (m *mockInvoker) IsAvailable() bool {
panic("implement me")
}
func (m *mockInvoker) Destroy() {
panic("implement me")
}
func (m *mockInvoker) Invoke(context.Context, protocol.Invocation) protocol.Result {
// for getMetadataInfo and ServiceInstancesChangedListenerImpl onEvent
serviceInfo := &common.ServiceInfo{ServiceKey: "test", MatchKey: "test"}
services := make(map[string]*common.ServiceInfo)
services["test"] = serviceInfo
return &protocol.RPCResult{
Rest: &common.MetadataInfo{
Services: services,
},
}
}