p2p-swift / Tests / IrdP2PTests / GatheringPolicyTests.swift
230 words
 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
import Foundation
import Testing
import WebRTC
@testable import IrdP2P
@testable import IrdP2PSignaling
import IrdP2PCrypto

@Suite struct GatheringPolicyTests {
    @Test func theFlagMapsToTheWebRTCPolicy() {
        #expect(RoomOptions().continualGathering)
        #expect(WebRTCPeerConnection.gatheringPolicy(continual: true) == .gatherContinually)
        #expect(WebRTCPeerConnection.gatheringPolicy(continual: false) == .gatherOnce)
    }

    @Test(arguments: [true, false]) func theOptionReachesTheFactoryOnBothSides(continual: Bool) async throws {
        let h = Harness()
        var options = h.options()
        options.continualGathering = continual
        let (a, ca, fa) = try await h.join(peer: lowPeer, options: options)     // offers
        let (b, cb, fb) = try await h.join(peer: highPeer, options: options)    // answers
        #expect(await ca.waitFor(.peerReady(highPeer, .direct)))
        #expect(await cb.waitFor(.peerReady(lowPeer, .direct)))
        #expect(fa.continualGathering == [continual] && fb.continualGathering == [continual])
        await a.leave(); await b.leave()
    }

    /// A factory written against the one-method protocol keeps compiling and is still what rooms get.
    struct LegacyFactory: PeerConnectionFactory {
        let inner: InMemoryPeerConnectionFactory
        let made = Counter()
        func makePeerConnection(ice: IceConfiguration) async throws -> any PeerConnectionHandle {
            made.increment()
            return try await inner.makePeerConnection(ice: ice)
        }
    }

    final class Counter: @unchecked Sendable {
        private let lock = NSLock()
        private var n = 0
        var value: Int { lock.withLock { n } }
        func increment() { lock.withLock { n += 1 } }
    }

    @Test func factoriesWithoutTheNewMethodAreForwardedTo() async throws {
        let h = Harness()
        let legacy = LegacyFactory(inner: h.webrtc)
        var options = h.options()
        options.continualGathering = false
        let a = try await h.app(peerConnections: legacy).join(secret: h.secret, peerId: lowPeer, options: options)
        let (b, cb) = try await h.join(peer: highPeer)
        #expect(await cb.waitFor(.peerReady(lowPeer, .direct)))
        #expect(legacy.made.value == 1)
        await a.leave(); await b.leave()
    }
}

static mirror of HEAD · about · clone: git clone https://ird.cod-sursa.ro/p2p-swift.git