Commit 52f81ec7 authored by AlexStocks's avatar AlexStocks

Add: test and add pb case

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