-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Concurrency Improvements in ImageDownloadService (#435)
- Loading branch information
Showing
8 changed files
with
213 additions
and
104 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,31 +1,63 @@ | ||
import Gravatar | ||
import UIKit | ||
|
||
package class TestImageCache: ImageCaching, @unchecked Sendable { | ||
package final class TestImageCache: ImageCaching, @unchecked Sendable { | ||
let imageCache = ImageCache() | ||
|
||
package private(set) var getImageCallsCount = 0 | ||
package private(set) var setImageCallsCount = 0 | ||
package private(set) var setTaskCallsCount = 0 | ||
package typealias CacheMessage = (operation: CacheMessageType, key: String) | ||
private var cacheMessages = [CacheMessage]() | ||
|
||
package enum CacheMessageType { | ||
case setToNil | ||
case inProgress | ||
case ready | ||
case get | ||
} | ||
|
||
package var getImageCallsCount: Int { messageCount(type: .get) } | ||
package var setImageCallsCount: Int { messageCount(type: .ready) } | ||
package var setTaskCallsCount: Int { messageCount(type: .inProgress) } | ||
|
||
// Serial queue to synchronize access to shared mutable state | ||
private let accessQueue = DispatchQueue(label: "com.testImageCache.accessQueue") | ||
|
||
package init() {} | ||
|
||
package func setEntry(_ entry: Gravatar.CacheEntry?, for key: String) { | ||
guard let entry else { | ||
imageCache.setEntry(nil, for: key) | ||
return | ||
} | ||
switch entry { | ||
case .inProgress: | ||
setTaskCallsCount += 1 | ||
case .ready: | ||
setImageCallsCount += 1 | ||
accessQueue.sync { | ||
var message: CacheMessage | ||
defer { cacheMessages.append(message) } | ||
guard let entry else { | ||
imageCache.setEntry(nil, for: key) | ||
message = (operation: .setToNil, key: key) | ||
return | ||
} | ||
switch entry { | ||
case .inProgress: | ||
message = (operation: .inProgress, key: key) | ||
case .ready: | ||
message = (operation: .ready, key: key) | ||
} | ||
imageCache.setEntry(entry, for: key) | ||
} | ||
imageCache.setEntry(entry, for: key) | ||
} | ||
|
||
package func getEntry(with key: String) -> Gravatar.CacheEntry? { | ||
getImageCallsCount += 1 | ||
return imageCache.getEntry(with: key) | ||
accessQueue.sync { | ||
cacheMessages.append(CacheMessage(operation: .get, key: key)) | ||
return imageCache.getEntry(with: key) | ||
} | ||
} | ||
|
||
package func messageCount(type: CacheMessageType) -> Int { | ||
accessQueue.sync { | ||
cacheMessages.filter { $0.operation == type }.count | ||
} | ||
} | ||
|
||
package func messageCount(type: CacheMessageType, forKey key: String) -> Int { | ||
accessQueue.sync { | ||
cacheMessages.filter { $0.operation == type && $0.key == key }.count | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.