Skip to content

Commit

Permalink
Fix test execution (#600)
Browse files Browse the repository at this point in the history
Test execution currently fails due to logging being bootstrapped both in
`AudioEngineTests` and `VideoViewTests`. This PR defines `LKTestCase`, a
subclass of `XCTestCase`, which performs global initialization once per
execution. All existing test cases now use this as their superclass.
  • Loading branch information
ladvoc authored Feb 22, 2025
1 parent 61e5bd1 commit 0440e22
Show file tree
Hide file tree
Showing 34 changed files with 66 additions and 42 deletions.
2 changes: 1 addition & 1 deletion Tests/LiveKitTests/AVAudioPCMRingBufferTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ import XCTest
import CoreAudio
#endif

final class AVAudioPCMRingBufferTests: XCTestCase {
final class AVAudioPCMRingBufferTests: LKTestCase {
var format: AVAudioFormat!

override func setUp() {
Expand Down
2 changes: 1 addition & 1 deletion Tests/LiveKitTests/AsyncRetryTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
@testable import LiveKit
import XCTest

class AsyncRetryTests: XCTestCase {
class AsyncRetryTests: LKTestCase {
override func setUpWithError() throws {}

override func tearDown() async throws {}
Expand Down
2 changes: 1 addition & 1 deletion Tests/LiveKitTests/AudioConverterTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ import AVFAudio
@testable import LiveKit
import XCTest

class AudioConverterTests: XCTestCase {
class AudioConverterTests: LKTestCase {
func testConvertFormat() async throws {
// Sample audio
let audioDownloadUrl = URL(string: "https://github.com/rafaelreis-hotmart/Audio-Sample-files/raw/refs/heads/master/sample.wav")!
Expand Down
7 changes: 1 addition & 6 deletions Tests/LiveKitTests/AudioEngineTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,7 @@
import LiveKitWebRTC
import XCTest

class AudioEngineTests: XCTestCase {
override class func setUp() {
LiveKitSDK.setLoggerStandardOutput()
RTCSetMinDebugLogLevel(.info)
}

class AudioEngineTests: LKTestCase {
override func tearDown() async throws {}

#if !targetEnvironment(simulator)
Expand Down
2 changes: 1 addition & 1 deletion Tests/LiveKitTests/AudioProcessingTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ import LiveKitWebRTC
@testable import LiveKit
import XCTest

class AudioProcessingTests: XCTestCase, AudioCustomProcessingDelegate {
class AudioProcessingTests: LKTestCase, AudioCustomProcessingDelegate {
var _initSampleRate: Double = 0.0
var _initChannels: Int = 0

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
@testable import LiveKit
import XCTest

final class BroadcastImageCodecTests: XCTestCase {
final class BroadcastImageCodecTests: LKTestCase {
private var codec: BroadcastImageCodec!

override func setUpWithError() throws {
Expand Down
2 changes: 1 addition & 1 deletion Tests/LiveKitTests/Broadcast/IPCChannelTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
import Network
import XCTest

final class IPCChannelTests: XCTestCase {
final class IPCChannelTests: LKTestCase {
private var socketPath: SocketPath!

enum TestSetupError: Error {
Expand Down
2 changes: 1 addition & 1 deletion Tests/LiveKitTests/Broadcast/SocketPathTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
@testable import LiveKit
import XCTest

final class SocketPathTests: XCTestCase {
final class SocketPathTests: LKTestCase {
func testValid() throws {
let path = "/tmp/a.sock"
let socketPath = try XCTUnwrap(SocketPath(path))
Expand Down
2 changes: 1 addition & 1 deletion Tests/LiveKitTests/BroadcastManagerTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ import Combine
@testable import LiveKit
import XCTest

class BroadcastManagerTests: XCTestCase {
class BroadcastManagerTests: LKTestCase {
private var manager: BroadcastManager!

override func setUp() {
Expand Down
2 changes: 1 addition & 1 deletion Tests/LiveKitTests/CompleterTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
@testable import LiveKit
import XCTest

class CompleterTests: XCTestCase {
class CompleterTests: LKTestCase {
override func setUpWithError() throws {}

override func tearDown() async throws {}
Expand Down
2 changes: 1 addition & 1 deletion Tests/LiveKitTests/DarwinNotificationCenterTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ import Combine
@testable import LiveKit
import XCTest

class DarwinNotificationCenterTests: XCTestCase {
class DarwinNotificationCenterTests: LKTestCase {
func testPublisher() throws {
let receiveFirst = XCTestExpectation(description: "Receive from 1st subscriber")
let receiveSecond = XCTestExpectation(description: "Receive from 2nd subscriber")
Expand Down
2 changes: 1 addition & 1 deletion Tests/LiveKitTests/DeviceManager.swift
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
@testable import LiveKit
import XCTest

class DeviceManagerTests: XCTestCase {
class DeviceManagerTests: LKTestCase {
func testListDevices() async throws {
let devices = try await DeviceManager.shared.devices()
print("Devices: \(devices.map { "(facingPosition: \(String(describing: $0.facingPosition)))" }.joined(separator: ", "))")
Expand Down
2 changes: 1 addition & 1 deletion Tests/LiveKitTests/E2EE/Thread.swift
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
import LiveKitWebRTC
import XCTest

class E2EEThreadTests: XCTestCase {
class E2EEThreadTests: LKTestCase {
// Attempt to crash LKRTCFrameCryptor initialization
func testCreateFrameCryptor() async throws {
// Create peerConnection
Expand Down
2 changes: 1 addition & 1 deletion Tests/LiveKitTests/Extensions/AVAudioPCMBufferTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ import AVFoundation
@testable import LiveKit
import XCTest

class AVAudioPCMBufferTests: XCTestCase {
class AVAudioPCMBufferTests: LKTestCase {
func testResample() {
// Test case 1: Resample to a higher sample rate
testResampleHelper(fromSampleRate: 44100, toSampleRate: 48000, expectedSuccess: true)
Expand Down
2 changes: 1 addition & 1 deletion Tests/LiveKitTests/Extensions/StringTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
@testable import LiveKit
import XCTest

final class StringTests: XCTestCase {
final class StringTests: LKTestCase {
func testByteLength() {
// ASCII characters (1 byte each)
XCTAssertEqual("hello".byteLength, 5)
Expand Down
2 changes: 1 addition & 1 deletion Tests/LiveKitTests/FunctionTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
@testable import LiveKit
import XCTest

class FunctionTests: XCTestCase {
class FunctionTests: LKTestCase {
func testRangeMerge() async throws {
let range1 = 10 ... 20
let range2 = 5 ... 15
Expand Down
33 changes: 33 additions & 0 deletions Tests/LiveKitTests/LKTestCase.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
/*
* Copyright 2025 LiveKit
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

@testable import LiveKit
import LiveKitWebRTC
import XCTest

/// Subclass of XCTestCase that performs global initialization.
class LKTestCase: XCTestCase {
private static let _globalSetup: Bool = {
LiveKitSDK.setLoggerStandardOutput()
RTCSetMinDebugLogLevel(.info)
return true
}()

override func setUp() {
assert(Self._globalSetup, "Global initialization failed")
super.setUp()
}
}
2 changes: 1 addition & 1 deletion Tests/LiveKitTests/MuteTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,7 @@ let muteEngineSteps: [TestEngineStep] = [
TestEngineStep(transition: .init(outputEnabled: .value(false)), assert: .init(engineRunning: false)),
]

class MuteTests: XCTestCase {
class MuteTests: LKTestCase {
override func setUp() {
super.setUp()
continueAfterFailure = false
Expand Down
2 changes: 1 addition & 1 deletion Tests/LiveKitTests/ObjCHelpersTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
import LKObjCHelpers
import XCTest

class ObjCHelperTests: XCTestCase {
class ObjCHelperTests: LKTestCase {
func testHelper() {
LKObjCHelpers.finishBroadcastWithoutError(nil)
}
Expand Down
2 changes: 1 addition & 1 deletion Tests/LiveKitTests/ParticipantTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
@testable import LiveKit
import XCTest

class ParticipantTests: XCTestCase {
class ParticipantTests: LKTestCase {
func testLocalParticipantIdentity() async throws {
try await withRooms([RoomTestingOptions()]) { rooms in
// Alias to Room
Expand Down
2 changes: 1 addition & 1 deletion Tests/LiveKitTests/PublishBufferCapturerTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ import AVFoundation
@testable import LiveKit
import XCTest

class PublishBufferCapturerTests: XCTestCase {
class PublishBufferCapturerTests: LKTestCase {
func testPublishBufferTrack() async throws {
try await withRooms([RoomTestingOptions(canPublish: true), RoomTestingOptions(canSubscribe: true)]) { rooms in
// Alias to Rooms
Expand Down
2 changes: 1 addition & 1 deletion Tests/LiveKitTests/PublishDataTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
@testable import LiveKit
import XCTest

class PublishDataTests: XCTestCase {
class PublishDataTests: LKTestCase {
// Test with canSubscribe: true
func testPublishDataReceiverCanSubscribe() async throws {
try await _publishDataTest(receiverRoomOptions: RoomTestingOptions(canSubscribe: true))
Expand Down
2 changes: 1 addition & 1 deletion Tests/LiveKitTests/PublishMicrophoneTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ import CoreMedia
@testable import LiveKit
import XCTest

class PublishMicrophoneTests: XCTestCase {
class PublishMicrophoneTests: LKTestCase {
func testConcurrentMicPublish() async throws {
try await withRooms([RoomTestingOptions(canPublish: true)]) { rooms in
// Alias to Room
Expand Down
2 changes: 1 addition & 1 deletion Tests/LiveKitTests/QueueActorTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
@testable import LiveKit
import XCTest

class QueueActorTests: XCTestCase {
class QueueActorTests: LKTestCase {
private lazy var queue = QueueActor<String> { print($0) }

override func setUpWithError() throws {}
Expand Down
2 changes: 1 addition & 1 deletion Tests/LiveKitTests/RoomTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
@testable import LiveKit
import XCTest

class RoomTests: XCTestCase {
class RoomTests: LKTestCase {
func testRoomProperties() async throws {
try await withRooms([RoomTestingOptions()]) { rooms in
// Alias to Room
Expand Down
2 changes: 1 addition & 1 deletion Tests/LiveKitTests/RpcTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
@testable import LiveKit
import XCTest

class RpcTests: XCTestCase {
class RpcTests: LKTestCase {
// Mock DataChannelPair to intercept outgoing packets
class MockDataChannelPair: DataChannelPair {
var packetHandler: (Livekit_DataPacket) -> Void
Expand Down
2 changes: 1 addition & 1 deletion Tests/LiveKitTests/SDKTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
@testable import LiveKit
import XCTest

class SDKTests: XCTestCase {
class SDKTests: LKTestCase {
func testReadVersion() {
print("LiveKitSDK.version: \(LiveKitSDK.version)")
}
Expand Down
2 changes: 1 addition & 1 deletion Tests/LiveKitTests/SerialRunnerActor.swift
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
@testable import LiveKit
import XCTest

class SerialRunnerActorTests: XCTestCase {
class SerialRunnerActorTests: LKTestCase {
let serialRunner = SerialRunnerActor<Void>()
var counterValue: Int = 0
var resultValues: [String] = []
Expand Down
2 changes: 1 addition & 1 deletion Tests/LiveKitTests/Support/Room.swift
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ struct RoomTestingOptions {
}
}

extension XCTestCase {
extension LKTestCase {
private func readEnvironmentString(for key: String, defaultValue: String) -> String {
if let string = ProcessInfo.processInfo.environment[key]?.trimmingCharacters(in: .whitespacesAndNewlines), !string.isEmpty {
return string
Expand Down
2 changes: 1 addition & 1 deletion Tests/LiveKitTests/Support/Tracks.swift
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ import AVFoundation
@testable import LiveKit
import XCTest

extension XCTestCase {
extension LKTestCase {
// Creates a LocalVideoTrack with BufferCapturer, generates frames for approx 30 seconds
func createSampleVideoTrack(targetFps: Int = 30, _ onCapture: @escaping (CMSampleBuffer) -> Void) async throws -> (Task<Void, any Error>) {
// Sample video
Expand Down
2 changes: 1 addition & 1 deletion Tests/LiveKitTests/Support/Xcode14.2Backport.swift
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ public extension URLSession {

// Support for Xcode 14.2
#if !compiler(>=5.8)
extension XCTestCase {
extension LKTestCase {
func fulfillment(of expectations: [XCTestExpectation], timeout: TimeInterval, enforceOrder: Bool = false) async {
await withCheckedContinuation { continuation in
// This function operates by blocking a background thread instead of one owned by libdispatch or by the
Expand Down
2 changes: 1 addition & 1 deletion Tests/LiveKitTests/ThreadSafetyTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
@testable import LiveKit
import XCTest

class ThreadSafetyTests: XCTestCase {
class ThreadSafetyTests: LKTestCase {
struct TestState: Equatable {
var dictionary = [String: String]()
var counter = 0
Expand Down
2 changes: 1 addition & 1 deletion Tests/LiveKitTests/TrackTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ class TestTrack: LocalAudioTrack {
}
}

class TrackTests: XCTestCase {
class TrackTests: LKTestCase {
#if os(iOS) || os(visionOS) || os(tvOS)
func testConcurrentStartStop() async throws {
// Set config func to watch state changes.
Expand Down
6 changes: 1 addition & 5 deletions Tests/LiveKitTests/VideoViewTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,7 @@
@testable import LiveKit
import XCTest

class VideoViewTests: XCTestCase {
override class func setUp() {
LiveKitSDK.setLoggerStandardOutput()
}

class VideoViewTests: LKTestCase {
/// Test if avSampleBufferDisplayLayer is available immediately after creating VideoView.
@MainActor
func testAVSampleBufferDisplayLayer() {
Expand Down

0 comments on commit 0440e22

Please sign in to comment.