Skip to content
This repository has been archived by the owner on Jan 17, 2023. It is now read-only.

Commit

Permalink
Fix progress handlers called with fractionCompleted == 1 several times
Browse files Browse the repository at this point in the history
Setting the expectation to nil after it is fulfilled in order to avoid `*** Assertion failure in -[XCTestExpectation fulfill]` was just hiding the real issue.
  • Loading branch information
0xced committed Sep 21, 2016
1 parent 630bf2e commit 181d633
Show file tree
Hide file tree
Showing 3 changed files with 6 additions and 8 deletions.
4 changes: 2 additions & 2 deletions AFNetworking/AFURLSessionManager.m
Original file line number Diff line number Diff line change
Expand Up @@ -285,8 +285,8 @@ - (void)URLSession:(__unused NSURLSession *)session
dataTask:(__unused NSURLSessionDataTask *)dataTask
didReceiveData:(NSData *)data
{
self.downloadProgress.completedUnitCount = dataTask.countOfBytesReceived;
self.downloadProgress.totalUnitCount = dataTask.countOfBytesExpectedToReceive;
self.downloadProgress.completedUnitCount = dataTask.countOfBytesReceived;

[self.mutableData appendData:data];
}
Expand All @@ -296,8 +296,8 @@ - (void)URLSession:(NSURLSession *)session task:(NSURLSessionTask *)task
totalBytesSent:(int64_t)totalBytesSent
totalBytesExpectedToSend:(int64_t)totalBytesExpectedToSend{

self.uploadProgress.completedUnitCount = task.countOfBytesSent;
self.uploadProgress.totalUnitCount = task.countOfBytesExpectedToSend;
self.uploadProgress.completedUnitCount = task.countOfBytesSent;
}

#pragma mark - NSURLSessionDownloadDelegate
Expand Down
8 changes: 3 additions & 5 deletions Tests/Tests/AFHTTPSessionManagerTests.m
Original file line number Diff line number Diff line change
Expand Up @@ -228,7 +228,7 @@ - (void)testCanBeCopied {
#pragma mark - Progress

- (void)testDownloadProgressIsReportedForGET {
XCTestExpectation *expectation = [self expectationWithDescription:@"Progress Should equal 1.0"];
__weak XCTestExpectation *expectation = [self expectationWithDescription:@"Progress Should equal 1.0"];
[self.manager
GET:@"image"
parameters:nil
Expand All @@ -248,15 +248,14 @@ - (void)testUploadProgressIsReportedForPOST {
[payload appendString:@"AFNetworking"];
}

__weak __block XCTestExpectation *expectation = [self expectationWithDescription:@"Progress Should equal 1.0"];
__weak XCTestExpectation *expectation = [self expectationWithDescription:@"Progress Should equal 1.0"];

[self.manager
POST:@"post"
parameters:payload
progress:^(NSProgress * _Nonnull uploadProgress) {
if (uploadProgress.fractionCompleted == 1.0) {
[expectation fulfill];
expectation = nil;
}
}
success:nil
Expand All @@ -270,7 +269,7 @@ - (void)testUploadProgressIsReportedForStreamingPost {
[payload appendString:@"AFNetworking"];
}

__block __weak XCTestExpectation *expectation = [self expectationWithDescription:@"Progress Should equal 1.0"];
__weak XCTestExpectation *expectation = [self expectationWithDescription:@"Progress Should equal 1.0"];

[self.manager
POST:@"post"
Expand All @@ -281,7 +280,6 @@ - (void)testUploadProgressIsReportedForStreamingPost {
progress:^(NSProgress * _Nonnull uploadProgress) {
if (uploadProgress.fractionCompleted == 1.0) {
[expectation fulfill];
expectation = nil;
}
}
success:nil
Expand Down
2 changes: 1 addition & 1 deletion Tests/Tests/AFURLSessionManagerTests.m
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,7 @@ - (void)testUploadTaskDoesReportProgress {
fromData:[payload dataUsingEncoding:NSUTF8StringEncoding]
progress:^(NSProgress * _Nonnull uploadProgress) {
NSLog(@"%@", uploadProgress.localizedDescription);
if ([uploadProgress fractionCompleted] == 1.0) {
if (uploadProgress.fractionCompleted == 1.0) {
[expectation fulfill];
}
}
Expand Down

0 comments on commit 181d633

Please sign in to comment.