Component test reference

Interface Model

Interface model: messages, components, connections, protocols

The interface model is a Flex file describing your component’s messages, the components it talks to, and the message sequences it must honor. Verifier reads the model to learn what to send, what to expect back, and what counts as correct.

Verifier reads a compiled model — the JSON form of your Flex files — and you point at it with the flex_json_paths configuration key.

This page covers the part of Flex that Verifier reads. Flex is a larger language than that; for the language itself see flexlang.org, and for authoring models in Tangram Pro see docs.tangramflex.io.

What Verifier Needs

Verifier needs four things from a model. Everything else it ignores.

  • Messages — the named structures that go on the wire, with typed fields.
  • Component declarations — your component and every peer it exchanges messages with.
  • Connections — which components exchange which messages. Declared by name, or implied by the from and to clauses inside a protocol.
  • Local protocols — the message sequences your component must honor, with constraints on field values.

A model with messages but no protocol for your component gives Verifier nothing to test.

Messages and Field Types

A message is a struct marked message. Its fields are what Verifier generates and validates.

module example.sensor

message struct SampleRequest {
    id:     int32;
    window: uint32;
}

message struct SampleReport {
    id:      int32;
    celsius: float32;
    label:   string;
}

message struct SampleAck {
    id: int32;
}

message struct SampleError {
    id:     int32;
    reason: string;
}

Verifier generates and validates these field types:

  • Signed and unsigned integers of every width, for example int32, uint8.
  • Floating point: float32 and float64.
  • bool and bit.
  • string, including fixed-length strings.
  • Enumerations declared with enum.
  • Nested structs declared with struct.
  • Tagged unions declared with variant.
  • Optional<T>, where T is any of these types.
  • Lists of any of these types, written T[].

A list of optionals — Optional<int32>[] — is not supported.

Supporting types live alongside the messages that use them:

enum Mode uint32 {
    Idle    = 0;
    Sampling = 1;
}

struct Calibration {
    offset: float32;
    scale:  float32;
}

variant Trigger {
    Manual(int32);
    Scheduled(uint32);
    Calibrated(Calibration);
}

To pin a list to a fixed length, annotate the field with @Length. The value must be an integer literal:

message struct Frame {
    @Length(12)
    samples: int32[];
}

Without a @Length annotation or a length constraint in a protocol, Verifier picks a list length inside the limits set by the generation configuration section.

Components and Connections

Declare every participant, including your component:

component Sensor;
component Monitor;

send and recv are written from your component’s point of view. A protocol declared in Sensor describes what Sensor sends and what Sensor receives. Verifier plays the other side, so Verifier sends every message the protocol marks recv, and validates every message the protocol marks send. Getting this backwards is the most common modeling mistake.

The same convention governs connections. In connection C from A to B, the message flows from A to B. When your component is the to, Verifier sends on that connection; when your component is the from, Verifier receives.

You can leave connections implicit. A protocol step that names a peer with from or to creates a connection between the two components:

local protocol sampleFlow in Sensor {
    recv let req: SampleRequest from Monitor;
    send let rep: SampleReport  to   Monitor;
}

Or you can declare connections by name and route steps onto them with on. Name them when one pair of components needs more than one path, or when you want to bind each path to a different transport:

connection ControlIn  from Monitor to Sensor;
connection ReportOut  from Sensor  to Monitor;

local protocol sampleFlow in Sensor {
    recv let req: SampleRequest on ControlIn;
    send let rep: SampleReport  on ReportOut;
}

Connections are bound to transports in the configuration file. Component names and connection names are addressed by their fully-qualified Flex names, so the connection above is example.sensor.ControlIn in configuration. See verifier docs open config.

Protocols

A local protocol declares one message sequence, from the point of view of one component:

local protocol sampleFlow in Sensor {
    ...
}

Verifier tests only the protocols whose perspective is the component you are testing. Protocols declared in some other component are ignored.

Receiving

A recv step is a message your component receives, which means Verifier generates and sends it. Bind the message to a name so later steps can refer to its fields:

recv let req: SampleRequest from Monitor;

Add assuming to constrain the values Verifier generates:

recv let req: SampleRequest
    assuming req.window >= 1 && req.window <= 60
    from Monitor;

Use recv any when the step accepts any message of that type and you do not need the binding to survive:

recv any ack: SampleAck
    assuming ack.id == req.id
    from Monitor;

Sending

A send step is a message your component sends, which means Verifier waits for it and checks it. Add where to state what must hold:

send let rep: SampleReport
    where rep.id == req.id
       && rep.celsius >= -40.0 && rep.celsius <= 85.0
    to Monitor;

send any accepts any message of that type with no field checks:

send any SampleReport to Monitor;

The two constraint keywords are not interchangeable. assuming on a recv tells Verifier what to generate. where on a send tells Verifier what to validate.

Protocol Variables

Declare a variable with var or let and reuse it across steps. This is how a step refers to a value fixed earlier in the sequence:

local protocol echoFlow in Sensor {
    var probe: SampleRequest = SampleRequest {
        id     = 0;
        window = 1;
    };
    recv probe from Monitor;

    send let rep: SampleReport
        where rep.id == probe.id
        to Monitor;
}

You can also send a constructed message directly, without binding it:

send SampleReport {
        id      = req.id;
        celsius = 20.0;
    }
    to Monitor;

Any field of any earlier message in the sequence is available in a later constraint. rep.id == req.id is the workhorse form: it says the reply must carry the identifier from the request.

