Browse Source

Another attempt at stable ordering.

Jon Shier 2 years ago
parent
commit
934e5a734b
3 changed files with 11 additions and 4 deletions
  1. 4 4
      Source/Request.swift
  2. 1 0
      Source/SessionDelegate.swift
  3. 6 0
      Tests/WebSocketTests.swift

+ 4 - 4
Source/Request.swift

@@ -1843,7 +1843,8 @@ public final class WebSocketRequest: Request {
 
         socketMutableState.read { state in
             state.handlers.forEach { handler in
-                handler.queue.async { handler.handler(.connected(protocol: `protocol`)) }
+                // Saved handler calls out to serializationQueue immediately, then to handler's queue.
+                handler.handler(.connected(protocol: `protocol`))
             }
         }
 
@@ -1914,9 +1915,8 @@ public final class WebSocketRequest: Request {
         cancelTimedPing()
         socketMutableState.read { state in
             state.handlers.forEach { handler in
-                handler.queue.async {
-                    handler.handler(.disconnected(closeCode: closeCode, reason: reason))
-                }
+                // Saved handler calls out to serializationQueue immediately, then to handler's queue.
+                handler.handler(.disconnected(closeCode: closeCode, reason: reason))
             }
         }
     }

+ 1 - 0
Source/SessionDelegate.swift

@@ -212,6 +212,7 @@ extension SessionDelegate: URLSessionTaskDelegate {
     }
 
     open func urlSession(_ session: URLSession, task: URLSessionTask, didCompleteWithError error: Error?) {
+//        NSLog("URLSession: \(session), task: \(task), didCompleteWithError: \(error)")
         eventMonitor?.urlSession(session, task: task, didCompleteWithError: error)
 
         let request = stateProvider?.request(for: task)

+ 6 - 0
Tests/WebSocketTests.swift

@@ -16,6 +16,12 @@ import XCTest
 final class WebSocketTests: BaseTestCase {
 //    override var skipVersion: SkipVersion { .twenty }
 
+//    func testMany() {
+//        for _ in 0..<5000 {
+//            testThatWebSocketsCanReceiveMessageEvents()
+//        }
+//    }
+
     func testThatWebSocketsCanReceiveMessageEvents() {
         // Given
         let didConnect = expectation(description: "didConnect")