-
Notifications
You must be signed in to change notification settings - Fork 1
/
encoder_test.go
64 lines (60 loc) · 1.29 KB
/
encoder_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
52
53
54
55
56
57
58
59
60
61
62
63
64
// Copyright (c) 2019 Meng Huang ([email protected])
// 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 TestHeaderEncoder(t *testing.T) {
encoder := DefaultEncoder()
if encoder == nil {
t.Error("encoder is nil")
}
testHeaderEncoder("json", t)
testHeaderEncoder("code", t)
testHeaderEncoder("pb", t)
}
func testHeaderEncoder(headerEncoder string, t *testing.T) {
network := "tcp"
addr := ":9999"
codec := "json"
opts := DefaultOptions()
opts.Network = network
opts.Codec = codec
opts.HeaderEncoder = headerEncoder
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.ListenWithOptions(addr, opts)
}()
time.Sleep(time.Millisecond * 10)
conn, err := DialWithOptions(addr, opts)
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()
}