Skip to content

Add a unit test to verify behavior w/ URLSession:dataTask:didReceiveResponse:completionHandler: #14489

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

Draft
wants to merge 22 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from 8 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: 1 addition & 1 deletion .ruby-version
Original file line number Diff line number Diff line change
@@ -1 +1 @@
ruby-2.7
2.7.4
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,35 @@

@end

@interface FPRNSURLSessionDelegateProxy : NSProxy <NSURLSessionDelegate> {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you try re-running the tests with:

Suggested change
@interface FPRNSURLSessionDelegateProxy : NSProxy <NSURLSessionDelegate> {
@interface FPRNSURLSessionDelegateProxy : NSProxy {

// The wrapped delegate object.
id _delegate;
}

/** @return an instance of the delegate proxy. */
- (instancetype)initWithDelegate:(id <NSURLSessionDelegate>)delegate;

@end

@implementation FPRNSURLSessionDelegateProxy

- (instancetype)initWithDelegate:(id)delegate {
if (self) {
_delegate = delegate;
}
return self;
}

- (NSMethodSignature *)methodSignatureForSelector:(SEL)selector {
return [_delegate methodSignatureForSelector:selector];
}

- (void)forwardInvocation:(NSInvocation *)invocation {
[invocation invokeWithTarget:_delegate];
}

@end

@interface FPRNSURLSessionInstrumentTest : FPRTestCase

/** Test server to create connections to. */
Expand Down Expand Up @@ -403,7 +432,7 @@
[networkTrace
timeIntervalBetweenCheckpointState:FPRNetworkTraceCheckpointStateInitiated
andState:FPRNetworkTraceCheckpointStateRequestCompleted] > 0);
XCTAssertNil([FPRNetworkTrace networkTraceFromObject:uploadTask]);

Check failure on line 435 in FirebasePerformance/Tests/Unit/Instruments/FPRNSURLSessionInstrumentTest.m

View workflow job for this annotation

GitHub Actions / performance (tvOS, unit)

testDelegateURLSessionTaskDidSendBodyDataTotalBytesSentTotalBytesExpectedToSend, (([FPRNetworkTrace networkTraceFromObject:uploadTask]) == nil) failed: "Request: <NSMutableURLRequest: 0x60000001c2a0> { URL: http://localhost:49873/testUpload }"

Check failure on line 435 in FirebasePerformance/Tests/Unit/Instruments/FPRNSURLSessionInstrumentTest.m

View workflow job for this annotation

GitHub Actions / performance (tvOS, unit)

testDelegateURLSessionTaskDidSendBodyDataTotalBytesSentTotalBytesExpectedToSend, (([FPRNetworkTrace networkTraceFromObject:uploadTask]) == nil) failed: "Request: <NSMutableURLRequest: 0x600000018810> { URL: http://localhost:51056/testUpload }"

Check failure on line 435 in FirebasePerformance/Tests/Unit/Instruments/FPRNSURLSessionInstrumentTest.m

View workflow job for this annotation

GitHub Actions / performance (iOS, unit)

testDelegateURLSessionTaskDidSendBodyDataTotalBytesSentTotalBytesExpectedToSend, (([FPRNetworkTrace networkTraceFromObject:uploadTask]) == nil) failed: "Request: <NSMutableURLRequest: 0x600000026280> { URL: http://localhost:50487/testUpload }"

Check failure on line 435 in FirebasePerformance/Tests/Unit/Instruments/FPRNSURLSessionInstrumentTest.m

View workflow job for this annotation

GitHub Actions / performance (iOS, unit)

testDelegateURLSessionTaskDidSendBodyDataTotalBytesSentTotalBytesExpectedToSend, (([FPRNetworkTrace networkTraceFromObject:uploadTask]) == nil) failed: "Request: <NSMutableURLRequest: 0x60000001bfa0> { URL: http://localhost:52202/testUpload }"
}];
[instrument deregisterInstrumentors];
}
Expand Down Expand Up @@ -452,8 +481,8 @@
XCTAssertNotNil([FPRNetworkTrace networkTraceFromObject:dataTask]);
[self waitAndRunBlockAfterResponse:^(id self, GCDWebServerRequest *_Nonnull request,
GCDWebServerResponse *_Nonnull response) {
XCTAssertTrue(delegate.URLSessionDataTaskDidReceiveDataCalled);

Check failure on line 484 in FirebasePerformance/Tests/Unit/Instruments/FPRNSURLSessionInstrumentTest.m

View workflow job for this annotation

GitHub Actions / performance (tvOS, unit)

testDelegateURLSessionDataTaskDidReceiveData, ((delegate.URLSessionDataTaskDidReceiveDataCalled) is true) failed

Check failure on line 484 in FirebasePerformance/Tests/Unit/Instruments/FPRNSURLSessionInstrumentTest.m

View workflow job for this annotation

GitHub Actions / performance (tvOS, unit)

testDelegateURLSessionDataTaskDidReceiveData, ((delegate.URLSessionDataTaskDidReceiveDataCalled) is true) failed

Check failure on line 484 in FirebasePerformance/Tests/Unit/Instruments/FPRNSURLSessionInstrumentTest.m

View workflow job for this annotation

GitHub Actions / performance (tvOS, unit)

testDelegateURLSessionDataTaskDidReceiveData, ((delegate.URLSessionDataTaskDidReceiveDataCalled) is true) failed

Check failure on line 484 in FirebasePerformance/Tests/Unit/Instruments/FPRNSURLSessionInstrumentTest.m

View workflow job for this annotation

GitHub Actions / performance (iOS, unit)

testDelegateURLSessionDataTaskDidReceiveData, ((delegate.URLSessionDataTaskDidReceiveDataCalled) is true) failed

Check failure on line 484 in FirebasePerformance/Tests/Unit/Instruments/FPRNSURLSessionInstrumentTest.m

View workflow job for this annotation

GitHub Actions / performance (iOS, unit)

testDelegateURLSessionDataTaskDidReceiveData, ((delegate.URLSessionDataTaskDidReceiveDataCalled) is true) failed
XCTAssertNil([FPRNetworkTrace networkTraceFromObject:dataTask]);

Check failure on line 485 in FirebasePerformance/Tests/Unit/Instruments/FPRNSURLSessionInstrumentTest.m

View workflow job for this annotation

GitHub Actions / performance (tvOS, unit)

testDelegateURLSessionDataTaskDidReceiveData, (([FPRNetworkTrace networkTraceFromObject:dataTask]) == nil) failed: "Request: <NSURLRequest: 0x600000016170> { URL: http://localhost:49741/testBigDownload }"

Check failure on line 485 in FirebasePerformance/Tests/Unit/Instruments/FPRNSURLSessionInstrumentTest.m

View workflow job for this annotation

GitHub Actions / performance (tvOS, unit)

testDelegateURLSessionDataTaskDidReceiveData, (([FPRNetworkTrace networkTraceFromObject:dataTask]) == nil) failed: "Request: <NSURLRequest: 0x600000016400> { URL: http://localhost:50927/testBigDownload }"

Check failure on line 485 in FirebasePerformance/Tests/Unit/Instruments/FPRNSURLSessionInstrumentTest.m

View workflow job for this annotation

GitHub Actions / performance (tvOS, unit)

testDelegateURLSessionDataTaskDidReceiveData, (([FPRNetworkTrace networkTraceFromObject:dataTask]) == nil) failed: "Request: <NSURLRequest: 0x600000018820> { URL: http://localhost:52157/testBigDownload }"

Check failure on line 485 in FirebasePerformance/Tests/Unit/Instruments/FPRNSURLSessionInstrumentTest.m

View workflow job for this annotation

GitHub Actions / performance (iOS, unit)

testDelegateURLSessionDataTaskDidReceiveData, (([FPRNetworkTrace networkTraceFromObject:dataTask]) == nil) failed: "Request: <NSURLRequest: 0x600000021290> { URL: http://localhost:50315/testBigDownload }"

Check failure on line 485 in FirebasePerformance/Tests/Unit/Instruments/FPRNSURLSessionInstrumentTest.m

View workflow job for this annotation

GitHub Actions / performance (iOS, unit)

testDelegateURLSessionDataTaskDidReceiveData, (([FPRNetworkTrace networkTraceFromObject:dataTask]) == nil) failed: "Request: <NSURLRequest: 0x600000021380> { URL: http://localhost:52037/testBigDownload }"
}];
}
[instrument deregisterInstrumentors];
Expand Down Expand Up @@ -482,6 +511,60 @@
[instrument deregisterInstrumentors];
}

