-
Notifications
You must be signed in to change notification settings - Fork 384
/
push.go
78 lines (58 loc) · 1.56 KB
/
push.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
package testutil
import (
"fmt"
"sync"
"berty.tech/berty/v2/go/pkg/protocoltypes"
"berty.tech/berty/v2/go/pkg/pushtypes"
)
const (
DummyPushServerAddr = "server.test"
PushMockBundleID = "tech.berty.mock"
)
type PushMockedDispatcher struct {
mu sync.RWMutex
bundleID string
queue map[string][][]byte
}
func (d *PushMockedDispatcher) TokenType() pushtypes.PushServiceTokenType {
return pushtypes.PushServiceTokenType_PushTokenMQTT
}
func (d *PushMockedDispatcher) Dispatch(data []byte, receiver *protocoltypes.PushServiceReceiver) error {
d.mu.Lock()
defer d.mu.Unlock()
d.queue[string(receiver.Token)] = append(d.queue[string(receiver.Token)], data)
return nil
}
func (d *PushMockedDispatcher) BundleID() string {
return d.bundleID
}
func (d *PushMockedDispatcher) Shift(token []byte) []byte {
d.mu.Lock()
defer d.mu.Unlock()
if len(d.queue[string(token)]) == 0 {
return nil
}
var payload []byte
payload, d.queue[string(token)] = d.queue[string(token)][0], d.queue[string(token)][1:]
return payload
}
func (d *PushMockedDispatcher) Len(token []byte) int {
d.mu.RLock()
defer d.mu.RUnlock()
return len(d.queue[string(token)])
}
func (d *PushMockedDispatcher) Debug() string {
d.mu.RLock()
defer d.mu.RUnlock()
out := "Showing contents of the push dispatcher:\n\n"
for k, v := range d.queue {
out = fmt.Sprintf("%s%s: %d entrie(s)\n", out, k, len(v))
}
return out
}
func NewPushMockedDispatcher(bundleID string) *PushMockedDispatcher {
return &PushMockedDispatcher{
bundleID: bundleID,
queue: map[string][][]byte{},
}
}