Skip to content
Permalink
Browse files
调整单元测试结构
  • Loading branch information
Changeden committed Sep 23, 2021
1 parent 1923caa commit 6e7538031b7b7b3b19482db1d3ce26e862bbabc5
Showing 7 changed files with 35 additions and 110 deletions.
@@ -15,26 +15,24 @@
* limitations under the License.
*/

package hessian_test
package hessian

import (
"bufio"
"bytes"
"testing"
"time"
)

import (
dup "dup_struct_name"
dup "dubbo-go-hessian2-dup-struct-name-test"
)

import (
"github.com/apache/dubbo-go-hessian2"
"github.com/stretchr/testify/assert"
)

const (
EXPECTED_ERROR_MSG = "reflect.Set: value of type hessian_test.CaseZ is not assignable to type hessian_test.CaseZ"
EXPECTED_ERROR_MSG = "reflect.Set: value of type hessian.CaseZ is not assignable to type hessian.CaseZ"
)

type CaseZ struct {
@@ -45,41 +43,24 @@ func (CaseZ) JavaClassName() string {
return "com.test.caseZ"
}

func doTestHessianEncodeHeader(t *testing.T, packageType hessian.PackageType, responseStatus byte, body interface{}) ([]byte, error) {
hessian.RegisterPOJO(&dup.CaseZ{})
hessian.RegisterPOJO(&CaseZ{})
codecW := hessian.NewHessianCodec(nil)
resp, err := codecW.Write(hessian.Service{
Path: "test",
Interface: "ITest",
Version: "v1.0",
Method: "test",
Timeout: time.Second * 10,
}, hessian.DubboHeader{
SerialID: 2,
Type: packageType,
ID: 1,
ResponseStatus: responseStatus,
}, body)
assert.Nil(t, err)
return resp, err
}

func TestDupStructNameRequest(t *testing.T) {
packageType := hessian.PackageRequest
responseStatus := hessian.Zero
RegisterPOJO(&dup.CaseZ{})
RegisterPOJO(&CaseZ{})

packageType := PackageRequest
responseStatus := Zero
var body interface{}
body = []interface{}{"a"}
resp, err := doTestHessianEncodeHeader(t, packageType, responseStatus, body)
assert.Nil(t, err)

codecR := hessian.NewHessianCodec(bufio.NewReader(bytes.NewReader(resp)))
codecR := NewHessianCodec(bufio.NewReader(bytes.NewReader(resp)))

h := &hessian.DubboHeader{}
h := &DubboHeader{}
err = codecR.ReadHeader(h)
assert.Nil(t, err)
assert.Equal(t, byte(2), h.SerialID)
assert.Equal(t, packageType, h.Type&(hessian.PackageRequest|hessian.PackageResponse|hessian.PackageHeartbeat))
assert.Equal(t, packageType, h.Type&(PackageRequest|PackageResponse|PackageHeartbeat))
assert.Equal(t, int64(1), h.ID)
assert.Equal(t, responseStatus, h.ResponseStatus)

@@ -103,13 +84,13 @@ func TestDupStructNameResponse(t *testing.T) {
body = &CaseZ{Name: "TestDupStructNameResponse"}
err, codecR, h := doTestHeader(t, body)

decodedResponse := &hessian.Response{}
decodedResponse := &Response{}
decodedResponse.RspObj = &dup.CaseZ{}
err = codecR.ReadBody(decodedResponse)
assert.NotNil(t, err)
assert.Equal(t, EXPECTED_ERROR_MSG, err.Error())

decodedResponse = &hessian.Response{}
decodedResponse = &Response{}
decodedResponse.RspObj = &CaseZ{}
err = codecR.ReadBody(decodedResponse)
assert.Nil(t, err)
@@ -130,48 +111,51 @@ func TestDupStructNameResponse2(t *testing.T) {
body = &dup.CaseZ{Name: "TestDupStructNameResponse"}
err, codecR, h := doTestHeader(t, body)

decodedResponse := &hessian.Response{}
decodedResponse := &Response{}
decodedResponse.RspObj = &CaseZ{}
err = codecR.ReadBody(decodedResponse)
assert.NotNil(t, err)
assert.Equal(t, EXPECTED_ERROR_MSG, err.Error())

decodedResponse = &hessian.Response{}
decodedResponse = &Response{}
decodedResponse.RspObj = &dup.CaseZ{}
err = codecR.ReadBody(decodedResponse)
assert.Nil(t, err)

checkResponseBody(t, decodedResponse, h, body)
}

func doTestHeader(t *testing.T, body interface{}) (error, *hessian.HessianCodec, *hessian.DubboHeader) {
packageType := hessian.PackageResponse
responseStatus := hessian.Response_OK
func doTestHeader(t *testing.T, body interface{}) (error, *HessianCodec, *DubboHeader) {
RegisterPOJO(&dup.CaseZ{})
RegisterPOJO(&CaseZ{})

packageType := PackageResponse
responseStatus := Response_OK
resp, err := doTestHessianEncodeHeader(t, packageType, responseStatus, body)
assert.Nil(t, err)

codecR := hessian.NewHessianCodec(bufio.NewReader(bytes.NewReader(resp)))
codecR := NewHessianCodec(bufio.NewReader(bytes.NewReader(resp)))

h := &hessian.DubboHeader{}
h := &DubboHeader{}
err = codecR.ReadHeader(h)
assert.Nil(t, err)

assert.Equal(t, byte(2), h.SerialID)
assert.Equal(t, packageType, h.Type&(hessian.PackageRequest|hessian.PackageResponse|hessian.PackageHeartbeat))
assert.Equal(t, packageType, h.Type&(PackageRequest|PackageResponse|PackageHeartbeat))
assert.Equal(t, int64(1), h.ID)
assert.Equal(t, responseStatus, h.ResponseStatus)
return err, codecR, h
}

func checkResponseBody(t *testing.T, decodedResponse *hessian.Response, h *hessian.DubboHeader, body interface{}) {
func checkResponseBody(t *testing.T, decodedResponse *Response, h *DubboHeader, body interface{}) {
t.Log(decodedResponse)

if h.ResponseStatus != hessian.Zero && h.ResponseStatus != hessian.Response_OK {
if h.ResponseStatus != Zero && h.ResponseStatus != Response_OK {
assert.Equal(t, "java exception:"+body.(string), decodedResponse.Exception.Error())
return
}

in, _ := hessian.EnsureInterface(hessian.UnpackPtrValue(hessian.EnsurePackValue(body)), nil)
out, _ := hessian.EnsureInterface(hessian.UnpackPtrValue(hessian.EnsurePackValue(decodedResponse.RspObj)), nil)
in, _ := EnsureInterface(UnpackPtrValue(EnsurePackValue(body)), nil)
out, _ := EnsureInterface(UnpackPtrValue(EnsurePackValue(decodedResponse.RspObj)), nil)
assert.Equal(t, in, out)
}
5 go.mod
@@ -1,7 +1,12 @@
module github.com/apache/dubbo-go-hessian2

go 1.17

require (
dubbo-go-hessian2-dup-struct-name-test v0.0.2
github.com/dubbogo/gost v1.9.0
github.com/pkg/errors v0.9.1
github.com/stretchr/testify v1.4.0
)

replace dubbo-go-hessian2-dup-struct-name-test v0.0.2 => github.com/ChangedenCZD/dubbo-go-hessian2-dup-struct-name-test v0.0.2
2 go.sum
@@ -1,3 +1,5 @@
github.com/ChangedenCZD/dubbo-go-hessian2-dup-struct-name-test v0.0.2 h1:r/eEBK6t2iZ/YDqMKYIbm/BLwjOhF424LWSj1Bs6aSE=
github.com/ChangedenCZD/dubbo-go-hessian2-dup-struct-name-test v0.0.2/go.mod h1:fHkCJ3VeG0Sl5TpciR5/2xt8jUlrQJaHg5uNcRxxR9M=
github.com/davecgh/go-spew v1.1.0 h1:ZDRjVQ15GmhC3fiQ8ni8+OwkZQO4DARzQgrnXU1Liz8=
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/dubbogo/gost v1.9.0 h1:UT+dWwvLyJiDotxJERO75jB3Yxgsdy10KztR5ycxRAk=

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

0 comments on commit 6e75380

Please sign in to comment.