Skip to content

Commit

Permalink
chore: Use existential any
Browse files Browse the repository at this point in the history
test: Simultaneous upload/download test
  • Loading branch information
amosavian committed Nov 22, 2023
1 parent f5be59c commit 40c382c
Show file tree
Hide file tree
Showing 5 changed files with 80 additions and 27 deletions.
26 changes: 13 additions & 13 deletions AMSMB2/AMSMB2.swift
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ public typealias AMSMB2 = SMB2Manager
/// Implements SMB2 File operations.
@objc(AMSMB2Manager)
public class SMB2Manager: NSObject, NSSecureCoding, Codable, NSCopying, CustomReflectable, @unchecked Sendable {
public typealias SimpleCompletionHandler = (@Sendable (_ error: Error?) -> Void)?
public typealias SimpleCompletionHandler = (@Sendable (_ error: (any Error)?) -> Void)?
public typealias ReadProgressHandler = (@Sendable (_ bytes: Int64, _ total: Int64) -> Bool)?
public typealias WriteProgressHandler = (@Sendable (_ bytes: Int64) -> Bool)?
fileprivate typealias CopyProgressHandler = (@Sendable
Expand Down Expand Up @@ -181,7 +181,7 @@ public class SMB2Manager: NSObject, NSSecureCoding, Codable, NSCopying, CustomRe
case user, password, timeout
}

public required init(from decoder: Decoder) throws {
public required init(from decoder: any Decoder) throws {
let container = try decoder.container(keyedBy: CodingKeys.self)
let url = try container.decode(URL.self, forKey: .url)
guard url.scheme?.lowercased() == "smb" else {
Expand All @@ -203,7 +203,7 @@ public class SMB2Manager: NSObject, NSSecureCoding, Codable, NSCopying, CustomRe
super.init()
}

open func encode(to encoder: Encoder) throws {
open func encode(to encoder: any Encoder) throws {
var container = encoder.container(keyedBy: CodingKeys.self)
try container.encode(url, forKey: .url)
try container.encode(_domain, forKey: .domain)
Expand Down Expand Up @@ -346,7 +346,7 @@ public class SMB2Manager: NSObject, NSSecureCoding, Codable, NSCopying, CustomRe
*/
open func listShares(
enumerateHidden: Bool = false,
completionHandler: @Sendable @escaping (_ result: Result<[(name: String, comment: String)], Error>) -> Void
completionHandler: @Sendable @escaping (_ result: Result<[(name: String, comment: String)], any Error>) -> Void
) {
// Connecting to Interprocess Communication share
with(shareName: "IPC$", encrypted: false, completionHandler: completionHandler) { context in
Expand Down Expand Up @@ -377,7 +377,7 @@ public class SMB2Manager: NSObject, NSSecureCoding, Codable, NSCopying, CustomRe
/// Only for test case coverage
func _swift_listShares(
enumerateHidden: Bool = false,
completionHandler: @Sendable @escaping (_ result: Result<[(name: String, comment: String)], Error>) -> Void
completionHandler: @Sendable @escaping (_ result: Result<[(name: String, comment: String)], any Error>) -> Void
) {
with(shareName: "IPC$", encrypted: false, completionHandler: completionHandler) { context in
try context.shareEnumSwift().map(enumerateHidden: enumerateHidden)
Expand Down Expand Up @@ -406,7 +406,7 @@ public class SMB2Manager: NSObject, NSSecureCoding, Codable, NSCopying, CustomRe
*/
open func contentsOfDirectory(
atPath path: String, recursive: Bool = false,
completionHandler: @Sendable @escaping (_ result: Result<[[URLResourceKey: Any]], Error>) -> Void
completionHandler: @Sendable @escaping (_ result: Result<[[URLResourceKey: Any]], any Error>) -> Void
) {
with(completionHandler: completionHandler) { context in
try self.listDirectory(context: context, path: path, recursive: recursive)
Expand Down Expand Up @@ -443,7 +443,7 @@ public class SMB2Manager: NSObject, NSSecureCoding, Codable, NSCopying, CustomRe
*/
open func attributesOfFileSystem(
forPath path: String,
completionHandler: @Sendable @escaping (_ result: Result<[FileAttributeKey: Any], Error>) -> Void
completionHandler: @Sendable @escaping (_ result: Result<[FileAttributeKey: Any], any Error>) -> Void
) {
with(completionHandler: completionHandler) { context in
// This exactly matches implementation of Swift Foundation.
Expand Down Expand Up @@ -487,7 +487,7 @@ public class SMB2Manager: NSObject, NSSecureCoding, Codable, NSCopying, CustomRe
*/
open func attributesOfItem(
atPath path: String,
completionHandler: @Sendable @escaping (_ result: Result<[URLResourceKey: Any], Error>) -> Void
completionHandler: @Sendable @escaping (_ result: Result<[URLResourceKey: Any], any Error>) -> Void
) {
with(completionHandler: completionHandler) { context in
let stat = try context.stat(path)
Expand Down Expand Up @@ -606,7 +606,7 @@ public class SMB2Manager: NSObject, NSSecureCoding, Codable, NSCopying, CustomRe
*/
open func destinationOfSymbolicLink(
atPath path: String,
completionHandler: @Sendable @escaping (_ result: Result<String, Error>) -> Void
completionHandler: @Sendable @escaping (_ result: Result<String, any Error>) -> Void
) {
with(completionHandler: completionHandler) { context in
try context.readlink(path)
Expand Down Expand Up @@ -834,7 +834,7 @@ public class SMB2Manager: NSObject, NSSecureCoding, Codable, NSCopying, CustomRe
open func contents<R: RangeExpression>(
atPath path: String, range: R? = Range<UInt64>?.none,
progress: ReadProgressHandler,
completionHandler: @Sendable @escaping (_ result: Result<Data, Error>) -> Void
completionHandler: @Sendable @escaping (_ result: Result<Data, any Error>) -> Void
) where R.Bound: FixedWidthInteger {
let range = range?.int64Range ?? 0..<Int64.max
with(completionHandler: completionHandler) { context in
Expand Down Expand Up @@ -943,7 +943,7 @@ public class SMB2Manager: NSObject, NSSecureCoding, Codable, NSCopying, CustomRe
*/
open func contents<R: RangeExpression>(
atPath path: String, range: R? = Range<UInt64>?.none
) -> AsyncThrowingStream<Data, Error> where R.Bound: FixedWidthInteger {
) -> AsyncThrowingStream<Data, any Error> where R.Bound: FixedWidthInteger {
let range = range?.int64Range ?? 0..<Int64.max
let (result, continuation) = AsyncThrowingStream.makeStream(of: Data.self, bufferingPolicy: .unbounded)

Expand Down Expand Up @@ -1438,7 +1438,7 @@ extension SMB2Manager {
}

private func with<T>(
completionHandler: @Sendable @escaping (Result<T, Error>) -> Void,
completionHandler: @Sendable @escaping (Result<T, any Error>) -> Void,
handler: @Sendable @escaping (_ context: SMB2Context) throws -> T
) {
queue {
Expand All @@ -1451,7 +1451,7 @@ extension SMB2Manager {
}

private func with<T>(
shareName: String, encrypted: Bool, completionHandler: @Sendable @escaping (Result<T, Error>) -> Void,
shareName: String, encrypted: Bool, completionHandler: @Sendable @escaping (Result<T, any Error>) -> Void,
handler: @Sendable @escaping (_ context: SMB2Context) throws -> T
) {
queue {
Expand Down
4 changes: 2 additions & 2 deletions AMSMB2/Context.swift
Original file line number Diff line number Diff line change
Expand Up @@ -375,7 +375,7 @@ extension SMB2Context {
try withThreadSafeContext { context -> (Int32, DataType) in
var cb = CBData()
var resultData: DataType?
var dataHandlerError: Error?
var dataHandlerError: (any Error)?
cb.dataHandler = { ptr in
do {
resultData = try dataHandler(self, ptr)
Expand Down Expand Up @@ -413,7 +413,7 @@ extension SMB2Context {
try withThreadSafeContext { context -> (UInt32, DataType) in
var cb = CBData()
var resultData: DataType?
var dataHandlerError: Error?
var dataHandlerError: (any Error)?
cb.dataHandler = { ptr in
do {
resultData = try dataHandler(self, ptr)
Expand Down
4 changes: 2 additions & 2 deletions AMSMB2/Extensions.swift
Original file line number Diff line number Diff line change
Expand Up @@ -270,7 +270,7 @@ extension OutputStream {
}
}

func asyncHandler(_ continuation: CheckedContinuation<Void, Error>) -> @Sendable (_ error: Error?) -> Void {
func asyncHandler(_ continuation: CheckedContinuation<Void, any Error>) -> @Sendable (_ error: (any Error)?) -> Void {
{ error in
if let error = error {
continuation.resume(throwing: error)
Expand All @@ -280,7 +280,7 @@ func asyncHandler(_ continuation: CheckedContinuation<Void, Error>) -> @Sendable
}
}

func asyncHandler<T>(_ continuation: CheckedContinuation<T, Error>) -> @Sendable (Result<T, Error>) -> Void {
func asyncHandler<T>(_ continuation: CheckedContinuation<T, any Error>) -> @Sendable (Result<T, any Error>) -> Void {
{ result in
continuation.resume(with: result)
}
Expand Down
20 changes: 10 additions & 10 deletions AMSMB2/ObjCCompat.swift
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ extension SMB2Manager {
*/
@available(swift, obsoleted: 1.0)
@objc(connectShareWithName:completionHandler:)
open func __connectShare(name: String, completionHandler: @Sendable @escaping (_ error: Error?) -> Void) {
open func __connectShare(name: String, completionHandler: @Sendable @escaping (_ error: (any Error)?) -> Void) {
connectShare(name: name, completionHandler: completionHandler)
}

Expand Down Expand Up @@ -61,7 +61,7 @@ extension SMB2Manager {
@available(swift, obsoleted: 1.0)
@objc(listSharesWithCompletionHandler:)
public func __listShares(
completionHandler: @Sendable @escaping (_ names: [String], _ comments: [String], _ error: Error?) -> Void
completionHandler: @Sendable @escaping (_ names: [String], _ comments: [String], _ error: (any Error)?) -> Void
) {
listShares(enumerateHidden: false) { result in
switch result {
Expand All @@ -88,7 +88,7 @@ extension SMB2Manager {
@objc(listSharesWithEnumerateHidden:completionHandler:)
public func __listShares(
enumerateHidden: Bool,
completionHandler: @Sendable @escaping (_ names: [String], _ comments: [String], _ error: Error?) -> Void
completionHandler: @Sendable @escaping (_ names: [String], _ comments: [String], _ error: (any Error)?) -> Void
) {
listShares(enumerateHidden: enumerateHidden) { result in
switch result {
Expand All @@ -114,7 +114,7 @@ extension SMB2Manager {
@objc(contentsOfDirectoryAtPath:recursive:completionHandler:)
public func __contentsOfDirectory(
atPath path: String, recursive: Bool = false,
completionHandler: @Sendable @escaping (_ contents: [[URLResourceKey: Any]]?, _ error: Error?) -> Void
completionHandler: @Sendable @escaping (_ contents: [[URLResourceKey: Any]]?, _ error: (any Error)?) -> Void
) {
contentsOfDirectory(
atPath: path, recursive: recursive, completionHandler: convert(completionHandler)
Expand All @@ -135,7 +135,7 @@ extension SMB2Manager {
@objc(attributesOfFileSystemForPath:completionHandler:)
public func __attributesOfFileSystem(
forPath path: String,
completionHandler: @Sendable @escaping (_ attributes: [FileAttributeKey: Any]?, _ error: Error?) -> Void
completionHandler: @Sendable @escaping (_ attributes: [FileAttributeKey: Any]?, _ error: (any Error)?) -> Void
) {
attributesOfFileSystem(forPath: path, completionHandler: convert(completionHandler))
}
Expand All @@ -153,7 +153,7 @@ extension SMB2Manager {
@objc(attributesOfItemAtPath:completionHandler:)
public func __attributesOfItem(
atPath path: String,
completionHandler: @Sendable @escaping (_ file: [URLResourceKey: Any]?, _ error: Error?) -> Void
completionHandler: @Sendable @escaping (_ file: [URLResourceKey: Any]?, _ error: (any Error)?) -> Void
) {
attributesOfItem(atPath: path, completionHandler: convert(completionHandler))
}
Expand All @@ -172,7 +172,7 @@ extension SMB2Manager {
@objc(destinationOfSymbolicLinkAtPath:completionHandler:)
open func __destinationOfSymbolicLink(
atPath path: String,
completionHandler: @Sendable @escaping (_ destinationPath: String?, _ error: Error?) -> Void
completionHandler: @Sendable @escaping (_ destinationPath: String?, _ error: (any Error)?) -> Void
) {
destinationOfSymbolicLink(atPath: path, completionHandler: convert(completionHandler))
}
Expand Down Expand Up @@ -200,7 +200,7 @@ extension SMB2Manager {
@objc(contentsAtPath:fromOffset:toLength:progress:completionHandler:)
open func __contents(
atPath path: String, offset: Int64 = 0, length: Int = -1, progress: ReadProgressHandler,
completionHandler: @Sendable @escaping (_ contents: Data?, _ error: Error?) -> Void
completionHandler: @Sendable @escaping (_ contents: Data?, _ error: (any Error)?) -> Void
) {
guard offset >= 0 else {
let error = POSIXError(.EINVAL, description: "Invalid content offset.")
Expand Down Expand Up @@ -239,8 +239,8 @@ extension SMB2Manager {
}

extension SMB2Manager {
private func convert<T>(_ resultCompletion: @Sendable @escaping (T?, Error?) -> Void) -> (
@Sendable (Result<T, Error>) -> Void
private func convert<T>(_ resultCompletion: @Sendable @escaping (T?, (any Error)?) -> Void) -> (
@Sendable (Result<T, any Error>) -> Void
) {
{ result in
switch result {
Expand Down
53 changes: 53 additions & 0 deletions AMSMB2Tests/SMB2ManagerTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -386,6 +386,59 @@ class SMB2ManagerTests: XCTestCase {
XCTAssert(outputStream.streamStatus == .closed)
XCTAssert(FileManager.default.contentsEqual(atPath: url.path, andPath: dlURL.path))
}

func testSimultaneousUpload() async throws {
let redownload = false
let fileNums = 5
let files = (1...fileNums).map { "uploadsimtest\($0).dat" }
let urls = (1...fileNums).map {
let size: Int = random(max: 0xf00000)
print(#function, "test size \($0):", size)
return self.dummyFile(size: size)
}

let smb = SMB2Manager(url: server, credential: credential)!
try await smb.connectShare(name: share, encrypted: encrypted)

addTeardownBlock {
try? urls.forEach(FileManager.default.removeItem(at:))
try? urls
.map { $0.appendingPathExtension("download") }
.forEach(FileManager.default.removeItem(at:))
await withTaskGroup(of: Void.self) { group in
for file in files {
group.addTask{
try? await smb.removeFile(atPath: file)
}
}
await group.waitForAll()
}
for file in files {
try? await smb.removeFile(atPath: file)
}
}

try await withThrowingTaskGroup(of: Void.self) { group in
for (file, url) in zip(files, urls) {
group.addTask{
try await smb.uploadItem(at: url, toPath: file, progress: nil)
}
}

try await group.waitForAll()
}

guard redownload else { return }
try await withThrowingTaskGroup(of: Void.self) { group in
for (file, url) in zip(files, urls) {
group.addTask{
try await smb.downloadItem(atPath: file, to: url.appendingPathExtension("download"), progress: nil)
}
}

try await group.waitForAll()
}
}

func testTruncate() async throws {
let smb = SMB2Manager(url: server, credential: credential)!
Expand Down

0 comments on commit 40c382c

Please sign in to comment.