* migrated a lot of unit tests to Swift Testing and added a new implementation for deferred fulfillment more tests migration Cleaned the code manually to establish some good patterns more code improvements some more code improvements removed empty tests update project * more pr suggestions and cleanups * removed the TestSetup pattern * fixing claude not reusing tests * pr suggestion + added indent rule to swiftformat so that we can prevent AIs to change that
58 lines
1.5 KiB
Swift
58 lines
1.5 KiB
Swift
//
|
|
// Copyright 2025 Element Creations Ltd.
|
|
// Copyright 2023-2025 New Vector Ltd.
|
|
//
|
|
// SPDX-License-Identifier: AGPL-3.0-only OR LicenseRef-Element-Commercial.
|
|
// Please see LICENSE files in the repository root for full details.
|
|
//
|
|
|
|
@testable import ElementX
|
|
import Foundation
|
|
import Testing
|
|
|
|
@Suite
|
|
struct ExpiringTaskRunnerTests {
|
|
enum ExpiringTaskTestError: Error {
|
|
case failed
|
|
}
|
|
|
|
@Test
|
|
func succedingTask() async throws {
|
|
let runner = ExpiringTaskRunner {
|
|
try? await Task.sleep(for: .milliseconds(300))
|
|
return true
|
|
}
|
|
|
|
let result = try await runner.run(timeout: .seconds(1))
|
|
#expect(result == true)
|
|
}
|
|
|
|
@Test
|
|
func failingTask() async {
|
|
let runner: ExpiringTaskRunner<Result<String, ExpiringTaskTestError>> = ExpiringTaskRunner {
|
|
try? await Task.sleep(for: .milliseconds(300))
|
|
return .failure(.failed)
|
|
}
|
|
|
|
do {
|
|
_ = try await runner.run(timeout: .seconds(1))
|
|
} catch {
|
|
#expect(error as? ExpiringTaskTestError == ExpiringTaskTestError.failed)
|
|
}
|
|
}
|
|
|
|
@Test
|
|
func timeoutTask() async {
|
|
let runner = ExpiringTaskRunner {
|
|
try? await Task.sleep(for: .milliseconds(300))
|
|
return true
|
|
}
|
|
|
|
do {
|
|
_ = try await runner.run(timeout: .milliseconds(100))
|
|
} catch {
|
|
#expect(error as? ExpiringTaskRunnerError == ExpiringTaskRunnerError.timeout)
|
|
}
|
|
}
|
|
}
|