Skip to content
Merged
Show file tree
Hide file tree
Changes from all 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
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,24 @@ public class AppSyncWebSocketClient: NSObject, ApolloWebSocket.WebSocketClient,

// MARK: - ApolloWebSocket.WebSocketClient

public var request: URLRequest
public var delegate: ApolloWebSocket.WebSocketClientDelegate?
public var callbackQueue: DispatchQueue

private let requestLock = NSLock()
private var _request: URLRequest
public var request: URLRequest {
get {
requestLock.lock()
defer { requestLock.unlock() }
return _request
}
set {
requestLock.lock()
defer { requestLock.unlock() }
_request = newValue
}
}

// MARK: - Public

public var publisher: AnyPublisher<AppSyncWebSocketEvent, Never> {
Expand Down Expand Up @@ -71,7 +85,7 @@ public class AppSyncWebSocketClient: NSObject, ApolloWebSocket.WebSocketClient,
callbackQueue: DispatchQueue,
authorizer: AppSyncAuthorizer
) {
self.request = URLRequest(url: appSyncRealTimeEndpoint(endpointURL))
self._request = URLRequest(url: appSyncRealTimeEndpoint(endpointURL))
self.delegate = delegate
self.callbackQueue = callbackQueue
self.authorizer = authorizer
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,37 @@ final class AppSyncWebSocketClientTests: XCTestCase {
await fulfillment(of: [messageReceivedExpectation], timeout: 5)
}

func testConcurrentURLRequestAccess() async {
let endpoint = URL(string: "https://abc.appsync-api.us-east-1.amazonaws.com/graphql")!

let websocket = AppSyncWebSocketClient(
endpointURL: endpoint,
authorizer: APIKeyAuthorizer(apiKey: "apiKey"))

let raceConditionExpectation = expectation(description: "Race condition test")
raceConditionExpectation.expectedFulfillmentCount = 2

let iterations = 1000

// Thread 1: Continuous reading
DispatchQueue.global(qos: .userInitiated).async {
for _ in 0..<iterations {
_ = websocket.request.value(forHTTPHeaderField: "test-header")
}
raceConditionExpectation.fulfill()
}

// Thread 2: Continuous writing
DispatchQueue.global(qos: .userInitiated).async {
for i in 0..<iterations {
websocket.request.setValue("value-\(i)", forHTTPHeaderField: "test-header")
}
raceConditionExpectation.fulfill()
}

await fulfillment(of: [raceConditionExpectation], timeout: 5)
}

private func verifyConnected(
_ webSocketClient: AppSyncWebSocketClient,
autoConnectOnNetworkStatusChange: Bool = false,
Expand Down
Loading