title: Semaphore wait causing a block of AFNetworking?
body: <p>I'm trying to use unit testing to also test for networking. When called outside of my testing, the loginWithUserName:password: method properly fails and the outer failure block is called. When called from within my testing method, the failure block is never called (nor is the success block).</p>
  
  <p>I think perhaps my semaphore wait is causing the networking to also wait, but I would't think so since it is on a different thread. I want it on a different thread so I can do an asynchronous call. Can I fix this to work properly? Should I use a different technique?</p>
  
  <p>I have my test methods setup like this:</p>
  
  <pre><code>typedef void (^CompleteBlock)();
  
  - (void)testLogin {
      dispatch_semaphore_t sema = dispatch_semaphore_create(0);
  
      [self callLoginWithCompletion:^{
          XCTAssertTrue(true, @"login Complete"); // expand on this when I get basic premise working
          NSLog(@"asserted true");
          dispatch_semaphore_signal(sema);
      }];
  
      dispatch_semaphore_wait(sema, DISPATCH_TIME_FOREVER);
  }
  
  - (void)callLoginWithCompletion:(CompleteBlock)completeBlock {
      NSLog(@"login method called");
      [[NSNotificationCenter defaultCenter] addObserverForName:kLoginComplete
                                                        object:nil
                                                         queue:[NSOperationQueue mainQueue]
                                                    usingBlock:^(NSNotification *note) {
                                                        completeBlock();
                                                    }];
  
      [Network loginWithUserName:@"dummyUser" password:@"dummyPassword"];
  }
  </code></pre>
  
  <p>And my login method looks like this:
      static AFNetworkReachabilityManager *_reachabilityManager;</p>
  
  <pre><code>+ (AFHTTPRequestOperationManager *)gatewayClient {
      static dispatch_once_t onceToken;
  
      dispatch_once(&amp;onceToken, ^{
          _gatewayClient = [[AFHTTPRequestOperationManager alloc] initWithBaseURL:[NSURL URLWithString:kGatewayBaseURL]];
          _gatewayClient.responseSerializer.acceptableContentTypes = [NSSet setWithObject:@"text/plain"];
      });
  
      AFSecurityPolicy *securityPolicy = [AFSecurityPolicy policyWithPinningMode:AFSSLPinningModeNone];
      securityPolicy.allowInvalidCertificates = YES;
      [AFHTTPRequestOperationManager manager].securityPolicy = securityPolicy;
  
      return _gatewayClient;
  }
  
  + (NSString *)baseURLForType:(NSString *)type method:(NSString *)method {
      return [NSString stringWithFormat:@"api/%@/%@", type, method];
  }
  
  + (void)loginWithUserName:(NSString *)userName password:(NSString *)password {
      [Network.gatewayClient
       GET:[self baseURLForType:@"auth"
                         method:@"getpubtoken"]
       parameters:nil
       success:^(AFHTTPRequestOperation *operation, id responseObject) {
           _authToken = responseObject;
  
           NSDictionary *parameters = @{
                                        @"UserId": userName
                                        , @"Password": password
                                        , kApiVersion: kApiVersion
                                        , kApiKey: kApiKeyValue
                                        , kAuthToken: _authToken
                                        , kFundraisingPlatform: @(Blackbaud)
                                        };
  
           [Network.gatewayClient
            POST:[self baseURLForType:@"auth"
                               method:@"loginfundraiser"]
            parameters:parameters
            success:^(AFHTTPRequestOperation *operation, id responseObject) {
                NSDictionary *responseDict = (NSDictionary *)responseObject;
                NSDictionary *userInfo = nil;
  
                _authToken = responseDict[kAuthToken];
  
                if ([responseDict[@"Successful"] boolValue]) {
                    userInfo = @{ kAuthToken: responseObject[kAuthToken] };
                } else {
                    userInfo = @{ @"error": [[NSError alloc] initWithDomain:@"Authorization"
                                                                       code:-1000
                                                                   userInfo:@{ @"message": responseDict[@"ExtendedMessages"] }] };
                }
  
                [[NSNotificationCenter defaultCenter] postNotificationName:kLoginComplete
                                                                    object:nil
                                                                  userInfo:userInfo];
            }
  
            failure:^(AFHTTPRequestOperation *operation, NSError *error) {
                NSDictionary *userInfo = @{ @"error": error };
                [[NSNotificationCenter defaultCenter] postNotificationName:kLoginComplete
                                                                    object:nil
                                                                  userInfo:userInfo];
            }];
       }
  
       failure:^(AFHTTPRequestOperation *operation, NSError *error) {
           NSDictionary *userInfo = @{ @"error": error };
           [[NSNotificationCenter defaultCenter] postNotificationName:kLoginComplete
                                                               object:nil
                                                             userInfo:userInfo];
       }];
  }
  </code></pre>