This repository has been archived by the owner on Feb 1, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 5
/
mock_tracker.go
61 lines (50 loc) · 1.6 KB
/
mock_tracker.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
package rex
import "encoding/json"
type MockTracker struct {
BaseTracker
Messages map[string][][]byte
}
func NewMockTracker(metadata *EventMetadata) *MockTracker {
self := &MockTracker{}
self.EventMetadata = metadata
self.Messages = make(map[string][][]byte)
return self
}
func (self *MockTracker) Get(topic string, idx int) (map[string]interface{}, error) {
var r map[string]interface{}
if len(self.Messages) == 0 {
return nil, nil
}
msgs := self.Messages[topic]
if msgs == nil || len(msgs) < idx {
return nil, nil
}
err := json.Unmarshal(msgs[idx], &r)
return r, err
}
func (self *MockTracker) Close() {
}
func (self *MockTracker) FastMessage(topic string, message []byte) error {
self.Messages[topic] = append(self.Messages[topic], message)
return nil
}
func (self *MockTracker) FastEvent(topic string, e EventBase, full bool) error {
self.AddMetadata(e, full)
return self.FastMessage(topic, self.Encode(e))
}
func (self *MockTracker) FastEventMap(topic string, event map[string]interface{}, full bool) error {
self.AddMetadataMap(event, full)
return self.FastMessage(topic, self.EncodeMap(event))
}
func (self *MockTracker) SafeMessage(topic string, message []byte) error {
self.Messages[topic] = append(self.Messages[topic], message)
return nil
}
func (self *MockTracker) SafeEvent(topic string, e EventBase, full bool) error {
self.AddMetadata(e, full)
return self.SafeMessage(topic, self.Encode(e))
}
func (self *MockTracker) SafeEventMap(topic string, event map[string]interface{}, full bool) error {
self.AddMetadataMap(event, full)
return self.SafeMessage(topic, self.EncodeMap(event))
}