Branching

Use branch when the sequence can legally go more than one way. Each arm becomes a separate trace with its own verdict:

local protocol requestFlow in Sensor {
    recv let req: SampleRequest from Monitor;

    branch
    | true =>
        send let rep: SampleReport
            where rep.id == req.id
            to Monitor;
    | else =>
        send let err: SampleError
            where err.id == req.id
            to Monitor;
    end
}

The loop and do statements are not supported. Verifier reports them and ignores them.

Annotations

Annotations tune timing, routing, and end conditions. Declare each one in the model before you use it:

annotation Topic(name: string)
annotation End(term: string)
annotation Heartbeat(frequency: string, jitter: string)
annotation Timeout(duration: string)
annotation Delay(min: string, max: string)
annotation MinDelay(duration: string)
annotation MaxDelay(duration: string)
annotation Length(len: uint32)

Every annotation value except @Length is a quoted string. Durations are written as a number and a unit: 250ms, 1.5s. @Heartbeat also accepts a frequency in hertz, 10hz, which Verifier converts to a period.

@Topic — on a send or recv step

Sets the topic a message is published or subscribed on:

recv let req: SampleRequest
    from Monitor
    @Topic("sensor.SampleRequest");

Topic-routed transports need a topic for every message. Supply it here, or map topics in the configuration file with topic_map. Without either, Verifier stops before the test starts.

@End — on a local protocol

Sets what has to happen for the protocol to be considered finished:

@End("LastMessage")
local protocol sampleFlow in Sensor { ... }
  • ExitOrTimeout: the component exits, or the wait for the next message times out. This is the default when @End is absent.
  • Exit: the component exits.
  • Timeout: the wait for the next message times out. A component that exits instead is an error.
  • LastMessage: the protocol is finished as soon as its final step completes.
  • None: treated the same as leaving @End off, so the protocol ends on ExitOrTimeout.

@Heartbeat — on a local protocol

Makes the protocol repeat on a fixed cycle for the length of the attempt instead of ending once:

@Heartbeat("1s", "200ms")
local protocol pingBeat in Sensor {
    send any Ping to Monitor;
}
  • The first value is the cycle frequency, as a duration or in hertz.
  • The second value is the tolerance either side of the cycle. It is optional and defaults to ten percent of the frequency, never less than 1ms.
  • The tolerance must be less than the frequency.

A heartbeat protocol’s verdict is the first failing cycle if any cycle fails.

@Timeout — on a local protocol

Overrides the default message wait for this protocol only:

@Timeout("5s")
local protocol slowFlow in Sensor { ... }

The default comes from the timeout_ms configuration key.

@Delay, @MinDelay, @MaxDelay — on a recv step

Make Verifier wait before sending the message, so you can test how your component behaves when input is late:

recv let req: SampleRequest
    from Monitor
    @Delay("2s", "2.4s");
  • @Delay(min, max): wait a random time in the window. min must be less than max.
  • @MinDelay(d): wait at least d, at most the protocol’s timeout.
  • @MaxDelay(d): wait at most d.
  • @Delay cannot be combined with @MinDelay or @MaxDelay.

A delay window is clamped by the protocol’s timeout. If the window reaches or exceeds the timeout, the message may never arrive in time and Verifier warns you. Raise the protocol’s @Timeout, or shorten the delay.

What Verifier Can and Cannot Generate

Verifier solves each constraint to produce a message, and picks its strategy from the constraint’s shape. Straightforward constraints are solved directly. Harder ones fall back to generating values and testing them, which is slower and can fail to find a satisfying value when the constraint is tight.

Solved directly:

  • A field compared to a literal, an enum constant, or an array literal, with ==, !=, <, <=, >, or >=.
  • Conjunctions and disjunctions of those, with && and ||.
  • length(list) compared to an integer.
  • match on a variant or an optional, in a send constraint.

Solved by generate-and-test, so slow under tight constraints:

  • Two fields compared to each other, for example rep.id == req.id.
  • More than one unknown field in a single clause.
  • The modulus operator.

Not supported. Verifier reports these and the constraint does not drive generation:

  • Bitwise and shift operators: <<, >>, ^.
  • if/else expressions.
  • Block expressions containing assert or let.
  • Array comprehensions, and fold, filter, map, scan.
  • try expressions.
  • Array indexing inside a constraint, for example msg.samples[0] == 3.
  • match in a recv constraint. It works in send constraints only.
  • A some(x) pattern that binds the value. Use some(_), or match a literal.
  • A bind pattern in a match arm on a variant or optional field.
  • A branch guard used to constrain field values. Guards cannot drive generation; put the constraint in the send or recv step.
  • Custom function declarations and newtype declarations.

Run verifier doctor to get this list checked against your own model, with the line number of every constraint Verifier cannot use as written.

Generation Limits

Three configuration keys under generation bound what Verifier produces when a constraint leaves the value open:

  • recursionDepth (defaults to 20): how deep Verifier descends into nested structures. It logs an informational message on passing eighty percent of the limit and warns when it stops descending at the limit.
  • listLimits (minLength defaults to 0, maxLength to 10): the length range for a list with no @Length annotation and no length constraint.
  • floatLimits (min defaults to -1000000.0, max to 1000000.0): the value range for floating point fields.

Raise floatLimits and listLimits when your component’s real inputs live outside these ranges. See verifier docs open config.