Commit 52f81ec7 authored by AlexStocks's avatar AlexStocks

Add: test and add pb case

parent 6fcc015a
...@@ -109,70 +109,69 @@ func initSignal() { ...@@ -109,70 +109,69 @@ func initSignal() {
} }
func testJSON() { func testJSON() {
ts := rpc_examples.TestService{}
testReq := rpc_examples.TestReq{"aaa", "bbb", "ccc"} testReq := rpc_examples.TestReq{"aaa", "bbb", "ccc"}
testRsp := rpc_examples.TestRsp{} testRsp := rpc_examples.TestRsp{}
err := client.Call(rpc.CodecJson, "127.0.0.1:20000", "TestService", "Test", &testReq, &testRsp) err := client.Call(rpc.CodecJson, "127.0.0.1:20000", ts.Service(), "Test", &testReq, &testRsp)
if err != nil { if err != nil {
log.Error("client.Call(TestService::Test) = error:%s", jerrors.ErrorStack(err)) log.Error("client.Call(TestService::Test) = error:%s", jerrors.ErrorStack(err))
return return
} }
gxlog.CError("TestService::Test(param:%#v) = res:%s", testReq, testRsp) log.Info("TestService::Test(param:%#v) = res:%s", testReq, testRsp)
return addReq := rpc_examples.AddReq{1, 10}
addRsp := rpc_examples.AddRsp{}
var addResult int err = client.Call(rpc.CodecJson, "127.0.0.1:10000", ts.Service(), "Add", &addReq, &addRsp)
err = client.Call(rpc.CodecJson, "127.0.0.1:10000", "TestService", "Add", 1, &addResult)
if err != nil { if err != nil {
log.Error("client.Call(TestService::Add) = error:%s", jerrors.ErrorStack(err)) log.Error("client.Call(TestService::Add) = error:%s", jerrors.ErrorStack(err))
return return
} }
log.Info("TestService::Add(1) = res:%d", addResult) log.Info("TestService::Add(req:%#v) = res:%#v", addReq, addRsp)
var errResult int errReq := rpc_examples.ErrReq{1}
err = client.Call(rpc.CodecJson, "127.0.0.1:10000", "TestService", "Err", 2, &errResult) errRsp := rpc_examples.ErrRsp{}
err = client.Call(rpc.CodecJson, "127.0.0.1:20000", ts.Service(), "Err", &errReq, &errRsp)
if err != nil { if err != nil {
// error test case, this invocation should step into this branch. // error test case, this invocation should step into this branch.
log.Error("client.Call(TestService::Err) = error:%s", jerrors.ErrorStack(err)) log.Error("client.Call(TestService::Err) = error:%s", jerrors.ErrorStack(err))
return return
} }
log.Info("TestService::Err(2) = res:%s", errResult) log.Info("TestService::Err(req:%#v) = res:%s", errReq, errRsp)
} }
func testProtobuf() { func testProtobuf() {
ts := rpc_examples.TestService{}
testReq := rpc_examples.TestReq{"aaa", "bbb", "ccc"} testReq := rpc_examples.TestReq{"aaa", "bbb", "ccc"}
testRsp := rpc_examples.TestRsp{} testRsp := rpc_examples.TestRsp{}
err := client.Call(rpc.CodecProtobuf, "127.0.0.1:20000", "TestService", "Test", &testReq, &testRsp) err := client.Call(rpc.CodecProtobuf, "127.0.0.1:20000", ts.Service(), "Test", &testReq, &testRsp)
if err != nil { if err != nil {
log.Error("client.Call(TestService::Test) = error:%s", jerrors.ErrorStack(err)) log.Error("client.Call(TestService::Test) = error:%s", jerrors.ErrorStack(err))
return return
} }
gxlog.CError("TestService::Test(param:%#v) = res:%s", testReq, testRsp) log.Info("TestService::Test(param:%#v) = res:%s", testReq, testRsp)
return
// var addResult int addReq := rpc_examples.AddReq{1, 10}
// err = client.Call(rpc.CodecProtobuf, "127.0.0.1:20000", "TestService", "Add", 1, &addResult) addRsp := rpc_examples.AddRsp{}
// if err != nil { err = client.Call(rpc.CodecProtobuf, "127.0.0.1:10000", ts.Service(), "Add", &addReq, &addRsp)
// log.Error("client.Call(TestService::Add) = error:%s", jerrors.ErrorStack(err)) if err != nil {
// return log.Error("client.Call(TestService::Add) = error:%s", jerrors.ErrorStack(err))
// } return
// log.Info("TestService::Add(1) = res:%d", addResult) }
log.Info("TestService::Add(req:%#v) = res:%#v", addReq, addRsp)
var errResult int errReq := rpc_examples.ErrReq{1}
err = client.Call(rpc.CodecProtobuf, "127.0.0.1:20000", "TestService", "Err", 2, &errResult) errRsp := rpc_examples.ErrRsp{}
err = client.Call(rpc.CodecProtobuf, "127.0.0.1:20000", ts.Service(), "Err", &errReq, &errRsp)
if err != nil { if err != nil {
// error test case, this invocation should step into this branch. // error test case, this invocation should step into this branch.
log.Error("client.Call(TestService::Err) = error:%s", jerrors.ErrorStack(err)) log.Error("client.Call(TestService::Err) = error:%s", jerrors.ErrorStack(err))
return return
} }
log.Info("TestService::Err(2) = res:%s", errResult) log.Info("TestService::Err(req:%#v) = res:%#v", errReq, errRsp)
} }
func test() { func test() {
gxlog.CInfo("\nstart to run json rpc example:")
testJSON() testJSON()
time.Sleep(2e9) // testProtobuf()
gxlog.CInfo("\nstart to run protobuf rpc example:")
testProtobuf()
} }
This diff is collapsed.
...@@ -33,3 +33,20 @@ message TestReq { ...@@ -33,3 +33,20 @@ message TestReq {
message TestRsp { message TestRsp {
optional string A = 1 [(gogoproto.nullable) = false]; optional string A = 1 [(gogoproto.nullable) = false];
} }
message AddReq {
optional int32 A = 1 [(gogoproto.nullable) = false];
optional int32 B = 2 [(gogoproto.nullable) = false];
}
message AddRsp {
optional int32 Sum = 1 [(gogoproto.nullable) = false];
}
message ErrReq {
optional int32 A = 1 [(gogoproto.nullable) = false];
}
message ErrRsp {
optional int32 A = 1 [(gogoproto.nullable) = false];
}
\ No newline at end of file
package rpc_examples package rpc_examples
import ( import (
"errors" jerrors "github.com/juju/errors"
// "errors"
) )
type TestService struct { type TestService struct {
...@@ -16,17 +17,16 @@ func (r *TestService) Version() string { ...@@ -16,17 +17,16 @@ func (r *TestService) Version() string {
return "v1.0" return "v1.0"
} }
func (r *TestService) Test(arg TestReq, rsp *TestRsp) error { func (r *TestService) Test(req *TestReq, rsp *TestRsp) error {
rsp.A = arg.A + ", " + arg.B + ", " + arg.C rsp.A = req.A + ", " + req.B + ", " + req.C
return nil return nil
} }
func (r *TestService) Add(n int, res *int) error { func (r *TestService) Add(req *AddReq, rsp *AddRsp) error {
r.i += n rsp.Sum = req.A + req.B
*res = r.i + 100
return nil return nil
} }
func (r *TestService) Err(n int, res *int) error { func (r *TestService) Err(req *ErrReq, rsp *ErrRsp) error {
return errors.New("this is a error test") return jerrors.New("this is a error test")
} }
Markdown is supported
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