Skip to content

Updating PR 299 (NSURLSession) to work with the latest Foundation #426

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

Merged
merged 1 commit into from
Aug 23, 2016
Merged

Updating PR 299 (NSURLSession) to work with the latest Foundation #426

merged 1 commit into from
Aug 23, 2016

Conversation

pushkarnk
Copy link
Member

@pushkarnk pushkarnk commented Jun 28, 2016

Creating this PR for review, on @parkera 's suggestion. The changes that this PR does to pull request 299 include:

  1. Rebasing to the latest Foundation
  2. Addition of support for download task and upload task (with a completion handler)
  3. Enabling building on Ubuntu 14.04 (which has an older libcurl by default)
  4. Renaming internal types (classes, struct, enums and protocols) to start with _

Work in progress:

  1. Unit tests contributed to TestFoundation as a part of pull request 299 - these utilise types internal to Foundation - don't build on Linux. They need rework.
  2. Replacing uses of dispatch_data by NSData/Data backed by dispatch_data

Kindly review and comment. Thanks.

let errorCode: Int?
}
}
*/
Copy link
Contributor

Choose a reason for hiding this comment

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

Let's not check in commented out code, since its purpose is unclear.

@pushkarnk
Copy link
Member Author

Thanks for the comments. I started with a round of deleting commented code with no message.

@pushkarnk
Copy link
Member Author

I addressed a few of the comments today:

  1. Use of String.init?(bytes:encoding:) and String.init?(cString:encoding:) to create Strings from char pointers received from the CURL layer
  2. Adhering to the new naming conventions
  3. Moved some constants in HTTMessage inside a struct
  4. Replaced the use of a [UnicodeScalar] by NSCharacterSet

]

