Skip to content

Commit

Permalink
Merge pull request kean#377 from ken-broadsheet/master
Browse files Browse the repository at this point in the history
Fix a bug where ImagePipeline.loadImage could retain its completion block indefinitely
  • Loading branch information
kean authored Jun 16, 2020
2 parents e72c5b0 + d96c9c5 commit f07c471
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 0 deletions.
7 changes: 7 additions & 0 deletions Sources/ImagePipeline.swift
Original file line number Diff line number Diff line change
Expand Up @@ -887,3 +887,10 @@ public extension ImagePipeline {
}
}
}

// MARK: - Testing
internal extension ImagePipeline {
var taskCount: Int {
return tasks.count
}
}
3 changes: 3 additions & 0 deletions Sources/Task.swift
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,9 @@ final class Task<Value, Error>: TaskSubscriptionDelegate {
starter?(self)
starter = nil

// The task may have been completed synchronously by `starter`.
guard !isDisposed else { return nil }

return subscription
}

Expand Down
14 changes: 14 additions & 0 deletions Tests/ImagePipelineTests/ImagePipelineTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -471,6 +471,20 @@ class ImagePipelineMemoryCacheTests: XCTestCase {
XCTAssertEqual(dataLoader.createdTaskCount, 1)
XCTAssertNotNil(cache[Test.request])
}

func testTaskCountAfterCachedLoad() {
// Given
cache[Test.request] = ImageContainer(image: Test.image)

// When
expect(pipeline).toLoadImage(with: Test.request)
wait()

// Then
XCTAssertEqual(dataLoader.createdTaskCount, 0)
XCTAssertNotNil(cache[Test.request])
XCTAssertEqual(pipeline.taskCount, 0)
}
}

class ImagePipelineErrorHandlingTests: XCTestCase {
Expand Down
17 changes: 17 additions & 0 deletions Tests/TaskTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,23 @@ class TaskTests: XCTestCase {
XCTAssertNil(task.subscribe { _ in })
}

func testSubscribeToTaskWithSynchronousCompletionReturnsNil() {
// Given
let task = Task<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()
}

// Then
XCTAssertNil(subscription)
wait()
}

// MARK: - Ubsubscribe

func testWhenSubscriptionIsRemovedNoEventsAreSent() {
Expand Down

0 comments on commit f07c471

Please sign in to comment.