forked from BasedHardware/omi
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBaseDeviceConnectionLifecycleReliabilityTests.swift
More file actions
71 lines (60 loc) · 2.26 KB
/
Copy pathBaseDeviceConnectionLifecycleReliabilityTests.swift
File metadata and controls
71 lines (60 loc) · 2.26 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
import Combine
import CoreBluetooth
import XCTest
@testable import Omi_Computer
@MainActor
final class BaseDeviceConnectionLifecycleTests: XCTestCase {
func testTemplateLifecyclePreparesAndTearsDownExactlyOnce() async throws {
let transport = ReliabilityTestTransport(sessionGeneration: 11)
let connection = LifecycleHookConnection(
device: bluetoothReliabilityTestDevice,
transport: transport
)
try await connection.connect()
XCTAssertEqual(connection.prepareCallCount, 1)
do {
try await connection.connect()
XCTFail("A connection object represents exactly one session generation")
} catch let error as DeviceConnectionError {
guard case .alreadyConnected = error else {
return XCTFail("Unexpected error: \(error)")
}
}
await connection.disconnect()
await connection.disconnect()
XCTAssertEqual(connection.teardownCallCount, 1)
XCTAssertEqual(transport.disposeCallCount, 1)
}
func testFailedPreparationStillTearsDownExactlyOnce() async {
let transport = ReliabilityTestTransport(sessionGeneration: 12)
let connection = LifecycleHookConnection(
device: bluetoothReliabilityTestDevice,
transport: transport
)
connection.prepareError = BluetoothReliabilityTestError.expected
do {
try await connection.connect()
XCTFail("Expected preparation failure")
} catch {}
XCTAssertEqual(connection.prepareCallCount, 1)
XCTAssertEqual(connection.teardownCallCount, 1)
XCTAssertEqual(transport.disposeCallCount, 1)
}
func testUnexpectedPhysicalDisconnectTearsDownAndDelegatesExactlyOnce() async throws {
let transport = ReliabilityTestTransport(sessionGeneration: 13)
let connection = LifecycleHookConnection(
device: bluetoothReliabilityTestDevice,
transport: transport
)
let delegate = ConnectionDelegateDouble()
connection.delegate = delegate
try await connection.connect()
transport.emitDisconnected()
await waitForBluetoothReliabilityCondition { delegate.unexpectedDisconnectCount == 1 }
transport.emitDisconnected()
await Task.yield()
XCTAssertEqual(connection.teardownCallCount, 1)
XCTAssertEqual(transport.disposeCallCount, 1)
XCTAssertEqual(delegate.unexpectedDisconnectCount, 1)
}
}