forked from st3v/go-plugins
-
Notifications
You must be signed in to change notification settings - Fork 0
/
mqtt_mock.go
175 lines (134 loc) · 2.71 KB
/
mqtt_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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
package mqtt
import (
"math/rand"
"sync"
"time"
"github.com/eclipse/paho.mqtt.golang"
)
type mockClient struct {
sync.Mutex
connected bool
exit chan bool
subs map[string][]mqtt.MessageHandler
}
type mockMessage struct {
id uint16
topic string
qos byte
retained bool
payload interface{}
}
var (
_ mqtt.Client = newMockClient()
_ mqtt.Message = newMockMessage("mock", 0, false, nil)
)
func init() {
rand.Seed(time.Now().UnixNano())
}
func newMockClient() mqtt.Client {
return &mockClient{
subs: make(map[string][]mqtt.MessageHandler),
}
}
func newMockMessage(topic string, qos byte, retained bool, payload interface{}) mqtt.Message {
return &mockMessage{
id: uint16(rand.Int()),
topic: topic,
qos: qos,
retained: retained,
payload: payload,
}
}
func (m *mockMessage) Duplicate() bool {
return false
}
func (m *mockMessage) Qos() byte {
return m.qos
}
func (m *mockMessage) Retained() bool {
return m.retained
}
func (m *mockMessage) Topic() string {
return m.topic
}
func (m *mockMessage) MessageID() uint16 {
return m.id
}
func (m *mockMessage) Payload() []byte {
return m.payload.([]byte)
}
func (m *mockClient) AddRoute(topic string, h mqtt.MessageHandler) {
return
}
func (m *mockClient) IsConnected() bool {
m.Lock()
defer m.Unlock()
return m.connected
}
func (m *mockClient) Connect() mqtt.Token {
m.Lock()
defer m.Unlock()
if m.connected {
return nil
}
m.connected = true
m.exit = make(chan bool)
return &mqtt.ConnectToken{}
}
func (m *mockClient) Disconnect(uint) {
m.Lock()
defer m.Unlock()
if !m.connected {
return
}
m.connected = false
select {
case <-m.exit:
return
default:
close(m.exit)
}
}
func (m *mockClient) Publish(topic string, qos byte, retained bool, payload interface{}) mqtt.Token {
m.Lock()
defer m.Unlock()
if !m.connected {
return nil
}
msg := newMockMessage(topic, qos, retained, payload)
for _, sub := range m.subs[topic] {
sub(m, msg)
}
return &mqtt.PublishToken{}
}
func (m *mockClient) Subscribe(topic string, qos byte, h mqtt.MessageHandler) mqtt.Token {
m.Lock()
defer m.Unlock()
if !m.connected {
return nil
}
m.subs[topic] = append(m.subs[topic], h)
return &mqtt.SubscribeToken{}
}
func (m *mockClient) SubscribeMultiple(topics map[string]byte, h mqtt.MessageHandler) mqtt.Token {
m.Lock()
defer m.Unlock()
if !m.connected {
return nil
}
for topic, _ := range topics {
m.subs[topic] = append(m.subs[topic], h)
}
return &mqtt.SubscribeToken{}
}
func (m *mockClient) Unsubscribe(topics ...string) mqtt.Token {
m.Lock()
defer m.Unlock()
if !m.connected {
return nil
}
for _, topic := range topics {
delete(m.subs, topic)
}
return &mqtt.UnsubscribeToken{}
}