-
Notifications
You must be signed in to change notification settings - Fork 17
/
turbo_context_test.go
125 lines (97 loc) · 2.81 KB
/
turbo_context_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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
package turbo
import (
"log"
"testing"
)
type mockForwardEvent struct {
IForwardEvent
}
type mockForwarkHandler struct {
BaseForwardHandler
transport chan string
}
func (self *mockForwarkHandler) TypeAssert(event IEvent) bool {
_, ok := self.cast(event)
return ok
}
func (self *mockForwarkHandler) cast(event IEvent) (val *mockForwardEvent, ok bool) {
val, ok = event.(*mockForwardEvent)
return
}
func (self *mockForwarkHandler) Process(ctx *DefaultPipelineContext, event IEvent) error {
self.transport <- self.GetName()
log.Println("mockForwarkHandler|Process....")
//向后走网络传输
ctx.SendForward(event)
return nil
}
type mockBackwardEvent struct {
IBackwardEvent
}
type mockBackwardHandler struct {
BaseBackwardHandler
transport chan string
}
func (self *mockBackwardHandler) TypeAssert(event IEvent) bool {
_, ok := self.cast(event)
return ok
}
func (self *mockBackwardHandler) cast(event IEvent) (val *mockBackwardEvent, ok bool) {
val, ok = event.(*mockBackwardEvent)
return
}
func (self *mockBackwardHandler) Process(ctx *DefaultPipelineContext, event IEvent) error {
self.transport <- self.GetName()
log.Println("mockBackwardHandler|Process....")
//向后走网络传输
// ctx.SendForward(&mockForwardEvent{})
return nil
}
type mockDoubleSideHandler struct {
BaseDoubleSidedHandler
transport chan string
}
func (self *mockDoubleSideHandler) TypeAssert(event IEvent) bool {
ok := self.cast(event)
return ok
}
func (self *mockDoubleSideHandler) cast(event IEvent) bool {
_, okb := event.(*mockBackwardEvent)
_, okf := event.(*mockForwardEvent)
return okb || okf
}
func (self *mockDoubleSideHandler) Process(ctx *DefaultPipelineContext, event IEvent) error {
self.transport <- self.GetName()
log.Println("mockDoubleSideHandler|Process....")
//向后走网络传输
ctx.SendBackward(&mockBackwardEvent{})
return nil
}
//测试一下context
func TestPipelineContext(t *testing.T) {
transport := make(chan string, 10)
pipe := NewDefaultPipeline()
mfh := &mockForwarkHandler{transport: transport}
mfh.BaseForwardHandler = NewBaseForwardHandler("mockForwarkHandler", mfh)
pipe.RegisteHandler("mockForwarkHandler", mfh)
mbh := &mockBackwardHandler{transport: transport}
mbh.BaseBackwardHandler = NewBaseBackwardHandler("mockBackwardHandler", mbh)
pipe.RegisteHandler("mockBackwardHandler", mbh)
mdh := &mockDoubleSideHandler{transport: transport}
mdh.BaseDoubleSidedHandler = NewBaseDoubleSidedHandler("mockDoubleHandler", mdh)
pipe.RegisteHandler("mockDoubleHandler", mdh)
pipe.FireWork(&mockForwardEvent{})
seq := []string{"mockForwarkHandler", "mockDoubleHandler", "mockBackwardHandler"}
for i := 0; i < 3; i++ {
select {
case h := <-transport:
if h == seq[i] && len(seq) > 0 {
log.Printf("TRACE|%s....\n", h)
} else {
t.Fail()
}
default:
t.Fail()
}
}
}