Skip to content

Commit

Permalink
added test
Browse files Browse the repository at this point in the history
  • Loading branch information
kelindar committed May 16, 2020
1 parent ea0462d commit 7c14f7d
Show file tree
Hide file tree
Showing 4 changed files with 60 additions and 2 deletions.
4 changes: 2 additions & 2 deletions v2/emitter.go
Original file line number Diff line number Diff line change
Expand Up @@ -159,13 +159,13 @@ func (c *Client) onMessage(_ mqtt.Client, m mqtt.Message) {
log.Println("emitter:", err.Error())
}

var r PresenceEvent
r := PresenceEvent{msg, make([]PresenceInfo, 0)}
if msg.Event == "status" {
if err := json.Unmarshal([]byte(msg.Who), &r.Who); err != nil {
log.Println("emitter:", err.Error())
}
} else {
r.Who[0] = PresenceInfo{}
r.Who = append(r.Who, PresenceInfo{})
if err := json.Unmarshal([]byte(msg.Who), &r.Who[0]); err != nil {
log.Println("emitter:", err.Error())
}
Expand Down
21 changes: 21 additions & 0 deletions v2/emitter_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -121,3 +121,24 @@ func TestFormatShare(t *testing.T) {
topic := formatShare("/key/", "share1", "/a/b/c/", []Option{WithoutEcho()})
assert.Equal(t, "key/$share/share1/a/b/c/?me=0", topic)
}

func TestPresence(t *testing.T) {
c := NewClient()

var events []PresenceEvent
c.OnPresence(func(_ *Client, ev PresenceEvent) {
events = append(events, ev)
})

c.onMessage(nil, &message{
topic: "emitter/presence/",
payload: ` {"time":1589626821,"event":"status","channel":"retain-demo/","who":[{"id":"B"}, {"id":"C"}]}`,
})

c.onMessage(nil, &message{
topic: "emitter/presence/",
payload: ` {"time":1589626821,"event":"subscribe","channel":"retain-demo/","who":{"id":"A"}}`,
})

assert.Equal(t, 2, len(events))
}
1 change: 1 addition & 0 deletions v2/go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ github.com/eclipse/paho.mqtt.golang v1.2.0 h1:1F8mhG9+aO5/xpdtFkW4SxOJB67ukuDC3t
github.com/eclipse/paho.mqtt.golang v1.2.0/go.mod h1:H9keYFcgq3Qr5OUJm/JZI/i6U7joQ8SYLhZwfeOo6Ts=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/stretchr/objx v0.1.0 h1:4G4v2dO3VZwixGIRoQ5Lfboy6nUhCyYzaqnIAPPhYs4=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/testify v1.3.0 h1:TivCn/peBQ7UY8ooIcPgZFpTNSz0Q2U6UrFlUfqbe0Q=
github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI=
Expand Down
36 changes: 36 additions & 0 deletions v2/types_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,3 +15,39 @@ func TestUUID(t *testing.T) {
assert.NotNil(t, id)
assert.Len(t, id, 36)
}

type message struct {
duplicate bool
qos byte
retained bool
topic string
messageID uint16
payload string
}

func (m *message) Duplicate() bool {
return m.duplicate
}

func (m *message) Qos() byte {
return m.qos
}

func (m *message) Retained() bool {
return m.retained
}

func (m *message) Topic() string {
return m.topic
}

func (m *message) MessageID() uint16 {
return m.messageID
}

func (m *message) Payload() []byte {
return []byte(m.payload)
}

func (m *message) Ack() {
}

0 comments on commit 7c14f7d

Please sign in to comment.