-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathUtil.swift
99 lines (76 loc) · 2.63 KB
/
Util.swift
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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
@testable import Coder_Desktop
import Combine
import NetworkExtension
import SwiftUI
import ViewInspector
import VPNLib
@MainActor
class MockVPNService: VPNService, ObservableObject {
@Published var state: Coder_Desktop.VPNServiceState = .disabled
@Published var baseAccessURL: URL = .init(string: "https://dev.coder.com")!
@Published var menuState: VPNMenuState = .init()
var onStart: (() async -> Void)?
var onStop: (() async -> Void)?
func start() async {
state = .connecting
await onStart?()
}
func stop() async {
state = .disconnecting
await onStop?()
}
func configureTunnelProviderProtocol(proto _: NETunnelProviderProtocol?) {}
var startWhenReady: Bool = false
}
@MainActor
class MockFileSyncDaemon: FileSyncDaemon {
var logFile: URL = .init(filePath: "~/log.txt")
var sessionState: [VPNLib.FileSyncSession] = []
func refreshSessions() async {}
func deleteSessions(ids _: [String]) async throws(VPNLib.DaemonError) {}
var state: VPNLib.DaemonState = .running
func tryStart() async {}
func stop() async {}
func listSessions() async throws -> [VPNLib.FileSyncSession] {
[]
}
func createSession(arg _: CreateSyncSessionRequest) async throws(DaemonError) {}
func pauseSessions(ids _: [String]) async throws(VPNLib.DaemonError) {}
func resumeSessions(ids _: [String]) async throws(VPNLib.DaemonError) {}
func resetSessions(ids _: [String]) async throws(VPNLib.DaemonError) {}
}
extension Inspection: @unchecked Sendable, @retroactive InspectionEmissary {}
public func eventually(
timeout: Duration = .milliseconds(500),
interval: Duration = .milliseconds(10),
condition: @escaping () async throws -> Bool
) async throws -> Bool {
let endTime = ContinuousClock.now.advanced(by: timeout)
var lastError: Error?
while ContinuousClock.now < endTime {
do {
if try await condition() { return true }
lastError = nil
} catch {
lastError = error
try await Task.sleep(for: interval)
}
}
if let lastError {
throw lastError
}
return false
}
extension FileManager {
func makeTempDir() -> URL? {
let tempDirectory = FileManager.default.temporaryDirectory
let directoryName = String(Int.random(in: 0 ..< 1_000_000))
let directoryURL = tempDirectory.appendingPathComponent(directoryName)
do {
try FileManager.default.createDirectory(at: directoryURL, withIntermediateDirectories: true)
return directoryURL
} catch {
return nil
}
}
}