Guides

Test a component over a message broker

Test a component that talks through Kafka, ActiveMQ, NATS or RabbitMQ, with a test broker Tenon runs for you.

This guide sets up a component test for a component that talks through a message broker. By the end, Tenon starts a test broker for each run on your machine and removes it afterwards, and your pipeline runs the same broker as a job service, with the same configuration in both places. It uses Radar from the example model, talking through ActiveMQ.

Before you begin

  • Your component, built as a Linux x86-64 program that talks through one of the brokers in Supported brokers. A static build works best, because it runs inside Tenon’s image and needs nothing from it.
  • Its verifier.yaml, written by the Verifier’s setup wizard with your broker as the transport. Set up a component test runs the wizard. It’s fine if it still points at a broker on 127.0.0.1.
  • Docker, running. For CI, runners that can run job services: on GitLab, the Docker or Kubernetes executor, since the shell executor can’t run services; on GitHub, any runner that runs container jobs.

1. Give each message a topic

A broker routes each message by its topic, so the Verifier needs to know the topic of every message your component sends or receives. Add a @Topic annotation to each step of the component’s local protocol, using the topics your component really uses. The Interface Model page describes @Topic, and topic_map in verifier.yaml, which maps topics there instead.

Radar's local protocol, with topics
annotation Topic(name: string)

local protocol ReportTemperature in Radar {
  recv let req: ReadTemperature
    on RequestChan
    @Topic("thermal.request");
  send let rep: Temperature
    where rep.id == req.id
    on ReplyChan
    @Topic("thermal.reply");
}

Then check the model again with tenon flex check.

2. Add a test broker

In the component’s folder, run tenon verify component setup. It sees that your verifier.yaml uses a broker, and offers to run a test broker for local runs and CI. Say yes, and it writes the broker into env.toml as a service, named after the broker: activemq for ActiveMQ. That name is also the broker’s address on the test’s own network.

Setup then shows the changes verifier.yaml needs to reach the broker: its address becomes the service’s name, tcp://activemq:61616, and timeout_ms rises to 5000, because a round trip through a broker is slower than one on your own machine. It makes them only if you agree, and changes nothing else in the file, comments included.

Add a test broker
cd flex/components/Radartenon verify component setup
flex/components/Radar/env.toml
[[verify.env.service]]
name = "activemq"
image = "apache/activemq-classic:5.18.6"
ready = { tcp = 61616 }

3. Point your component at the broker

Setup changes where the Verifier finds the broker, but not where your component does, since only you know how it’s told. Give your component the service’s name as the broker’s host, in the command verifier.yaml starts it with, or however else it reads its configuration. Use the port your component speaks to the broker on, which can differ from the Verifier’s.

See the run before you start it
tenon verify component --flex ../../spec --dry-run

The dry run shows the network Tenon will create, the broker it will start there, and the command it will start your component with.

4. Run the test

Run the test as you would any other. Tenon creates a private Docker network for the run, starts the broker on it, and gives it 5 seconds to start. It then runs the Verifier and your component beside it, both reaching the broker by name, and removes the broker and the network afterwards, even when you press Ctrl-C.

Run the test
tenon verify component --flex ../../spec

This works the same way on macOS, Linux and Windows, and needs no host networking. A workflow that tests this component starts the broker on every run, too.

5. Run it in CI

Generate your pipeline with tenon workflow ci, from the project folder. Each test job runs its component’s brokers as GitLab or GitHub job services, with the service’s name as the alias, so verifier.yaml reaches the broker in CI exactly as it does on your machine.

Generate the pipeline
cd ../..tenon workflow ci System

In CI, Tenon starts nothing itself: your CI starts the services, and Tenon waits until each one answers, for up to a minute. On GitLab’s Kubernetes executor, services share the job’s network, so a broker’s ports mustn’t clash with ports your verifier.yaml binds. On GitHub Enterprise Server, a broker with a command in env.toml runs through docker run instead, under the same name. Run Tenon in CI covers the rest of the pipeline.

Supported brokers

Setup knows how to run these brokers, and each has been tested locally and in CI.

Broker Test broker image Notes
Kafka apache/kafka:3.9.0 A single node. The Verifier creates a topic for each @Topic before the test, and sends warm-up messages that your component should ignore.
ActiveMQ apache/activemq-classic:5.18.6 Setup sets use_text: false, so messages are sent as bytes, as serializers such as protobuf need.
NATS nats:2.10-alpine Setup sets the server’s address in settings.options.url.
RabbitMQ rabbitmq:3.13 The test broker has its own user, because RabbitMQ’s guest user can only log in from the broker’s own machine.

TCP, UDP and ZeroMQ need no broker: the Verifier and your component talk directly. MQTT isn’t supported yet.

Test against your own broker

To test against a broker you already run, answer no when setup offers a test broker. Nothing is written, and the test uses the address in verifier.yaml as it is, so make sure the Verifier can reach it from inside Docker on your machine, and from your runners in CI.

Troubleshooting

What you see What to check
Timed out while waiting for an event timeout_ms is still the Verifier’s 1-second default. Set it to 5000.
The broker can’t be reached on your machine verifier.yaml, or your component’s own settings, still point at 127.0.0.1. Use the service’s name.
service `activemq` was not reachable at activemq:61616 in CI The job has no service with that alias, or the runner uses the shell executor. Generate the pipeline again.
RabbitMQ refuses the login The address uses guest. Use the test broker’s own user.

Next steps