Skip to content

Commit

Permalink
Fix actor concurrency issues in tests
Browse files Browse the repository at this point in the history
  • Loading branch information
jszumski committed Mar 7, 2024
1 parent ae70285 commit 1dbd025
Show file tree
Hide file tree
Showing 15 changed files with 94 additions and 4 deletions.
18 changes: 17 additions & 1 deletion Tests/NukeExtensionsTests/ImageViewExtensionsTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ import TVUIKit

#if os(iOS) || os(tvOS) || os(macOS) || os(visionOS)

@MainActor
class ImageViewExtensionsTests: XCTestCase {
var imageView: _ImageView!
var observer: ImagePipelineObserver!
Expand Down Expand Up @@ -45,6 +44,7 @@ class ImageViewExtensionsTests: XCTestCase {

// MARK: - Loading

@MainActor
func testImageLoaded() {
// When requesting an image with request
expectToLoadImage(with: Test.request, into: imageView)
Expand All @@ -55,6 +55,7 @@ class ImageViewExtensionsTests: XCTestCase {
}

#if os(tvOS)
@MainActor
func testImageLoadedToTVPosterView() {
// Use local instance for this tvOS specific test for simplicity
let posterView = TVPosterView()
Expand All @@ -68,6 +69,7 @@ class ImageViewExtensionsTests: XCTestCase {
}
#endif

@MainActor
func testImageLoadedWithURL() {
// When requesting an image with URL
let expectation = self.expectation(description: "Image loaded")
Expand All @@ -80,6 +82,7 @@ class ImageViewExtensionsTests: XCTestCase {
XCTAssertNotNil(imageView.image)
}

@MainActor
func testLoadImageWithNilRequest() {
// WHEN
imageView.image = Test.image
Expand All @@ -96,6 +99,7 @@ class ImageViewExtensionsTests: XCTestCase {
XCTAssertNil(imageView.image)
}

@MainActor
func testLoadImageWithNilRequestAndPlaceholder() {
// GIVEN
let failureImage = Test.image
Expand All @@ -111,6 +115,7 @@ class ImageViewExtensionsTests: XCTestCase {

// MARK: - Managing Tasks

@MainActor
func testTaskReturned() {
// When requesting an image
let task = NukeExtensions.loadImage(with: Test.request, into: imageView)
Expand All @@ -122,6 +127,7 @@ class ImageViewExtensionsTests: XCTestCase {
XCTAssertEqual(task?.request.urlRequest, Test.request.urlRequest)
}

@MainActor
func testTaskIsNilWhenImageInMemoryCache() {
// When the requested image is stored in memory cache
let request = Test.request
Expand All @@ -136,6 +142,7 @@ class ImageViewExtensionsTests: XCTestCase {

// MARK: - Prepare For Reuse

@MainActor
func testViewPreparedForReuse() {
// Given an image view displaying an image
imageView.image = Test.image
Expand All @@ -147,6 +154,7 @@ class ImageViewExtensionsTests: XCTestCase {
XCTAssertNil(imageView.image)
}

@MainActor
func testViewPreparedForReuseDisabled() {
// Given an image view displaying an image
let image = Test.image
Expand All @@ -163,6 +171,7 @@ class ImageViewExtensionsTests: XCTestCase {

// MARK: - Memory Cache

@MainActor
func testMemoryCacheUsed() {
// Given the requested image stored in memory cache
let image = Test.image
Expand All @@ -175,6 +184,7 @@ class ImageViewExtensionsTests: XCTestCase {
XCTAssertEqual(imageView.image, image)
}

@MainActor
func testMemoryCacheDisabled() {
// Given the requested image stored in memory cache
imageCache[Test.request] = Test.container
Expand All @@ -190,6 +200,7 @@ class ImageViewExtensionsTests: XCTestCase {

// MARK: - Completion and Progress Closures

@MainActor
func testCompletionCalled() {
var didCallCompletion = false
let expectation = self.expectation(description: "Image loaded")
Expand All @@ -210,6 +221,7 @@ class ImageViewExtensionsTests: XCTestCase {
wait()
}

@MainActor
func testCompletionCalledImageFromCache() {
// GIVEN the requested image stored in memory cache
imageCache[Test.request] = Test.container
Expand All @@ -228,6 +240,7 @@ class ImageViewExtensionsTests: XCTestCase {
XCTAssertTrue(didCallCompletion)
}

@MainActor
func testProgressHandlerCalled() {
// GIVEN
dataLoader.results[Test.url] = .success(
Expand All @@ -252,6 +265,7 @@ class ImageViewExtensionsTests: XCTestCase {

// MARK: - Cancellation

@MainActor
func testRequestCancelled() {
dataLoader.isSuspended = true

Expand All @@ -268,6 +282,7 @@ class ImageViewExtensionsTests: XCTestCase {
wait()
}

@MainActor
func testRequestCancelledWhenNewRequestStarted() {
dataLoader.isSuspended = true

Expand All @@ -283,6 +298,7 @@ class ImageViewExtensionsTests: XCTestCase {
wait()
}

@MainActor
func testRequestCancelledWhenTargetGetsDeallocated() {
dataLoader.isSuspended = true

Expand Down
6 changes: 5 additions & 1 deletion Tests/NukeExtensionsTests/ImageViewIntegrationTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import XCTest

#if os(iOS) || os(tvOS) || os(macOS) || os(visionOS)

@MainActor
class ImageViewIntegrationTests: XCTestCase {
var imageView: _ImageView!
var pipeline: ImagePipeline!
Expand Down Expand Up @@ -44,6 +43,7 @@ class ImageViewIntegrationTests: XCTestCase {

// MARK: - Loading

@MainActor
func testImageLoaded() {
// When
expectToLoadImage(with: request, into: imageView)
Expand All @@ -53,6 +53,7 @@ class ImageViewIntegrationTests: XCTestCase {
XCTAssertNotNil(imageView.image)
}

@MainActor
func testImageLoadedWithURL() {
// When
let expectation = self.expectation(description: "Image loaded")
Expand All @@ -67,6 +68,7 @@ class ImageViewIntegrationTests: XCTestCase {

// MARK: - Loading with Invalid URL

@MainActor
func testLoadImageWithInvalidURLString() {
// WHEN
let expectation = self.expectation(description: "Image loaded")
Expand All @@ -80,6 +82,7 @@ class ImageViewIntegrationTests: XCTestCase {
XCTAssertNil(imageView.image)
}

@MainActor
func testLoadingWithNilURL() {
// GIVEN
var urlRequest = URLRequest(url: Test.url)
Expand Down Expand Up @@ -123,6 +126,7 @@ class ImageViewIntegrationTests: XCTestCase {
var recordedData = [Data?]()
}

@MainActor
func _testThatAttachedDataIsPassed() throws {
// GIVEN
pipeline = pipeline.reconfigured {
Expand Down
17 changes: 16 additions & 1 deletion Tests/NukeExtensionsTests/ImageViewLoadingOptionsTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import XCTest

#if os(iOS) || os(tvOS) || os(macOS) || os(visionOS)

@MainActor
class ImageViewLoadingOptionsTests: XCTestCase {
var mockCache: MockImageCache!
var dataLoader: MockDataLoader!
Expand Down Expand Up @@ -38,6 +37,7 @@ class ImageViewLoadingOptionsTests: XCTestCase {

// MARK: - Transition

@MainActor
func testCustomTransitionPerformed() {
// Given
var options = ImageLoadingOptions()
Expand All @@ -58,6 +58,7 @@ class ImageViewLoadingOptionsTests: XCTestCase {
}

// Tests https://github.com/kean/Nuke/issues/206
@MainActor
func testImageIsDisplayedFadeInTransition() {
// Given options with .fadeIn transition
let options = ImageLoadingOptions(transition: .fadeIn(duration: 10))
Expand All @@ -72,6 +73,7 @@ class ImageViewLoadingOptionsTests: XCTestCase {

// MARK: - Placeholder

@MainActor
func testPlaceholderDisplayed() {
// Given
var options = ImageLoadingOptions()
Expand All @@ -87,6 +89,7 @@ class ImageViewLoadingOptionsTests: XCTestCase {

// MARK: - Failure Image

@MainActor
func testFailureImageDisplayed() {
// Given
dataLoader.results[Test.url] = .failure(
Expand All @@ -105,6 +108,7 @@ class ImageViewLoadingOptionsTests: XCTestCase {
XCTAssertEqual(imageView.image, failureImage)
}

@MainActor
func testFailureImageTransitionRun() {
// Given
dataLoader.results[Test.url] = .failure(
Expand Down Expand Up @@ -137,6 +141,7 @@ class ImageViewLoadingOptionsTests: XCTestCase {

// MARK: - Content Modes

@MainActor
func testPlaceholderAndSuccessContentModesApplied() {
// Given
var options = ImageLoadingOptions()
Expand All @@ -156,6 +161,7 @@ class ImageViewLoadingOptionsTests: XCTestCase {
XCTAssertEqual(imageView.contentMode, .scaleAspectFill)
}

@MainActor
func testSuccessContentModeAppliedWhenFromMemoryCache() {
// Given
var options = ImageLoadingOptions()
Expand All @@ -174,6 +180,7 @@ class ImageViewLoadingOptionsTests: XCTestCase {
XCTAssertEqual(imageView.contentMode, .scaleAspectFill)
}

@MainActor
func testFailureContentModeApplied() {
// Given
var options = ImageLoadingOptions()
Expand Down Expand Up @@ -202,6 +209,7 @@ class ImageViewLoadingOptionsTests: XCTestCase {

// MARK: - Tint Colors

@MainActor
func testPlaceholderAndSuccessTintColorApplied() {
// Given
var options = ImageLoadingOptions()
Expand All @@ -222,6 +230,7 @@ class ImageViewLoadingOptionsTests: XCTestCase {
XCTAssertEqual(imageView.image?.renderingMode, .alwaysTemplate)
}

@MainActor
func testSuccessTintColorAppliedWhenFromMemoryCache() {
// Given
var options = ImageLoadingOptions()
Expand All @@ -241,6 +250,7 @@ class ImageViewLoadingOptionsTests: XCTestCase {
XCTAssertEqual(imageView.image?.renderingMode, .alwaysTemplate)
}

@MainActor
func testFailureTintColorApplied() {
// Given
var options = ImageLoadingOptions()
Expand Down Expand Up @@ -268,6 +278,7 @@ class ImageViewLoadingOptionsTests: XCTestCase {

// MARK: - Pipeline

@MainActor
func testCustomPipelineUsed() {
// Given
let dataLoader = MockDataLoader()
Expand All @@ -292,6 +303,7 @@ class ImageViewLoadingOptionsTests: XCTestCase {

// MARK: - Shared Options

@MainActor
func testSharedOptionsUsed() {
// Given
var options = ImageLoadingOptions.shared
Expand All @@ -311,6 +323,7 @@ class ImageViewLoadingOptionsTests: XCTestCase {

// MARK: - Cache Policy

@MainActor
func testReloadIgnoringCachedData() {
// When the requested image is stored in memory cache
var request = Test.request
Expand All @@ -329,6 +342,7 @@ class ImageViewLoadingOptionsTests: XCTestCase {
// MARK: - Misc

#if os(iOS) || os(tvOS) || os(visionOS)
@MainActor
func testTransitionCrossDissolve() {
// GIVEN
var options = ImageLoadingOptions()
Expand All @@ -352,6 +366,7 @@ class ImageViewLoadingOptionsTests: XCTestCase {
}
#endif

@MainActor
func testSettingDefaultProcessor() {
// GIVEN
var options = ImageLoadingOptions()
Expand Down
2 changes: 2 additions & 0 deletions Tests/NukeTests/ImagePipelineTests/DocumentationTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ private final class CheckGettingStarted02 {
}
}

@MainActor
private func checkGettingStarted03() async throws {
let request = ImageRequest(
url: URL(string: "http://example.com/image.jpeg"),
Expand Down Expand Up @@ -199,6 +200,7 @@ private func checkCacheLayers05() throws {

// MARK: - Performance

@MainActor
private func checkPerformance01() {
// Target size is in points.
let request = ImageRequest(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -475,6 +475,7 @@ class ImagePipelineCacheTests: XCTestCase {
XCTAssertEqual(cached.imageOrientation, .right)
}

@MainActor
func testThatImageOrientationIsPreservedForProcessedImages() throws {
// GIVEN opaque jpeg with orientation
let image = Test.image(named: "left-orientation", extension: "jpeg")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ class ImagePipelineDataCachingTests: XCTestCase {
}
}

@MainActor
func testGeneratedThumbnailDataIsStoredIncache() {
// When
let request = ImageRequest(url: Test.url, userInfo: [.thumbnailKey: ImageRequest.ThumbnailOptions(size: CGSize(width: 400, height: 400), unit: .pixels, contentMode: .aspectFit)])
Expand Down Expand Up @@ -596,6 +597,7 @@ class ImagePipelineDataCachePolicyTests: XCTestCase {
XCTAssertEqual(dataCache.store.count, 0)
}

@MainActor
func testProcessedImagesFromLocalStorageAreNotCached() {
// GIVEN
pipeline = pipeline.reconfigured {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,7 @@ class ImagePipelineImageCacheTests: XCTestCase {
XCTAssertNotNil(cache[Test.request])
}

@MainActor
func testGeneratedThumbnailDataIsStoredIncache() throws {
// When
let request = ImageRequest(url: Test.url, userInfo: [.thumbnailKey: ImageRequest.ThumbnailOptions(size: CGSize(width: 400, height: 400), unit: .pixels, contentMode: .aspectFit)])
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,7 @@ class ImagePipelineProgressiveDecodingTests: XCTestCase {
// MARK: - Image Processing

#if !os(macOS)
@MainActor
func testThatPartialImagesAreResized() {
// Given
let image = PlatformImage(data: dataLoader.data)
Expand Down
Loading

0 comments on commit 1dbd025

Please sign in to comment.