Skip to content
Snippets Groups Projects
dubbo_invoker_test.go 2.67 KiB
Newer Older
AlexStocks's avatar
AlexStocks 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.
 */
fangyincheng's avatar
fangyincheng committed

fangyincheng's avatar
fangyincheng committed
package dubbo

import (
flycash's avatar
flycash committed
	"context"
fangyincheng's avatar
fangyincheng committed
	"sync"
	"testing"
	"time"
)

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

import (
邹毅贤's avatar
邹毅贤 committed
	"github.com/apache/dubbo-go/common"
	"github.com/apache/dubbo-go/common/constant"
	"github.com/apache/dubbo-go/protocol/invocation"
fangyincheng's avatar
fangyincheng committed
)

func TestDubboInvoker_Invoke(t *testing.T) {
	proto, url := InitTest(t)

	c := &Client{
		pendingResponses: new(sync.Map),
fangyincheng's avatar
fangyincheng committed
		conf:             *clientConf,
xujianhai666's avatar
xujianhai666 committed
			ConnectTimeout: 3 * time.Second,
			RequestTimeout: 6 * time.Second,
fangyincheng's avatar
fangyincheng committed
	}
	c.pool = newGettyRPCClientConnPool(c, clientConf.PoolSize, time.Duration(int(time.Second)*clientConf.PoolTTL))

	invoker := NewDubboInvoker(url, c)
	user := &User{}

	inv := invocation.NewRPCInvocationWithOptions(invocation.WithMethodName("GetUser"), invocation.WithArguments([]interface{}{"1", "username"}),
		invocation.WithReply(user), invocation.WithAttachments(map[string]string{"test_key": "test_value"}))
fangyincheng's avatar
fangyincheng committed

	// Call
	res := invoker.Invoke(context.Background(), inv)
fangyincheng's avatar
fangyincheng committed
	assert.NoError(t, res.Error())
	assert.Equal(t, User{Id: "1", Name: "username"}, *res.Result().(*User))
	assert.Equal(t, "test_value", res.Attachments()["test_key"]) // test attachments for request/response
fangyincheng's avatar
fangyincheng committed

	// CallOneway
	inv.SetAttachments(constant.ASYNC_KEY, "true")
	res = invoker.Invoke(context.Background(), inv)
fangyincheng's avatar
fangyincheng committed
	assert.NoError(t, res.Error())

	// AsyncCall
	lock := sync.Mutex{}
	lock.Lock()
邹毅贤's avatar
邹毅贤 committed
	inv.SetCallBack(func(response common.CallbackResponse) {
		r := response.(AsyncCallbackResponse)
		assert.Equal(t, User{Id: "1", Name: "username"}, *r.Reply.(*Response).reply.(*User))
fangyincheng's avatar
fangyincheng committed
		lock.Unlock()
	})
	res = invoker.Invoke(context.Background(), inv)
fangyincheng's avatar
fangyincheng committed
	assert.NoError(t, res.Error())

	// Err_No_Reply
	inv.SetAttachments(constant.ASYNC_KEY, "false")
	inv.SetReply(nil)
	res = invoker.Invoke(context.Background(), inv)
	assert.EqualError(t, res.Error(), "request need @response")
fangyincheng's avatar
fangyincheng committed

	// destroy
	lock.Lock()
	proto.Destroy()
	lock.Unlock()
}