Skip to content
Snippets Groups Projects
Commit 535d719f authored by fangyincheng's avatar fangyincheng
Browse files

Tst:add test for protocol/jsonrpc

parent cbd05336
No related branches found
No related tags found
No related merge requests found
projectKey=dubbo-go
serverUrl=http://10.5.93.51:9000
serverVersion=7.2.1.14109
dashboardUrl=http://10.5.93.51:9000/dashboard?id=dubbo-go
ceTaskId=AWq7jn_CCGPI4YFoFYV9
ceTaskUrl=http://10.5.93.51:9000/api/ce/task?id=AWq7jn_CCGPI4YFoFYV9
File added
package jsonrpc
import (
"context"
"errors"
"strings"
"testing"
"time"
)
import (
"github.com/stretchr/testify/assert"
)
import (
"github.com/dubbo/go-for-apache-dubbo/common"
"github.com/dubbo/go-for-apache-dubbo/common/constant"
"github.com/dubbo/go-for-apache-dubbo/protocol"
)
type (
User struct {
Id string `json:"id"`
Name string `json:"name"`
}
UserProvider struct {
user map[string]User
}
)
func TestHTTPClient_Call(t *testing.T) {
methods, err := common.ServiceMap.Register("jsonrpc", &UserProvider{})
assert.NoError(t, err)
assert.Equal(t, "GetUser,GetUser1", methods)
// Export
proto := GetProtocol()
url, err := common.NewURL(context.Background(), "jsonrpc://127.0.0.1:20000/com.ikurento.user.UserProvider?anyhost=true&"+
"application=BDTService&category=providers&default.timeout=10000&dubbo=dubbo-provider-golang-1.0.0&"+
"environment=dev&interface=com.ikurento.user.UserProvider&ip=192.168.56.1&methods=GetUser%2C&"+
"module=dubbogo+user-info+server&org=ikurento.com&owner=ZX&pid=1447&revision=0.0.1&"+
"side=provider&timeout=3000&timestamp=1556509797245")
assert.NoError(t, err)
proto.Export(protocol.NewBaseInvoker(url))
client := NewHTTPClient(&HTTPOptions{
HandshakeTimeout: time.Second,
HTTPTimeout: time.Second,
})
// call GetUser
ctx := context.WithValue(context.Background(), constant.DUBBOGO_CTX_KEY, map[string]string{
"X-Proxy-Id": "dubbogo",
"X-Services": url.Path,
"X-Method": "GetUser",
})
req := client.NewRequest(url, "GetUser", []interface{}{"1", "username"})
reply := &User{}
err = client.Call(ctx, url, req, reply)
assert.NoError(t, err)
assert.Equal(t, "1", reply.Id)
assert.Equal(t, "username", reply.Name)
// call GetUser1
ctx = context.WithValue(context.Background(), constant.DUBBOGO_CTX_KEY, map[string]string{
"X-Proxy-Id": "dubbogo",
"X-Services": url.Path,
"X-Method": "GetUser1",
})
req = client.NewRequest(url, "GetUser1", []interface{}{""})
reply = &User{}
err = client.Call(ctx, url, req, reply)
assert.True(t, strings.Contains(err.Error(), "500 Internal Server Error"))
assert.True(t, strings.Contains(err.Error(), "\\\"result\\\":{},\\\"error\\\":{\\\"code\\\":-32000,\\\"message\\\":\\\"error\\\"}"))
// destroy
proto.Destroy()
}
func (u *UserProvider) GetUser(ctx context.Context, req []interface{}, rsp *User) error {
rsp.Id = req[0].(string)
rsp.Name = req[1].(string)
return nil
}
func (u *UserProvider) GetUser1(ctx context.Context, req []interface{}, rsp *User) error {
return errors.New("error")
}
func (u *UserProvider) Service() string {
return "com.ikurento.user.UserProvider"
}
func (u *UserProvider) Version() string {
return ""
}
package jsonrpc
import (
"encoding/json"
"testing"
)
import (
"github.com/stretchr/testify/assert"
)
type TestData struct {
Test string
}
func TestJsonClientCodec_Write(t *testing.T) {
cd := &CodecData{
ID: 1,
Method: "GetUser",
Args: []interface{}{"args", 2},
}
codec := newJsonClientCodec()
data, err := codec.Write(cd)
assert.NoError(t, err)
assert.Equal(t, "{\"jsonrpc\":\"2.0\",\"method\":\"GetUser\",\"params\":[\"args\",2],\"id\":1}\n", string(data))
}
func TestJsonClientCodec_Read(t *testing.T) {
codec := newJsonClientCodec()
codec.pending[1] = "GetUser"
rsp := &TestData{}
err := codec.Read([]byte("{\"jsonrpc\":\"2.0\",\"id\":1,\"result\":{\"Test\":\"test\"}}\n"), rsp)
assert.NoError(t, err)
assert.Equal(t, "test", rsp.Test)
//error
codec.pending[1] = "GetUser"
err = codec.Read([]byte("{\"jsonrpc\":\"2.0\",\"id\":1,\"error\":{\"code\":-32000,\"message\":\"error\"}}\n"), rsp)
assert.EqualError(t, err, "{\"code\":-32000,\"message\":\"error\"}")
}
func TestServerCodec_Write(t *testing.T) {
codec := newServerCodec()
a := json.RawMessage([]byte("1"))
codec.req = serverRequest{Version: "1.0", Method: "GetUser", ID: &a}
data, err := codec.Write("error", &TestData{Test: "test"})
assert.NoError(t, err)
assert.Equal(t, "{\"jsonrpc\":\"2.0\",\"id\":1,\"result\":{\"Test\":\"test\"},\"error\":{\"code\":-32000,\"message\":\"error\"}}\n", string(data))
}
func TestServerCodec_Read(t *testing.T) {
codec := newServerCodec()
header := map[string]string{}
err := codec.ReadHeader(header, []byte("{\"jsonrpc\":\"2.0\",\"method\":\"GetUser\",\"params\":[\"args\",2],\"id\":1}\n"))
assert.EqualError(t, err, "{\"code\":-32601,\"message\":\"Method not found\"}")
header["HttpMethod"] = "POST"
err = codec.ReadHeader(header, []byte("{\"jsonrpc\":\"2.0\",\"method\":\"GetUser\",\"params\":[\"args\",2],\"id\":1}\n"))
assert.NoError(t, err)
assert.Equal(t, "1", string([]byte(*codec.req.ID)))
assert.Equal(t, "GetUser", codec.req.Method)
assert.Equal(t, "2.0", codec.req.Version)
assert.Equal(t, "[\"args\",2]", string([]byte(*codec.req.Params)))
req := []interface{}{}
err = codec.ReadBody(&req)
assert.NoError(t, err)
assert.Equal(t, "args", req[0])
assert.Equal(t, float64(2), req[1])
}
...@@ -19,7 +19,7 @@ import ( ...@@ -19,7 +19,7 @@ import (
func TestJsonrpcProtocol_Export(t *testing.T) { func TestJsonrpcProtocol_Export(t *testing.T) {
// Export // Export
proto := GetProtocol() proto := GetProtocol()
url, err := common.NewURL(context.Background(), "dubbo://127.0.0.1:20000/com.ikurento.user.UserProvider?anyhost=true&"+ url, err := common.NewURL(context.Background(), "jsonrpc://127.0.0.1:20000/com.ikurento.user.UserProvider?anyhost=true&"+
"application=BDTService&category=providers&default.timeout=10000&dubbo=dubbo-provider-golang-1.0.0&"+ "application=BDTService&category=providers&default.timeout=10000&dubbo=dubbo-provider-golang-1.0.0&"+
"environment=dev&interface=com.ikurento.user.UserProvider&ip=192.168.56.1&methods=GetUser%2C&"+ "environment=dev&interface=com.ikurento.user.UserProvider&ip=192.168.56.1&methods=GetUser%2C&"+
"module=dubbogo+user-info+server&org=ikurento.com&owner=ZX&pid=1447&revision=0.0.1&"+ "module=dubbogo+user-info+server&org=ikurento.com&owner=ZX&pid=1447&revision=0.0.1&"+
...@@ -49,7 +49,7 @@ func TestJsonrpcProtocol_Export(t *testing.T) { ...@@ -49,7 +49,7 @@ func TestJsonrpcProtocol_Export(t *testing.T) {
func TestJsonrpcProtocol_Refer(t *testing.T) { func TestJsonrpcProtocol_Refer(t *testing.T) {
// Refer // Refer
proto := GetProtocol() proto := GetProtocol()
url, err := common.NewURL(context.Background(), "dubbo://127.0.0.1:20000/com.ikurento.user.UserProvider?anyhost=true&"+ url, err := common.NewURL(context.Background(), "jsonrpc://127.0.0.1:20000/com.ikurento.user.UserProvider?anyhost=true&"+
"application=BDTService&category=providers&default.timeout=10000&dubbo=dubbo-provider-golang-1.0.0&"+ "application=BDTService&category=providers&default.timeout=10000&dubbo=dubbo-provider-golang-1.0.0&"+
"environment=dev&interface=com.ikurento.user.UserProvider&ip=192.168.56.1&methods=GetUser%2C&"+ "environment=dev&interface=com.ikurento.user.UserProvider&ip=192.168.56.1&methods=GetUser%2C&"+
"module=dubbogo+user-info+server&org=ikurento.com&owner=ZX&pid=1447&revision=0.0.1&"+ "module=dubbogo+user-info+server&org=ikurento.com&owner=ZX&pid=1447&revision=0.0.1&"+
......
...@@ -220,7 +220,6 @@ func (z *zookeeperClient) unregisterEvent(zkPath string, event *chan struct{}) { ...@@ -220,7 +220,6 @@ func (z *zookeeperClient) unregisterEvent(zkPath string, event *chan struct{}) {
} else { } else {
z.eventRegistry[zkPath] = a z.eventRegistry[zkPath] = a
} }
break
} }
z.Unlock() z.Unlock()
} }
......
sonar.projectKey=dubbo-go
sonar.projectName=dubbo-go
sonar.host.url=http://10.5.93.51:9000
sonar.sourceEncoding=UTF-8
sonar.sources=.
sonar.exclusions=**/*_test.go,examples/**
sonar.tests=.
sonar.test.inclusions=**/*_test.go
sonar.test.exclusions=examples/**
sonar.go.coverage.reportPaths=./coverage.out
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment