# Pub/Sub

> Publish messages to Pub/Sub topics and check the messages your services publish.

Source: https://axx.nimbusxr.us/references/steps/gcp-pubsub/

Publish messages to Pub/Sub topics and check the messages your services publish.

The steps use the scenario's project (`the {word} gcp project with the following properties:`, from gcp-core).

**Checking a topic** does not take messages from anyone: for the topics a run checks, Axx creates a subscription of its own (`axx-<run>-<topic>`) once the apps are up, and deletes it when the run ends. A check only looks at the messages received since its scenario started; match on data unique to the scenario, since scenarios run in parallel. The conditions are paths into the message data (JSON) and `attribute <name>` rows for its attributes.

## `gcp-pubsub.send`

```gherkin
When a message is published to the {word} pubsub topic:
  """
  ...
  """
```

Send a message whose body is the doc string to a pubsub topic.

**Parameters:** `{word}` (one word, no spaces)

**Example:**

```gherkin
When a message is published to the invoice-events pubsub topic:
```

_Since 0.1.0._

## `gcp-pubsub.send.file`

```gherkin
When the {filepath} message is published to the {word} pubsub topic[[ with the following attributes:]]
```

Send a message whose body is the file (resolved against `resources`) to a pubsub topic, with the attributes of the table (`name | value`).

**Variants** (optional parts in `[[...]]` above):

- `the {filepath} message is published to the {word} pubsub topic`
- `the {filepath} message is published to the {word} pubsub topic with the following attributes:`

**Parameters:** `{filepath}` (A file of the project, without whitespace: a path relative to the `resources` directories or to the directory of axx.yaml, or an absolute path. Editors link it to the file), `{word}` (one word, no spaces)

**Example:**

```gherkin
When the messages/shipment-delivered.json message is published to the invoice-events pubsub topic
When the messages/shipment-delivered.json message is published to the invoice-events pubsub topic with the following attributes:
```

_Since 0.1.0._

## `gcp-pubsub.received`

```gherkin
Then [[within {duration} ]]the {word} pubsub topic has a message where:
  | ... | ... |
```

Wait (10s, or the given time) until the pubsub topic has a message, received since the scenario started, that meets every row: `path | value` on the JSON body (a field name, a dotted path or a JSONPath, compared as text; `null` for null and `undefined` for absent), or `attribute <name> | value` on a attribute sent with it. Axx creates a subscription of its own to the topic for the run.

**Variants** (optional parts in `[[...]]` above):

- `the {word} pubsub topic has a message where:`
- `within {duration} the {word} pubsub topic has a message where:`

**Parameters:** `{duration}` (A duration in seconds or minutes, e.g. `5s` or `2m`), `{word}` (one word, no spaces)

**Example:**

```gherkin
Then within 30s the invoice-events pubsub topic has a message where:
```

_Since 0.1.0._
