mirror of
https://github.com/go-mqtt/mqtt.git
synced 2025-08-07 11:42:52 +03:00
FIX: mqtttest signature mismatch with Publish.
This commit is contained in:
@@ -13,11 +13,16 @@ import (
|
||||
|
||||
// NewPublishStub returns a new stub for mqtt.Client Publish with a fixed return
|
||||
// value.
|
||||
func NewPublishStub(returnFix error) func(message []byte, topic string) error {
|
||||
return func(message []byte, topic string) error {
|
||||
func NewPublishStub(returnFix error) func(quit <-chan struct{}, message []byte, topic string) error {
|
||||
return func(quit <-chan struct{}, message []byte, topic string) error {
|
||||
select {
|
||||
case <-quit:
|
||||
return mqtt.ErrCanceled
|
||||
default:
|
||||
return returnFix
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Transfer defines a message exchange.
|
||||
type Transfer struct {
|
||||
@@ -33,6 +38,12 @@ func NewReadSlicesMock(t testing.TB, want ...Transfer) func() (message, topic []
|
||||
|
||||
var wantIndex uint64
|
||||
|
||||
t.Cleanup(func() {
|
||||
if n := uint64(len(want)) - atomic.LoadUint64(&wantIndex); n > 0 {
|
||||
t.Errorf("want %d more MQTT ReadSlices", n)
|
||||
}
|
||||
})
|
||||
|
||||
return func() (message, topic []byte, err error) {
|
||||
i := atomic.AddUint64(&wantIndex, 1) - 1
|
||||
if i >= uint64(len(want)) {
|
||||
@@ -51,7 +62,7 @@ func NewReadSlicesMock(t testing.TB, want ...Transfer) func() (message, topic []
|
||||
|
||||
// NewPublishMock returns a new mock for mqtt.Client Publish, which compares the
|
||||
// invocation with want in order of appearance.
|
||||
func NewPublishMock(t testing.TB, want ...Transfer) func(message []byte, topic string) error {
|
||||
func NewPublishMock(t testing.TB, want ...Transfer) func(quit <-chan struct{}, message []byte, topic string) error {
|
||||
t.Helper()
|
||||
|
||||
var wantIndex uint64
|
||||
@@ -62,7 +73,14 @@ func NewPublishMock(t testing.TB, want ...Transfer) func(message []byte, topic s
|
||||
}
|
||||
})
|
||||
|
||||
return func(message []byte, topic string) error {
|
||||
return func(quit <-chan struct{}, message []byte, topic string) error {
|
||||
select {
|
||||
case <-quit:
|
||||
return mqtt.ErrCanceled
|
||||
default:
|
||||
break
|
||||
}
|
||||
|
||||
i := atomic.AddUint64(&wantIndex, 1) - 1
|
||||
if i >= uint64(len(want)) {
|
||||
t.Errorf("unwanted MQTT publish of %#x to %q", message, topic)
|
||||
|
37
mqtttest/mqtttest_test.go
Normal file
37
mqtttest/mqtttest_test.go
Normal file
@@ -0,0 +1,37 @@
|
||||
package mqtttest_test
|
||||
|
||||
import (
|
||||
"testing"
|
||||
|
||||
"github.com/pascaldekloe/mqtt"
|
||||
"github.com/pascaldekloe/mqtt/mqtttest"
|
||||
)
|
||||
|
||||
// Signatures
|
||||
var (
|
||||
client mqtt.Client
|
||||
subscribe = client.Subscribe
|
||||
unsubscribe = client.Unsubscribe
|
||||
publish = client.Publish
|
||||
publishAck = client.PublishAtLeastOnce
|
||||
readSlices = client.ReadSlices
|
||||
)
|
||||
|
||||
// Won't compile on failure.
|
||||
func TestSignatureMatch(t *testing.T) {
|
||||
var c mqtt.Client
|
||||
// check dupe assumptions
|
||||
subscribe = c.SubscribeLimitAtMostOnce
|
||||
subscribe = c.SubscribeLimitAtLeastOnce
|
||||
publishAck = c.PublishExactlyOnce
|
||||
|
||||
// check fits
|
||||
readSlices = mqtttest.NewReadSlicesMock(t)
|
||||
publish = mqtttest.NewPublishMock(t)
|
||||
publish = mqtttest.NewPublishStub(nil)
|
||||
publishAck = mqtttest.NewPublishAckStub(nil)
|
||||
subscribe = mqtttest.NewSubscribeMock(t)
|
||||
subscribe = mqtttest.NewSubscribeStub(nil)
|
||||
unsubscribe = mqtttest.NewUnsubscribeMock(t)
|
||||
unsubscribe = mqtttest.NewUnsubscribeStub(nil)
|
||||
}
|
Reference in New Issue
Block a user