Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -287,12 +287,15 @@ struct InternalsClientManagerTests {
sessionConfiguration: overflowing
)

// Then: served straight away, the ceiling overshot rather than enforced, and nothing
// in flight was torn down to get there.
// Then: served without waiting for a busy client, the ceiling overshot rather than
// enforced, and nothing in flight was torn down to get there. The bound is not a
// speed claim: a caller that waited for a client to free up would wait for the 60 s
// request that holds it, or for ever, and a loaded runner takes a second or more
// over a call that does nothing but bookkeeping.
#if canImport(Darwin)
#expect(DispatchTime.now().uptimeNanoseconds - start < 1_000_000_000)
#expect(DispatchTime.now().uptimeNanoseconds - start < 30_000_000_000)
#else
#expect(ContinuousClock.now - start < .seconds(1))
#expect(ContinuousClock.now - start < .seconds(30))
#endif
#expect(manager.count == maximumCount + 1)
#expect(busy.allSatisfy { $0.isRunning })
Expand Down
70 changes: 70 additions & 0 deletions Tests/RequestDLTests/Image/AppKit/NSImageViewRequestDLTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import SwiftAsyncStream
import Testing

@_spi(Private) @testable import RequestDL
@testable import RequestDLTestSupport

#if canImport(AppKit) && !targetEnvironment(macCatalyst)
import AppKit
Expand Down Expand Up @@ -107,6 +108,75 @@ struct NSImageViewRequestDLTests {
// Then
#expect(await !completed.value)
}

/// The completion not firing is not the same as the request being stopped: a cell that is
/// reused has to free the connection of the image it no longer shows.
@Test
func reusingTheImageViewInterruptsTheRequestOfThePreviousLoad() async throws {
// Given
let imageView = NSImageView()
let started = Flag()
let interrupted = Flag()

imageView.rdl.setImage(
id: "interrupted-by-reuse-\(UUID())",
task: StubImageTask {
await started.set()

do {
try await Task.sleep(nanoseconds: 60_000_000_000)
} catch {
await interrupted.set()
throw error
}

return .init(head: .stub, payload: .onePixelPNG)
}
)

try await eventually { await started.value }

// When
imageView.rdl.setImage(
id: "replacement-\(UUID())",
task: StubImageTask { .init(head: .stub, payload: .onePixelPNG) }
)

// Then
try await eventually { await interrupted.value }
}

@Test
func cancelInterruptsTheRequestOfThePendingLoad() async throws {
// Given
let imageView = NSImageView()
let started = Flag()
let interrupted = Flag()

imageView.rdl.setImage(
id: "interrupted-by-cancel-\(UUID())",
task: StubImageTask {
await started.set()

do {
try await Task.sleep(nanoseconds: 60_000_000_000)
} catch {
await interrupted.set()
throw error
}

return .init(head: .stub, payload: .onePixelPNG)
}
)

try await eventually { await started.value }

// When
imageView.rdl.cancel()

// Then
try await eventually { await interrupted.value }
}
}

// MARK: - Test helpers
Expand Down
70 changes: 70 additions & 0 deletions Tests/RequestDLTests/Image/UIKit/UIImageViewRequestDLTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import SwiftAsyncStream
import Testing

@_spi(Private) @testable import RequestDL
@testable import RequestDLTestSupport

#if canImport(UIKit) && !os(watchOS)
import UIKit
Expand Down Expand Up @@ -107,6 +108,75 @@ struct UIImageViewRequestDLTests {
// Then
#expect(await !completed.value)
}

/// The completion not firing is not the same as the request being stopped: a cell that is
/// reused has to free the connection of the image it no longer shows.
@Test
func reusingTheImageViewInterruptsTheRequestOfThePreviousLoad() async throws {
// Given
let imageView = UIImageView()
let started = Flag()
let interrupted = Flag()

imageView.rdl.setImage(
id: "interrupted-by-reuse-\(UUID())",
task: StubImageTask {
await started.set()

do {
try await Task.sleep(nanoseconds: 60_000_000_000)
} catch {
await interrupted.set()
throw error
}

return .init(head: .stub, payload: .onePixelPNG)
}
)

try await eventually { await started.value }

// When
imageView.rdl.setImage(
id: "replacement-\(UUID())",
task: StubImageTask { .init(head: .stub, payload: .onePixelPNG) }
)

// Then
try await eventually { await interrupted.value }
}

@Test
func cancelInterruptsTheRequestOfThePendingLoad() async throws {
// Given
let imageView = UIImageView()
let started = Flag()
let interrupted = Flag()

imageView.rdl.setImage(
id: "interrupted-by-cancel-\(UUID())",
task: StubImageTask {
await started.set()

do {
try await Task.sleep(nanoseconds: 60_000_000_000)
} catch {
await interrupted.set()
throw error
}

return .init(head: .stub, payload: .onePixelPNG)
}
)

try await eventually { await started.value }

// When
imageView.rdl.cancel()

// Then
try await eventually { await interrupted.value }
}
}

// MARK: - Test helpers
Expand Down
Loading