/** Tests that the called delegate selector is wrapped and calls through. */
- (void)testDelegateURLSessionDownloadDidReceiveResponseCompletionHandler {
FPRNSURLSessionInstrument *instrument;
NSURLSessionDataTask *dataTask;
@autoreleasepool {
instrument = [[FPRNSURLSessionInstrument alloc] init];
[instrument registerInstrumentors];
FPRNSURLSessionCompleteTestDelegate *delegate =
[[FPRNSURLSessionCompleteTestDelegate alloc] init];
NSURLSessionConfiguration *configuration =
[NSURLSessionConfiguration defaultSessionConfiguration];
NSURLSession *session = [NSURLSession sessionWithConfiguration:configuration
delegate:delegate
delegateQueue:nil];
NSURL *URL = [self.testServer.serverURL URLByAppendingPathComponent:@"testBigDownload"];
dataTask = [session dataTaskWithURL:URL];
[dataTask resume];
XCTAssertNotNil([FPRNetworkTrace networkTraceFromObject:dataTask]);
[self waitAndRunBlockAfterResponse:^(id self, GCDWebServerRequest *_Nonnull request,
GCDWebServerResponse *_Nonnull response) {
XCTAssertTrue(delegate.URLSessionDataTaskDidReceiveResponseCompletionHandlerCalled);
XCTAssertNil([FPRNetworkTrace networkTraceFromObject:dataTask]);

Check failure on line 535 in FirebasePerformance/Tests/Unit/Instruments/FPRNSURLSessionInstrumentTest.m

View workflow job for this annotation

GitHub Actions / performance (tvOS, unit)

testDelegateURLSessionDownloadDidReceiveResponseCompletionHandler, (([FPRNetworkTrace networkTraceFromObject:dataTask]) == nil) failed: "Request: <NSURLRequest: 0x60000001a760> { URL: http://localhost:49754/testBigDownload }"

Check failure on line 535 in FirebasePerformance/Tests/Unit/Instruments/FPRNSURLSessionInstrumentTest.m

View workflow job for this annotation

GitHub Actions / performance (tvOS, unit)

testDelegateURLSessionDownloadDidReceiveResponseCompletionHandler, (([FPRNetworkTrace networkTraceFromObject:dataTask]) == nil) failed: "Request: <NSURLRequest: 0x60000001a490> { URL: http://localhost:50938/testBigDownload }"

Check failure on line 535 in FirebasePerformance/Tests/Unit/Instruments/FPRNSURLSessionInstrumentTest.m

View workflow job for this annotation

GitHub Actions / performance (iOS, unit)

testDelegateURLSessionDownloadDidReceiveResponseCompletionHandler, (([FPRNetworkTrace networkTraceFromObject:dataTask]) == nil) failed: "Request: <NSURLRequest: 0x600000020670> { URL: http://localhost:50332/testBigDownload }"

Check failure on line 535 in FirebasePerformance/Tests/Unit/Instruments/FPRNSURLSessionInstrumentTest.m

View workflow job for this annotation

GitHub Actions / performance (iOS, unit)

testDelegateURLSessionDownloadDidReceiveResponseCompletionHandler, (([FPRNetworkTrace networkTraceFromObject:dataTask]) == nil) failed: "Request: <NSURLRequest: 0x60000001a9c0> { URL: http://localhost:52054/testBigDownload }"
}];
}
[instrument deregisterInstrumentors];
}

