1
0
mirror of https://github.com/go-mqtt/mqtt.git synced 2025-08-08 22:42:05 +03:00

FIX: mqtttest signature mismatch with Publish.

This commit is contained in:
Pascal S. de Kloe
2021-02-18 23:06:09 +01:00
parent e884abeef8
commit d5046be0f5
2 changed files with 60 additions and 5 deletions

View File

@@ -13,9 +13,14 @@ import (
// NewPublishStub returns a new stub for mqtt.Client Publish with a fixed return // NewPublishStub returns a new stub for mqtt.Client Publish with a fixed return
// value. // value.
func NewPublishStub(returnFix error) func(message []byte, topic string) error { func NewPublishStub(returnFix error) func(quit <-chan struct{}, message []byte, topic string) error {
return func(message []byte, topic string) error { return func(quit <-chan struct{}, message []byte, topic string) error {
return returnFix select {
case <-quit:
return mqtt.ErrCanceled
default:
return returnFix
}
} }
} }
@@ -33,6 +38,12 @@ func NewReadSlicesMock(t testing.TB, want ...Transfer) func() (message, topic []
var wantIndex uint64 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) { return func() (message, topic []byte, err error) {
i := atomic.AddUint64(&wantIndex, 1) - 1 i := atomic.AddUint64(&wantIndex, 1) - 1
if i >= uint64(len(want)) { 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 // NewPublishMock returns a new mock for mqtt.Client Publish, which compares the
// invocation with want in order of appearance. // 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() t.Helper()
var wantIndex uint64 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 i := atomic.AddUint64(&wantIndex, 1) - 1
if i >= uint64(len(want)) { if i >= uint64(len(want)) {
t.Errorf("unwanted MQTT publish of %#x to %q", message, topic) t.Errorf("unwanted MQTT publish of %#x to %q", message, topic)

37
mqtttest/mqtttest_test.go Normal file
View 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)
}