mirror of
https://github.com/kean/Nuke.git
synced 2024-11-24 11:26:14 +03:00
Update TaskTests
This commit is contained in:
parent
9f7eb9cafb
commit
13d363e609
@ -2,8 +2,8 @@
|
||||
//
|
||||
// Copyright (c) 2015-2024 Alexander Grebenyuk (github.com/kean).
|
||||
|
||||
@testable import Nuke
|
||||
import Testing
|
||||
@testable import Nuke
|
||||
|
||||
@Suite @ImagePipelineActor struct RateLimiterTests {
|
||||
let rateLimiter = RateLimiter(rate: 10, burst: 2)
|
||||
|
@ -2,14 +2,14 @@
|
||||
//
|
||||
// Copyright (c) 2015-2024 Alexander Grebenyuk (github.com/kean).
|
||||
|
||||
import XCTest
|
||||
import Testing
|
||||
import Foundation
|
||||
@testable import Nuke
|
||||
|
||||
@ImagePipelineActor
|
||||
class TaskTests: XCTestCase {
|
||||
@Suite @ImagePipelineActor struct TaskTests {
|
||||
// MARK: - Starter
|
||||
|
||||
func testStarterCalledOnFirstSubscription() {
|
||||
@Test func starterCalledOnFirstSubscription() {
|
||||
// Given
|
||||
var startCount = 0
|
||||
_ = SimpleTask<Int, Error>(starter: { _ in
|
||||
@ -17,10 +17,10 @@ class TaskTests: XCTestCase {
|
||||
})
|
||||
|
||||
// Then
|
||||
XCTAssertEqual(startCount, 0)
|
||||
#expect(startCount == 0)
|
||||
}
|
||||
|
||||
func testStarterCalledWhenSubscriptionIsAdded() {
|
||||
@Test func starterCalledWhenSubscriptionIsAdded() {
|
||||
// Given
|
||||
var startCount = 0
|
||||
let task = SimpleTask<Int, Error>(starter: { _ in
|
||||
@ -31,10 +31,10 @@ class TaskTests: XCTestCase {
|
||||
_ = task.subscribe { _ in }
|
||||
|
||||
// Then started is called
|
||||
XCTAssertEqual(startCount, 1)
|
||||
#expect(startCount == 1)
|
||||
}
|
||||
|
||||
func testStarterOnlyCalledOnce() {
|
||||
@Test func starterOnlyCalledOnce() {
|
||||
// Given
|
||||
var startCount = 0
|
||||
let task = SimpleTask<Int, Error>(starter: { _ in
|
||||
@ -46,10 +46,10 @@ class TaskTests: XCTestCase {
|
||||
_ = task.subscribe { _ in }
|
||||
|
||||
// Then started is only called once
|
||||
XCTAssertEqual(startCount, 1)
|
||||
#expect(startCount == 1)
|
||||
}
|
||||
|
||||
func testStarterIsDeallocated() {
|
||||
@Test func tarterIsDeallocated() {
|
||||
// Given
|
||||
class Foo {
|
||||
}
|
||||
@ -64,18 +64,18 @@ class TaskTests: XCTestCase {
|
||||
})
|
||||
}
|
||||
|
||||
XCTAssertNotNil(weakFoo, "Foo is retained by starter")
|
||||
#expect(weakFoo != nil, "Foo is retained by starter")
|
||||
|
||||
// When first subscription is added and starter is called
|
||||
_ = task.subscribe { _ in }
|
||||
|
||||
// Then
|
||||
XCTAssertNil(weakFoo, "Started wasn't deallocated")
|
||||
#expect(weakFoo == nil, "Started wasn't deallocated")
|
||||
}
|
||||
|
||||
// MARK: - Subscribe
|
||||
|
||||
func testWhenSubscriptionAddedEventsAreForwarded() {
|
||||
@Test func whenSubscriptionAddedEventsAreForwarded() {
|
||||
// Given
|
||||
let task = SimpleTask<Int, MyError>(starter: {
|
||||
$0.send(progress: TaskProgress(completed: 1, total: 2))
|
||||
@ -91,7 +91,7 @@ class TaskTests: XCTestCase {
|
||||
}
|
||||
|
||||
// Then
|
||||
XCTAssertEqual(recordedEvents, [
|
||||
#expect(recordedEvents == [
|
||||
.progress(TaskProgress(completed: 1, total: 2)),
|
||||
.value(1, isCompleted: false),
|
||||
.progress(TaskProgress(completed: 2, total: 2)),
|
||||
@ -99,7 +99,7 @@ class TaskTests: XCTestCase {
|
||||
])
|
||||
}
|
||||
|
||||
func testBothSubscriptionsReceiveEvents() {
|
||||
@Test func bothSubscriptionsReceiveEvents() {
|
||||
// Given
|
||||
let task = AsyncTask<Int, MyError>()
|
||||
|
||||
@ -107,20 +107,20 @@ class TaskTests: XCTestCase {
|
||||
var eventCount = 0
|
||||
|
||||
_ = task.subscribe { event in
|
||||
XCTAssertEqual(event, .value(1, isCompleted: false))
|
||||
#expect(event == .value(1, isCompleted: false))
|
||||
eventCount += 1 }
|
||||
_ = task.subscribe { event in
|
||||
XCTAssertEqual(event, .value(1, isCompleted: false))
|
||||
#expect(event == .value(1, isCompleted: false))
|
||||
eventCount += 1
|
||||
}
|
||||
|
||||
task.send(value: 1)
|
||||
|
||||
// Then
|
||||
XCTAssertEqual(eventCount, 2)
|
||||
#expect(eventCount == 2)
|
||||
}
|
||||
|
||||
func testCantSubscribeToAlreadyCancelledTask() {
|
||||
@Test func cantSubscribeToAlreadyCancelledTask() {
|
||||
// Given
|
||||
let task = SimpleTask<Int, MyError>(starter: { _ in })
|
||||
let subscription = task.subscribe { _ in }
|
||||
@ -129,10 +129,10 @@ class TaskTests: XCTestCase {
|
||||
subscription?.unsubscribe()
|
||||
|
||||
// Then
|
||||
XCTAssertNil(task.subscribe { _ in })
|
||||
#expect(task.subscribe { _ in } == nil)
|
||||
}
|
||||
|
||||
func testCantSubscribeToAlreadySucceededTask() {
|
||||
@Test func cantSubscribeToAlreadySucceededTask() {
|
||||
// Given
|
||||
let task = AsyncTask<Int, MyError>()
|
||||
_ = task.subscribe { _ in }
|
||||
@ -141,10 +141,10 @@ class TaskTests: XCTestCase {
|
||||
task.send(value: 1, isCompleted: true)
|
||||
|
||||
// Then
|
||||
XCTAssertNil(task.subscribe { _ in })
|
||||
#expect(task.subscribe { _ in } == nil)
|
||||
}
|
||||
|
||||
func testCantSubscribeToAlreadyFailedTasks() {
|
||||
@Test func cantSubscribeToAlreadyFailedTasks() {
|
||||
// Given
|
||||
let task = AsyncTask<Int, MyError>()
|
||||
_ = task.subscribe { _ in }
|
||||
@ -153,29 +153,27 @@ class TaskTests: XCTestCase {
|
||||
task.send(error: .init(raw: "1"))
|
||||
|
||||
// Then
|
||||
XCTAssertNil(task.subscribe { _ in })
|
||||
#expect(task.subscribe { _ in } == nil)
|
||||
}
|
||||
|
||||
func testSubscribeToTaskWithSynchronousCompletionReturnsNil() {
|
||||
@Test func subscribeToTaskWithSynchronousCompletionReturnsNil() async {
|
||||
// Given
|
||||
let task = SimpleTask<Int, MyError> { (task) in
|
||||
task.send(value: 0, isCompleted: true)
|
||||
}
|
||||
|
||||
// When
|
||||
let expectation = self.expectation(description: "Observer called")
|
||||
let subscription = task.subscribe { _ in
|
||||
expectation.fulfill()
|
||||
// When/Then
|
||||
await withUnsafeContinuation { continuation in
|
||||
let subscription = task.subscribe { _ in
|
||||
continuation.resume()
|
||||
}
|
||||
#expect(subscription == nil)
|
||||
}
|
||||
|
||||
// Then
|
||||
XCTAssertNil(subscription)
|
||||
wait()
|
||||
}
|
||||
|
||||
// MARK: - Ubsubscribe
|
||||
|
||||
func testWhenSubscriptionIsRemovedNoEventsAreSent() {
|
||||
@Test func whenSubscriptionIsRemovedNoEventsAreSent() {
|
||||
// Given
|
||||
let task = AsyncTask<Int, MyError>()
|
||||
var recordedEvents = [AsyncTask<Int, MyError>.Event]()
|
||||
@ -186,10 +184,10 @@ class TaskTests: XCTestCase {
|
||||
task.send(value: 1)
|
||||
|
||||
// Then
|
||||
XCTAssertTrue(recordedEvents.isEmpty, "Expect no events to be received by observer after subscription is removed")
|
||||
#expect(recordedEvents.isEmpty, "Expect no events to be received by observer after subscription is removed")
|
||||
}
|
||||
|
||||
func testWhenSubscriptionIsRemovedTaskBecomesDisposed() {
|
||||
@Test func whenSubscriptionIsRemovedTaskBecomesDisposed() {
|
||||
// Given
|
||||
let task = AsyncTask<Int, MyError>()
|
||||
let subscription = task.subscribe { _ in }
|
||||
@ -198,10 +196,10 @@ class TaskTests: XCTestCase {
|
||||
subscription?.unsubscribe()
|
||||
|
||||
// Then
|
||||
XCTAssertTrue(task.isDisposed, "Expect task to be marked as disposed")
|
||||
#expect(task.isDisposed, "Expect task to be marked as disposed")
|
||||
}
|
||||
|
||||
func testWhenSubscriptionIsRemovedOnCancelIsCalled() {
|
||||
@Test func whenSubscriptionIsRemovedOnCancelIsCalled() {
|
||||
// Given
|
||||
let task = AsyncTask<Int, MyError>()
|
||||
let subscription = task.subscribe { _ in }
|
||||
@ -215,39 +213,39 @@ class TaskTests: XCTestCase {
|
||||
subscription?.unsubscribe()
|
||||
|
||||
// Then
|
||||
XCTAssertTrue(onCancelledIsCalled)
|
||||
#expect(onCancelledIsCalled)
|
||||
}
|
||||
|
||||
func testWhenSubscriptionIsRemovedOperationIsCancelled() {
|
||||
@Test func whenSubscriptionIsRemovedOperationIsCancelled() {
|
||||
// Given
|
||||
let operation = Foundation.Operation()
|
||||
let task = SimpleTask<Int, MyError>(starter: { $0.operation = operation })
|
||||
let subscription = task.subscribe { _ in }
|
||||
XCTAssertFalse(operation.isCancelled)
|
||||
#expect(!operation.isCancelled)
|
||||
|
||||
// When
|
||||
subscription?.unsubscribe()
|
||||
|
||||
// Then
|
||||
XCTAssertTrue(operation.isCancelled)
|
||||
#expect(operation.isCancelled)
|
||||
}
|
||||
|
||||
func testWhenSubscriptionIsRemovedDependencyIsCancelled() {
|
||||
@Test func whenSubscriptionIsRemovedDependencyIsCancelled() {
|
||||
// Given
|
||||
let operation = Foundation.Operation()
|
||||
let dependency = SimpleTask<Int, MyError>(starter: { $0.operation = operation })
|
||||
let task = SimpleTask<Int, MyError>(starter: { $0.dependency = dependency.subscribe { _ in } })
|
||||
let subscription = task.subscribe { _ in }
|
||||
XCTAssertFalse(operation.isCancelled)
|
||||
#expect(!operation.isCancelled)
|
||||
|
||||
// When
|
||||
subscription?.unsubscribe()
|
||||
|
||||
// Then
|
||||
XCTAssertTrue(operation.isCancelled)
|
||||
#expect(operation.isCancelled)
|
||||
}
|
||||
|
||||
func testWhenOneOfTwoSubscriptionsAreRemovedTaskNotCancelled() {
|
||||
@Test func whenOneOfTwoSubscriptionsAreRemovedTaskNotCancelled() {
|
||||
// Given
|
||||
let operation = Foundation.Operation()
|
||||
let task = SimpleTask<Int, MyError>(starter: { $0.operation = operation })
|
||||
@ -258,10 +256,10 @@ class TaskTests: XCTestCase {
|
||||
subscription1?.unsubscribe()
|
||||
|
||||
// Then
|
||||
XCTAssertFalse(operation.isCancelled)
|
||||
#expect(!operation.isCancelled)
|
||||
}
|
||||
|
||||
func testWhenTwoOfTwoSubscriptionsAreRemovedTaskIsCancelled() {
|
||||
@Test func whenTwoOfTwoSubscriptionsAreRemovedTaskIsCancelled() {
|
||||
// Given
|
||||
let operation = Foundation.Operation()
|
||||
let task = SimpleTask<Int, MyError>(starter: { $0.operation = operation })
|
||||
@ -273,12 +271,12 @@ class TaskTests: XCTestCase {
|
||||
subscription2?.unsubscribe()
|
||||
|
||||
// Then
|
||||
XCTAssertTrue(operation.isCancelled)
|
||||
#expect(operation.isCancelled)
|
||||
}
|
||||
|
||||
// MARK: - Priority
|
||||
|
||||
func testWhenPriorityIsUpdatedOperationPriorityAlsoUpdated() {
|
||||
@Test func whenPriorityIsUpdatedOperationPriorityAlsoUpdated() {
|
||||
// Given
|
||||
let operation = Foundation.Operation()
|
||||
let task = SimpleTask<Int, MyError>(starter: { $0.operation = operation })
|
||||
@ -288,10 +286,10 @@ class TaskTests: XCTestCase {
|
||||
subscription?.setPriority(.high)
|
||||
|
||||
// Then
|
||||
XCTAssertEqual(operation.queuePriority, .high)
|
||||
#expect(operation.queuePriority == .high)
|
||||
}
|
||||
|
||||
func testWhenTaskChangesOperationPriorityUpdated() { // Or sets operation later
|
||||
@Test func whenTaskChangesOperationPriorityUpdated() { // Or sets operation later
|
||||
// Given
|
||||
let task = AsyncTask<Int, MyError>()
|
||||
let subscription = task.subscribe { _ in }
|
||||
@ -302,10 +300,10 @@ class TaskTests: XCTestCase {
|
||||
task.operation = operation
|
||||
|
||||
// Then
|
||||
XCTAssertEqual(operation.queuePriority, .high)
|
||||
#expect(operation.queuePriority == .high)
|
||||
}
|
||||
|
||||
func testThatPriorityCanBeLowered() {
|
||||
@Test func priorityCanBeLowered() {
|
||||
// Given
|
||||
let operation = Foundation.Operation()
|
||||
let task = SimpleTask<Int, MyError>(starter: { $0.operation = operation })
|
||||
@ -315,10 +313,10 @@ class TaskTests: XCTestCase {
|
||||
subscription?.setPriority(.low)
|
||||
|
||||
// Then
|
||||
XCTAssertEqual(operation.queuePriority, .low)
|
||||
#expect(operation.queuePriority == .low)
|
||||
}
|
||||
|
||||
func testThatPriorityEqualMaximumPriorityOfAllSubscriptions() {
|
||||
@Test func priorityEqualMaximumPriorityOfAllSubscriptions() {
|
||||
// Given
|
||||
let operation = Foundation.Operation()
|
||||
let task = SimpleTask<Int, MyError>(starter: { $0.operation = operation })
|
||||
@ -330,10 +328,10 @@ class TaskTests: XCTestCase {
|
||||
subscription2?.setPriority(.high)
|
||||
|
||||
// Then
|
||||
XCTAssertEqual(operation.queuePriority, .high)
|
||||
#expect(operation.queuePriority == .high)
|
||||
}
|
||||
|
||||
func testWhenSubscriptionIsRemovedPriorityIsUpdated() {
|
||||
@Test func subscriptionIsRemovedPriorityIsUpdated() {
|
||||
// Given
|
||||
let operation = Foundation.Operation()
|
||||
let task = SimpleTask<Int, MyError>(starter: { $0.operation = operation })
|
||||
@ -347,10 +345,10 @@ class TaskTests: XCTestCase {
|
||||
subscription2?.unsubscribe()
|
||||
|
||||
// Then
|
||||
XCTAssertEqual(operation.queuePriority, .low)
|
||||
#expect(operation.queuePriority == .low)
|
||||
}
|
||||
|
||||
func testWhenSubscriptionLowersPriorityButExistingSubscriptionHasHigherPriporty() {
|
||||
@Test func whenSubscriptionLowersPriorityButExistingSubscriptionHasHigherPriporty() {
|
||||
// Given
|
||||
let operation = Foundation.Operation()
|
||||
let task = SimpleTask<Int, MyError>(starter: { $0.operation = operation })
|
||||
@ -362,10 +360,10 @@ class TaskTests: XCTestCase {
|
||||
subscription1?.setPriority(.low)
|
||||
|
||||
// Then order of updating sub
|
||||
XCTAssertEqual(operation.queuePriority, .high)
|
||||
#expect(operation.queuePriority == .high)
|
||||
}
|
||||
|
||||
func testPriorityOfDependencyUpdated() {
|
||||
@Test func priorityOfDependencyUpdated() {
|
||||
// Given
|
||||
let operation = Foundation.Operation()
|
||||
let dependency = SimpleTask<Int, MyError>(starter: { $0.operation = operation })
|
||||
@ -376,12 +374,12 @@ class TaskTests: XCTestCase {
|
||||
subscription?.setPriority(.high)
|
||||
|
||||
// Then
|
||||
XCTAssertEqual(operation.queuePriority, .high)
|
||||
#expect(operation.queuePriority == .high)
|
||||
}
|
||||
|
||||
// MARK: - Dispose
|
||||
|
||||
func testExecutingTaskIsntDisposed() {
|
||||
@Test func executingTaskIsntDisposed() {
|
||||
// Given
|
||||
let task = AsyncTask<Int, MyError>()
|
||||
var isDisposeCalled = false
|
||||
@ -392,11 +390,11 @@ class TaskTests: XCTestCase {
|
||||
task.send(value: 1) // Casually sending value
|
||||
|
||||
// Then
|
||||
XCTAssertFalse(isDisposeCalled)
|
||||
XCTAssertFalse(task.isDisposed)
|
||||
#expect(!isDisposeCalled)
|
||||
#expect(!task.isDisposed)
|
||||
}
|
||||
|
||||
func testThatTaskIsDisposedWhenCancelled() {
|
||||
@Test func taskIsDisposedWhenCancelled() {
|
||||
// Given
|
||||
let task = SimpleTask<Int, MyError>(starter: { _ in })
|
||||
var isDisposeCalled = false
|
||||
@ -407,11 +405,11 @@ class TaskTests: XCTestCase {
|
||||
subscription?.unsubscribe()
|
||||
|
||||
// Then
|
||||
XCTAssertTrue(isDisposeCalled)
|
||||
XCTAssertTrue(task.isDisposed)
|
||||
#expect(isDisposeCalled)
|
||||
#expect(task.isDisposed)
|
||||
}
|
||||
|
||||
func testThatTaskIsDisposedWhenCompletedWithSuccess() {
|
||||
@Test func taskIsDisposedWhenCompletedWithSuccess() {
|
||||
// Given
|
||||
let task = AsyncTask<Int, MyError>()
|
||||
var isDisposeCalled = false
|
||||
@ -422,11 +420,11 @@ class TaskTests: XCTestCase {
|
||||
task.send(value: 1, isCompleted: true)
|
||||
|
||||
// Then
|
||||
XCTAssertTrue(isDisposeCalled)
|
||||
XCTAssertTrue(task.isDisposed)
|
||||
#expect(isDisposeCalled)
|
||||
#expect(task.isDisposed)
|
||||
}
|
||||
|
||||
func testThatTaskIsDisposedWhenCompletedWithFailure() {
|
||||
@Test func taskIsDisposedWhenCompletedWithFailure() {
|
||||
// Given
|
||||
let task = AsyncTask<Int, MyError>()
|
||||
var isDisposeCalled = false
|
||||
@ -437,8 +435,8 @@ class TaskTests: XCTestCase {
|
||||
task.send(error: .init(raw: "1"))
|
||||
|
||||
// Then
|
||||
XCTAssertTrue(isDisposeCalled)
|
||||
XCTAssertTrue(task.isDisposed)
|
||||
#expect(isDisposeCalled)
|
||||
#expect(task.isDisposed)
|
||||
}
|
||||
}
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user