Asynchronous Unit testing

  • Hi,

    I hope this is the right list to ask...

    I'd like to unit test some code that relies heavily on timers and
    calls like
    [[NSNotificationQueue defaultQueue] enqueueNotification:
    [NSNotification notificationWithName:kSomeNotification object:self]
    postingStyle:NSPostWhenIdle];

    i.e. my -(void) testSomething method returns immediately and actual
    test will run some time later. Is that possible at all with the
    SenTestingKit?

    Roddi

    ---
    "Less code is better code" - Wil Shipley

    Ruotger Skupin, Mac OS X Software Engineering

    ilink Kommunikationssysteme GmbH
    Münzstr. 13; 10178 Berlin - Germany
  • On 2/8/06 9:09 AM, "Ruotger Skupin" <Ruotger.Skupin...> wrote:

    > Hi,
    >
    > I hope this is the right list to ask...
    >
    > I'd like to unit test some code that relies heavily on timers and calls like
    > [[NSNotificationQueue defaultQueue] enqueueNotification:[NSNotification
    > notificationWithName:kSomeNotification object:self]
    > postingStyle:NSPostWhenIdle];
    >
    > i.e. my -(void) testSomething method returns immediately and actual test will
    > run some time later. Is that possible at all with the SenTestingKit?
    >
    > Roddi

    I did some unit testing on a web spider I wrote. Had the same issue .. I
    wasn't sure when it would be done. Here's my code:

    [websiteScanner startScan];
    while ([websiteScanner isDoneProcessing] == NO) {
        [[NSRunLoop currentRunLoop] runUntilDate:[NSDate
    dateWithTimeIntervalSinceNow:1.0]];
        NSLog(@"Polling...");
    }

    After that I would do all of my asserts.

    ~ Mike
    --
    Work: http://ClickableBliss.com
    Play: http://MikeZornek.com
  • Unfortunately it's not as easy as that. My code requires that I
    return and have the run loop call me back (thus the NSPostWhenIdle).
    That seems to involve setting up a thread, a run loop, a lock etc,
    etc...

    Maybe it's easier to write my own testing kit?

    Roddi

    Am 08.02.2006 um 16:53 schrieb Mike Zornek:

    > On 2/8/06 9:09 AM, "Ruotger Skupin" <Ruotger.Skupin...> wrote:
    >
    >> Hi,
    >>
    >> I hope this is the right list to ask...
    >>
    >> I'd like to unit test some code that relies heavily on timers and
    >> calls like
    >> [[NSNotificationQueue defaultQueue] enqueueNotification:
    >> [NSNotification
    >> notificationWithName:kSomeNotification object:self]
    >> postingStyle:NSPostWhenIdle];
    >>
    >> i.e. my -(void) testSomething method returns immediately and
    >> actual test will
    >> run some time later. Is that possible at all with the SenTestingKit?
    >>
    >> Roddi
    >
    > I did some unit testing on a web spider I wrote. Had the same
    > issue .. I
    > wasn't sure when it would be done. Here's my code:
    >
    > [websiteScanner startScan];
    > while ([websiteScanner isDoneProcessing] == NO) {
    > [[NSRunLoop currentRunLoop] runUntilDate:[NSDate
    > dateWithTimeIntervalSinceNow:1.0]];
    > NSLog(@"Polling...");
    > }
    >
    > After that I would do all of my asserts.
    >
    > ~ Mike
    > --
    > Work: http://ClickableBliss.com
    > Play: http://MikeZornek.com
    >
    ---
    "Less code is better code" - Wil Shipley

    Ruotger Skupin, Mac OS X Software Engineering

    ilink Kommunikationssysteme GmbH
    Münzstr. 13; 10178 Berlin - Germany

    >
  • On Feb 8, 2006, at 10:04 AM, Ruotger Skupin wrote:

    > Unfortunately it's not as easy as that. My code requires that I
    > return and have the run loop call me back (thus the
    > NSPostWhenIdle). That seems to involve setting up a thread, a run
    > loop, a lock etc, etc...

    OCUnit doesn't run unit tests within a runloop itself.  You should be
    able to run one yourself from within your tests as Mike showed.  You
    shouldn't have to set up threads and locks and such to do that.

    > Maybe it's easier to write my own testing kit?

    I wouldn't go that far.

    It might be worth trying to decoupling your functional code more from
    the mechanism that invokes it.  This will allow you to invoke your
    functional code directly from within your tests, and send your
    objects the same messages in the same sequence as they would receive
    from the timers and queues.  (Or as they would receive from the
    bridge methods or objects that you use to connect the timers and
    queues to your code.)

      -- Chris
  • for the record, Mike's method works. Here's my ready to use example
    code:

    #import <SenTestingKit/SenTestingKit.h>

    @interface AsyncTest : SenTestCase
    {
    BOOL _isDone;
    }

    @end

    @implementation AsyncTest

    - (void) setUp
    {
    _isDone = NO;
    }

    - (void) test000AsyncTesting
    {
    [NSRunLoop currentRunLoop];
    [[NSNotificationCenter defaultCenter] addObserver:self
    selector:@selector(async001:) name:@"async000" object:self];
    [[NSNotificationQueue defaultQueue] enqueueNotification:
    [NSNotification notificationWithName:@"async000" object:self]
    postingStyle:NSPostWhenIdle];

    while (!_isDone)
    {
      [[NSRunLoop currentRunLoop] runUntilDate:[NSDate
    dateWithTimeIntervalSinceNow:1.0]];
      NSLog(@"Polling...");
    }
    }

    - (void) async000:(NSNotification*)notification;
    {
    _isDone = YES;
    }
    @end

    Am 08.02.2006 um 16:53 schrieb Mike Zornek:

    > On 2/8/06 9:09 AM, "Ruotger Skupin" <Ruotger.Skupin...> wrote:
    >
    >> Hi,
    >>
    >> I hope this is the right list to ask...
    >>
    >> I'd like to unit test some code that relies heavily on timers and
    >> calls like
    >> [[NSNotificationQueue defaultQueue] enqueueNotification:
    >> [NSNotification
    >> notificationWithName:kSomeNotification object:self]
    >> postingStyle:NSPostWhenIdle];
    >>
    >> i.e. my -(void) testSomething method returns immediately and
    >> actual test will
    >> run some time later. Is that possible at all with the SenTestingKit?
    >>
    >> Roddi
    >
    > I did some unit testing on a web spider I wrote. Had the same
    > issue .. I
    > wasn't sure when it would be done. Here's my code:
    >
    > [websiteScanner startScan];
    > while ([websiteScanner isDoneProcessing] == NO) {
    > [[NSRunLoop currentRunLoop] runUntilDate:[NSDate
    > dateWithTimeIntervalSinceNow:1.0]];
    > NSLog(@"Polling...");
    > }
    >
    > After that I would do all of my asserts.
    >
    > ~ Mike
    > --
    > Work: http://ClickableBliss.com
    > Play: http://MikeZornek.com
    >
    >
    > _______________________________________________
    > Do not post admin requests to the list. They will be ignored.
    > Xcode-users mailing list      (<Xcode-users...>)
    > Help/Unsubscribe/Update your Subscription:
    > http://lists.apple.com/mailman/options/xcode-users/ruotger.skupin%
    > 40ilink.de
    >

    ---
    "Less code is better code" - Wil Shipley

    Ruotger Skupin, Mac OS X Software Engineering

    ilink Kommunikationssysteme GmbH
    Münzstr. 13; 10178 Berlin - Germany
previous month february 2006 next month
MTWTFSS
    1 2 3 4 5
6 7 8 9 10 11 12
13 14 15 16 17 18 19
20 21 22 23 24 25 26
27 28          
Go to today