aboutsummaryrefslogtreecommitdiff
path: root/catbus_test.go
diff options
context:
space:
mode:
authorEthel Morgan <eth@ethulhu.co.uk>2020-06-30 16:08:31 +0100
committerEthel Morgan <eth@ethulhu.co.uk>2020-06-30 16:08:31 +0100
commit8c3ec81e8400f1d43dc2ca6a65753be60cae8d80 (patch)
treeb23eb305accb1cffc4bede497fce7bb90aa4760b /catbus_test.go
parent5d0003e5a36359afa21d6509de330b9fa2e81d70 (diff)
dedupe repeat messages by default for catbus.Subscribe()v0.0.6
this is configurable with ClientOptions.SubscribeEveryMessage.
Diffstat (limited to 'catbus_test.go')
-rw-r--r--catbus_test.go111
1 files changed, 111 insertions, 0 deletions
diff --git a/catbus_test.go b/catbus_test.go
index b345c48..ecabcdd 100644
--- a/catbus_test.go
+++ b/catbus_test.go
@@ -21,6 +21,117 @@ type (
}
)
+func TestSubscribe(t *testing.T) {
+ tests := []struct {
+ messages []Message
+ subscribeEveryMessage bool
+ want []Message
+ }{
+ {
+ messages: []Message{
+ {
+ Topic: "home/tv/power",
+ Retention: Retain,
+ Payload: "on",
+ },
+ {
+ Topic: "home/tv/power",
+ Retention: Retain,
+ Payload: "on",
+ },
+ },
+ want: []Message{
+ {
+ Topic: "home/tv/power",
+ Retention: Retain,
+ Payload: "on",
+ },
+ },
+ },
+ {
+ messages: []Message{
+ {
+ Topic: "home/tv/power",
+ Retention: Retain,
+ Payload: "on",
+ },
+ {
+ Topic: "home/tv/power",
+ Retention: Retain,
+ Payload: "off",
+ },
+ },
+ want: []Message{
+ {
+ Topic: "home/tv/power",
+ Retention: Retain,
+ Payload: "on",
+ },
+ {
+ Topic: "home/tv/power",
+ Retention: Retain,
+ Payload: "off",
+ },
+ },
+ },
+ {
+ messages: []Message{
+ {
+ Topic: "home/tv/power",
+ Retention: Retain,
+ Payload: "on",
+ },
+ {
+ Topic: "home/tv/power",
+ Retention: Retain,
+ Payload: "on",
+ },
+ },
+ subscribeEveryMessage: true,
+ want: []Message{
+ {
+ Topic: "home/tv/power",
+ Retention: Retain,
+ Payload: "on",
+ },
+ {
+ Topic: "home/tv/power",
+ Retention: Retain,
+ Payload: "on",
+ },
+ },
+ },
+ }
+
+ for i, tt := range tests {
+ fakeMQTT := &fakeMQTT{
+ callbackByTopic: map[string]mqtt.MessageHandler{},
+ payloadByTopic: map[string]string{},
+ }
+
+ catbus := &client{
+ mqtt: fakeMQTT,
+ payloadByTopic: map[string]string{},
+ subscribeEveryMessage: tt.subscribeEveryMessage,
+
+ syncCallbacks: true,
+ }
+
+ var got []Message
+ catbus.Subscribe("home/tv/power", func(_ Client, msg Message) {
+ got = append(got, msg)
+ })
+
+ for _, msg := range tt.messages {
+ fakeMQTT.send(msg.Topic, msg.Retention, msg.Payload)
+ }
+
+ if !reflect.DeepEqual(got, tt.want) {
+ t.Errorf("[%d]: got %v, want %v", i, got, tt.want)
+ }
+ }
+}
+
func TestOnConnect(t *testing.T) {
tests := []struct {
payloadByTopic map[string]string