Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add more tests around stopping fetchers with callbacks. #414

Merged
merged 1 commit into from
Nov 8, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions UnitTests/GTMSessionFetcherFetchingTest.h
Original file line number Diff line number Diff line change
Expand Up @@ -85,11 +85,13 @@ extern NSString *const kGTMGettysburgFileName;
@interface TestAuthorizer : NSObject <GTMSessionFetcherAuthorizer>

@property(atomic, assign, getter=isAsync) BOOL async;
@property(atomic, assign) NSUInteger delay; // Only honored if async
@property(atomic, assign, getter=isExpired) BOOL expired;
@property(atomic, assign) BOOL willFailWithError;

+ (instancetype)syncAuthorizer;
+ (instancetype)asyncAuthorizer;
+ (instancetype)asyncAuthorizerDelayed:(NSUInteger)delaySeconds;
+ (instancetype)expiredSyncAuthorizer;
+ (instancetype)expiredAsyncAuthorizer;

Expand Down
67 changes: 60 additions & 7 deletions UnitTests/GTMSessionFetcherFetchingTest.m
Original file line number Diff line number Diff line change
Expand Up @@ -1578,19 +1578,56 @@ - (void)testImmediateCancelFetchWithCallback {
[self internalCancelFetchWithCallback:0];
}

- (void)testDelayedSyncAuthCancelFetchWithCallback {
[self internalCancelFetchWithCallback:1 authorizer:[TestAuthorizer syncAuthorizer]];
}

- (void)testImmediateSyncAuthCancelFetchWithCallback {
XCTSkip(@"Has failed on CI, but not locally, needs investigation.");
[self internalCancelFetchWithCallback:0 authorizer:[TestAuthorizer syncAuthorizer]];
}

- (void)testDelayedAsyncAuthCancelFetchWithCallback {
XCTSkip(@"Currently fails, needs fixing.");
[self internalCancelFetchWithCallback:1 authorizer:[TestAuthorizer asyncAuthorizer]];
}

- (void)testImmediateAsyncAuthCancelFetchWithCallback {
XCTSkip(@"Currently fails, needs fixing.");
[self internalCancelFetchWithCallback:0 authorizer:[TestAuthorizer asyncAuthorizer]];
}

- (void)testDelayedAsyncDelayedAuthCancelFetchWithCallback {
XCTSkip(@"Currently fails, needs fixing.");
[self internalCancelFetchWithCallback:1 authorizer:[TestAuthorizer asyncAuthorizerDelayed:2]];
}

- (void)testImmediateAsyncDelayedAuthCancelFetchWithCallback {
XCTSkip(@"Currently fails, needs fixing.");
thomasvl marked this conversation as resolved.
Show resolved Hide resolved
[self internalCancelFetchWithCallback:0 authorizer:[TestAuthorizer asyncAuthorizerDelayed:1]];
}

- (void)internalCancelFetchWithCallback:(unsigned int)sleepTime {
[self internalCancelFetchWithCallback:sleepTime authorizer:nil];
}

#pragma clang diagnostic ignored "-Wdeprecated"
- (void)internalCancelFetchWithCallback:(unsigned int)sleepTime
authorizer:(nullable id<GTMFetcherAuthorizationProtocol>)authorizer {
#pragma clang diagnostic pop
if (!_isServerRunning) return;

CREATE_START_STOP_NOTIFICATION_EXPECTATIONS(1, 1);

FetcherNotificationsCounter *fnctr = [[FetcherNotificationsCounter alloc] init];

__block GTMSessionFetcher *fetcher;

NSString *timeoutFileURLString = [self localURLStringToTestFileName:kGTMGettysburgFileName
parameters:@{@"sleep" : @"10"}];
fetcher = [self fetcherWithURLString:timeoutFileURLString];
GTMSessionFetcher *fetcher = [self fetcherWithURLString:timeoutFileURLString];
fetcher.stopFetchingTriggersCompletionHandler = YES;
if (authorizer) {
fetcher.authorizer = authorizer;
}
XCTestExpectation *expectation = [self expectationWithDescription:@"Expect to call callback"];
[fetcher beginFetchWithCompletionHandler:^(NSData *data, NSError *error) {
XCTAssertNil(data, @"error data unexpected");
Expand Down Expand Up @@ -2871,7 +2908,8 @@ - (void)testFetcherRedirectURLHandling {
@end

@implementation TestAuthorizer
@synthesize async = _async, expired = _expired, willFailWithError = _willFailWithError;
@synthesize async = _async, delay = _delay, expired = _expired,
willFailWithError = _willFailWithError;

+ (instancetype)syncAuthorizer {
return [[self alloc] init];
Expand All @@ -2883,6 +2921,13 @@ + (instancetype)asyncAuthorizer {
return authorizer;
}

+ (instancetype)asyncAuthorizerDelayed:(NSUInteger)delaySeconds {
TestAuthorizer *authorizer = [self syncAuthorizer];
authorizer.async = YES;
authorizer.delay = delaySeconds;
return authorizer;
}

+ (instancetype)expiredSyncAuthorizer {
TestAuthorizer *authorizer = [self syncAuthorizer];
authorizer.expired = YES;
Expand All @@ -2908,9 +2953,17 @@ - (void)authorizeRequest:(NSMutableURLRequest *)request
}

if (self.async) {
dispatch_async(dispatch_get_main_queue(), ^{
handler(error);
});
if (self.delay) {
dispatch_time_t delay_time =
dispatch_time(DISPATCH_TIME_NOW, (int64_t)(self.delay * NSEC_PER_SEC));
dispatch_after(delay_time, dispatch_get_main_queue(), ^{
handler(error);
});
} else {
dispatch_async(dispatch_get_main_queue(), ^{
handler(error);
});
}
} else {
handler(error);
}
Expand Down
Loading