Am 12.12.12 16:08 schrieb "Babak Vahdat" unter
<babak.vah...@swissonline.ch>:

>
>
>Am 12.12.12 14:23 schrieb "camel.user2.ch" unter
><camel.user...@gmail.com>:
>
>>Hi,
>>
>>I'm running an integration test using the CamelSpringJUnit4ClassRunner to
>>test a route JMS -> Processor -> SFTP endpoint. In my test, I use a
>>ProducerTemplate to fire a message to the JMS endpoint and mock the SFTP
>>endpoint where I assert sftpMockEndpoint.setExpectedMessageCount(1). This
>>all works fine.
>>
>>I've now written a test where I send an invalid message to the JMS queue.
>>Normally this is caught by the processor which logs the message and stops
>>any further processing. The SFTP endpoint should not receive a message in
>>this case, so I've added:
>>
>>sftpMockEndpoint.setExpectedMessageCount(0)
>>
>>When I run this test and pass in a valid message (i.e. one that reaches
>>the
>>SFTP endpoint), I'm still getting a positive result - the SFTP endpoint
>>receives a message AND the setExpectedMessageCount(0) assertion passes.
>
>Hi
>
>If you don't want an explicit Thread.sleep() call inside your code then
>you could make use of the MockEndpoint.setSleepForEmptyTest() API.
>
>Babak
>
>>
>>I believe that because the JMS queue is asynchronous, the assertions are
>>being fired before the message has passed along the route  - is this
>>correct? E.g.:
>>
>>1. set mock expectation sftpMockEndpoint.expectedMessageCount(0);
>>2. fire message to JMS queue. Starts an async process and return control
>>to
>>test method
>>3. test method runs sftpMockEndpoint.assertIsSatisfied(); which asserts
>>that
>>the SFTP mock has received no messages. Assertion is correct.
>>4. JMS messages continues to be processed and eventually makes it's way
>>to
>>the SFTP mock, which results in 1 message being received after the test
>>assertions have been processed.
>>
>>Please could you confirm if this is the case and if there is anything I
>>can
>>do to suspend processing of the assertions until the message has gone all
>>the way through the route?

And yes this is indeed the case as consuming a given JMS message happens
inside it's own dedicated thread whereas asserting on the Mockendpoint
occurs inside the main thread. So there's a race condition here one has to
take care of because otherwise (as you've already mentioned) you could end
up with false positive tests.

Also another possible approach (instead of Thread.sleep() or
MockEndpoint.setSleepForEmptyTest()) is to make use of NotifyBuilder where
after firing the exchange into your route through the ProducerTemplate,
using the NotifyBuilder you would wait for the routing of the given
exchange to be completed/failed/done. And then *afterwards* assert on
whatever condition you want on the MockEndpoint. In this way your
assertion will be for sure *after* the routing of the given exchange. For
the semantics of completed/failed/done see:

http://camel.apache.org/notifybuilder.html

Babak

>> I tried adding a
>>sftpMockEndpoint.whenAnyExchangeReceived(new Processor() { // throw
>>Exception }); to trigger a failure in the test whenever the SFTP mock
>>received an Exchange but unfortunately the Exception gets swallowed up by
>>the test framework.
>>
>>The only solution I have found is to add a Thread.sleep(100); before the
>>test assertions (assertIsSatisfied) are processed, but this is ugly and I
>>don't want arbitrary sleeps within my code.
>>
>>Any help would be greatly appreciated and apologies for the length of
>>this
>>post - I wanted to include as much information as possible but not post
>>great swathes of unintelligible code.
>>
>>Thanks again,
>>John
>>
>>
>>
>>
>>--
>>View this message in context:
>>http://camel.465427.n5.nabble.com/mock-expectedMessageCount-0-resulting-i
>>n
>>-a-false-positive-tp5723952.html
>>Sent from the Camel - Users mailing list archive at Nabble.com.
>
>


Reply via email to