Skip to content
Snippets Groups Projects
service_test.go 3.86 KiB
Newer Older
vito.he's avatar
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.
 */

package inmemory

import (
	"fmt"
	"testing"
)

import (
	"github.com/stretchr/testify/assert"
)

import (
	"github.com/apache/dubbo-go/common"
	"github.com/apache/dubbo-go/metadata/definition"
)

func TestMetadataService(t *testing.T) {
flycash's avatar
flycash committed
	mts, _ := NewMetadataService()
vito.he's avatar
vito.he committed
	serviceName := "com.ikurento.user.UserProvider"
	group := "group1"
	version := "0.0.1"
	protocol := "dubbo"
	beanName := "UserProvider"
vito.he's avatar
vito.he committed

	u2, err := common.NewURL(fmt.Sprintf(
		"%v://127.0.0.1:20000/com.ikurento.user.UserProvider2?anyhost=true&"+
			"application=BDTService&category=providers&default.timeout=10000&dubbo=dubbo-provider-golang-1.0.0&"+
			"environment=dev&interface=%v&ip=192.168.56.1&methods=GetUser&module=dubbogo+user-info+server&org=ikurento.com&"+
			"owner=ZX&pid=1447&revision=0.0.1&side=provider&timeout=3000&timestamp=1556509797245&group=%v&version=%v&bean.name=%v",
		protocol, serviceName, group, version, beanName))
	assert.NoError(t, err)
	mts.ExportURL(u2)

	u3, err := common.NewURL(fmt.Sprintf(
		"%v://127.0.0.1:20000/com.ikurento.user.UserProvider3?anyhost=true&"+
			"application=BDTService&category=providers&default.timeout=10000&dubbo=dubbo-provider-golang-1.0.0&"+
			"environment=dev&interface=%v&ip=192.168.56.1&methods=GetUser&module=dubbogo+user-info+server&org=ikurento.com&"+
			"owner=ZX&pid=1447&revision=0.0.1&side=provider&timeout=3000&timestamp=1556509797245&group=%v&version=%v&bean.name=%v",
		protocol, serviceName, group, version, beanName))
	assert.NoError(t, err)
	mts.ExportURL(u3)

	u, err := common.NewURL(fmt.Sprintf(
		"%v://127.0.0.1:20000/com.ikurento.user.UserProvider1?anyhost=true&"+
			"application=BDTService&category=providers&default.timeout=10000&dubbo=dubbo-provider-golang-1.0.0&"+
			"environment=dev&interface=%v&ip=192.168.56.1&methods=GetUser&module=dubbogo+user-info+server&org=ikurento.com&"+
			"owner=ZX&pid=1447&revision=0.0.1&side=provider&timeout=3000&timestamp=1556509797245&group=%v&version=%v&bean.name=%v",
		protocol, serviceName, group, version, beanName))
	assert.NoError(t, err)
vito.he's avatar
vito.he committed
	mts.ExportURL(u)
vito.he's avatar
vito.he committed
	list, _ := mts.GetExportedURLs(serviceName, group, version, protocol)
flycash's avatar
flycash committed
	assert.Equal(t, 3, len(list))
vito.he's avatar
vito.he committed
	mts.SubscribeURL(u)

	mts.SubscribeURL(u)
vito.he's avatar
vito.he committed
	list2, _ := mts.GetSubscribedURLs()
flycash's avatar
flycash committed
	assert.Equal(t, 1, len(list2))
vito.he's avatar
vito.he committed

	mts.UnexportURL(u)
vito.he's avatar
vito.he committed
	list3, _ := mts.GetExportedURLs(serviceName, group, version, protocol)
flycash's avatar
flycash committed
	assert.Equal(t, 2, len(list3))
vito.he's avatar
vito.he committed

	mts.UnsubscribeURL(u)
vito.he's avatar
vito.he committed
	list4, _ := mts.GetSubscribedURLs()
flycash's avatar
flycash committed
	assert.Equal(t, 0, len(list4))
vito.he's avatar
vito.he committed

	userProvider := &definition.UserProvider{}
	common.ServiceMap.Register(serviceName, protocol, group, version, userProvider)
vito.he's avatar
vito.he committed
	mts.PublishServiceDefinition(u)
vito.he's avatar
vito.he committed
	expected := "{\"CanonicalName\":\"com.ikurento.user.UserProvider\",\"CodeSource\":\"\"," +
		"\"Methods\":[{\"Name\":\"GetUser\",\"ParameterTypes\":[\"slice\"],\"ReturnType\":\"ptr\"," +
		"\"Parameters\":null}],\"Types\":null}"
vito.he's avatar
vito.he committed
	def1, _ := mts.GetServiceDefinition(serviceName, group, version)
vito.he's avatar
vito.he committed
	assert.Equal(t, expected, def1)
vito.he's avatar
vito.he committed
	serviceKey := definition.ServiceDescriperBuild(serviceName, group, version)
vito.he's avatar
vito.he committed
	def2, _ := mts.GetServiceDefinitionByServiceKey(serviceKey)
vito.he's avatar
vito.he committed
	assert.Equal(t, expected, def2)
vito.he's avatar
vito.he committed
}