Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions Sources/Dependencies/DependencyValues/FireAndForget.swift
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ private enum FireAndForgetKey: DependencyKey {
public static let liveValue = FireAndForget { priority, operation in
Task(priority: priority) { try await operation() }
}
public static let testValue = FireAndForget { _, operation in
try? await operation()
public static let testValue = FireAndForget { priority, operation in
await Task(priority: priority) { try? await operation() }.value
}
}
48 changes: 34 additions & 14 deletions Tests/DependenciesTests/FireAndForgetTests.swift
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import Dependencies
import XCTest

@MainActor
final class FireAndForgetTests: XCTestCase {
@Dependency(\.fireAndForget) var fireAndForget

Expand All @@ -16,6 +17,23 @@ final class FireAndForgetTests: XCTestCase {
XCTAssertEqual(value, true)
}

func testTestContext_Cancellation() async throws {
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this test failed without the change.

let didExecute = ActorIsolated(false)

let task = Task {
await self.fireAndForget {
try await Task.sleep(nanoseconds: 1_000_000_000)
await didExecute.setValue(true)
}
}
try await Task.sleep(nanoseconds: 500_000_000)
task.cancel()
await task.value

let value = await didExecute.value
XCTAssertEqual(value, true)
}

func testLiveContext() async throws {
try await withDependencies {
$0.context = .live
Expand All @@ -36,21 +54,23 @@ final class FireAndForgetTests: XCTestCase {
}
}

func testLiveContext_DependencyAccess() async {
await withDependencies {
$0.context = .live
$0.date.now = Date(timeIntervalSince1970: 1_234_567_890)
} operation: {
let date = ActorIsolated<Date?>(nil)
#if !os(Linux)
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm able to reproduce this test hang locally but couldn't figure it out easily so just punting on it for now. maybe a linux concurrency bug :/

func testLiveContext_DependencyAccess() async {
await withDependencies {
$0.context = .live
$0.date.now = Date(timeIntervalSince1970: 1_234_567_890)
} operation: {
let date = ActorIsolated<Date?>(nil)

await self.fireAndForget {
@Dependency(\.date.now) var now: Date
await date.setValue(now)
}
await self.fireAndForget {
@Dependency(\.date.now) var now: Date
await date.setValue(now)
}

while await date.value == nil {}
let value = await date.value
XCTAssertEqual(value, Date(timeIntervalSince1970: 1_234_567_890))
while await date.value == nil {}
let value = await date.value
XCTAssertEqual(value, Date(timeIntervalSince1970: 1_234_567_890))
}
}
}
#endif
}