Semaphore等待导致AFNetworking阻塞?

4

我想利用单元测试来测试网络功能。当在测试之外调用loginWithUserName:password:方法时,它会正确地失败并调用外部失败块。但是,在我的测试方法内部调用时,失败块从未被调用(成功块也没有被调用)。

我认为,我的信号量等待可能会导致网络也等待,但我不认为这样会发生,因为它在不同的线程上运行。我希望它在不同的线程上运行,以便我可以进行异步调用。我该如何修复它以正常工作?我应该使用其他技术吗?

我的测试方法设置如下:

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"];
}

我的登录方法看起来像这样: static AFNetworkReachabilityManager *_reachabilityManager;
+ (AFHTTPRequestOperationManager *)gatewayClient {
    static dispatch_once_t onceToken;

    dispatch_once(&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];
     }];
}

AFNetworking将其完成块分派到主队列,因此如果您使用信号量阻塞主线程,则完成块将死锁。 - Rob
在这种情况下,更好的模式是在登录完成后发布通知,所有需要执行进一步操作的类都订阅接收该通知,您可以继续进行而不必使用信号量。 - holex
在网络登录方法的成功/失败块中,我发送通知。问题在于测试,如何暂停测试直到收到网络通知?我能告诉AFNetworking使用不同的队列来调用块吗? - Aaron Bratcher
2个回答

3

我知道对于回答来说已经太晚了,这里是为了那些来到这个线程的人。AFNetwork Manager有一个完成队列,默认是主队列。你可以将其更改为另一个队列,比如后台队列,以避免锁定。

[AFHTTPSessionManager manager].completionQueue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_BACKGROUND, 0);

2

好的... 主要问题在于AFNetworking的队列通知都在主线程上。信号量正在阻塞主线程,所以两个响应都被阻塞了。

对于测试来说,必须指定一个新的NSOperationQueue来取代mainQueue。 对于网络类,必须指定一个新的completionQueue。似乎没有办法在这些方法上设置默认的completionQueue。已经就此问题提出了一个问题。

下面是新的测试代码和网络子集。

@implementation NetworkTests

NSOperationQueue * testOperationQueue;

- (void)setUp {
    [super setUp];
    testOperationQueue = [[NSOperationQueue alloc] init];
}

- (void)testLogin {
    dispatch_semaphore_t sema = dispatch_semaphore_create(0);

    [[NSNotificationCenter defaultCenter] addObserverForName:kLoginComplete
                                                      object:nil
                                                       queue:testOperationQueue
                                                  usingBlock:^(NSNotification *note) {
                                                      XCTAssertTrue(true, @"login Complete"); // expand on this when I get basic premise working
                                                      NSLog(@"asserted true");
                                                      dispatch_semaphore_signal(sema);
                                                  }];


    [Network loginWithUserName:@"testname" password:@"password"];
    dispatch_semaphore_wait(sema, DISPATCH_TIME_FOREVER);
}

@end

对于网络,它有以下内容:

static dispatch_queue_t completionQueue; // initialized when the _gatewayClient object is created

+ (void)loginWithUserName:(NSString *)userName password:(NSString *)password {
    AFHTTPRequestOperation *outerOperation =
    [Network.gatewayClient
     GET:[self baseURLForType:@"auth"
                       method:@"getpubtoken"]
     parameters:nil
     success:^(AFHTTPRequestOperation *operation, id responseObject) {
         // do stuff here
     }

     failure:^(AFHTTPRequestOperation *operation, NSError *error) {
         NSDictionary *userInfo = @{ @"error": error };
         [[NSNotificationCenter defaultCenter] postNotificationName:kLoginComplete
                                                             object:nil
                                                           userInfo:userInfo];
     }];

    outerOperation.completionQueue = completionQueue;
}

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接