-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Refactoring * Fix example * Add tests * Fix default option handling * Fix paho.IsConnected
- Loading branch information
Showing
8 changed files
with
273 additions
and
86 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
// Copyright 2019 The mqtt-go authors. | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
package mqtt | ||
|
||
import ( | ||
"github.com/at-wat/mqtt-go" | ||
paho "github.com/eclipse/paho.mqtt.golang" | ||
) | ||
|
||
func (c *pahoWrapper) wrapMessageHandler(h paho.MessageHandler) mqtt.Handler { | ||
return mqtt.HandlerFunc(func(m *mqtt.Message) { | ||
h(c, wrapMessage(m)) | ||
}) | ||
} | ||
|
||
func wrapMessage(msg *mqtt.Message) paho.Message { | ||
return &wrappedMessage{msg} | ||
} | ||
|
||
type wrappedMessage struct { | ||
*mqtt.Message | ||
} | ||
|
||
func (m *wrappedMessage) Duplicate() bool { | ||
return m.Message.Dup | ||
} | ||
|
||
func (m *wrappedMessage) Qos() byte { | ||
return byte(m.Message.QoS) | ||
} | ||
|
||
func (m *wrappedMessage) Retained() bool { | ||
return m.Message.Retain | ||
} | ||
|
||
func (m *wrappedMessage) Topic() string { | ||
return m.Message.Topic | ||
} | ||
|
||
func (m *wrappedMessage) MessageID() uint16 { | ||
return m.Message.ID | ||
} | ||
|
||
func (m *wrappedMessage) Payload() []byte { | ||
return m.Message.Payload | ||
} | ||
|
||
func (m *wrappedMessage) Ack() { | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
package mqtt | ||
|
||
import ( | ||
"bytes" | ||
"testing" | ||
|
||
"github.com/at-wat/mqtt-go" | ||
paho "github.com/eclipse/paho.mqtt.golang" | ||
) | ||
|
||
func TestWrapMessageHandler(t *testing.T) { | ||
msg := make(chan paho.Message, 100) | ||
ph := func(c paho.Client, m paho.Message) { | ||
msg <- m | ||
} | ||
h := (&pahoWrapper{}).wrapMessageHandler(ph) | ||
|
||
h.Serve(&mqtt.Message{ | ||
Topic: "topic", | ||
QoS: mqtt.QoS1, | ||
Payload: []byte{0x01, 0x02}, | ||
Dup: true, | ||
Retain: true, | ||
ID: 0x1234, | ||
}) | ||
|
||
if len(msg) != 1 { | ||
t.Fatalf("Expected number of handled messages: 1, got: %d", len(msg)) | ||
} | ||
m := <-msg | ||
if m.Topic() != "topic" { | ||
t.Errorf("Expected topic: 'topic', got: '%s'", m.Topic()) | ||
} | ||
if m.Qos() != 1 { | ||
t.Errorf("Expected QoS: 1, got: %d", m.Qos()) | ||
} | ||
if !bytes.Equal([]byte{0x01, 0x02}, m.Payload()) { | ||
t.Errorf("Expected payload: [1, 2], got: %v", m.Payload()) | ||
} | ||
if !m.Duplicate() { | ||
t.Errorf("Expected dup: true, got: %v", m.Duplicate()) | ||
} | ||
if !m.Retained() { | ||
t.Errorf("Expected retain: true, got: %v", m.Retained()) | ||
} | ||
if m.MessageID() != 0x1234 { | ||
t.Errorf("Expected ID: 1234, got: %x", m.MessageID()) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
// +build integration | ||
|
||
// Copyright 2019 The mqtt-go authors. | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
package mqtt | ||
|
||
import ( | ||
"bytes" | ||
"net/url" | ||
"testing" | ||
"time" | ||
|
||
paho "github.com/eclipse/paho.mqtt.golang" | ||
) | ||
|
||
func TestIntegration_PublishSubscribe(t *testing.T) { | ||
opts := paho.NewClientOptions() | ||
server, err := url.Parse("mqtt://localhost:1883") | ||
if err != nil { | ||
t.Fatalf("Unexpected error: '%v'", err) | ||
} | ||
opts.Servers = []*url.URL{server} | ||
opts.AutoReconnect = false | ||
opts.ClientID = "PahoWrapper" | ||
opts.KeepAlive = 0 | ||
|
||
cli := NewClient(opts) | ||
token := cli.Connect() | ||
if !token.WaitTimeout(5 * time.Second) { | ||
t.Fatal("Connect timeout") | ||
} | ||
msg := make(chan paho.Message, 100) | ||
token = cli.Subscribe("paho", 1, func(c paho.Client, m paho.Message) { | ||
msg <- m | ||
}) | ||
if !token.WaitTimeout(5 * time.Second) { | ||
t.Fatal("Subscribe timeout") | ||
} | ||
token = cli.Publish("paho", 1, false, []byte{0x12}) | ||
if !token.WaitTimeout(5 * time.Second) { | ||
t.Fatal("Publish timeout") | ||
} | ||
|
||
if !cli.IsConnected() { | ||
t.Error("Not connected") | ||
} | ||
if !cli.IsConnectionOpen() { | ||
t.Error("Not connection open") | ||
} | ||
|
||
select { | ||
case m := <-msg: | ||
if m.Topic() != "paho" { | ||
t.Errorf("Expected topic: 'topic', got: %s", m.Topic()) | ||
} | ||
if !bytes.Equal(m.Payload(), []byte{0x12}) { | ||
t.Errorf("Expected payload: [18], got: %v", m.Payload()) | ||
} | ||
case <-time.After(5 * time.Second): | ||
t.Fatal("Message timeout") | ||
} | ||
cli.Disconnect(10) | ||
time.Sleep(time.Second) | ||
|
||
if cli.IsConnected() { | ||
t.Error("Connected after disconnect") | ||
} | ||
if cli.IsConnectionOpen() { | ||
t.Error("Connection open after disconnect") | ||
} | ||
} |
Oops, something went wrong.