AsyncModule: Is there a synchronous implementation to support unit test scenarios

21 views
Skip to first unread message

Xaver Birrer

unread,
Apr 23, 2012, 10:35:40 AM4/23/12
to bbvcommon
Hi all

Is there a synchronous (single threaded) AsyncModule implementation to
support unit test purposes?
Like: what you enqueue gets immediately executed.


Thanks already for all the support.
and best regards
Xavi

PS: I hope I do not get RTFM'd ;-)

Urs Enzler

unread,
Apr 23, 2012, 10:43:11 AM4/23/12
to bbvc...@googlegroups.com
Hi Xavi

There is a way - it's ugly but it works :-)

The idea is to use an extension that cancels message enqueue and directly executes the handler method with the message.

Add an extension to the async module with the following handler for the event BeforeEnqueueMessage

private void ModuleControllerOnBeforeEnqueueMessage(object sender, EnqueueMessageEventArgs enqueueMessageEventArgs)
        {
            enqueueMessageEventArgs.Cancel = true;

            MethodInfo method = null;

            foreach (MethodInfo methodInfo in enqueueMessageEventArgs.Module.GetType().GetMethods())
            {
                if (Attribute.IsDefined(methodInfo, typeof(MessageConsumerAttribute), true))
                {
                    method = methodInfo;
                }
            }

            method.Invoke(enqueueMessageEventArgs.Module, new[] { enqueueMessageEventArgs.Message });
        }

Note that this works only if there is a single message consumer method. Otherwise, you have to add a bit more logic.

You can find a working sample at https://github.com/appccelerate/sensorsample in the master branch.

btw. this is one reason more to replace the async module with something a little bit easier to test.
So no RTFM-danger :-D

Cheers
Urs

Xaver Birrer

unread,
Apr 24, 2012, 8:52:23 AM4/24/12
to bbvcommon
Once more, thanks a lot for the immediate response.

I see your workaround and well.. yeah.. since we have multiple
handlers it won't get any nicer.
Our enqueing logic is trivial and the clients access' the controller
over an interface, so we decided to make the handlers public and call
them directly from the unit test. This will do it for the time being.

Hope we meet in Stans at the GW!

Regards
Xavi

On Apr 23, 4:43 pm, Urs Enzler <urs.enz...@gmail.com> wrote:
> Hi Xavi
>
> There is a way - it's ugly but it works :-)
>
> The idea is to use an extension that cancels message enqueue and directly
> executes the handler method with the message.
>
> Add an extension to the async module with the following handler for the
> event BeforeEnqueueMessage
>
> private void ModuleControllerOnBeforeEnqueueMessage(object sender, EnqueueMessageEventArgs enqueueMessageEventArgs)
>         {
>             enqueueMessageEventArgs.Cancel = true;
>
>             MethodInfo method = null;
>
>             foreach (MethodInfo methodInfo in enqueueMessageEventArgs.Module.GetType().GetMethods())
>             {
>                 if (Attribute.IsDefined(methodInfo, typeof(MessageConsumerAttribute), true))
>                 {
>                     method = methodInfo;
>                 }
>             }
>
>             method.Invoke(enqueueMessageEventArgs.Module, new[] { enqueueMessageEventArgs.Message });
>         }
>
> Note that this works only if there is a single message consumer method.
> Otherwise, you have to add a bit more logic.
>
> You can find a working sample athttps://github.com/appccelerate/sensorsamplein the master branch.
Reply all
Reply to author
Forward
0 new messages