Skip to content

make download test utilities thread-safe #3322

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Mar 5, 2021
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
6 changes: 6 additions & 0 deletions Sources/Basics/ConcurrencyHelpers.swift
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,12 @@ public final class ThreadSafeKeyValueStore<Key, Value> where Key: Hashable {
}
}

public func map<T>(_ transform: ((key: Key, value: Value)) throws -> T) rethrows -> [T] {
try self.lock.withLock {
try self.underlying.map(transform)
}
}

public func mapValues<T>(_ transform: (Value) throws -> T) rethrows -> [Key: T] {
try self.lock.withLock {
try self.underlying.mapValues(transform)
Expand Down
27 changes: 12 additions & 15 deletions Sources/SPMTestSupport/MockArchiver.swift
Original file line number Diff line number Diff line change
Expand Up @@ -8,17 +8,12 @@
See http://swift.org/CONTRIBUTORS.txt for Swift project authors
*/

import Basics
import TSCBasic
import TSCUtility
import Foundation

public class MockArchiver: Archiver {
public typealias Extract = (
MockArchiver,
AbsolutePath,
AbsolutePath,
(Result<Void, Error>) -> Void
) -> Void
public typealias Handler = (MockArchiver, AbsolutePath, AbsolutePath, (Result<Void, Error>) -> Void) -> Void

public struct Extraction: Equatable {
public let archivePath: AbsolutePath
Expand All @@ -31,21 +26,23 @@ public class MockArchiver: Archiver {
}

public let supportedExtensions: Set<String> = ["zip"]
public var extractions: [Extraction] = []
public var extract: Extract!
public let extractions = ThreadSafeArrayStore<Extraction>()
public let handler: Handler?

public init(extract: Extract? = nil) {
self.extract = extract ?? { archiver, archivePath, destinationPath, completion in
self.extractions.append(Extraction(archivePath: archivePath, destinationPath: destinationPath))
completion(.success(()))
}
public init(handler: Handler? = nil) {
self.handler = handler
}

public func extract(
from archivePath: AbsolutePath,
to destinationPath: AbsolutePath,
completion: @escaping (Result<Void, Error>) -> Void
) {
self.extract(self, archivePath, destinationPath, completion)
if let handler = self.handler {
handler(self, archivePath, destinationPath, completion)
} else {
self.extractions.append(Extraction(archivePath: archivePath, destinationPath: destinationPath))
completion(.success(()))
}
}
}
18 changes: 10 additions & 8 deletions Sources/SPMTestSupport/MockHashAlgorithm.swift
Original file line number Diff line number Diff line change
Expand Up @@ -12,19 +12,21 @@ import Basics
import TSCBasic

public class MockHashAlgorithm: HashAlgorithm {
public typealias Hash = (ByteString) -> ByteString
public typealias Handler = (ByteString) -> ByteString

public private(set) var hashes = ThreadSafeArrayStore<ByteString>()
private var hashFunction: Hash!
private let handler: Handler?

public init(hash: Hash? = nil) {
self.hashFunction = hash ?? { hash in
public init(handler: Handler? = nil) {
self.handler = handler
}

public func hash(_ hash: ByteString) -> ByteString {
if let handler = self.handler {
return handler(hash)
} else {
self.hashes.append(hash)
return ByteString(hash.contents.reversed())
}
}

public func hash(_ bytes: ByteString) -> ByteString {
return self.hashFunction(bytes)
}
}
16 changes: 8 additions & 8 deletions Tests/WorkspaceTests/WorkspaceTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -4266,7 +4266,7 @@ final class WorkspaceTests: XCTestCase {
func testArtifactDownloadHappyPath() throws {
let sandbox = AbsolutePath("/tmp/ws/")
let fs = InMemoryFileSystem()
var downloads = [Foundation.URL: AbsolutePath]()
let downloads = ThreadSafeKeyValueStore<Foundation.URL, AbsolutePath>()

// returns a dummy zipfile for the requested artifact
let httpClient = HTTPClient(handler: { request, _, completion in
Expand Down Expand Up @@ -4301,7 +4301,7 @@ final class WorkspaceTests: XCTestCase {
})

// create a dummy xcframework directory from the request archive
let archiver = MockArchiver(extract: { archiver, archivePath, destinationPath, completion in
let archiver = MockArchiver(handler: { archiver, archivePath, destinationPath, completion in
do {
let name: String
switch archivePath.basename {
Expand Down Expand Up @@ -4441,7 +4441,7 @@ final class WorkspaceTests: XCTestCase {
func testArtifactDownloadWithPreviousState() throws {
let sandbox = AbsolutePath("/tmp/ws/")
let fs = InMemoryFileSystem()
var downloads = [Foundation.URL: AbsolutePath]()
let downloads = ThreadSafeKeyValueStore<Foundation.URL, AbsolutePath>()

// returns a dummy zipfile for the requested artifact
let httpClient = HTTPClient(handler: { request, _, completion in
Expand Down Expand Up @@ -4478,7 +4478,7 @@ final class WorkspaceTests: XCTestCase {
})

// create a dummy xcframework directory from the request archive
let archiver = MockArchiver(extract: { archiver, archivePath, destinationPath, completion in
let archiver = MockArchiver(handler: { archiver, archivePath, destinationPath, completion in
do {
let name: String
switch archivePath.basename {
Expand Down Expand Up @@ -4731,7 +4731,7 @@ final class WorkspaceTests: XCTestCase {
}
})

let archiver = MockArchiver(extract: { _, _, destinationPath, completion in
let archiver = MockArchiver(handler: { _, _, destinationPath, completion in
XCTAssertEqual(destinationPath, AbsolutePath("/tmp/ws/.build/artifacts/A"))
completion(.failure(DummyError()))
})
Expand Down Expand Up @@ -4871,7 +4871,7 @@ final class WorkspaceTests: XCTestCase {
func testDownloadArchiveIndexFilesHappyPath() throws {
let sandbox = AbsolutePath("/tmp/ws/")
let fs = InMemoryFileSystem()
var downloads = [Foundation.URL: AbsolutePath]()
let downloads = ThreadSafeKeyValueStore<Foundation.URL, AbsolutePath>()
let hostToolchain = try UserToolchain(destination: .hostDestination())

let ariFiles = [
Expand Down Expand Up @@ -4951,7 +4951,7 @@ final class WorkspaceTests: XCTestCase {
})

// create a dummy xcframework directory from the request archive
let archiver = MockArchiver(extract: { archiver, archivePath, destinationPath, completion in
let archiver = MockArchiver(handler: { archiver, archivePath, destinationPath, completion in
do {
let name: String
switch archivePath.basename {
Expand Down Expand Up @@ -5338,7 +5338,7 @@ final class WorkspaceTests: XCTestCase {
})

// create a dummy xcframework directory from the request archive
let archiver = MockArchiver(extract: { archiver, archivePath, destinationPath, completion in
let archiver = MockArchiver(handler: { archiver, archivePath, destinationPath, completion in
do {
let name: String
switch archivePath.basename {
Expand Down