Skip to content
Permalink
Browse files
优化单元测试
  • Loading branch information
Changeden committed Sep 23, 2021
1 parent 1a59e82 commit 8f630f53d03cc9889d5e39751db797c757e62bf9
Showing 6 changed files with 95 additions and 25 deletions.
3 go.mod
@@ -1,10 +1,7 @@
module github.com/apache/dubbo-go-hessian2

require (
dup_struct_name v0.0.0
github.com/dubbogo/gost v1.9.0
github.com/pkg/errors v0.9.1
github.com/stretchr/testify v1.4.0
)

replace dup_struct_name => ./dup_struct_name_demo
File renamed without changes.
@@ -15,7 +15,7 @@
* limitations under the License.
*/

package hessian
package hessian_test

type CaseZ struct {
Name string
@@ -15,14 +15,20 @@
* limitations under the License.
*/

package hessian
package hessian_test

import (
"bufio"
"bytes"
dup "dup_struct_name"
"github.com/apache/dubbo-go-hessian2"
"github.com/stretchr/testify/assert"
"testing"
"time"
)

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

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

func TestDupStructNameRequest(t *testing.T) {
RegisterPOJO(&dup.CaseZ{})
RegisterPOJO(&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
}

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

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

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

@@ -62,39 +85,51 @@ func TestDupStructNameRequest(t *testing.T) {
}

func TestDupStructNameResponse(t *testing.T) {
RegisterPOJO(&dup.CaseZ{})
RegisterPOJO(&CaseZ{})

packageType := PackageResponse
responseStatus := Response_OK
packageType := hessian.PackageResponse
responseStatus := hessian.Response_OK
var body interface{}
body = &CaseZ{Name: "TestDupStructNameResponse"}
resp, err := doTestHessianEncodeHeader(t, packageType, responseStatus, body)
assert.Nil(t, err)

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

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

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

decodedResponse := &Response{}
defer func() {
if err := recover(); err != nil {
if errStr, ok := err.(string); ok {
assert.Equal(t, EXPECTED_ERROR_MSG, errStr)
}
}
}()

decodedResponse := &hessian.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.RspObj = &CaseZ{}
err = codecR.ReadBody(decodedResponse)
assert.Nil(t, err)

t.Log(decodedResponse)

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

in, _ := EnsureInterface(UnpackPtrValue(EnsurePackValue(body)), nil)
out, _ := EnsureInterface(UnpackPtrValue(EnsurePackValue(decodedResponse.RspObj)), nil)
in, _ := hessian.EnsureInterface(hessian.UnpackPtrValue(hessian.EnsurePackValue(body)), nil)
out, _ := hessian.EnsureInterface(hessian.UnpackPtrValue(hessian.EnsurePackValue(decodedResponse.RspObj)), nil)
assert.Equal(t, in, out)
}
@@ -0,0 +1,22 @@
module hessian_test

go 1.17

require (
dup_struct_name v0.0.0
github.com/apache/dubbo-go-hessian2 v0.0.0
github.com/stretchr/testify v1.4.0
)

require (
github.com/davecgh/go-spew v1.1.0
github.com/dubbogo/gost v1.9.0
github.com/pkg/errors v0.9.1
github.com/pmezard/go-difflib v1.0.0
gopkg.in/yaml.v2 v2.2.2
)

replace (
dup_struct_name => ./dup_struct_name_demo
github.com/apache/dubbo-go-hessian2 => ../
)
@@ -0,0 +1,16 @@
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=
github.com/dubbogo/gost v1.9.0/go.mod h1:pPTjVyoJan3aPxBPNUX0ADkXjPibLo+/Ib0/fADXSG8=
github.com/pkg/errors v0.8.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
github.com/pkg/errors v0.9.1 h1:FEBLx1zS214owpjy7qsBeixbURkuhQAwrK5UwLGTwt4=
github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/testify v1.4.0 h1:2E4SXV/wtOkTonXsotYi4li6zVWxYlZuYNCXe9XRJyk=
github.com/stretchr/testify v1.4.0/go.mod h1:j7eGeouHqKxXV5pUuKE4zz7dFj8WfuZ+81PSLYec5m4=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/yaml.v2 v2.2.2 h1:ZCJp+EgiOT7lHqUV2J862kp8Qj64Jo6az82+3Td9dZw=
gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=

0 comments on commit 8f630f5

Please sign in to comment.