Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Contribute to GitLab
Sign in
Toggle navigation
G
getty
Project
Project
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
wei.xuan
getty
Commits
52f81ec7
Commit
52f81ec7
authored
Aug 07, 2018
by
AlexStocks
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Add: test and add pb case
parent
6fcc015a
Expand all
Hide whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
845 additions
and
46 deletions
+845
-46
main.go
rpc/client/app/main.go
+27
-28
service.pb.go
rpc/proto/service.pb.go
+793
-10
service.proto
rpc/proto/service.proto
+17
-0
test.go
rpc/proto/test.go
+8
-8
No files found.
rpc/client/app/main.go
View file @
52f81ec7
...
...
@@ -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
(
"
\n
start to run json rpc example:"
)
testJSON
()
time
.
Sleep
(
2e9
)
gxlog
.
CInfo
(
"
\n
start to run protobuf rpc example:"
)
testProtobuf
()
// testProtobuf()
}
rpc/proto/service.pb.go
View file @
52f81ec7
This diff is collapsed.
Click to expand it.
rpc/proto/service.proto
View file @
52f81ec7
...
...
@@ -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
rpc/proto/test.go
View file @
52f81ec7
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
j
errors
.
New
(
"this is a error test"
)
}
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment