Skip to content

Commit b50d508

Browse files
authored
Fix cron tests (#952)
1 parent fb2af35 commit b50d508

File tree

4 files changed

+53
-39
lines changed

4 files changed

+53
-39
lines changed

Sources/StreamVideo/Utils/IdleTimerAdapter/IdleTimerAdapter.swift

Lines changed: 17 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -4,37 +4,41 @@
44

55
// swiftlint:disable discourage_task_init
66

7+
import Combine
78
import Foundation
89
#if canImport(UIKit)
910
import UIKit
1011
#endif
1112

1213
final class IdleTimerAdapter: @unchecked Sendable {
1314

15+
private var observationCancellable: AnyCancellable?
1416
private(set) var isIdleTimerDisabled: Bool = false
15-
private let disposableBag = DisposableBag()
1617

17-
init(_ streamVideo: StreamVideo) {
18+
convenience init(_ streamVideo: StreamVideo) {
19+
self.init(streamVideo.state.$activeCall.eraseToAnyPublisher())
20+
}
21+
22+
init(_ publisher: AnyPublisher<Call?, Never>) {
1823
#if canImport(UIKit)
1924
Task { @MainActor in
2025
self.isIdleTimerDisabled = UIApplication.shared.isIdleTimerDisabled
2126
}
22-
streamVideo
23-
.state
24-
.$activeCall
27+
observationCancellable = publisher
2528
.map { $0 != nil }
26-
.sinkTask(storeIn: disposableBag) { @MainActor [weak self] in self?.didUpdate(hasActiveCall: $0) }
27-
.store(in: disposableBag)
29+
.receive(on: DispatchQueue.main)
30+
.sink { [weak self] hasActiveCall in
31+
MainActor.assumeIsolated { [weak self] in
32+
UIApplication.shared.isIdleTimerDisabled = hasActiveCall
33+
self?.isIdleTimerDisabled = hasActiveCall
34+
}
35+
}
2836
#endif
2937
}
3038

31-
// MARK: - Private helpers
32-
33-
@MainActor
34-
private func didUpdate(hasActiveCall: Bool) {
39+
deinit {
3540
#if canImport(UIKit)
36-
UIApplication.shared.isIdleTimerDisabled = hasActiveCall
37-
isIdleTimerDisabled = hasActiveCall
41+
Task { @MainActor in UIApplication.shared.isIdleTimerDisabled = false }
3842
#endif
3943
}
4044
}

StreamVideoTests/Utils/AudioSession/AudioRecorder/Namespace/Middleware/StreamCallAudioRecorder_ShouldRecordMiddlewareTests.swift

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,12 @@ final class StreamCallAudioRecorder_ShouldRecordMiddlewareTests: StreamVideoTest
1414

1515
private lazy var mockAudioStore: MockRTCAudioStore! = .init()
1616

17+
override func setUp() {
18+
super.setUp()
19+
_ = PermissionStore.currentValue
20+
_ = mockAudioStore
21+
}
22+
1723
override func tearDown() {
1824
mockAudioStore?.dismantle()
1925
mockAudioStore = nil
@@ -41,6 +47,7 @@ final class StreamCallAudioRecorder_ShouldRecordMiddlewareTests: StreamVideoTest
4147

4248
let call = await MockCall(.dummy())
4349
try await call.microphone.enable()
50+
await fulfilmentInMainActor { call.state.callSettings.audioOn }
4451
streamVideo.state.activeCall = call
4552

4653
await safeFulfillment(of: [validation])
@@ -64,6 +71,7 @@ final class StreamCallAudioRecorder_ShouldRecordMiddlewareTests: StreamVideoTest
6471

6572
let call = await MockCall(.dummy())
6673
try await call.microphone.enable()
74+
await fulfilmentInMainActor { call.state.callSettings.audioOn }
6775
streamVideo.state.activeCall = call
6876

6977
await wait(for: 0.1)
@@ -100,6 +108,7 @@ final class StreamCallAudioRecorder_ShouldRecordMiddlewareTests: StreamVideoTest
100108

101109
let call = await MockCall(.dummy())
102110
try await call.microphone.enable()
111+
await fulfilmentInMainActor { call.state.callSettings.audioOn }
103112
streamVideo.state.activeCall = call
104113

105114
await safeFulfillment(of: [validation])

StreamVideoTests/Utils/IdleTimerAdapter/IdleTimerAdapter_Tests.swift

Lines changed: 15 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,15 @@
22
// Copyright © 2025 Stream.io Inc. All rights reserved.
33
//
44

5+
import Combine
56
import Foundation
67
@testable import StreamVideo
78
import XCTest
89

910
final class IdleTimerAdapter_Tests: XCTestCase, @unchecked Sendable {
1011

11-
private var mockStreamVideo: MockStreamVideo! = .init()
12-
private lazy var subject: IdleTimerAdapter! = .init(mockStreamVideo)
12+
private var activeCallSubject: CurrentValueSubject<Call?, Never>! = .init(nil)
13+
private lazy var subject: IdleTimerAdapter! = .init(activeCallSubject.eraseToAnyPublisher())
1314

1415
override func setUp() {
1516
super.setUp()
@@ -18,37 +19,40 @@ final class IdleTimerAdapter_Tests: XCTestCase, @unchecked Sendable {
1819

1920
override func tearDown() {
2021
subject = nil
21-
mockStreamVideo = nil
22+
activeCallSubject.send(nil)
23+
activeCallSubject = nil
2224
super.tearDown()
2325
}
2426

2527
// MARK: - hasActiveCall
2628

2729
func test_hasActiveCall_isTrue_IdleTimerIsDisabled() async {
28-
mockStreamVideo.state.activeCall = .dummy()
30+
activeCallSubject.send(.dummy())
2931

30-
await fulfilmentInMainActor { self.subject.isIdleTimerDisabled == true }
32+
await fulfilmentInMainActor {
33+
let result = self.subject.isIdleTimerDisabled == true
34+
return result
35+
}
3136
}
3237

3338
func test_hasActiveCall_isFalse_IdleTimerIsEnabled() async {
34-
mockStreamVideo.state.activeCall = nil
39+
activeCallSubject.send(nil)
3540

3641
await fulfilmentInMainActor { self.subject.isIdleTimerDisabled == false }
3742
}
3843

3944
func test_hasActiveCall_changesFromFalseToTrue_firstIsEnabledThenDisabled() async {
40-
mockStreamVideo.state.activeCall = nil
45+
activeCallSubject.send(nil)
4146
await fulfilmentInMainActor { self.subject.isIdleTimerDisabled == false }
4247

43-
mockStreamVideo.state.activeCall = .dummy()
48+
activeCallSubject.send(.dummy())
4449
await fulfilmentInMainActor { self.subject.isIdleTimerDisabled == true }
4550
}
4651

4752
func test_hasActiveCall_changesFromTrueToFalse_firstIsDisabledThenEnabled() async {
48-
mockStreamVideo.state.activeCall = .dummy()
49-
await fulfilmentInMainActor { self.subject.isIdleTimerDisabled == true }
53+
activeCallSubject.send(.dummy())
54+
activeCallSubject.send(nil)
5055

51-
mockStreamVideo.state.activeCall = nil
5256
await fulfilmentInMainActor { self.subject.isIdleTimerDisabled == false }
5357
}
5458
}

StreamVideoTests/WebRTC/v2/WebRTCAuthenticator_Tests.swift

Lines changed: 12 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -157,11 +157,10 @@ final class WebRTCAuthenticator_Tests: XCTestCase, @unchecked Sendable {
157157
options: options
158158
)
159159

160-
let callSettings = await mockCoordinatorStack
161-
.coordinator
162-
.stateAdapter
163-
.callSettings
164-
XCTAssertEqual(callSettings, .init(expected.call.settings))
160+
await fulfillment {
161+
let callSettings = await self.mockCoordinatorStack.coordinator.stateAdapter.callSettings
162+
return callSettings == .init(expected.call.settings)
163+
}
165164
}
166165

167166
func test_authenticate_withCreateFalseAndInitialCallSettings_shouldSetInitialCallSettings() async throws {
@@ -190,11 +189,10 @@ final class WebRTCAuthenticator_Tests: XCTestCase, @unchecked Sendable {
190189
options: options
191190
)
192191

193-
let callSettings = await mockCoordinatorStack
194-
.coordinator
195-
.stateAdapter
196-
.callSettings
197-
XCTAssertEqual(callSettings, initialCallSettings)
192+
await fulfillment {
193+
let callSettings = await self.mockCoordinatorStack.coordinator.stateAdapter.callSettings
194+
return callSettings == initialCallSettings
195+
}
198196
}
199197

200198
func test_authenticate_withCreateFalseWithoutInitialCallSettings_shouldSetCallSettingsFromResponse() async throws {
@@ -214,11 +212,10 @@ final class WebRTCAuthenticator_Tests: XCTestCase, @unchecked Sendable {
214212
options: options
215213
)
216214

217-
let callSettings = await mockCoordinatorStack
218-
.coordinator
219-
.stateAdapter
220-
.callSettings
221-
XCTAssertEqual(callSettings, .init(expected.call.settings))
215+
await fulfillment {
216+
let callSettings = await self.mockCoordinatorStack.coordinator.stateAdapter.callSettings
217+
return callSettings == .init(expected.call.settings)
218+
}
222219
}
223220

224221
func test_authenticate_updatesVideoOptions() async throws {

0 commit comments

Comments
 (0)