Skip to content

Counters are not incremented before async producers are triggered #175

@danielsitnik

Description

@danielsitnik

Hello guys.

I have a use case where the client calls an HTTP endpoint, receives a 202 status code, and the real response payload is delivered asynchronously through a queue. So far I have been able to model this use case in mockintosh by using an async producer and triggering it in my endpoint with triggerAsyncProducer.

My problem right now is that the full use case requires the endpoint to return a correlation-id to the caller, and the async message delivered later must reference this same correlation-id.

I almost made it work by using the counters feature, with a config like this:

services:
- name: Correlation Service
  endpoints:
  - path: /correlation
    response:
      status: 202
      body: '{"correlationId": "{{counter ''correlationIdCounter''}}"}'
      triggerAsyncProducer: rabbit-producer
- name: Rabbit Service
  type: amqp
  address: localhost:5672
  actors:
  - name: rabbit-producer
    produce:
      queue: mockintosh
      key: mockintosh
      value: '{"message": "ok", "correlationId": "{{correlationIdCounter}}"}'

My problem right now is that it seems that the async message is produced BEFORE the counter has been incremented.
So in the first execution, the http endpoint correctly gives me a correlation-id of 1, but I receive this in my queue:

{"message": "ok", "correlationId": "{{correlationIdCounter}}"}

For the next executions, the http endpoint correctly returns the incremented counter, but the produced message is always one step behind.

I don't know if this is a bug or the expected behavior.
If this is expected, is there any other way to make my use case work?

One idea that came to my mind is this: mockintosh keeps a count of the requests as we can see on the Statistics tab. If there's a way to reference this count in the templated responses (something like {{requestCount}}), then I could also use this count to model my correlation-id requirement.

I was thinking about adding some conditionals to check if the counter exists (first execution) and then always adding 1 to it's value but I haven't tried it yet, don't even know if it's possible.

Thanks!

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions