Skip to content

Commit

Permalink
Update PR feedback, add more tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Kaspik committed Sep 11, 2018
1 parent 3c54e1f commit 864c8d6
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 7 deletions.
10 changes: 9 additions & 1 deletion AFNetworking/AFURLSessionManager.h
Original file line number Diff line number Diff line change
Expand Up @@ -196,7 +196,15 @@ NS_ASSUME_NONNULL_BEGIN
@param cancelPendingTasks Whether or not to cancel pending tasks.
*/
- (void)invalidateSessionCancelingTasks:(BOOL)cancelPendingTasks;
- (void)invalidateSessionCancelingTasks:(BOOL)cancelPendingTasks DEPRECATED_ATTRIBUTE;

/**
Invalidates the managed session, optionally canceling pending tasks and optionally resets given session.
@param cancelPendingTasks Whether or not to cancel pending tasks.
@param resetSession Whether or not to reset the session of the manager.
*/
- (void)invalidateSessionCancelingTasks:(BOOL)cancelPendingTasks resetSession:(BOOL)resetSession;

///-------------------------
/// @name Running Data Tasks
Expand Down
12 changes: 10 additions & 2 deletions AFNetworking/AFURLSessionManager.m
Original file line number Diff line number Diff line change
Expand Up @@ -404,7 +404,8 @@ + (void)load {
7) If the current class implementation of `resume` is not equal to the super class implementation of `resume` AND the current implementation of `resume` is not equal to the original implementation of `af_resume`, THEN swizzle the methods
8) Set the current class to the super class, and repeat steps 3-8
*/
NSURLSession * session = [NSURLSession sharedSession];
NSURLSessionConfiguration *configuration = [NSURLSessionConfiguration ephemeralSessionConfiguration];
NSURLSession *session = [NSURLSession sessionWithConfiguration:configuration];
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wnonnull"
NSURLSessionDataTask *localDataTask = [session dataTaskWithURL:nil];
Expand All @@ -424,6 +425,7 @@ + (void)load {
}

[localDataTask cancel];
[session finishTasksAndInvalidate];
}
}

Expand Down Expand Up @@ -724,12 +726,18 @@ - (NSArray *)downloadTasks {
#pragma mark -

- (void)invalidateSessionCancelingTasks:(BOOL)cancelPendingTasks {
[self invalidateSessionCancelingTasks:cancelPendingTasks resetSession:NO];
}

- (void)invalidateSessionCancelingTasks:(BOOL)cancelPendingTasks resetSession:(BOOL)resetSession {
if (cancelPendingTasks) {
[self.session invalidateAndCancel];
} else {
[self.session finishTasksAndInvalidate];
}
self.session = nil;
if (resetSession) {
self.session = nil;
}
}

#pragma mark -
Expand Down
16 changes: 12 additions & 4 deletions Tests/Tests/AFURLSessionManagerTests.m
Original file line number Diff line number Diff line change
Expand Up @@ -70,10 +70,10 @@ - (void)setUp {
- (void)tearDown {
[super tearDown];
[self.localManager.session.configuration.URLCache removeAllCachedResponses];
[self.localManager invalidateSessionCancelingTasks:YES];
[self.localManager invalidateSessionCancelingTasks:YES resetSession:YES];
self.localManager = nil;

[self.backgroundManager invalidateSessionCancelingTasks:YES];
[self.backgroundManager invalidateSessionCancelingTasks:YES resetSession:YES];
self.backgroundManager = nil;
}

Expand Down Expand Up @@ -158,11 +158,19 @@ - (void)testSessionTaskDoesReportMetrics {
[self waitForExpectationsWithCommonTimeout];
}

- (void)testSessionIsStillValid {

NSURLSession *session = self.localManager.session;
[self.localManager invalidateSessionCancelingTasks:YES resetSession:NO];

XCTAssertEqual(session, self.localManager.session);
}

- (void)testSessionRecreatesAgain {

[self.localManager setValue:nil forKey:@"session"];
[self.localManager invalidateSessionCancelingTasks:YES resetSession:NO];

XCTAssertNotNil([self.localManager valueForKey:@"session"]);
XCTAssertNotNil(self.localManager.session);
}

- (void)testUploadTaskDoesReportProgress {
Expand Down

0 comments on commit 864c8d6

Please sign in to comment.