|
| 1 | +// |
| 2 | +// Task+TimeoutTests.swift |
| 3 | +// FlyingFox |
| 4 | +// |
| 5 | +// Created by Simon Whitty on 15/02/2022. |
| 6 | +// Copyright © 2022 Simon Whitty. All rights reserved. |
| 7 | +// |
| 8 | +// Distributed under the permissive MIT license |
| 9 | +// Get the latest version from here: |
| 10 | +// |
| 11 | +// https://github.com/swhitty/FlyingFox |
| 12 | +// |
| 13 | +// Permission is hereby granted, free of charge, to any person obtaining a copy |
| 14 | +// of this software and associated documentation files (the "Software"), to deal |
| 15 | +// in the Software without restriction, including without limitation the rights |
| 16 | +// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 17 | +// copies of the Software, and to permit persons to whom the Software is |
| 18 | +// furnished to do so, subject to the following conditions: |
| 19 | +// |
| 20 | +// The above copyright notice and this permission notice shall be included in all |
| 21 | +// copies or substantial portions of the Software. |
| 22 | +// |
| 23 | +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 24 | +// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 25 | +// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 26 | +// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 27 | +// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 28 | +// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
| 29 | +// SOFTWARE. |
| 30 | +// |
| 31 | + |
| 32 | +@testable import FlyingFox |
| 33 | +import XCTest |
| 34 | + |
| 35 | +final class TaskTimeoutTests: XCTestCase { |
| 36 | + |
| 37 | + func testTimeoutReturnsSuccess_WhenTimeoutDoesNotExpire() async throws { |
| 38 | + // given |
| 39 | + let value = try await Task(timeout: 0.5) { |
| 40 | + "Fish" |
| 41 | + }.value |
| 42 | + |
| 43 | + // then |
| 44 | + XCTAssertEqual(value, "Fish") |
| 45 | + } |
| 46 | + |
| 47 | + func testTimeoutThrowsError_WhenTimeoutExpires() async { |
| 48 | + // given |
| 49 | + let task = Task<Void, Error>(timeout: 0.5) { |
| 50 | + try? await Task.sleep(nanoseconds: 10_000_000_000) |
| 51 | + } |
| 52 | + |
| 53 | + // then |
| 54 | + do { |
| 55 | + _ = try await task.value |
| 56 | + XCTFail("Expected TimeoutError") |
| 57 | + } catch { |
| 58 | + XCTAssertTrue(error is TimeoutError) |
| 59 | + } |
| 60 | + } |
| 61 | + |
| 62 | + |
| 63 | + func testTimeoutCancels() async { |
| 64 | + // given |
| 65 | + let task = Task(timeout: 0.5) { |
| 66 | + try? await Task.sleep(nanoseconds: 10_000_000_000) |
| 67 | + } |
| 68 | + |
| 69 | + // when |
| 70 | + task.cancel() |
| 71 | + |
| 72 | + // then |
| 73 | + do { |
| 74 | + _ = try await task.value |
| 75 | + XCTFail("Expected CancellationError") |
| 76 | + } catch { |
| 77 | + XCTAssertTrue(error is CancellationError) |
| 78 | + } |
| 79 | + } |
| 80 | +} |
0 commit comments