|
| 1 | +@testable import Coder_Desktop |
| 2 | +import Foundation |
| 3 | +import GRPC |
| 4 | +import NIO |
| 5 | +import Subprocess |
| 6 | +import Testing |
| 7 | +import VPNLib |
| 8 | +import XCTest |
| 9 | + |
| 10 | +@MainActor |
| 11 | +@Suite(.timeLimit(.minutes(1))) |
| 12 | +class FileSyncDaemonTests { |
| 13 | + let tempDir: URL |
| 14 | + let mutagenBinary: URL |
| 15 | + let mutagenDataDirectory: URL |
| 16 | + let mutagenAlphaDirectory: URL |
| 17 | + let mutagenBetaDirectory: URL |
| 18 | + |
| 19 | + // Before each test |
| 20 | + init() throws { |
| 21 | + tempDir = FileManager.default.makeTempDir()! |
| 22 | + #if arch(arm64) |
| 23 | + let binaryName = "mutagen-darwin-arm64" |
| 24 | + #elseif arch(x86_64) |
| 25 | + let binaryName = "mutagen-darwin-amd64" |
| 26 | + #endif |
| 27 | + mutagenBinary = Bundle.main.url(forResource: binaryName, withExtension: nil)! |
| 28 | + mutagenDataDirectory = tempDir.appending(path: "mutagen") |
| 29 | + mutagenAlphaDirectory = tempDir.appending(path: "alpha") |
| 30 | + try FileManager.default.createDirectory(at: mutagenAlphaDirectory, withIntermediateDirectories: true) |
| 31 | + mutagenBetaDirectory = tempDir.appending(path: "beta") |
| 32 | + try FileManager.default.createDirectory(at: mutagenBetaDirectory, withIntermediateDirectories: true) |
| 33 | + } |
| 34 | + |
| 35 | + // After each test |
| 36 | + deinit { |
| 37 | + try? FileManager.default.removeItem(at: tempDir) |
| 38 | + } |
| 39 | + |
| 40 | + private func statesEqual(_ first: DaemonState, _ second: DaemonState) -> Bool { |
| 41 | + switch (first, second) { |
| 42 | + case (.stopped, .stopped): |
| 43 | + true |
| 44 | + case (.running, .running): |
| 45 | + true |
| 46 | + case (.unavailable, .unavailable): |
| 47 | + true |
| 48 | + default: |
| 49 | + false |
| 50 | + } |
| 51 | + } |
| 52 | + |
| 53 | + @Test |
| 54 | + func fullSync() async throws { |
| 55 | + let daemon = MutagenDaemon(mutagenPath: mutagenBinary, mutagenDataDirectory: mutagenDataDirectory) |
| 56 | + #expect(statesEqual(daemon.state, .stopped)) |
| 57 | + #expect(daemon.sessionState.count == 0) |
| 58 | + |
| 59 | + // The daemon won't start until we create a session |
| 60 | + await daemon.tryStart() |
| 61 | + #expect(statesEqual(daemon.state, .stopped)) |
| 62 | + #expect(daemon.sessionState.count == 0) |
| 63 | + |
| 64 | + try await daemon.createSession( |
| 65 | + arg: .init( |
| 66 | + alpha: .init( |
| 67 | + path: mutagenAlphaDirectory.path(), |
| 68 | + protocolKind: .local |
| 69 | + ), |
| 70 | + beta: .init( |
| 71 | + path: mutagenBetaDirectory.path(), |
| 72 | + protocolKind: .local |
| 73 | + ) |
| 74 | + ) |
| 75 | + ) |
| 76 | + |
| 77 | + // Daemon should have started itself |
| 78 | + #expect(statesEqual(daemon.state, .running)) |
| 79 | + #expect(daemon.sessionState.count == 1) |
| 80 | + |
| 81 | + // Write a file to Alpha |
| 82 | + let alphaFile = mutagenAlphaDirectory.appendingPathComponent("test.txt") |
| 83 | + try "Hello, World!".write(to: alphaFile, atomically: true, encoding: .utf8) |
| 84 | + #expect( |
| 85 | + await eventually(timeout: .seconds(5), interval: .milliseconds(100)) { @MainActor in |
| 86 | + return FileManager.default.fileExists( |
| 87 | + atPath: self.mutagenBetaDirectory.appending(path: "test.txt").path() |
| 88 | + ) |
| 89 | + }) |
| 90 | + |
| 91 | + try await daemon.deleteSessions(ids: daemon.sessionState.map(\.id)) |
| 92 | + #expect(daemon.sessionState.count == 0) |
| 93 | + // Daemon should have stopped itself once all sessions are deleted |
| 94 | + #expect(statesEqual(daemon.state, .stopped)) |
| 95 | + } |
| 96 | + |
| 97 | + @Test |
| 98 | + func autoStopStart() async throws { |
| 99 | + let daemon = MutagenDaemon(mutagenPath: mutagenBinary, mutagenDataDirectory: mutagenDataDirectory) |
| 100 | + #expect(statesEqual(daemon.state, .stopped)) |
| 101 | + #expect(daemon.sessionState.count == 0) |
| 102 | + |
| 103 | + try await daemon.createSession( |
| 104 | + arg: .init( |
| 105 | + alpha: .init( |
| 106 | + path: mutagenAlphaDirectory.path(), |
| 107 | + protocolKind: .local |
| 108 | + ), |
| 109 | + beta: .init( |
| 110 | + path: mutagenBetaDirectory.path(), |
| 111 | + protocolKind: .local |
| 112 | + ) |
| 113 | + ) |
| 114 | + ) |
| 115 | + |
| 116 | + try await daemon.createSession( |
| 117 | + arg: .init( |
| 118 | + alpha: .init( |
| 119 | + path: mutagenAlphaDirectory.path(), |
| 120 | + protocolKind: .local |
| 121 | + ), |
| 122 | + beta: .init( |
| 123 | + path: mutagenBetaDirectory.path(), |
| 124 | + protocolKind: .local |
| 125 | + ) |
| 126 | + ) |
| 127 | + ) |
| 128 | + |
| 129 | + #expect(statesEqual(daemon.state, .running)) |
| 130 | + #expect(daemon.sessionState.count == 2) |
| 131 | + |
| 132 | + try await daemon.deleteSessions(ids: [daemon.sessionState[0].id]) |
| 133 | + #expect(daemon.sessionState.count == 1) |
| 134 | + #expect(statesEqual(daemon.state, .running)) |
| 135 | + |
| 136 | + try await daemon.deleteSessions(ids: [daemon.sessionState[0].id]) |
| 137 | + #expect(daemon.sessionState.count == 0) |
| 138 | + #expect(statesEqual(daemon.state, .stopped)) |
| 139 | + } |
| 140 | + |
| 141 | + @Test |
| 142 | + func orphaned() async throws { |
| 143 | + let daemon1 = MutagenDaemon(mutagenPath: mutagenBinary, mutagenDataDirectory: mutagenDataDirectory) |
| 144 | + await daemon1.refreshSessions() |
| 145 | + try await daemon1.createSession(arg: |
| 146 | + .init( |
| 147 | + alpha: .init( |
| 148 | + path: mutagenAlphaDirectory.path(), |
| 149 | + protocolKind: .local |
| 150 | + ), |
| 151 | + beta: .init( |
| 152 | + path: mutagenBetaDirectory.path(), |
| 153 | + protocolKind: .local |
| 154 | + ) |
| 155 | + ) |
| 156 | + ) |
| 157 | + #expect(statesEqual(daemon1.state, .running)) |
| 158 | + #expect(daemon1.sessionState.count == 1) |
| 159 | + |
| 160 | + let daemon2 = MutagenDaemon(mutagenPath: mutagenBinary, mutagenDataDirectory: mutagenDataDirectory) |
| 161 | + await daemon2.tryStart() |
| 162 | + #expect(statesEqual(daemon2.state, .running)) |
| 163 | + |
| 164 | + // Daemon 2 should have killed daemon 1, causing it to fail |
| 165 | + #expect(daemon1.state.isFailed) |
| 166 | + } |
| 167 | +} |
0 commit comments