2 Replies Latest reply on Aug 4, 2010 2:45 AM by Wubzorz

    Async problem when using FlexUnit4

    Wubzorz Level 1

      Hi all,

       

      I have a problem I could use a little help with. I'm trying to change a test to FlexUnit4 (first used FlexUnit 0.9), but when using the Async class I get a "Timeout Occurred before expected event" (from the ExpectAsync.handleAsyncTimeOut function (line 315)).

       

      The function looks like this:

       

       

      [Test(async)]
       public function GetApplicationMenuTest():void
       {
      var event:GetApplicationMenuEvent=new GetApplicationMenuEvent(this.loader, applicationId);
      
       var asyncHandler:Function=Async.asyncHandler(this, resultHandler, 500);
       
       this.loader.fireEvent(event, asyncHandler);
       }
      

       

       

      The loader.fireEvent(event:MVCEvent, callback:Function) method is a function that will set a busy indicator on the screen and that fires the event. When the event is fired a server call is eventually made, and we wait for a responds. When the responds is received the retrieved data is processed until finally the function this.event.loader.receiveData(resultObject) is called. Where the loader is the same as the function above and the resultObject is the processed responds data.

       

      This receivedata function removes the busy indicator from the screen and calls the the callback:Function as can be seen from the super class of the loader:

       

       

      public function receiveData(data:Object):void
       {
         this.callBackFunction(data);
       }
      

       

       

      The callBackFunction is the following function in our test:

       

       

      private function resultHandler(data:EibDataObject):void
       {
       Assert.assertTrue("The session is not ok", data.sessionOK);
       Assert.assertTrue("Fault messages have been retrieved", data.messages.length <= 0);
       }
      

       

       

      Using this setup described above I get a "Timeout Occurred before expected event". I never had a problem using the FlexUnit 0.9 setup that looked as follows:

       

       

      public function GetApplicationMenuTest():void
       {
       var applicationId:String = "";
       
       var event:GetApplicationMenuEvent = new GetApplicationMenuEvent(this.loader, applicationId);
       this.loader.fireEvent(event, addAsync(resultHandler, Model.TIMEOUT));
       }
      

       

       

      So the only difference in the test is the callback:Function that is set.

       

      I checked out the code of FlexUnit4 (http://opensource.adobe.com/svn/opensource/flexunit/branches/4.x/FlexUnit4) to debug the problem and I found that the method AsyncHandler.handleEvent(event:Event) would not be entered. When I changed this method to AsyncHandler.handleEvent(data:Object) I saw that the argument passed to this function was not an event but  the resultObject described above.

       

      This is offcourse pretty logical because AsyncHandler.handleEvent is the method that is returned as the Function from Async.asyncHandler(...).

       

      What do I need to do to change

       

       

      public function GetApplicationMenuTest():void
      {
      var applicationId:String = "";
      
      var event:GetApplicationMenuEvent = new GetApplicationMenuEvent(this.loader, applicationId);
      this.loader.fireEvent(event, addAsync(resultHandler, Model.TIMEOUT));
      }
      
      

       

      to FlexUnit 4 in the scenario described above? Hope you guys can help

       

      Greetz,

      Rick

       

      Also I have a small question, what is the 'timeout=xxx' statement for in the [Test] metadata when you also provide a timeout to the async method?

        • 1. Re: Async problem when using FlexUnit4
          mlabriola Level 4

          A few things for you:

           

          1) The opensource.adobe.com address is always lagging behind. Our active website is www.flexunit.org and our active code development is at github. We just update the adobe site with major release versions.

           

          2) The timeout in the Test metadata: We support multiple simultaneous outstanding events in FlexUnit 4. In other words, you can say, you expect a given object to broadcast event1, followed by event2, followed by event3. The timeout in the Test is an overall timeout for all of the steps, so each is given their own timeout, but in total they should not exceed the number assigned in the Test metadata

           

          3) As you noted the async methods are expecting to handle an event and you are using them with a callback, which is the root of the problem. I clearly missed the fact that the .9 version would allow this use case, so, if you would, I would appreciate if you could file an enhancement request in our project at bugs.adobe.com and I can work on getting this resolved for the next version.

           

          In the meantime, you could use the responder functionality, which seems like it would work fine, albeit not as elegant as I would like. I can't test this out at my current location, but wanted to get you an answer asap, so post back with issues.

           

          [Test(async)]

          public function GetApplicationMenuTest():void

          {

             var event:GetApplicationMenuEvent=new GetApplicationMenuEvent(this.loader, applicationId);

             var responder:Responder = new mx.rpc.Responder( resultHandler, null );

             var asyncResponder:IResponder=Async.asyncResponder(this, responder, 500);

           

             this.loader.fireEvent(event, asyncResponder.result );

          }

          Mike

          • 2. Re: Async problem when using FlexUnit4
            Wubzorz Level 1

            Thanks for the clear and quick responds Michael!

             

            1) I'll add a new repository to my subclipse repository list.

             

            2) Finally a complete explanation about the timeout in the metadata. You couldn't have been any clearer

             

            3 )Thanks for the answer, I'll try out the responder. What I did myself for the time being was to encapsulate the 'resultObject' variable (the processed responds data) in a (what I call) DataTransportEvent. This event is like the AsyncEvent, so just an event that can hold an extra argument.

            This DataTransportEvent would then be the argument that was provided when invoking the callback Function.

             

            I'll create a small test file that will show what we did. Don't know if it will be today, but soon

             

            Thanks again,

            Rick