mqtttest

package
v1.0.2 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: May 9, 2023 License: CC0-1.0 Imports: 6 Imported by: 0

Documentation

Overview

Package mqtttest provides utilities for MQTT testing.

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func NewPublishExchangeStub added in v1.0.0

func NewPublishExchangeStub(errFix error, exchangeFix ...error) func(message []byte, topic string) (exchange <-chan error, err error)

NewPublishExchangeStub returns a stub for mqtt.Client PublishAtLeastOnce or PublishExactlyOnce with a fixed return value.

The exchangeFix errors are applied to the exchange return, with an option for ExchangeBlock entries. An mqtt.ErrClosed in the exchangeFix keeps the exchange channel open (without an extra ExchangeBlock entry).

Example
package main

import (
	"errors"
	"fmt"
	"time"

	"github.com/pascaldekloe/mqtt/mqtttest"
)

func main() {
	PublishExchange := mqtttest.NewPublishExchangeStub(nil,
		mqtttest.ExchangeBlock{Delay: time.Millisecond},
		errors.New("test storage failure"),
	)

	exchange, err := PublishExchange([]byte("Hi!"), "announce")
	if err != nil {
		fmt.Println("publish error:", err)
		return
	}
	for err := range exchange {
		fmt.Println("exchange error:", err)
	}
}
Output:

exchange error: test storage failure

func NewPublishMock

func NewPublishMock(t testing.TB, want ...Transfer) func(quit <-chan struct{}, message []byte, topic string) error

NewPublishMock returns a new mock for mqtt.Client Publish, which compares the invocation with want in order of appearance.

func NewPublishStub

func NewPublishStub(fix error) func(quit <-chan struct{}, message []byte, topic string) error

NewPublishStub returns a new stub for mqtt.Client Publish with a fixed return value.

func NewReadSlicesMock

func NewReadSlicesMock(t testing.TB, want ...Transfer) func() (message, topic []byte, err error)

NewReadSlicesMock returns a new mock for mqtt.Client ReadSlices, which returns the Transfers in order of appearance.

func NewReadSlicesStub added in v1.0.0

func NewReadSlicesStub(fix Transfer) func() (message, topic []byte, err error)

NewReadSlicesStub returns a new stub for mqtt.Client ReadSlices with a fixed return value.

func NewSubscribeMock

func NewSubscribeMock(t testing.TB, want ...Filter) func(quit <-chan struct{}, topicFilters ...string) error

NewSubscribeMock returns a new mock for mqtt.Client Subscribe, which compares the invocation with want in order of appearece.

func NewSubscribeStub

func NewSubscribeStub(fix error) func(quit <-chan struct{}, topicFilters ...string) error

NewSubscribeStub returns a stub for mqtt.Client Subscribe with a fixed return value.

func NewUnsubscribeMock

func NewUnsubscribeMock(t testing.TB, want ...Filter) func(quit <-chan struct{}, topicFilters ...string) error

NewUnsubscribeMock returns a new mock for mqtt.Client Unsubscribe, which compares the invocation with want in order of appearece.

func NewUnsubscribeStub

func NewUnsubscribeStub(fix error) func(quit <-chan struct{}, topicFilters ...string) error

NewUnsubscribeStub returns a stub for mqtt.Client Unsubscribe with a fixed return value.

Types

type ExchangeBlock

type ExchangeBlock struct {
	Delay time.Duration // zero defaults to indefinite
}

ExchangeBlock prevents exchange <-chan error submission.

func (ExchangeBlock) Error

func (b ExchangeBlock) Error() string

Error implements the standard error interface.

type Filter

type Filter struct {
	Topics []string // order is ignored
	Err    error    // result
}

Filter defines a subscription exchange.

type Transfer

type Transfer struct {
	Message []byte // payload
	Topic   string // destination
	Err     error  // result
}

Transfer defines a message exchange.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL