-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathloopbacktransport_test.go
77 lines (57 loc) · 1.38 KB
/
loopbacktransport_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 gorpc
import (
"testing"
log "github.com/Sirupsen/logrus"
)
type TestTransport struct {
LoopbackTransport
received chan string
}
func (this *TestTransport) init() {
this.received = make(chan string)
this.Protocol = GetFactory().MakeProtocol()
this.LoopbackTransport.SetFactory(GetFactory())
this.LoopbackTransport.Init(this.Receive, nil)
}
func (this *TestTransport) Receive(id, message string) {
this.received <- (id + ":" + message)
}
func TestLoopbackTransport(t *testing.T) {
beginTest("TestLoopbackTransport")
trans := &TestTransport{}
trans.init()
trans.Start()
trans.write("1", "Hello")
result := <-trans.received
if result != "1:Hello" {
t.Errorf("Result is wrong: (%T)%v", result,result)
}
trans.write("2", "World")
result = <-trans.received
if result != "2:World" {
t.Errorf("Result is wrong: (%T)%v", result,result)
}
log.Debug("Sending quit to LoopbackTransport")
trans.quit <- true
endTest()
}
func emptyReceive(id, message string) {
}
func BenchmarkTransport(b *testing.B) {
var count int = 0
f := GetFactory()
transport := f.MakeTransport(nil).(*LoopbackTransport)
transport.Init(func(id, message string) {
count--
}, nil)
transport.Start()
b.ResetTimer()
for i:=0; i < b.N; i++ {
count++
transport.write("Id", "Message");
}
transport.quit <- true
if count != 0 {
b.Errorf("Count should be zero: %v", count)
}
}