diff options
author | Ethel Morgan <eth@ethulhu.co.uk> | 2020-06-24 12:24:00 +0100 |
---|---|---|
committer | Ethel Morgan <eth@ethulhu.co.uk> | 2020-06-24 12:24:00 +0100 |
commit | 9ccb33b03d4d2ac71fe0f1fec1ee0a64cca157f5 (patch) | |
tree | 01ac690aa2f4b39e9c859521a3336c2de5d57895 /catbus_test.go | |
parent | b8209be849b80f1018fd5d4aa8c209fd91326a8c (diff) |
use string instead of []bytev0.0.2
subsequently, create a catbus.Message wrapper struct for mqtt.Message.
Diffstat (limited to 'catbus_test.go')
-rw-r--r-- | catbus_test.go | 66 |
1 files changed, 33 insertions, 33 deletions
diff --git a/catbus_test.go b/catbus_test.go index d07367b..cc22ebb 100644 --- a/catbus_test.go +++ b/catbus_test.go @@ -17,38 +17,38 @@ import ( type ( message struct { retention Retention - payload []byte + payload string } ) func TestOnConnect(t *testing.T) { tests := []struct { - payloadByTopic map[string][]byte + payloadByTopic map[string]string subscribe []string receive map[string]message - want map[string][]byte + want map[string]string }{ { - payloadByTopic: map[string][]byte{ - "tv/power": []byte("off"), + payloadByTopic: map[string]string{ + "tv/power": "off", }, - want: map[string][]byte{ - "tv/power": []byte("off"), + want: map[string]string{ + "tv/power": "off", }, }, { - payloadByTopic: map[string][]byte{ - "tv/power": []byte("off"), + payloadByTopic: map[string]string{ + "tv/power": "off", }, subscribe: []string{ "tv/power", }, receive: map[string]message{ - "tv/power": {Retain, []byte("on")}, + "tv/power": {Retain, "on"}, }, - want: map[string][]byte{ - "tv/power": []byte("on"), + want: map[string]string{ + "tv/power": "on", }, }, { @@ -56,24 +56,24 @@ func TestOnConnect(t *testing.T) { "tv/power", }, receive: map[string]message{ - "tv/power": {Retain, []byte("on")}, + "tv/power": {Retain, "on"}, }, - want: map[string][]byte{ - "tv/power": []byte("on"), + want: map[string]string{ + "tv/power": "on", }, }, { - payloadByTopic: map[string][]byte{ - "tv/power": []byte("off"), + payloadByTopic: map[string]string{ + "tv/power": "off", }, subscribe: []string{ "tv/power", }, receive: map[string]message{ - "tv/power": {DontRetain, []byte("on")}, + "tv/power": {DontRetain, "on"}, }, - want: map[string][]byte{ - "tv/power": []byte("on"), + want: map[string]string{ + "tv/power": "on", }, }, { @@ -81,33 +81,33 @@ func TestOnConnect(t *testing.T) { "tv/power", }, receive: map[string]message{ - "tv/power": {DontRetain, []byte("on")}, + "tv/power": {DontRetain, "on"}, }, - want: map[string][]byte{}, + want: map[string]string{}, }, { - payloadByTopic: map[string][]byte{ - "tv/power": []byte("off"), + payloadByTopic: map[string]string{ + "tv/power": "off", }, subscribe: []string{ "tv/power", }, receive: map[string]message{ - "tv/power": {DontRetain, []byte{}}, + "tv/power": {DontRetain, ""}, }, - want: map[string][]byte{}, + want: map[string]string{}, }, } for i, tt := range tests { fakeMQTT := &fakeMQTT{ callbackByTopic: map[string]mqtt.MessageHandler{}, - payloadByTopic: map[string][]byte{}, + payloadByTopic: map[string]string{}, } catbus := &Client{ mqtt: fakeMQTT, - payloadByTopic: map[string][]byte{}, + payloadByTopic: map[string]string{}, onconnectTimerByTopic: map[string]*time.Timer{}, onconnectDelay: 1 * time.Millisecond, onconnectJitter: 1, @@ -141,7 +141,7 @@ type ( mqtt.Client callbackByTopic map[string]mqtt.MessageHandler - payloadByTopic map[string][]byte + payloadByTopic map[string]string } fakeMessage struct { @@ -156,9 +156,9 @@ type ( ) func (f *fakeMQTT) Publish(topic string, qos byte, retain bool, payload interface{}) mqtt.Token { - bytes, ok := payload.([]byte) + bytes, ok := payload.(string) if !ok { - panic(fmt.Sprintf("expected type []byte, got %v", reflect.TypeOf(payload))) + panic(fmt.Sprintf("expected type string, got %v", reflect.TypeOf(payload))) } log.Printf("topic %q payload %s", topic, payload) @@ -170,7 +170,7 @@ func (f *fakeMQTT) Subscribe(topic string, qos byte, callback mqtt.MessageHandle return &fakeToken{} } -func (f *fakeMQTT) send(topic string, retention Retention, payload []byte) { +func (f *fakeMQTT) send(topic string, retention Retention, payload string) { // if retention == Retain { // f.payloadByTopic[topic] = payload // } @@ -179,7 +179,7 @@ func (f *fakeMQTT) send(topic string, retention Retention, payload []byte) { msg := &fakeMessage{ topic: topic, retained: bool(retention), - payload: payload, + payload: []byte(payload), } callback(f, msg) } |