You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@camel.apache.org by Karel Bernolet <ka...@gmail.com> on 2018/03/02 17:45:29 UTC

Unit Test Polling Consumer

Hello,

--short version --

I am trying to unit test a route calling a polling consumer (currently a
http endpoint)
Unfortunately, mock endpoints do not support consumers. Is there an
equivalent to mock endpoints to be used in case of a polling consumer?

I currently replace the endpoint with a seda endpoint where I send the
exchange to be polled to before running the test, but this does not provide
a way to assert if the endpoint has been consumed. Preferably, the test
endpoint also supports a timeout equivalent to the mock endpoint.

--longer version --
I have a requirement where I need to poll an endpoint every x seconds. All
the available messages need to be processed as fast as possible. In case of
an error or no messages found, it should delay the next poll to avoid
having too many error logs.
As this completely describes a scheduled poll consumer with his
backoffTresholds, I decided to use this endpoint. The
DefaultScheduledPollConsumer however does not propagate exceptions while
polling the endpoint, hence the backoffThresholds are never triggered.
To avoid this, I overrided the poll method to rethrow the exception:

> @Override
> protected int poll() throws Exception {
>     Exchange exchange = consumer.receive();
>     getProcessor().process(exchange);
>     if (exchange.getException() != null) {
>       throw exchange.getException();
>     } else {
>       return exchange.getIn().getHeader(exchange.SCHEDULER_POLLED_MESSAGES, true, Boolean.class) ? 1 : 0;
>     }
>
> }
>
> While trying to unit test this, I need a polling consumer in which I can
inject messages to be polled.  As described above, I currently replace the
endpoint by a seda endpoint where I can send exchanges to be consumed to.
What I would now like to verify in the test is how many polls on that
endpoint have been done (to make sure the backoffTresholds work as
expected).


I hope someone can direct me to either the good component or an alternative
way to solve this requirement.

mvg
Karel

Re: Unit Test Polling Consumer

Posted by Quinn Stevenson <qu...@pronoia-solutions.com>.
You could setup another HTTP route in your test that returns what you’re after to exercise your test conditions - I’ve done that quite a bit in the past.

> On Mar 2, 2018, at 10:45 AM, Karel Bernolet <ka...@gmail.com> wrote:
> 
> Hello,
> 
> --short version --
> 
> I am trying to unit test a route calling a polling consumer (currently a
> http endpoint)
> Unfortunately, mock endpoints do not support consumers. Is there an
> equivalent to mock endpoints to be used in case of a polling consumer?
> 
> I currently replace the endpoint with a seda endpoint where I send the
> exchange to be polled to before running the test, but this does not provide
> a way to assert if the endpoint has been consumed. Preferably, the test
> endpoint also supports a timeout equivalent to the mock endpoint.
> 
> --longer version --
> I have a requirement where I need to poll an endpoint every x seconds. All
> the available messages need to be processed as fast as possible. In case of
> an error or no messages found, it should delay the next poll to avoid
> having too many error logs.
> As this completely describes a scheduled poll consumer with his
> backoffTresholds, I decided to use this endpoint. The
> DefaultScheduledPollConsumer however does not propagate exceptions while
> polling the endpoint, hence the backoffThresholds are never triggered.
> To avoid this, I overrided the poll method to rethrow the exception:
> 
>> @Override
>> protected int poll() throws Exception {
>>    Exchange exchange = consumer.receive();
>>    getProcessor().process(exchange);
>>    if (exchange.getException() != null) {
>>      throw exchange.getException();
>>    } else {
>>      return exchange.getIn().getHeader(exchange.SCHEDULER_POLLED_MESSAGES, true, Boolean.class) ? 1 : 0;
>>    }
>> 
>> }
>> 
>> While trying to unit test this, I need a polling consumer in which I can
> inject messages to be polled.  As described above, I currently replace the
> endpoint by a seda endpoint where I can send exchanges to be consumed to.
> What I would now like to verify in the test is how many polls on that
> endpoint have been done (to make sure the backoffTresholds work as
> expected).
> 
> 
> I hope someone can direct me to either the good component or an alternative
> way to solve this requirement.
> 
> mvg
> Karel