mqtttestbroker

package
v0.7.0 Latest Latest
Warning

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

Go to latest
Published: Nov 25, 2026 License: MIT Imports: 15 Imported by: 0 modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

Documentation

Overview

Package mqtttestbroker is a minimal in-process MQTT 3.1.1 broker for tests. It implements just enough of the protocol for real clients to connect (plain TCP or TLS), subscribe, publish, receive publishes, ping and disconnect — including last-will delivery on ungraceful drops. It is a test double: no auth, no retain store, no QoS 2, no persistence — do not use it in production.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func RawConnect

func RawConnect(addr, clientID string, will *Will) (net.Conn, error)

RawConnect dials a plaintext broker and hand-writes a minimal MQTT 3.1.1 CONNECT packet, returning the open connection after reading the CONNACK. It exists so tests can drop a client ungracefully — closing the returned connection without a DISCONNECT packet is what triggers the broker's last-will path, something a well-behaved client library never does on demand.

Types

type Broker

type Broker struct {

	// Published records every publish the broker saw, in order —
	// handy for assertions on what the client side sent. Guarded by
	// mu — read it through PublishedMessages().
	Published []PublishedMessage
	// contains filtered or unexported fields
}

Broker is a running test broker.

func Start

func Start() (*Broker, error)

Start runs a plaintext broker on a random loopback port.

func StartTLS

func StartTLS() (*Broker, error)

StartTLS runs a TLS broker on a random loopback port using a self-signed server certificate valid for 127.0.0.1/localhost. The CA PEM is available via CAPEM so tests can configure client-side verification through the same file-path knobs real flows use.

func (*Broker) Addr

func (b *Broker) Addr() string

Addr is the broker address in host:port form.

func (*Broker) CAPEM

func (b *Broker) CAPEM() []byte

CAPEM returns the PEM bytes of the CA that signed the server certificate (TLS brokers only — nil otherwise).

func (*Broker) Close

func (b *Broker) Close()

Close stops the listener and drops every client connection.

func (*Broker) PublishedMessages

func (b *Broker) PublishedMessages() []PublishedMessage

PublishedMessages returns a copy of the recorded publishes — the broker's serve goroutines append while tests read.

type PublishedMessage

type PublishedMessage struct {
	Topic   string
	Payload []byte
	// QoS and Retain are the flags the publisher sent on the wire —
	// recorded so tests can assert what the client side published.
	QoS    byte
	Retain bool
}

PublishedMessage is one publish observed by the broker.

type Will

type Will struct {
	Topic   string
	Payload string
	QoS     byte
	Retain  bool
}

Will is the last-will a raw client registers at CONNECT time.

Jump to

Keyboard shortcuts

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