-
Notifications
You must be signed in to change notification settings - Fork 602
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
Redirect stubbing not working reliably #230
Comments
🤔 Interesting, thanks for the report. |
Hey @nevil did you manage to investigate this issue further and understand what was causing it? |
@AliSoftware This does slow down my unit test execution a bit (though not more than 10 seconds :-) ) so I hope to have time to continue investigating soon. |
Hey @nevil ! Any chance you'll have time to reopen the investigation anytime soon, or maybe at least re-test with a more recent iOS/SDK/Xcode to see if we can close this? |
I've encountered the same issue. Here's a commit adding a unit test that semi-reliably reproduces the issue. I've been able to reproduce the issue in the iOS 11 simulator and directly in macOS. I'm using Xcode 9.3 on macOS 10.13.4. You might have to run the new unit tests several times to see a failure. When the failure happens, the 302 redirect is not followed. There's no response object, but there's also no error object. The body of the 302 response is returned. |
I don't have a full investigation for this issue but It seemed to me like the timing of the call of the completionHandler from For example, if I take your test, add a delegate, and implement it like below then the redirect test never fails. - (void)URLSession:(NSURLSession *)session task:(NSURLSessionTask *)task willPerformHTTPRedirection:(NSHTTPURLResponse *)response newRequest:(NSURLRequest *)request completionHandler:(void (^)(NSURLRequest * _Nullable))completionHandler {
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
usleep(1000);
completionHandler(request);
});
} |
I've been able to reproduce the issue without any OHHTTPStubs code at all. I suspect there's a race condition in Apple's implementation of In the attached project (RedirectBugDemo.zip), The unit tests run a simple The test without the delay fails on average once per 100,000 runs. The test with the delay has not failed in over 1,000,000 runs. I've submitted a Radar and cloned it to OpenRadar as well. |
Thanks a lot @morrowa for all the investigative work, that's very interesting! |
First of all, thanks for this great tool!
Sorry for the disorganized report
I've been having an issue that our redirect unit tests occasionally fail.
In the latest release I saw a fix that I was hoping to solve my problem ( #224) but unfortunately not.
Now I have a way to make the redirect stubbing fail reliably. A small modification to OHHTTPStubs.m:
With the above we get the failure 100% of the time.
I added a test to NSURLSessionTests.m:
After inserting the sleep the data task completion handler starts receiving the redirect response instead of the response after the redirect.
I can fix it by not calling didReceiveResponse for the redirect (301 code) but based on #175 this does not seem to be a valid solution
I will investigate further but wanted to create a ticket in case anyone already have a clue about the root cause.
The text was updated successfully, but these errors were encountered: