Skip to content

Commit

Permalink
updating tests
Browse files Browse the repository at this point in the history
  • Loading branch information
swhitty committed Sep 8, 2024
1 parent 9c1670d commit 441ddbf
Showing 1 changed file with 17 additions and 8 deletions.
25 changes: 17 additions & 8 deletions Tests/TaskTimeoutTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,8 @@ final class TaskTimeoutTests: XCTestCase {
@MainActor
func testMainActor_ReturnsValue() async throws {
let val = try await withThrowingTimeout(seconds: 1) {
MainActor.assertIsolated()
try await Task.sleep(nanoseconds: 1_000)
MainActor.assertIsolated()
return "Fish"
}
Expand All @@ -49,6 +51,7 @@ final class TaskTimeoutTests: XCTestCase {
try await withThrowingTimeout(seconds: 0.05) {
MainActor.assertIsolated()
try? await Task.sleep(nanoseconds: 60_000_000_000)
MainActor.assertIsolated()
}
XCTFail("Expected Error")
} catch {
Expand All @@ -72,13 +75,13 @@ final class TaskTimeoutTests: XCTestCase {
}

func testActor_ReturnsValue() async throws {
let val = try await TestActor().returningString("Fish")
let val = try await TestActor("Fish").returningValue()
XCTAssertEqual(val, "Fish")
}

func testActorThrowsError_WhenTimeoutExpires() async {
do {
_ = try await TestActor().returningString(
_ = try await TestActor().returningValue(
after: 60,
timeout: 0.05
)
Expand All @@ -97,17 +100,23 @@ public struct NonSendable<T> {
}
}

final actor TestActor {
final actor TestActor<T: Sendable> {

private var value: T

func returningString(_ string: String = "Fish", after sleep: TimeInterval = 0, timeout: TimeInterval = 1) async throws -> String {
try await returningValue(string, after: sleep, timeout: timeout)
init(_ value: T) {
self.value = value
}

func returningValue<T: Sendable>(_ value: T, after sleep: TimeInterval = 0, timeout: TimeInterval = 1) async throws -> T {
init() where T == String {
self.init("fish")
}

func returningValue(after sleep: TimeInterval = 0, timeout: TimeInterval = 1) async throws -> T {
try await withThrowingTimeout(seconds: timeout) {
assertIsolated()
try await Task.sleep(nanoseconds: UInt64(sleep * 1_000_000_000))
return value
assertIsolated()
return self.value
}
}
}

0 comments on commit 441ddbf

Please sign in to comment.