- Notifications
You must be signed in to change notification settings - Fork3
chore: add file sync daemon tests#129
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to ourterms of service andprivacy statement. We’ll occasionally send you account related emails.
Already on GitHub?Sign in to your account
Merged
Uh oh!
There was an error while loading.Please reload this page.
Merged
Changes fromall commits
Commits
Show all changes
8 commits Select commitHold shift + click to select a range
30ccbf0
chore: add file sync daemon tests
ethanndicksonbf28b11
inject daemon in tests
ethanndicksonc17bc15
fixup
ethanndicksonc5c972f
fixup
ethanndicksona07bd8e
review
ethanndickson54701c2
rebase
ethanndickson2a8ab20
ignore vcs directories
ethanndicksone2a0f1b
account for swift 6 sendable's house of cards
ethanndicksonFile filter
Filter by extension
Conversations
Failed to load comments.
Loading
Uh oh!
There was an error while loading.Please reload this page.
Jump to
Jump to file
Failed to load files.
Loading
Uh oh!
There was an error while loading.Please reload this page.
Diff view
Diff view
There are no files selected for viewing
6 changes: 5 additions & 1 deletionCoder-Desktop/Coder-Desktop/Coder_DesktopApp.swift
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.Learn more about bidirectional Unicode characters
2 changes: 1 addition & 1 deletionCoder-Desktop/Coder-Desktop/Preview Content/PreviewFileSync.swift
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.Learn more about bidirectional Unicode characters
4 changes: 0 additions & 4 deletionsCoder-Desktop/Coder-Desktop/Views/FileSync/FileSyncConfig.swift
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.Learn more about bidirectional Unicode characters
7 changes: 4 additions & 3 deletionsCoder-Desktop/Coder-Desktop/Views/FileSync/FileSyncSessionModal.swift
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.Learn more about bidirectional Unicode characters
4 changes: 2 additions & 2 deletionsCoder-Desktop/Coder-DesktopTests/FilePickerTests.swift
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.Learn more about bidirectional Unicode characters
167 changes: 167 additions & 0 deletionsCoder-Desktop/Coder-DesktopTests/FileSyncDaemonTests.swift
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,167 @@ | ||
@testable import Coder_Desktop | ||
import Foundation | ||
import GRPC | ||
import NIO | ||
import Subprocess | ||
import Testing | ||
import VPNLib | ||
import XCTest | ||
@MainActor | ||
@Suite(.timeLimit(.minutes(1))) | ||
class FileSyncDaemonTests { | ||
let tempDir: URL | ||
let mutagenBinary: URL | ||
let mutagenDataDirectory: URL | ||
let mutagenAlphaDirectory: URL | ||
let mutagenBetaDirectory: URL | ||
// Before each test | ||
init() throws { | ||
ethanndickson marked this conversation as resolved. Show resolvedHide resolvedUh oh!There was an error while loading.Please reload this page. | ||
tempDir = FileManager.default.makeTempDir()! | ||
#if arch(arm64) | ||
let binaryName = "mutagen-darwin-arm64" | ||
#elseif arch(x86_64) | ||
let binaryName = "mutagen-darwin-amd64" | ||
#endif | ||
mutagenBinary = Bundle.main.url(forResource: binaryName, withExtension: nil)! | ||
mutagenDataDirectory = tempDir.appending(path: "mutagen") | ||
mutagenAlphaDirectory = tempDir.appending(path: "alpha") | ||
try FileManager.default.createDirectory(at: mutagenAlphaDirectory, withIntermediateDirectories: true) | ||
mutagenBetaDirectory = tempDir.appending(path: "beta") | ||
try FileManager.default.createDirectory(at: mutagenBetaDirectory, withIntermediateDirectories: true) | ||
} | ||
// After each test | ||
deinit { | ||
try? FileManager.default.removeItem(at: tempDir) | ||
} | ||
private func statesEqual(_ first: DaemonState, _ second: DaemonState) -> Bool { | ||
switch (first, second) { | ||
case (.stopped, .stopped): | ||
true | ||
case (.running, .running): | ||
true | ||
case (.unavailable, .unavailable): | ||
true | ||
default: | ||
false | ||
} | ||
} | ||
@Test | ||
func fullSync() async throws { | ||
let daemon = MutagenDaemon(mutagenPath: mutagenBinary, mutagenDataDirectory: mutagenDataDirectory) | ||
#expect(statesEqual(daemon.state, .stopped)) | ||
#expect(daemon.sessionState.count == 0) | ||
// The daemon won't start until we create a session | ||
await daemon.tryStart() | ||
#expect(statesEqual(daemon.state, .stopped)) | ||
#expect(daemon.sessionState.count == 0) | ||
try await daemon.createSession( | ||
arg: .init( | ||
alpha: .init( | ||
path: mutagenAlphaDirectory.path(), | ||
protocolKind: .local | ||
), | ||
beta: .init( | ||
path: mutagenBetaDirectory.path(), | ||
protocolKind: .local | ||
) | ||
) | ||
) | ||
// Daemon should have started itself | ||
#expect(statesEqual(daemon.state, .running)) | ||
#expect(daemon.sessionState.count == 1) | ||
// Write a file to Alpha | ||
let alphaFile = mutagenAlphaDirectory.appendingPathComponent("test.txt") | ||
try "Hello, World!".write(to: alphaFile, atomically: true, encoding: .utf8) | ||
#expect( | ||
await eventually(timeout: .seconds(5), interval: .milliseconds(100)) { @MainActor in | ||
return FileManager.default.fileExists( | ||
atPath: self.mutagenBetaDirectory.appending(path: "test.txt").path() | ||
) | ||
}) | ||
try await daemon.deleteSessions(ids: daemon.sessionState.map(\.id)) | ||
#expect(daemon.sessionState.count == 0) | ||
// Daemon should have stopped itself once all sessions are deleted | ||
#expect(statesEqual(daemon.state, .stopped)) | ||
} | ||
@Test | ||
func autoStopStart() async throws { | ||
let daemon = MutagenDaemon(mutagenPath: mutagenBinary, mutagenDataDirectory: mutagenDataDirectory) | ||
#expect(statesEqual(daemon.state, .stopped)) | ||
#expect(daemon.sessionState.count == 0) | ||
try await daemon.createSession( | ||
arg: .init( | ||
alpha: .init( | ||
path: mutagenAlphaDirectory.path(), | ||
protocolKind: .local | ||
), | ||
beta: .init( | ||
path: mutagenBetaDirectory.path(), | ||
protocolKind: .local | ||
) | ||
) | ||
) | ||
try await daemon.createSession( | ||
arg: .init( | ||
alpha: .init( | ||
path: mutagenAlphaDirectory.path(), | ||
protocolKind: .local | ||
), | ||
beta: .init( | ||
path: mutagenBetaDirectory.path(), | ||
protocolKind: .local | ||
) | ||
) | ||
) | ||
#expect(statesEqual(daemon.state, .running)) | ||
#expect(daemon.sessionState.count == 2) | ||
try await daemon.deleteSessions(ids: [daemon.sessionState[0].id]) | ||
#expect(daemon.sessionState.count == 1) | ||
#expect(statesEqual(daemon.state, .running)) | ||
try await daemon.deleteSessions(ids: [daemon.sessionState[0].id]) | ||
#expect(daemon.sessionState.count == 0) | ||
#expect(statesEqual(daemon.state, .stopped)) | ||
} | ||
@Test | ||
func orphaned() async throws { | ||
let daemon1 = MutagenDaemon(mutagenPath: mutagenBinary, mutagenDataDirectory: mutagenDataDirectory) | ||
await daemon1.refreshSessions() | ||
try await daemon1.createSession(arg: | ||
.init( | ||
alpha: .init( | ||
path: mutagenAlphaDirectory.path(), | ||
protocolKind: .local | ||
), | ||
beta: .init( | ||
path: mutagenBetaDirectory.path(), | ||
protocolKind: .local | ||
) | ||
) | ||
) | ||
#expect(statesEqual(daemon1.state, .running)) | ||
#expect(daemon1.sessionState.count == 1) | ||
let daemon2 = MutagenDaemon(mutagenPath: mutagenBinary, mutagenDataDirectory: mutagenDataDirectory) | ||
await daemon2.tryStart() | ||
#expect(statesEqual(daemon2.state, .running)) | ||
// Daemon 2 should have killed daemon 1, causing it to fail | ||
#expect(daemon1.state.isFailed) | ||
} | ||
} |
28 changes: 18 additions & 10 deletionsCoder-Desktop/Coder-DesktopTests/Util.swift
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.Learn more about bidirectional Unicode characters
23 changes: 7 additions & 16 deletionsCoder-Desktop/VPNLib/FileSync/FileSyncDaemon.swift
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.Learn more about bidirectional Unicode characters
Oops, something went wrong.
Uh oh!
There was an error while loading.Please reload this page.
Oops, something went wrong.
Uh oh!
There was an error while loading.Please reload this page.
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.