Flex unit aysnc problem: Error: Asynchronous Event Received out of Order

1.1k Views Asked by At

I am writing test cases to test function with flexunit 4. I am using aysnc method. But when I add two or more asyncHandlers to the instance. I meet the problem: Error: Asynchronous Event Received out of Order. How to resolve this problem? Thanks.

Code snippets:

[Test(order=1, async, description="synchronize content on line")]
    public function  testSynchronizeContentOnline():void
    {
        var passThroughData:Object = new Object();

        var asyncHandler1:Function = Async.asyncHandler(this, authFailureHandler, 60000, null, timeoutHandler);
        var asyncHandler:Function = Async.asyncHandler(this, authSuccessHandler, 60000, null, timeoutHandler);

        caseManager.addEventListener(CaseAuthEvent.AUTH_SUCCESS, 
            asyncHandler);

        caseManager.addEventListener(CaseAuthEvent.AUTH_FAILURE, 
            asyncHandler1);
        caseManager.authenticate("admin", "admin");

        trace('test');
    }

    private function timeoutHandler(event:Event):void 
    {
        Assert.fail( "Timeout reached before event");
    }

    private var authFailed:Boolean = false;
    private function authFailureHandler(event:CaseAuthEvent, passThroughData:Object):void
    {
        trace("authFailure:" + event.type);
        authFailed = true;

    }

    private var authSucceed:Boolean = false;
    private function authSuccessHandler(event:CaseAuthEvent, passThroughData:Object):void
    {
        trace("authSucceed:" + event.type);
        authSucceed = true;
        Assert.assertTrue(true);

    }
2

There are 2 best solutions below

2
On

That would be because you're adding order to your test cases, which is seems something else is dispatching before the first one is complete. To quote the ordering part of the flex unit wiki:

Your tests need to act independently of each other, so the point of ordering your tests in a custom way is not to ensure that test A sets up some state that test B needs. If this is the reason you are reading this section, please reconsider. Tests need to be independent of each other and generally independent of order.

Which I completely agree with. There should not be any order in your tests. The tests themselves sets the state of what needs to be done.

0
On

Your test will work if you test success and fail separately. So basically have 2 tests, one adds an async handler for your events success, the other for the events fail. Here is an example of the 2 tests as I would approach them...

[Test(async)]
public function  testEventSuccess():void
{
    var passThroughData:Object = new Object();

    var asyncHandler:Function = Async.asyncHandler(this, authSuccessHandler, 60000, null, timeoutHandler);

    caseManager.addEventListener(CaseAuthEvent.AUTH_SUCCESS, 
        asyncHandler);

    caseManager.authenticate("admin", "admin");
}

[Test(async)]
public function  testEventFailure():void
{
    var passThroughData:Object = new Object();

    var asyncHandler:Function = Async.asyncHandler(this, authFailureHandler, 60000, null, timeoutHandler);

    caseManager.addEventListener(CaseAuthEvent.AUTH_FAILURE, 
        asyncHandler);
    caseManager.authenticate("admin", "admin");
}

Remember to make a new instance of your caseManager in your set up function and its good practice to remove ref to it in the tearDown as the simple code snippet shows, I've just assumed the caseManager is of type CaseManager.

[Before]
public function setUp():void
{
    caseManager = new CaseManager();
}

[After]
public function tearDown():void
{
    caseManager = null;
}