Skip to main content
AndrewZellman
Adobe Employee
Adobe Employee
August 3, 2009
Question

Using Async to wait for a specific event

  • August 3, 2009
  • 1 reply
  • 2644 views

I've simplified the creation and deletion calls and used a generic item.  In my before method, I call Async.handleEvent to wait for PropertyChangeEvents, but this only waits for the first one.  My problem lies in the fact that ItemManager is an IEventDispatcher that sends multiple property change events, but I need to wait for the specific event with the property "item".  However, when I call Async inside the handler, it does not keep the Test method from running, Before assumes that it's Async responsibilities have been fulfilled and launches the next stage of tests.  How can I keep Before from moving on until an event with a specific property has been received?

public class Test
    {
        private static const TIME_OUT:int = 3000;
       
        [Before(async)]
        public function runBeforeEveryTest() : void
        {   
            // Create a new item
            item.create();  // This is an asynchronous creation that sends many Property Change events
           
            //Wait for the item to be created before continuing tests.
            Async.handleEvent( this, ItemManager.instance, PropertyChangeEvent.PROPERTY_CHANGE, handlePropertyChangeForItem, TIME_OUT);
        }
       
        [After]
        public function runAfterEveryTest() : void
        {
            // Delete the Item
            item.delete();
        }

        [Test(async)]
        public function itemExists() : void
        {
                  Assert.assertTrue( item.exists() );
        }

       private function handlePropertyChangeForItem(evt:PropertyChangeEvent, eventObject:Object) : void
         {
             if (evt.property == "item")
             {
                 // Now that the item has been created, we can start the tests.
                 return;
             }
             else
             {
                 // Keep waiting.
                 Async.handleEvent( this, ItemManager.instance, PropertyChangeEvent.PROPERTY_CHANGE, handlePropertyChangeForItem, TIME_OUT);
             }
         }

}

Thanks in advance, it's greatly appreciated!

This topic has been closed for replies.

1 reply

Participating Frequently
August 3, 2009

We do have something coming along that will make this easier, however, and more importantly, I think you are seeing a bug.

Even though it isn't elegant, recreating a new Async call in the handler should work. Can you please file a bug in the bugbase. I might be able to have someone try to reproduce and get back to you today.

Mike

Participating Frequently
August 4, 2009

I can't reproduce this issue. All seems to be working fine with multiple deferred Async calls. I am pasting my code in. Since I did not have your manager, I tried to repiicate this with a timer. If you could look at my code and see if it differs from yours in a significant way, I can take another shot, but it seems to be working as expected.

package

import flash.events.TimerEvent;

import flash.utils.Timer;

import mx.events.PropertyChangeEvent;

import org.flexunit.Assert;

import org.flexunit.async.Async;

public class NestedAsync {

private static const TIME_OUT:int = 3000;

private var timer:Timer;

[

Before(async)]

public function runBeforeEveryTest() : void {

  timer =

new Timer( 1000, 5 );

  Async.handleEvent(

this, timer, TimerEvent.TIMER, handleTimerTick, TIME_OUT);timer.start();

}

[After]

public function runAfterEveryTest() : void {

  timer.stop();

  timer =

null;

}

[

Test(async)]

public function itemExists() : void {

Assert.assertTrue(

true );

}

private function handleTimerTick(evt:TimerEvent, eventObject:Object) : void {

  if ( timer.currentCount == 5 ) {

    // Now that the item has been created, we can start the tests.

    return;

  }

  else  {

    // Keep waiting.

    Async.handleEvent(

this, timer, TimerEvent.TIMER, handlePropertyChangeForItem, TIME_OUT);

  }

}

}

}

suite.cases {

AndrewZellman
Adobe Employee
Adobe Employee
August 4, 2009

I just tried running your code, and first, after the // Keep Waiting, you should have your handler function set to handleTimerTick, so it checks after each tick.  Then it works.  In looking at how to best simplify my code and still reproduce the errors, I found an issue in my code, that I have since resolved.  Thanks for all the info, I'll let you know if I have more questions later.