Merge pull request #2527 from drodriguez/tests-rewrite-cookie-tests

[test] Rewrite URLSession cookie tests to be independent.
diff --git a/CoreFoundation/URL.subproj/CFURLSessionInterface.c b/CoreFoundation/URL.subproj/CFURLSessionInterface.c
index 35f820e..31a91a0 100644
--- a/CoreFoundation/URL.subproj/CFURLSessionInterface.c
+++ b/CoreFoundation/URL.subproj/CFURLSessionInterface.c
@@ -110,10 +110,6 @@
     return MakeEasyCode(curl_easy_setopt(curl,  option.value, a));
 }
 
-CFURLSessionEasyCode CFURLSession_easy_setopt_csf(CFURLSessionEasyHandle _Nonnull curl, CFURLSessionOption option, int (*_Nonnull a)(void *_Nullable clientp, CFURLSession_socket_t)) {
-     return MakeEasyCode(curl_easy_setopt(curl, option.value, a));
-}
-
 CFURLSessionEasyCode CFURLSession_easy_getinfo_long(CFURLSessionEasyHandle _Nonnull curl, CFURLSessionInfo info, long *_Nonnull a) {
     return MakeEasyCode(curl_easy_getinfo(curl, info.value, a));
 }
diff --git a/CoreFoundation/URL.subproj/CFURLSessionInterface.h b/CoreFoundation/URL.subproj/CFURLSessionInterface.h
index 47cf08a..816212d 100644
--- a/CoreFoundation/URL.subproj/CFURLSessionInterface.h
+++ b/CoreFoundation/URL.subproj/CFURLSessionInterface.h
@@ -601,7 +601,7 @@
 CF_EXPORT CFURLSessionMultiCode CFURLSession_multi_setopt_l(CFURLSessionMultiHandle _Nonnull multi_handle, CFURLSessionMultiOption option, long a);
 CF_EXPORT CFURLSessionMultiCode CFURLSession_multi_setopt_sf(CFURLSessionMultiHandle _Nonnull multi_handle, CFURLSessionMultiOption option, int (*_Nonnull a)(CFURLSessionEasyHandle _Nonnull, CFURLSession_socket_t, int, void *_Nullable, void *_Nullable));
 CF_EXPORT CFURLSessionMultiCode CFURLSession_multi_setopt_tf(CFURLSessionMultiHandle _Nonnull multi_handle, CFURLSessionMultiOption option, int (*_Nonnull a)(CFURLSessionMultiHandle _Nonnull, long, void *_Nullable));
-CF_EXPORT CFURLSessionEasyCode CFURLSession_easy_setopt_csf(CFURLSessionEasyHandle _Nonnull curl, CFURLSessionOption option, int (*_Nonnull a)(void* _Nullable clientp, CFURLSession_socket_t));
+
 CF_EXPORT CFURLSessionEasyCode CFURLSessionInit(void);
 
 
diff --git a/Foundation/URLSession/libcurl/EasyHandle.swift b/Foundation/URLSession/libcurl/EasyHandle.swift
index 9ab572a..65e124c 100644
--- a/Foundation/URLSession/libcurl/EasyHandle.swift
+++ b/Foundation/URLSession/libcurl/EasyHandle.swift
@@ -486,14 +486,6 @@
                 return 1
             }
         }.asError()
-
-        try! CFURLSession_easy_setopt_csf(rawHandle, CFURLSessionOptionCLOSESOCKETFUNCTION) { (clientp: UnsafeMutableRawPointer?, socket: CFURLSession_socket_t) -> Int32 in
-            // Don't let CURL close the socket here because the
-            // dispatch sources are associated with it and we need to
-            // cancel them before closing the file descriptor.
-            return 0
-        }.asError()
-
         // seeking in input stream
         try! CFURLSession_easy_setopt_ptr(rawHandle, CFURLSessionOptionSEEKDATA, UnsafeMutableRawPointer(Unmanaged.passUnretained(self).toOpaque())).asError()
         try! CFURLSession_easy_setopt_seek(rawHandle, CFURLSessionOptionSEEKFUNCTION, { (userdata, offset, origin) -> Int32 in
diff --git a/Foundation/URLSession/libcurl/MultiHandle.swift b/Foundation/URLSession/libcurl/MultiHandle.swift
index 8a389c5..13d246e 100644
--- a/Foundation/URLSession/libcurl/MultiHandle.swift
+++ b/Foundation/URLSession/libcurl/MultiHandle.swift
@@ -122,9 +122,7 @@
         } else if socketSources != nil && action == .unregister {
             // We need to release the stored pointer:
             if let opaque = socketSourcePtr {
-                let s: Unmanaged<_SocketSources> = Unmanaged<_SocketSources>.fromOpaque(opaque)
-                s.takeUnretainedValue().tearDown(socket: socket, queue: queue)
-                s.release()
+                Unmanaged<_SocketSources>.fromOpaque(opaque).release()
             }
             socketSources = nil
         }
@@ -409,7 +407,6 @@
 fileprivate class _SocketSources {
     var readSource: DispatchSource?
     var writeSource: DispatchSource?
-    let activeSockets = DispatchGroup()
 
     func createReadSource(socket: CFURLSession_socket_t, queue: DispatchQueue, handler: DispatchWorkItem) {
         guard readSource == nil else { return }
@@ -418,13 +415,7 @@
 #else
         let s = DispatchSource.makeReadSource(fileDescriptor: socket, queue: queue)
 #endif
-        activeSockets.enter()
         s.setEventHandler(handler: handler)
-        s.setCancelHandler(handler: DispatchWorkItem { [weak self] in
-            guard let self = self else { return }
-            self.activeSockets.leave()
-        })
-
         readSource = s as? DispatchSource
         s.resume()
     }
@@ -436,26 +427,12 @@
 #else
         let s = DispatchSource.makeWriteSource(fileDescriptor: socket, queue: queue)
 #endif
-        activeSockets.enter()
-        s.setCancelHandler(handler: DispatchWorkItem { [weak self] in
-            guard let self = self else { return }
-            self.activeSockets.leave()
-        })
         s.setEventHandler(handler: handler)
         writeSource = s as? DispatchSource
         s.resume()
     }
 
-    func tearDown(socket: CFURLSession_socket_t, queue: DispatchQueue) {
-        activeSockets.notify(queue: queue) {
-            withExtendedLifetime(self) {
-#if os(Windows)
-                closesocket(socket)
-#else
-                close(socket)
-#endif
-            }
-        }
+    func tearDown() {
         if let s = readSource {
             s.cancel()
         }