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

Add mqtttest NewReadSlicesStub.

This commit is contained in:
Pascal S. de Kloe
2021-03-14 13:57:56 +01:00
parent 4ecaaad6ab
commit bcaea67003
2 changed files with 14 additions and 5 deletions

View File

@@ -18,6 +18,18 @@ type Transfer struct {
Err error // result
}
// NewReadSlicesStub returns a new stub for mqtt.Client ReadSlices with a fixed
// return value.
func NewReadSlicesStub(fix Transfer) func() (message, topic []byte, err error) {
return func() (message, topic []byte, err error) {
// use copies to prevent some hard to trace issues
message = make([]byte, len(fix.Message))
copy(message, fix.Message)
topic = []byte(fix.Topic)
return message, topic, fix.Err
}
}
// NewReadSlicesMock returns a new mock for mqtt.Client ReadSlices, which
// returns the Transfers in order of appearance.
func NewReadSlicesMock(t testing.TB, want ...Transfer) func() (message, topic []byte, err error) {
@@ -39,11 +51,7 @@ func NewReadSlicesMock(t testing.TB, want ...Transfer) func() (message, topic []
return
}
// use copies to prevent some hard to trace issues
message = make([]byte, len(want[i].Message))
copy(message, want[i].Message)
topic = []byte(want[i].Topic)
return message, topic, want[i].Err
return NewReadSlicesStub(want[i])()
}
}

View File

@@ -26,6 +26,7 @@ func TestSignatureMatch(t *testing.T) {
publishEnqueued = c.PublishExactlyOnce
// check fits
readSlices = mqtttest.NewReadSlicesStub(mqtttest.Transfer{})
readSlices = mqtttest.NewReadSlicesMock(t)
publish = mqtttest.NewPublishMock(t)
publish = mqtttest.NewPublishStub(nil)