/** Tests that the called delegate selector is wrapped and calls through. */
- (void)testProxyDelegateURLSessionDownloadDidReceiveResponseCompletionHandler {
FPRNSURLSessionInstrument *instrument;
NSURLSessionDataTask *dataTask;
@autoreleasepool {
instrument = [[FPRNSURLSessionInstrument alloc] init];
[instrument registerInstrumentors];
FPRNSURLSessionCompleteTestDelegate *delegate =
[[FPRNSURLSessionCompleteTestDelegate alloc] init];
FPRNSURLSessionDelegateProxy *proxyDelegate = [[FPRNSURLSessionDelegateProxy alloc] initWithDelegate:delegate];
NSURLSessionConfiguration *configuration =
[NSURLSessionConfiguration defaultSessionConfiguration];
NSURLSession *session = [NSURLSession sessionWithConfiguration:configuration
delegate:proxyDelegate
delegateQueue:nil];
NSURL *URL = [self.testServer.serverURL URLByAppendingPathComponent:@"testBigDownload"];
dataTask = [session dataTaskWithURL:URL];
[dataTask resume];
XCTAssertNotNil([FPRNetworkTrace networkTraceFromObject:dataTask]);
[self waitAndRunBlockAfterResponse:^(id self, GCDWebServerRequest *_Nonnull request,
GCDWebServerResponse *_Nonnull response) {
XCTAssertTrue(delegate.URLSessionDataTaskDidReceiveResponseCompletionHandlerCalled);
}];
}
[instrument deregisterInstrumentors];
}

/** Tests that the called delegate selector is wrapped and calls through. */
- (void)testDelegateURLSessionDownloadTaskDidResumeAtOffsetExpectedTotalBytes {
FPRNSURLSessionInstrument *instrument = [[FPRNSURLSessionInstrument alloc] init];
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,12 @@ NS_ASSUME_NONNULL_BEGIN
/** Set to YES when URLSession:dataTask:didReceiveData: is called, used for testing. */
@property(nonatomic) BOOL URLSessionDataTaskDidReceiveDataCalled;

/** Set to YES when
* URLSession:dataTask:didReceiveResponse:completionHandler: is called, used
* for testing.
*/
@property(nonatomic) BOOL URLSessionDataTaskDidReceiveResponseCompletionHandlerCalled;

/** Set to YES when URLSession:downloadTask:didFinishDownloadingToURL: is called, used for testing.
*/
@property(nonatomic) BOOL URLSessionDownloadTaskDidFinishDownloadingToURLCalled;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,13 @@ - (void)URLSession:(NSURLSession *)session
self.URLSessionDownloadTaskDidWriteDataTotalBytesWrittenTotalBytesCalled = YES;
}

- (void)URLSession:(NSURLSession *)session
dataTask:(NSURLSessionDataTask *)dataTask
didReceiveResponse:(NSURLResponse *)response
completionHandler:(void (^)(NSURLSessionResponseDisposition disposition))completionHandler {
self.URLSessionDataTaskDidReceiveResponseCompletionHandlerCalled = YES;
}

@end

@interface FPRNSURLSessionTestDownloadDelegate ()
Expand Down
Loading