Skip to content

Commit

Permalink
fix WebSocket close logic & tests
Browse files Browse the repository at this point in the history
  • Loading branch information
hiroshihorie committed Jan 1, 2024
1 parent 3c0e5ca commit 45550c1
Show file tree
Hide file tree
Showing 15 changed files with 42 additions and 36 deletions.
2 changes: 1 addition & 1 deletion Sources/LiveKit/Core/Engine+SignalClientDelegate.swift
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2023 LiveKit
* Copyright 2024 LiveKit
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down
2 changes: 1 addition & 1 deletion Sources/LiveKit/Core/Engine+TransportDelegate.swift
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2023 LiveKit
* Copyright 2024 LiveKit
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down
2 changes: 1 addition & 1 deletion Sources/LiveKit/Core/Engine.swift
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2023 LiveKit
* Copyright 2024 LiveKit
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down
2 changes: 1 addition & 1 deletion Sources/LiveKit/Core/Room+SignalClientDelegate.swift
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2023 LiveKit
* Copyright 2024 LiveKit
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down
2 changes: 1 addition & 1 deletion Sources/LiveKit/Core/Room.swift
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2023 LiveKit
* Copyright 2024 LiveKit
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down
2 changes: 1 addition & 1 deletion Sources/LiveKit/Core/SignalClient.swift
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2023 LiveKit
* Copyright 2024 LiveKit
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down
2 changes: 1 addition & 1 deletion Sources/LiveKit/Core/Transport.swift
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2023 LiveKit
* Copyright 2024 LiveKit
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down
2 changes: 1 addition & 1 deletion Sources/LiveKit/Extensions/CustomStringConvertible.swift
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2023 LiveKit
* Copyright 2024 LiveKit
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down
2 changes: 1 addition & 1 deletion Sources/LiveKit/Protocols/SignalClientDelegate.swift
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2023 LiveKit
* Copyright 2024 LiveKit
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down
2 changes: 1 addition & 1 deletion Sources/LiveKit/Support/AsyncCompleter.swift
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2023 LiveKit
* Copyright 2024 LiveKit
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down
2 changes: 1 addition & 1 deletion Sources/LiveKit/Support/AsyncQueueActor.swift
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2023 LiveKit
* Copyright 2024 LiveKit
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down
24 changes: 16 additions & 8 deletions Sources/LiveKit/Support/WebSocket.swift
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2023 LiveKit
* Copyright 2024 LiveKit
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -68,10 +68,10 @@ class WebSocket: NSObject, Loggable, AsyncSequence, URLSessionWebSocketDelegate
}

func close() {
task.cancel(with: .goingAway, reason: nil)
task.cancel(with: .normalClosure, reason: nil)
connectContinuation?.resume(throwing: LiveKitError(.cancelled))
connectContinuation = nil
streamContinuation?.finish()
streamContinuation?.finish(throwing: LiveKitError(.cancelled))
streamContinuation = nil
}

Expand All @@ -83,7 +83,7 @@ class WebSocket: NSObject, Loggable, AsyncSequence, URLSessionWebSocketDelegate

private func waitForNextValue() {
guard task.closeCode == .invalid else {
streamContinuation?.finish()
streamContinuation?.finish(throwing: LiveKitError(.invalidState))
streamContinuation = nil
return
}
Expand All @@ -98,7 +98,7 @@ class WebSocket: NSObject, Loggable, AsyncSequence, URLSessionWebSocketDelegate
continuation.yield(message)
self?.waitForNextValue()
} catch {
continuation.finish(throwing: error)
continuation.finish(throwing: LiveKitError.from(error: error))
self?.streamContinuation = nil
}
})
Expand All @@ -119,10 +119,18 @@ class WebSocket: NSObject, Loggable, AsyncSequence, URLSessionWebSocketDelegate
}

func urlSession(_: URLSession, task _: URLSessionTask, didCompleteWithError error: Error?) {
log("didCompleteWithError: \(String(describing: error))", .error)
connectContinuation?.resume(throwing: LiveKitError.from(error: error) ?? LiveKitError(.unknown))
log("didCompleteWithError: \(String(describing: error))", error != nil ? .error : .debug)

if let error {
let lkError = LiveKitError.from(error: error) ?? LiveKitError(.unknown)
connectContinuation?.resume(throwing: lkError)
streamContinuation?.finish(throwing: lkError)
} else {
connectContinuation?.resume()
streamContinuation?.finish()
}

connectContinuation = nil
streamContinuation?.finish()
streamContinuation = nil
}
}
4 changes: 2 additions & 2 deletions Tests/LiveKitTests/AsyncRetryTests.swift
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2023 LiveKit
* Copyright 2024 LiveKit
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand All @@ -25,7 +25,7 @@ class AsyncRetryTests: XCTestCase {
func testRetry1() async throws {
let test = Task.retrying(maxRetryCount: 1) { totalAttempts, currentAttempt in
print("[TEST] Retrying with remaining attemps: \(currentAttempt)/\(totalAttempts)...")
throw EngineError.state(message: "Test error")
throw LiveKitError(.invalidState, message: "Test error")
return "Complete"
}

Expand Down
12 changes: 6 additions & 6 deletions Tests/LiveKitTests/CompleterTests.swift
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2023 LiveKit
* Copyright 2024 LiveKit
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand All @@ -26,13 +26,13 @@ class CompleterTests: XCTestCase {
let completer = AsyncCompleter<Void>(label: "Test01", timeOut: .seconds(1))
do {
try await completer.wait()
} catch AsyncCompleterError.timedOut {
} catch let error as LiveKitError where error.type == .timedOut {
print("Timed out 1")
}
// Re-use
do {
try await completer.wait()
} catch AsyncCompleterError.timedOut {
} catch let error as LiveKitError where error.type == .timedOut {
print("Timed out 2")
}
}
Expand All @@ -53,14 +53,14 @@ class CompleterTests: XCTestCase {
// Cancel after 1 second
try await Task.sleep(until: .now + .seconds(1), clock: .continuous)
print("Task 2: Cancelling completer...")
completer.cancel()
completer.reset()
}

try await group.waitForAll()
}
} catch let error as AsyncCompleterError where error == .timedOut {
} catch let error as LiveKitError where error.type == .timedOut {
print("Completer timed out")
} catch let error as AsyncCompleterError where error == .cancelled {
} catch let error as LiveKitError where error.type == .cancelled {
print("Completer cancelled")
} catch {
print("Unknown error: \(error)")
Expand Down
16 changes: 7 additions & 9 deletions Tests/LiveKitTests/WebSocketTests.swift
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2023 LiveKit
* Copyright 2024 LiveKit
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand All @@ -26,22 +26,20 @@ class WebSocketTests: XCTestCase {
print("Connecting...")
let socket = try await WebSocket(url: URL(string: "wss://socketsbay.com/wss/v2/1/demo/")!)

print("Connected. Waiting for messages...")
print("Connected, waiting for messages...")
do {
for try await message in socket {
switch message {
case let .string(string):
print(string)
case let .data(data):
print(data)
@unknown default:
print("unknown message received")
case let .string(string): print("Received String: \(string)")
case let .data(data): print("Received Data: \(data)")
@unknown default: print("Received unknown message")
}
}
} catch {
print("Error: \(error)")
throw error
}

print("Completed.")
print("Completed")
}
}

0 comments on commit 45550c1

Please sign in to comment.