Skip to content

Commit

Permalink
Solve code issue.
Browse files Browse the repository at this point in the history
  • Loading branch information
sunquakes committed Aug 16, 2024
1 parent cb3a008 commit 4f918fb
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 26 deletions.
19 changes: 11 additions & 8 deletions test/http_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,9 @@ import (
"time"
)

const EQUAL_MESSAGE_TEMPLETE = "%d + %d expected be %d, but %d got"
const ERROR_MESSAGE_TEMPLETE = "Error expected be %s, but %s got"

type IntRpc struct{}

type Params struct {
Expand Down Expand Up @@ -46,7 +49,7 @@ func TestHttpCall(t *testing.T) {
result := new(Result)
_ = c.Call("Add", &params, result, false)
if *result != 3 {
t.Errorf("%d + %d expected be %d, but %d got", params.A, params.B, 3, *result)
t.Errorf(EQUAL_MESSAGE_TEMPLETE, params.A, params.B, 3, *result)
}
}

Expand All @@ -62,7 +65,7 @@ func TestHttpCallMethod(t *testing.T) {
result := new(Result)
_ = c.Call("Add", &params, result, false)
if *result != 3 {
t.Errorf("%d + %d expected be %d, but %d got", params.A, params.B, 3, *result)
t.Errorf(EQUAL_MESSAGE_TEMPLETE, params.A, params.B, 3, *result)
}
}

Expand All @@ -78,7 +81,7 @@ func TestHttpNotifyCall(t *testing.T) {
result := new(Result)
_ = c.Call("Add", &params, result, true)
if *result != 5 {
t.Errorf("%d + %d expected be %d, but %d got", params.A, params.B, 5, *result)
t.Errorf(EQUAL_MESSAGE_TEMPLETE, params.A, params.B, 5, *result)
}
}

Expand All @@ -97,10 +100,10 @@ func TestHttpBatchCall(t *testing.T) {
err2 := c.BatchAppend("Add", Params{2, 3}, result2, false)
_ = c.BatchCall()
if *err2 != nil || *result2 != 5 {
t.Errorf("%d + %d expected be %d, but %d got", 2, 3, 5, result2)
t.Errorf(EQUAL_MESSAGE_TEMPLETE, 2, 3, 5, result2)
}
if (*err1).Error() != common.CodeMap[common.MethodNotFound] {
t.Errorf("Error message expected be %s, but %s got", common.CodeMap[common.MethodNotFound], (*err1).Error())
t.Errorf(ERROR_MESSAGE_TEMPLETE, common.CodeMap[common.MethodNotFound], (*err1).Error())
}
}

Expand Down Expand Up @@ -152,7 +155,7 @@ func TestHttpConsul(t *testing.T) {
result := new(Result)
c.Call("Add", &params, result, false)
if *result != 21 {
t.Errorf("%d + %d expected be %d, but %d got", params.A, params.B, 21, *result)
t.Errorf(EQUAL_MESSAGE_TEMPLETE, params.A, params.B, 21, *result)
}
}

Expand All @@ -178,7 +181,7 @@ func TestHttpNacos(t *testing.T) {
result := new(Result)
c.Call("Add", &params, result, false)
if *result != 21 {
t.Errorf("%d + %d expected be %d, but %d got", params.A, params.B, 21, *result)
t.Errorf(EQUAL_MESSAGE_TEMPLETE, params.A, params.B, 21, *result)
}
}

Expand All @@ -194,6 +197,6 @@ func TestDataType(t *testing.T) {
result := new(int)
_ = c.Call("Sub", &params, result, false)
if *result != 1 {
t.Errorf("%d + %d expected be %d, but %d got", params.A, params.B, 1, *result)
t.Errorf(EQUAL_MESSAGE_TEMPLETE, params.A, params.B, 1, *result)
}
}
38 changes: 20 additions & 18 deletions test/tcp_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ import (
"time"
)

const CUSTOM_ERROR = "Custom Error"

func TestTcpCall(t *testing.T) {
s, _ := jsonrpc4go.NewServer("tcp", 3601)
s.Register(new(IntRpc))
Expand All @@ -29,7 +31,7 @@ func TestTcpCall(t *testing.T) {
result := new(Result)
c.Call("Add", &params, result, false)
if *result != 3 {
t.Errorf("%d + %d expected be %d, but %d got", params.A, params.B, 3, *result)
t.Errorf(EQUAL_MESSAGE_TEMPLETE, params.A, params.B, 3, *result)
}
}

Expand All @@ -45,7 +47,7 @@ func TestTcpCallMethod(t *testing.T) {
result := new(Result)
c.Call("Add", &params, result, false)
if *result != 3 {
t.Errorf("%d + %d expected be %d, but %d got", params.A, params.B, 3, *result)
t.Errorf(EQUAL_MESSAGE_TEMPLETE, params.A, params.B, 3, *result)
}
}

Expand All @@ -61,7 +63,7 @@ func TestTcpNotifyCall(t *testing.T) {
result := new(Result)
s.Call("Add", &params, result, true)
if *result != 5 {
t.Errorf("%d + %d expected be %d, but %d got", params.A, params.B, 5, *result)
t.Errorf(EQUAL_MESSAGE_TEMPLETE, params.A, params.B, 5, *result)
}
}

Expand All @@ -81,10 +83,10 @@ func TestTcpBatchCall(t *testing.T) {
c.BatchCall()

if *err2 != nil || *result2 != 5 {
t.Errorf("%d + %d expected be %d, but %d got", 2, 3, 5, result2)
t.Errorf(EQUAL_MESSAGE_TEMPLETE, 2, 3, 5, result2)
}
if (*err1).Error() != common.CodeMap[common.MethodNotFound] {
t.Errorf("Error message expected be %s, but %s got", common.CodeMap[common.MethodNotFound], (*err1).Error())
t.Errorf(ERROR_MESSAGE_TEMPLETE, common.CodeMap[common.MethodNotFound], (*err1).Error())
}
}

Expand All @@ -102,7 +104,7 @@ func TestSetOption(t *testing.T) {
result := new(Result)
s.Call("Add", &params, result, false)
if *result != 3 {
t.Errorf("%d + %d expected be %d, but %d got", params.A, params.B, 3, *result)
t.Errorf(EQUAL_MESSAGE_TEMPLETE, params.A, params.B, 3, *result)
}
}

Expand Down Expand Up @@ -138,7 +140,7 @@ func TestSetHooks(t *testing.T) {
result := new(Result)
s.Call("Add", &params, result, false)
if *result != 3 {
t.Errorf("%d + %d expected be %d, but %d got", params.A, params.B, 3, *result)
t.Errorf(EQUAL_MESSAGE_TEMPLETE, params.A, params.B, 3, *result)
}
}

Expand All @@ -147,7 +149,7 @@ func TestSetHooksCustomError(t *testing.T) {
go func() {
s, _ := jsonrpc4go.NewServer("tcp", 3607)
s.SetBeforeFunc(func(id any, method string, p any) error {
return errors.New("Custom Error")
return errors.New(CUSTOM_ERROR)
})
s.Register(new(IntRpc))
s.Start()
Expand All @@ -156,8 +158,8 @@ func TestSetHooksCustomError(t *testing.T) {
s, _ := jsonrpc4go.NewClient("IntRpc", "tcp", "127.0.0.1:3607")
result := new(Result)
err := s.Call("Add", &params, result, false)
if err.Error() != "Custom Error" {
t.Errorf("Error expected be %s, but %s got", "Custom Error", err.Error())
if err.Error() != CUSTOM_ERROR {
t.Errorf(ERROR_MESSAGE_TEMPLETE, CUSTOM_ERROR, err.Error())
}
}

Expand All @@ -174,16 +176,16 @@ func TestRateLimit(t *testing.T) {
result := new(Result)
err := s.Call("Add", &params, result, false)
if err != nil {
t.Errorf("Error expected be %s, but %s got", "nil", err.Error())
t.Errorf(ERROR_MESSAGE_TEMPLETE, "nil", err.Error())
}
err = s.Call("Add", &params, result, false)
if err.Error() != "Too many requests" {
t.Errorf("Error expected be %s, but %s got", "Too many requests", err.Error())
t.Errorf(ERROR_MESSAGE_TEMPLETE, "Too many requests", err.Error())
}
time.Sleep(time.Duration(5) * time.Second)
err = s.Call("Add", &params, result, false)
if err != nil {
t.Errorf("Error expected be %s, but %s got", "nil", err.Error())
t.Errorf(ERROR_MESSAGE_TEMPLETE, "nil", err.Error())
}
}

Expand Down Expand Up @@ -259,7 +261,7 @@ func TestCoTcpCall(t *testing.T) {
result := new(Result)
c.Call("Add", &params, result, false)
if *result != (i + j) {
t.Errorf("%d + %d expected be %d, but %d got", params.A, params.B, (i + j), *result)
t.Errorf(EQUAL_MESSAGE_TEMPLETE, params.A, params.B, (i + j), *result)
}
}
}(&wg)
Expand Down Expand Up @@ -287,7 +289,7 @@ func TestFailConnect(t *testing.T) {
for i := 0; i < 20; i++ {
err = s.Call("Add", &params, result, false)
if err != nil {
t.Errorf("%d + %d expected be %d, but %d got", params.A, params.B, 3, *result)
t.Errorf(EQUAL_MESSAGE_TEMPLETE, params.A, params.B, 3, *result)
}
}
}
Expand Down Expand Up @@ -316,7 +318,7 @@ func TestRibbonTcpCall(t *testing.T) {
result := new(Result)
c.Call("Add", &params, result, false)
if *result != (i + j) {
t.Errorf("%d + %d expected be %d, but %d got", params.A, params.B, (i + j), *result)
t.Errorf(EQUAL_MESSAGE_TEMPLETE, params.A, params.B, (i + j), *result)
}
}
}(&wg)
Expand Down Expand Up @@ -346,7 +348,7 @@ func TestTcpConsul(t *testing.T) {
result := new(Result)
c.Call("Add", &params, result, false)
if *result != 21 {
t.Errorf("%d + %d expected be %d, but %d got", params.A, params.B, 21, *result)
t.Errorf(EQUAL_MESSAGE_TEMPLETE, params.A, params.B, 21, *result)
}
}

Expand All @@ -372,6 +374,6 @@ func TestTcpNacos(t *testing.T) {
result := new(Result)
c.Call("Add", &params, result, false)
if *result != 21 {
t.Errorf("%d + %d expected be %d, but %d got", params.A, params.B, 21, *result)
t.Errorf(EQUAL_MESSAGE_TEMPLETE, params.A, params.B, 21, *result)
}
}

0 comments on commit 4f918fb

Please sign in to comment.