-
Notifications
You must be signed in to change notification settings - Fork 35
/
mock.go
144 lines (111 loc) · 2.6 KB
/
mock.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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
package beehive
import (
"errors"
"time"
"github.com/kandoo/beehive/Godeps/_workspace/src/golang.org/x/net/context"
"github.com/kandoo/beehive/state"
)
// MockMsg is a mock for Msg.
type MockMsg msg
func (m MockMsg) To() uint64 {
return m.MsgTo
}
func (m MockMsg) From() uint64 {
return m.MsgFrom
}
func (m MockMsg) Data() interface{} {
return m.MsgData
}
func (m MockMsg) Type() string {
return MsgType(m.MsgData)
}
func (m MockMsg) IsBroadCast() bool {
return m.MsgTo == Nil
}
func (m MockMsg) IsUnicast() bool {
return m.MsgTo != Nil
}
func (m MockMsg) NoReply() bool {
return m.MsgFrom == Nil
}
// MockRcvContext is a mock for RcvContext.
type MockRcvContext struct {
CtxHive Hive
CtxApp string
CtxDicts *state.InMem
CtxID uint64
CtxMsgs []Msg
// TODO(soheil): add message handling methods.
}
func (m MockRcvContext) Hive() Hive {
return m.CtxHive
}
func (m MockRcvContext) App() string {
return m.CtxApp
}
func (m *MockRcvContext) Dict(name string) state.Dict {
if m.CtxDicts == nil {
m.CtxDicts = state.NewInMem()
}
return m.CtxDicts.Dict(name)
}
func (m MockRcvContext) ID() uint64 {
return m.CtxID
}
func (m MockRcvContext) Printf(format string, a ...interface{}) {}
func (m *MockRcvContext) Emit(msgData interface{}) {
msg := MockMsg{
MsgData: msgData,
MsgFrom: m.ID(),
}
m.CtxMsgs = append(m.CtxMsgs, msg)
}
func (m MockRcvContext) SendToCell(msgData interface{}, app string,
cell CellKey) {
}
func (m MockRcvContext) DeferReply(msg Msg) Repliable {
return Repliable{From: msg.From()}
}
func (m *MockRcvContext) SendToBee(msgData interface{}, to uint64) {
msg := MockMsg{
MsgData: msgData,
MsgFrom: m.ID(),
MsgTo: to,
}
m.CtxMsgs = append(m.CtxMsgs, msg)
}
func (m *MockRcvContext) Reply(msg Msg, replyData interface{}) error {
if msg.NoReply() {
return errors.New("cannot reply")
}
m.SendToBee(replyData, msg.To())
return nil
}
func (m MockRcvContext) StartDetached(h DetachedHandler) uint64 {
return 0
}
func (m MockRcvContext) StartDetachedFunc(start StartFunc, stop StopFunc,
rcv RcvFunc) uint64 {
return 0
}
func (m MockRcvContext) LockCells(keys []CellKey) error {
return nil
}
func (m MockRcvContext) Snooze(d time.Duration) {}
func (m MockRcvContext) BeeLocal() interface{} {
return nil
}
func (m MockRcvContext) SetBeeLocal(d interface{}) {}
func (m MockRcvContext) BeginTx() error {
return nil
}
func (m MockRcvContext) CommitTx() error {
return nil
}
func (m MockRcvContext) AbortTx() error {
return nil
}
func (m MockRcvContext) Sync(ctx context.Context, req interface{}) (
res interface{}, err error) {
return m.CtxHive.Sync(ctx, req)
}