if "XCTEST_BUILD_DIR" in Configuration.current.variables:
swift_cflags += [
'-I${XCTEST_BUILD_DIR}',
'-L${XCTEST_BUILD_DIR}',
'-I${SYSROOT}/usr/include/libxml2'
'-I${SYSROOT}/usr/include/libxml2',
'-I/usr/include/curl'
Copy link
Contributor

Choose a reason for hiding this comment

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

This should also be ${SYSROOT} attached just like the libxml case so that way cross compile development can specify a root

@pushkarnk
Copy link
Member Author

More changes:

  1. Tried to replace CInt with Int but that can't be done everywhere since libcurl accepts and returns Int32s.
  2. Replace NSURL with struct URL.

@pushkarnk
Copy link
Member Author

I have replaced Unsafe pointers with struct Data in all places except for the libcurl callbacks

@parkera
Copy link
Contributor

parkera commented Jul 6, 2016

Ok - I want to merge this but I have one strict requirement - we need at least the start of a test suite. Is it possible to get some kind of straightforward loop-back test in place so that we know we aren't going to accidentally break this with changes in the future?

@pushkarnk
Copy link
Member Author

Sure @parkera I will work on getting some of Daniel's tests running.

@pushkarnk
Copy link
Member Author

pushkarnk commented Jul 8, 2016

@parkera @phausler : Whats the equivalent of Xcode's "Enable Testability" on Linux? All of Daniel's test cases could be added here if we could get the @testable import Foundation to compile, since most of his tests use internal classes. I see this error currently:

TestFoundation/TestNSURLSession.swift:17:22: error: module 'Foundation' was not compiled for testing
    @testable import Foundation

@parkera
Copy link
Contributor

parkera commented Jul 8, 2016

Isn't the main purpose of @testable to expose some otherwise-private implementation details? Do the tests use those, or do they primarily test the public interface?

@pushkarnk
Copy link
Member Author

pushkarnk commented Jul 8, 2016

Some tests use the internal classes to test the public API. Also, there are tests for the internal classes too. We'd need @testable for both

@parkera
Copy link
Contributor

parkera commented Jul 8, 2016

IIRC the challenge with @testable on Linux was that the build bot would have to build it this way when running its own tests, but we don't want that to be the way it's built for final consumption...

@pushkarnk
Copy link
Member Author

pushkarnk commented Jul 8, 2016

I see that -enable-testing is the flag. If you think using this isn't a good idea, the other option is to rewrite the tests.

@danieleggert
Copy link
Contributor

Just seeing this now. I’ve started rebasing my old PR, too.

@pushkarnk
Copy link
Member Author

@seabaylea @parkera - As per your suggestions I have done the following:

  1. Migrated to the new Dispatch API
  2. Added 6 basic tests (4 data task + 2 download task) with external URLs - TestURLSession.swift. I think uploadTask tests need to be done via the loopback way.
  3. Given that (2) is a stopgap way of testing URLSession, opened a Jira task requesting for loopback tests contributed from the community - https://bugs.swift.org/browse/SR-2118

I've also rebased to the latest Foundation.

@parkera
Copy link
Contributor

parkera commented Jul 20, 2016

Awesome. Kicking off a test.

@parkera
Copy link
Contributor

parkera commented Jul 20, 2016

@swift-ci please test

@seabaylea
Copy link
Contributor

@parkera "We're going to need a remote URL that's as close to 100% reliable as possible." - agreed, as well as building in some resiliency to ignore requests that timeout (rather than an actual failure).

@parkera
Copy link
Contributor

parkera commented Aug 22, 2016

Hm, it failed again

Test Case 'TestURLSession.test_dataTaskWithURLRequest' started at 16:00:24.139
TestFoundation/TestNSURLSession.swift:72: error: TestURLSession.test_dataTaskWithURLRequest : Asynchronous wait failed - Exceeded timeout of 5.0 seconds, with unfulfilled expectations: data task
TestFoundation/TestNSURLSession.swift:73: error: TestURLSession.test_dataTaskWithURLRequest : XCTAssertEqual failed: ("unknown") is not equal to ("Lima") - test_dataTaskWithURLRequest returned an unexpected result

@seabaylea
Copy link
Contributor

I'm taking a look at whether we can ignore cases where it times out - unfortunately timeout isn't yet implemented (although that looks pretty straightforward to add).

@phausler
Copy link
Contributor

I was getting similar failures when I was merging it in previously. IIRC it had to do with the JSON getting emitted differently from the id to Any conversions

@phausler
Copy link
Contributor

Also this PR really needs updates to the xcode project; we should be able to test and run this on Darwin in addition to Linux.

@jrose-apple
Copy link
Contributor

Ah, is the Dispatch overlay also rebuilt before you recompile Foundation? I remember we had odd ordering around that at one point.

@parkera
Copy link
Contributor

parkera commented Aug 22, 2016

@jrose-apple I'm not sure how to tell. Can you look at the build log and let me know what you think?

@seabaylea
Copy link
Contributor

@parkera @jrose-apple whilst foundation is in build-presets.ini before libdispatch (I've always done it the other way in local builds), from the CI build log it looks to me like libdispatch, including the overlay, builds first regardless.

@seabaylea
Copy link
Contributor

If we want to add timeout support, and use that to add either retry logic or ignore the test, it looks like we just need to add the following at line 243 in MultiHandle.swift:

        case (CFURLSessionEasyCodeCOULDNT_CONNECT, ETIMEDOUT):
            return NSURLErrorTimedOut

@jrose-apple
Copy link
Contributor

Okay, yeah, it looks like it's building in the right order. Actually, it looks like it's finding the dispatch headers, but not the overlay. Maybe that's not in the include paths for swiftpm?

@jrose-apple
Copy link
Contributor

(The reason the compiler doesn't catch the missing overlay is because I wanted people to feel free to move things between their Swift and C code when it was safe to do so.)

@parkera
Copy link
Contributor

parkera commented Aug 22, 2016

@pushkarnk Can you update the PR as @seabaylea recommends? Once the test passes reliably we can merge this.

@seabaylea
Copy link
Contributor

@jrose-apple thanks - that was it. I've raised PRs against SwiftPM and XCTest to add the build-time location of the Swift 3 overlay to their build/test-time include paths:
swiftlang/swift-package-manager#615
swiftlang/swift-corelibs-xctest#169

@jrose-apple
Copy link
Contributor

How does this work when we build a toolchain package? Is the Dispatch overlay installed next to the standard library as it is on Apple platforms?

@seabaylea
Copy link
Contributor

For the toolchain we install:

./lib/swift/dispatch/dispatch.h
./lib/swift/linux/libdispatch.la
./lib/swift/linux/libdispatch.so
./lib/swift/shims/DispatchShims.h
./lib/swift/linux/x86_64/Dispatch.swiftdoc
./lib/swift/linux/x86_64/Dispatch.swiftmodule

which matches the location of the other libraries

Also includes support for downloadTask and uploadTask with Data and a completion handler.
The original PR 299 doesn't build on Ubuntu 14.04, due to an older level of libcurl. This
rework removes some of that code (it does not affect any functionality). Updated to use the
Dispatch 3.0 API. Includes basic tests with external URLs.
@pushkarnk
Copy link
Member Author

@parkera , @seabaylea : I have added some resiliency in the tests by handling timeouts. I also had to add a basic implementation for URLSessionConfiguration.timeoutIntervalForRequest

Can we test again please, to see how things go?

@parkera
Copy link
Contributor

parkera commented Aug 23, 2016

@swift-ci please test linux

@parkera
Copy link
Contributor

parkera commented Aug 23, 2016

It passed!

@parkera parkera merged commit c26f465 into swiftlang:master Aug 23, 2016
@pushkarnk
Copy link
Member Author

Thanks @parkera

@phausler - I'll work on updating the Xcode project next.

@pushkarnk
Copy link
Member Author

Appears #586 does it. Thanks.

@pushkarnk pushkarnk deleted the nsurlsession-new branch August 24, 2016 06:37
@modocache
Copy link
Contributor

One of the tests added in this pull request, TestURLSession.test_dataTaskWithURLCompletionHandler, has begun failing. Apparently when this test was written, https://restcountries.eu/rest/v1/name/USA?fullText=true returned "Washington D.C.". It now returns "Washington, D.C.".

/cc @pushkarnk @parkera

@parkera
Copy link
Contributor

parkera commented Aug 24, 2016

It looks like #426 addresses that.

@modocache
Copy link
Contributor

Yup, my bad. Sorry for the noise!

@parkera
Copy link
Contributor

parkera commented Aug 24, 2016

Sorry, it's #590 actually.

@tanner0101
Copy link

Will this PR be included in the swift-3.0-branch?

@parkera
Copy link
Contributor

parkera commented Aug 25, 2016

We're planning on merging master to swift-3.0-branch once things are settled down a bit.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

10 participants