-
Notifications
You must be signed in to change notification settings - Fork 1
/
rpc_test.go
51 lines (47 loc) · 974 Bytes
/
rpc_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
// Copyright (c) 2019 Meng Huang (mhboy@outlook.com)
// This package is licensed under a MIT license that can be found in the LICENSE file.
package rpc
import (
"github.com/hslam/rpc/examples/codec/json/service"
"sync"
"testing"
"time"
)
func TestRPC(t *testing.T) {
network := "tcp"
addr := ":9999"
codec := "json"
server := NewServer()
err := server.Register(new(service.Arith))
if err != nil {
t.Error(err)
}
wg := sync.WaitGroup{}
wg.Add(1)
go func() {
defer wg.Done()
server.Listen(network, addr, codec)
}()
time.Sleep(time.Millisecond * 10)
conn, err := Dial(network, addr, codec)
if err != nil {
t.Error(err)
}
err = conn.Ping()
if err != nil {
t.Error(err)
}
A := int32(4)
B := int32(8)
req := &service.ArithRequest{A: A, B: B}
var res service.ArithResponse
if err := conn.Call("Arith.Multiply", req, &res); err != nil {
t.Error(err)
}
if res.Pro != A*B {
t.Error(res.Pro)
}
conn.Close()
server.Close()
wg.Wait()
}