Skip to content

Commit

Permalink
add unit test
Browse files Browse the repository at this point in the history
  • Loading branch information
liuyuecai committed Jun 15, 2022
1 parent 41dbf53 commit 9f4d8cc
Show file tree
Hide file tree
Showing 43 changed files with 775 additions and 590 deletions.
6 changes: 4 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,17 @@ require (
github.com/BurntSushi/toml v1.1.0 // indirect
github.com/apache/dubbo-getty v1.4.8
github.com/dubbogo/gost v1.11.23
github.com/dubbogo/tools v1.0.9 // indirect
github.com/fagongzi/goetty v1.3.1
github.com/fagongzi/log v0.0.0-20170831135209-9a647df25e0e
github.com/fagongzi/util v0.0.0-20181102105153-fd38e0f42a4f
github.com/golang/snappy v0.0.4 // indirect
github.com/kr/pretty v0.3.0 // indirect
github.com/natefinch/lumberjack v2.0.0+incompatible
github.com/pkg/errors v0.9.1
github.com/rogpeppe/go-internal v1.8.0 // indirect
github.com/stretchr/testify v1.7.0
go.uber.org/atomic v1.9.0
go.uber.org/zap v1.19.1
gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c // indirect
gopkg.in/natefinch/lumberjack.v2 v2.0.0 // indirect
vimagination.zapto.org/byteio v0.0.0-20200222190125-d27cba0f0b10
vimagination.zapto.org/memio v0.0.0-20200222190306-588ebc67b97d // indirect
Expand Down
528 changes: 4 additions & 524 deletions go.sum

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion pkg/config/getty_config.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ type GettySessionParam struct {
TCPNoDelay bool `default:"true" yaml:"tcp_no_delay" json:"tcp_no_delay,omitempty"`
TCPKeepAlive bool `default:"true" yaml:"tcp_keep_alive" json:"tcp_keep_alive,omitempty"`
KeepAlivePeriod time.Duration `default:"180" yaml:"keep_alive_period" json:"keep_alive_period,omitempty"`
CronPeriod time.Duration `default:"1" yaml:"keep_alive_period" json:"keep_alive_period,omitempty"`
CronPeriod time.Duration `default:"1" yaml:"cron_period" json:"cron_period,omitempty"`
TCPRBufSize int `default:"262144" yaml:"tcp_r_buf_size" json:"tcp_r_buf_size,omitempty"`
TCPWBufSize int `default:"65536" yaml:"tcp_w_buf_size" json:"tcp_w_buf_size,omitempty"`
TCPReadTimeout time.Duration `default:"1" yaml:"tcp_read_timeout" json:"tcp_read_timeout,omitempty"`
Expand Down
2 changes: 1 addition & 1 deletion pkg/protocol/codec/branch_register_req_codec.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ import (
)

func init() {
GetCodecManager().RegisterCodec(CodeTypeSeata, &BranchRegisterRequestCodec{})
GetCodecManager().RegisterCodec(CodecTypeSeata, &BranchRegisterRequestCodec{})
}

type BranchRegisterRequestCodec struct {
Expand Down
41 changes: 41 additions & 0 deletions pkg/protocol/codec/branch_register_req_codec_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
/*
* 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 codec

import (
"github.com/seata/seata-go/pkg/protocol/branch"
"github.com/seata/seata-go/pkg/protocol/message"
"github.com/stretchr/testify/assert"
"testing"
)

func TestBranchRegisterRequestCodec(t *testing.T) {
msg := message.BranchRegisterRequest{
Xid: "abc134",
ResourceId: "124",
LockKey: "a:1,b:2",
ApplicationData: []byte("abc"),
BranchType: branch.BranchTypeTCC,
}

codec := BranchRegisterRequestCodec{}
bytes := codec.Encode(msg)
msg2 := codec.Decode(bytes)

assert.Equal(t, msg, msg2)
}
28 changes: 14 additions & 14 deletions pkg/protocol/codec/branch_register_response_codec.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ import (
)

func init() {
GetCodecManager().RegisterCodec(CodeTypeSeata, &BranchRegisterResponseCodec{})
GetCodecManager().RegisterCodec(CodecTypeSeata, &BranchRegisterResponseCodec{})
}

type BranchRegisterResponseCodec struct {
Expand Down Expand Up @@ -59,8 +59,8 @@ func (c *BranchRegisterResponseCodec) Encode(in interface{}) []byte {
buf := goetty.NewByteBuf(0)
resp, _ := in.(message.BranchRegisterResponse)

resultCode := ReadByte(buf)
if resultCode == byte(message.ResultCodeFailed) {
buf.WriteByte(byte(resp.ResultCode))
if resp.ResultCode == message.ResultCodeFailed {
var msg string
if len(resp.Msg) > 128 {
msg = resp.Msg[:128]
Expand All @@ -72,17 +72,17 @@ func (c *BranchRegisterResponseCodec) Encode(in interface{}) []byte {

buf.WriteByte(byte(resp.TransactionExceptionCode))
branchID := uint64(resp.BranchId)
branchIdBytes := []byte{
byte(branchID >> 56),
byte(branchID >> 48),
byte(branchID >> 40),
byte(branchID >> 32),
byte(branchID >> 24),
byte(branchID >> 16),
byte(branchID >> 8),
byte(branchID),
}
buf.Write(branchIdBytes)
//branchIdBytes := []byte{
// byte(branchID >> 56),
// byte(branchID >> 48),
// byte(branchID >> 40),
// byte(branchID >> 32),
// byte(branchID >> 24),
// byte(branchID >> 16),
// byte(branchID >> 8),
// byte(branchID),
//}
buf.WriteUInt64(branchID)
return buf.RawBuf()
}

Expand Down
44 changes: 44 additions & 0 deletions pkg/protocol/codec/branch_register_response_codec_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
/*
* 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 codec

import (
"github.com/seata/seata-go/pkg/protocol/message"
"github.com/seata/seata-go/pkg/protocol/transaction"
"github.com/stretchr/testify/assert"
"testing"
)

func TestBranchRegisterResponseCodec(t *testing.T) {
msg := message.BranchRegisterResponse{
AbstractTransactionResponse: message.AbstractTransactionResponse{
AbstractResultMessage: message.AbstractResultMessage{
ResultCode: message.ResultCodeFailed,
Msg: "FAILED",
},
TransactionExceptionCode: transaction.TransactionExceptionCodeUnknown,
},
BranchId: 124356567,
}

codec := BranchRegisterResponseCodec{}
bytes := codec.Encode(msg)
msg2 := codec.Decode(bytes)

assert.Equal(t, msg, msg2)
}
9 changes: 4 additions & 5 deletions pkg/protocol/codec/codec.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,12 +33,11 @@ import (

type CodecType byte

// TODO 待重构
const (
CodeTypeSeata = CodecType(0x1)
CodeTypeProtobuf = CodecType(0x2)
CodeTypeKRYO = CodecType(0x4)
CodeTypeFST = CodecType(0x8)
CodecTypeSeata = CodecType(0x1)
CodecTypeProtobuf = CodecType(0x2)
CodecTypeKRYO = CodecType(0x4)
CodecTypeFST = CodecType(0x8)
)

type Codec interface {
Expand Down
File renamed without changes.
26 changes: 9 additions & 17 deletions pkg/protocol/codec/common_global_end_request_codec.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,25 +43,17 @@ func (c *CommonGlobalEndRequestCodec) Decode(in []byte) interface{} {

buf := goetty.NewByteBuf(len(in))
buf.Write(in)
var length uint16

var xidLen int
if buf.Readable() >= 2 {
xidLen = int(ReadUInt16(buf))
length = ReadUInt16(buf)
if length > 0 {
bytes := make([]byte, length)
res.Xid = string(Read(buf, bytes))
}
if buf.Readable() >= xidLen {
xidBytes := make([]byte, xidLen)
xidBytes = Read(buf, xidBytes)
res.Xid = string(xidBytes)
}

var extraDataLen int
if buf.Readable() >= 2 {
extraDataLen = int(ReadUInt16(buf))
}
if buf.Readable() >= extraDataLen {
extraDataBytes := make([]byte, xidLen)
extraDataBytes = Read(buf, extraDataBytes)
res.ExtraData = extraDataBytes
length = ReadUInt16(buf)
if length > 0 {
bytes := make([]byte, length)
res.ExtraData = Read(buf, bytes)
}

return res
Expand Down
2 changes: 1 addition & 1 deletion pkg/protocol/codec/common_identify_request_codec.go
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ func (c *AbstractIdentifyRequestCodec) Decode(in []byte) interface{} {
return msg
}
len = ReadUInt16(buf)
if len > 0 && uint16(buf.Readable()) > len {
if len > 0 && uint16(buf.Readable()) >= len {
extraDataBytes := make([]byte, len)
msg.ExtraData = Read(buf, extraDataBytes)
}
Expand Down
2 changes: 1 addition & 1 deletion pkg/protocol/codec/global_begin_request_codec.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ import (
)

func init() {
GetCodecManager().RegisterCodec(CodeTypeSeata, &GlobalBeginRequestCodec{})
GetCodecManager().RegisterCodec(CodecTypeSeata, &GlobalBeginRequestCodec{})
}

type GlobalBeginRequestCodec struct {
Expand Down
37 changes: 37 additions & 0 deletions pkg/protocol/codec/global_begin_request_codec_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
/*
* 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 codec

import (
"github.com/seata/seata-go/pkg/protocol/message"
"github.com/stretchr/testify/assert"
"testing"
)

func TestGlobalBeginRequestCodec(t *testing.T) {
msg := message.GlobalBeginRequest{
Timeout: 100,
TransactionName: "SeataGoTransaction",
}

codec := GlobalBeginRequestCodec{}
bytes := codec.Encode(msg)
msg2 := codec.Decode(bytes)

assert.Equal(t, msg, msg2)
}
2 changes: 1 addition & 1 deletion pkg/protocol/codec/global_begin_response_codec.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ import (
)

func init() {
GetCodecManager().RegisterCodec(CodeTypeSeata, &GlobalBeginResponseCodec{})
GetCodecManager().RegisterCodec(CodecTypeSeata, &GlobalBeginResponseCodec{})
}

type GlobalBeginResponseCodec struct {
Expand Down
46 changes: 46 additions & 0 deletions pkg/protocol/codec/global_begin_response_codec_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
/*
* 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 codec

import (
"github.com/seata/seata-go/pkg/protocol/message"
"github.com/seata/seata-go/pkg/protocol/transaction"
"github.com/stretchr/testify/assert"
"testing"
)

func TestGlobalBeginResponseCodec(t *testing.T) {
msg := message.GlobalBeginResponse{
AbstractTransactionResponse: message.AbstractTransactionResponse{
AbstractResultMessage: message.AbstractResultMessage{
ResultCode: message.ResultCodeFailed,
Msg: "FAILED",
},
TransactionExceptionCode: transaction.TransactionExceptionCodeBeginFailed,
},

Xid: "test-transaction-id",
ExtraData: []byte("TestExtraData"),
}

codec := GlobalBeginResponseCodec{}
bytes := codec.Encode(msg)
msg2 := codec.Decode(bytes)

assert.Equal(t, msg, msg2)
}
2 changes: 1 addition & 1 deletion pkg/protocol/codec/global_commit_req_codec.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ import (
)

func init() {
GetCodecManager().RegisterCodec(CodeTypeSeata, &GlobalCommitRequestCodec{})
GetCodecManager().RegisterCodec(CodecTypeSeata, &GlobalCommitRequestCodec{})
}

type GlobalCommitRequestCodec struct {
Expand Down
39 changes: 39 additions & 0 deletions pkg/protocol/codec/global_commit_req_codec_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
/*
* 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 codec

import (
"github.com/seata/seata-go/pkg/protocol/message"
"github.com/stretchr/testify/assert"
"testing"
)

func TestGlobalCommitRequestCodec(t *testing.T) {
msg := message.GlobalCommitRequest{
AbstractGlobalEndRequest: message.AbstractGlobalEndRequest{
Xid: "test-transaction-id",
ExtraData: []byte("TestExtraData"),
},
}

codec := GlobalCommitRequestCodec{}
bytes := codec.Encode(msg)
msg2 := codec.Decode(bytes)

assert.Equal(t, msg, msg2)
}
Loading

0 comments on commit 9f4d8cc

Please sign in to comment.