forked from ailidani/paxi
-
Notifications
You must be signed in to change notification settings - Fork 0
/
codec_test.go
76 lines (59 loc) · 1.1 KB
/
codec_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
65
66
67
68
69
70
71
72
73
74
75
76
package paxi
import (
"bytes"
"encoding/gob"
"testing"
)
type A struct {
I int
S string
B bool
}
type B struct {
S string
}
func TestCodecGob(t *testing.T) {
gob.Register(A{})
gob.Register(B{})
var send interface{}
var recv interface{}
buf := new(bytes.Buffer)
c := NewCodec("gob", buf)
send = A{1, "a", true}
c.Encode(&send)
c.Decode(&recv)
if send.(A) != recv.(A) {
t.Errorf("expect send %v and recv %v to be euqal", send, recv)
}
send = B{"test"}
c.Encode(&send)
c.Decode(&recv)
if send.(B) != recv.(B) {
t.Errorf("expect send %v and recv %v to be euqal", send, recv)
}
}
func BenchmarkCodecGob(b *testing.B) {
gob.Register(A{})
var send interface{}
var recv interface{}
buf := new(bytes.Buffer)
c := NewCodec("gob", buf)
send = A{1, "a", true}
b.ResetTimer()
for i := 0; i < b.N; i++ {
c.Encode(&send)
c.Decode(&recv)
}
}
func BenchmarkCodecJSON(b *testing.B) {
buf := new(bytes.Buffer)
var send interface{}
var recv interface{}
c := NewCodec("json", buf)
send = A{1, "a", true}
b.ResetTimer()
for i := 0; i < b.N; i++ {
c.Encode(&send)
c.Decode(&recv)
}
}