mockintosh icon indicating copy to clipboard operation
mockintosh copied to clipboard

Counters are not incremented before async producers are triggered

Open danielsitnik opened this issue 1 year ago • 1 comments

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!

danielsitnik avatar Aug 24 '22 16:08 danielsitnik

Ok, small update to my first post. I think this might really be a bug.

I changed the configuration so that the endpoint returns the correlation-id in a header, instead of the body:

services:
- name: Correlation Service
  endpoints:
  - path: /correlation
    response:
      status: 202
      headers:
        x-correlation-id: {{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}}"}'

AND IT WORKS! 😄 First execution returns 1 in the http response and also in the produced message!

In summary, when counter is used in the headers, the value is incremented before the async producer is triggered. When it's used in the body, it seems to be the other way around.

Since this is not a deterministic behavior, I'd say this is a bug.

danielsitnik avatar Aug 24 '22 16:08 danielsitnik