Skip to content

Commit

Permalink
[UnitTests] Fix order-dependent synchronous decorator tests
Browse files Browse the repository at this point in the history
The decorator tests which used synchronous decorators started failing
if they were run alone (before any of the other tests).

This broke in
d469daa
, when I enabled `GTMStandardUserAgentProvider` by default in fetchers
which don't otherwise specify a user-agent provider.

The issue is that `GTMStandardUserAgentProvider` itself is
asynchronous, but only the first time it's ever invoked (after that,
it caches its result in a process-wide cache).

The synchronous decorator tests assumed they'd always stay synchronous,
but this wasn't the case if they used `GTMStandardUserAgentProvider`
and were the first tests to run in the process.

This fixes the issue by always providing a `GTMUserAgentStringProvider`
for the synchronous decorator tests.

Tested:
  `swift test` passes.
  Also ran test
  `-[GTMSessionFetcherServiceTest testMultipleDecoratorsSynchronous]`
  by itself and ensured it failed before this change and passed after
  this change.
  • Loading branch information
bhamiltoncx authored and thomasvl committed Jan 4, 2024
1 parent 115f75e commit 27d603b
Showing 1 changed file with 13 additions and 0 deletions.
13 changes: 13 additions & 0 deletions UnitTests/GTMSessionFetcherServiceTest.m
Original file line number Diff line number Diff line change
Expand Up @@ -1185,6 +1185,10 @@ - (void)testSingleDecoratorSynchronous {
[GTMSessionFetcherService mockFetcherServiceWithFakedData:nil fakedError:nil];
[service addDecorator:decorator];
GTMSessionFetcher *fetcher = [service fetcherWithURLString:@"https://www.html5zombo.com"];
// Ensure this test always runs the decorators synchronously -- if it's the first
// test to run, then the `GTMStandardUserAgentProvider` will asynchronously add
// the User-Agent.
fetcher.userAgentProvider = [[GTMUserAgentStringProvider alloc] initWithUserAgentString:@"Lynx"];
XCTestExpectation *fetchCompleteExpectation = [self expectationWithDescription:@"Fetch complete"];
[fetcher
beginFetchWithCompletionHandler:^(__unused NSData *fetchData, __unused NSError *fetchError) {
Expand Down Expand Up @@ -1244,6 +1248,7 @@ - (void)testSingleDecoratorSynchronousAddHeaderOnRetry {
fetcher.retryEnabled = YES;
fetcher.minRetryInterval = 0.01;
fetcher.maxRetryInterval = 0.05;
fetcher.userAgentProvider = [[GTMUserAgentStringProvider alloc] initWithUserAgentString:@"Lynx"];
XCTestExpectation *fetchCompleteExpectation = [self expectationWithDescription:@"Fetch complete"];
[fetcher
beginFetchWithCompletionHandler:^(__unused NSData *fetchData, __unused NSError *fetchError) {
Expand All @@ -1267,6 +1272,7 @@ - (void)testSingleDecoratorSynchronousWithError {
[GTMSessionFetcherService mockFetcherServiceWithFakedData:nil fakedError:nil];
[service addDecorator:decorator];
GTMSessionFetcher *fetcher = [service fetcherWithURLString:@"https://www.html5zombo.com"];
fetcher.userAgentProvider = [[GTMUserAgentStringProvider alloc] initWithUserAgentString:@"Lynx"];
XCTestExpectation *fetchCompleteExpectation = [self expectationWithDescription:@"Fetch complete"];
[fetcher beginFetchWithCompletionHandler:^(__unused NSData *fetchData, NSError *fetchError) {
XCTAssertEqualObjects(fetchError, error);
Expand Down Expand Up @@ -1298,6 +1304,7 @@ - (void)testMultipleDecoratorsSynchronousWithErrorShouldNotCallSubsequentDecorat
[service addDecorator:decoratorA];
[service addDecorator:decoratorB];
GTMSessionFetcher *fetcher = [service fetcherWithURLString:@"https://www.html5zombo.com"];
fetcher.userAgentProvider = [[GTMUserAgentStringProvider alloc] initWithUserAgentString:@"Lynx"];
XCTestExpectation *fetchCompleteExpectation = [self expectationWithDescription:@"Fetch complete"];
[fetcher beginFetchWithCompletionHandler:^(__unused NSData *fetchData, NSError *fetchError) {
XCTAssertEqualObjects(fetchError, error);
Expand All @@ -1323,6 +1330,7 @@ - (void)testMultipleDecoratorsSynchronous {
[service addDecorator:decoratorB];
[service addDecorator:decoratorC];
GTMSessionFetcher *fetcher = [service fetcherWithURLString:@"https://www.html5zombo.com"];
fetcher.userAgentProvider = [[GTMUserAgentStringProvider alloc] initWithUserAgentString:@"Lynx"];
XCTestExpectation *fetchCompleteExpectation = [self expectationWithDescription:@"Fetch complete"];
[fetcher
beginFetchWithCompletionHandler:^(__unused NSData *fetchData, __unused NSError *fetchError) {
Expand All @@ -1349,6 +1357,7 @@ - (void)testMultipleDecoratorsSynchronousFetchedDataAndError {
[service addDecorator:decoratorA];
[service addDecorator:decoratorB];
GTMSessionFetcher *fetcher = [service fetcherWithURLString:@"https://www.html5zombo.com"];
fetcher.userAgentProvider = [[GTMUserAgentStringProvider alloc] initWithUserAgentString:@"Lynx"];
XCTestExpectation *fetchCompleteExpectation = [self expectationWithDescription:@"Fetch complete"];
[fetcher
beginFetchWithCompletionHandler:^(__unused NSData *fetchData, __unused NSError *fetchError) {
Expand Down Expand Up @@ -1376,20 +1385,24 @@ - (void)testSingleDecoratorSynchronousWithDifferentHeadersForEachRequest {
[GTMSessionFetcherService mockFetcherServiceWithFakedData:nil fakedError:nil];
[service addDecorator:decorator];
GTMSessionFetcher *fetcherA = [service fetcherWithURLString:@"https://www.html5zombo.com"];
fetcherA.userAgentProvider = [[GTMUserAgentStringProvider alloc] initWithUserAgentString:@"Lynx"];

XCTestExpectation *fetchCompleteExpectationA =
[self expectationWithDescription:@"Fetch complete"];
[fetcherA
beginFetchWithCompletionHandler:^(__unused NSData *fetchData, __unused NSError *fetchError) {
[fetchCompleteExpectationA fulfill];
}];
GTMSessionFetcher *fetcherB = [service fetcherWithURLString:@"https://www.html5zombo.com"];
fetcherB.userAgentProvider = [[GTMUserAgentStringProvider alloc] initWithUserAgentString:@"Lynx"];
XCTestExpectation *fetchCompleteExpectationB =
[self expectationWithDescription:@"Fetch complete"];
[fetcherB
beginFetchWithCompletionHandler:^(__unused NSData *fetchData, __unused NSError *fetchError) {
[fetchCompleteExpectationB fulfill];
}];
GTMSessionFetcher *fetcherC = [service fetcherWithURLString:@"https://www.html5zombo.com"];
fetcherC.userAgentProvider = [[GTMUserAgentStringProvider alloc] initWithUserAgentString:@"Lynx"];
XCTestExpectation *fetchCompleteExpectationC =
[self expectationWithDescription:@"Fetch complete"];
[fetcherC
Expand Down

0 comments on commit 27d603b

Please sign in to comment.