GithubHelp home page GithubHelp logo

dispatch_semaphore_t ignored about specta HOT 4 CLOSED

specta avatar specta commented on June 3, 2024
dispatch_semaphore_t ignored

from specta.

Comments (4)

jspahrsummers avatar jspahrsummers commented on June 3, 2024

A few things here:

  1. You're creating the semaphore with an initial count of 1. This means that the first dispatch_semaphore_wait will always complete immediately, which is probably not what you want.
  2. The semaphore isn't recreated each time the beforeAll block is run (relevant if it's in a describe block). I'd probably move the variable and initialization into there.
  3. If -loginWithUserName:… needs to do any work on the main thread — like delivering its results there — it won't be able to, since you'll be blocking on the semaphore.

Instead, if you're also using expecta, you can use the will matcher to solve all of the above problems:

beforeAll(^{
    // This is run once and only once before all of the examples
    // in this group and before any beforeEach blocks.

    // Set a timeout of 100 sec
    setAsyncSpecTimeout(100);

    Login *login = [[Login alloc] init];

    __block BOOL completed = NO;
    [login loginWithUserName:@"user"
                    passWord:@"pass"
                   onSuccess:^(id responseObject) {
                       // Handle successful login
                       completed = YES;
                   }
                     onError:^(NSError *err) {
                       // Handle error
                         completed = YES;
                     }];

    expect(completed).will.beTruthy();
});

As a sidenote, 100 seconds is way too long to be waiting for results in a unit test. It's probably safe to say that any more than 10 seconds should be considered a failure.

from specta.

cyupa avatar cyupa commented on June 3, 2024

The timeout was left that way because of a very big lag in the first place. I changed that but it doesn't make any difference to the issue.

Yes, I am also using specta but it fails. You could try for yourself the example you gave and see if it fails, for me it does. I added some brakepoints in both success and failure callbacks, they are never called.

Ok, so the semaphore would block, I'll drop it.

Now, following your suggestion I'm having something like this:

// The authToken that will be used in all the tests
__block NSString *authToken = nil;

beforeAll(^{
        // This is run once and only once before all of the examples
        // in this group and before any beforeEach blocks.

        // Set a timeout of 20 sec
        setAsyncSpecTimeout(20);

        UserSession *sessionManager = [UserSession sharedInstance];
        [sessionManager userSessionWithUsername:@"user"
                                          password:@"pass"
                                          success:^(UserSessionData *session) {
                                                 // Set the authToken string
                                                 authToken = session.authToken;
                                     }
                                          failure:^(NSError *error) {
                                                 // Handle the error
                                    }];

        expect(authToken).will.beInstanceOf([NSString class]);

    });

The will matcher fails. It tries to do the other tests, which also fail, because they make use of the authToken.

from specta.

jspahrsummers avatar jspahrsummers commented on June 3, 2024

I added some brakepoints in both success and failure callbacks, they are never called.

This sounds like a problem with the method you're using, then. Does it work in a small sample application?

from specta.

orta avatar orta commented on June 3, 2024

Closing due to lack of a response.

from specta.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.