Skip to content

Commit

Permalink
Merge branch 'main' into jbe/sendable_document_and_streams
Browse files Browse the repository at this point in the history
  • Loading branch information
jbelkins authored Sep 23, 2024
2 parents 5dfc2a7 + fc8e819 commit ee90d27
Show file tree
Hide file tree
Showing 3 changed files with 116 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
//
// Copyright Amazon.com Inc. or its affiliates.
// All Rights Reserved.
//
// SPDX-License-Identifier: Apache-2.0
//

/// Runs a test concurrently for the number of times specified
///
/// The test is run repeatedly using a Swift task group; each test run is performed as a "child task".
/// The function returns when all test runs have completed, and rethrows if a test run throws.
/// - Parameters:
/// - count: The number of test runs
/// - test: The function pointer for the test to run
/// - Throws: Any error thrown by one of the test runs.
public func repeatConcurrently(count: Int, test: @escaping () async throws -> Void) async throws {
try await withThrowingTaskGroup(of: Void.self) { taskGroup in
for _ in 0..<count {
taskGroup.addTask {
try await test()
}
}
try await taskGroup.waitForAll()
}
}

/// Runs a test concurrently for the number of times specified with args
///
/// The test is run repeatedly using a Swift task group; each test run is performed as a "child task".
/// The function returns when all test runs have completed, and rethrows if a test run throws.
/// - Parameters:
/// - count: The number of test runs
/// - test: The function pointer for the test to run
/// - args: Any values to pass along to test function
/// - Throws: Any error thrown by one of the test runs.
public func repeatConcurrentlyWithArgs(
count: Int,
test: @escaping (Any...) async throws -> Void,
args: Any...
) async throws {
try await withThrowingTaskGroup(of: Void.self) { taskGroup in
for _ in 0..<count {
taskGroup.addTask {
try await test(args)
}
}
try await taskGroup.waitForAll()
}
}
10 changes: 10 additions & 0 deletions IntegrationTests/Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,9 @@ import PackageDescription
// MARK: - Target dependencies

extension Target.Dependency {
// Test utility module
static var awsIntegrationTestUtils: Self { "AWSIntegrationTestUtils" }

// AWS modules
static var awsClientRuntime: Self { .product(name: "AWSClientRuntime", package: "aws-sdk-swift") }
static var awsSDKCommon: Self { .product(name: "AWSSDKCommon", package: "aws-sdk-swift") }
Expand All @@ -36,6 +39,12 @@ let package = Package(
.iOS(.v13),
.tvOS(.v13),
.watchOS(.v6)
],
targets: [
.target(
name: "AWSIntegrationTestUtils",
path: "./AWSIntegrationTestUtils"
)
]
)

Expand Down Expand Up @@ -123,6 +132,7 @@ func addIntegrationTestTarget(_ name: String) {
.awsSDKIdentity,
.smithyIdentity,
.awsSDKCommon,
.awsIntegrationTestUtils,
.product(name: name, package: "aws-sdk-swift")
] + additionalDependencies.map {
Target.Dependency.product(name: $0, package: "aws-sdk-swift", condition: nil)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
//
// Copyright Amazon.com Inc. or its affiliates.
// All Rights Reserved.
//
// SPDX-License-Identifier: Apache-2.0
//

import Foundation
import Smithy
import SmithyStreams
import XCTest
import AWSS3
import AWSIntegrationTestUtils

class S3ConcurrentTests: S3XCTestCase {
public var fileData: Data!
let MEGABYTE: Double = 1_000_000

// Payload below 1,048,576 bytes; sends as simple data payload
func test_100x_1MB_getObject() async throws {
fileData = try generateDummyTextData(numMegabytes: MEGABYTE)
try await repeatConcurrentlyWithArgs(count: 100, test: getObject, args: fileData!)
}

// Payload over 1,048,576 bytes; uses aws chunked encoding & flexible checksum
func test_100x_1_5MB_getObject() async throws {
fileData = try generateDummyTextData(numMegabytes: MEGABYTE * 1.5)
try await repeatConcurrentlyWithArgs(count: 100, test: getObject, args: fileData!)
}

/* Helper functions */

// Generates text data in increments of 10 bytes
func generateDummyTextData(numMegabytes: Double) throws -> Data {
let segmentData = Data("1234567890".utf8)
var wholeData = Data()
for _ in 0..<(Int(numMegabytes)/10) {
wholeData.append(segmentData)
}
return wholeData
}

// Puts data to S3, gets the uploaded file, then asserts retrieved data equals original data
func getObject(args: Any...) async throws {
guard let data = args[0] as? Data else {
throw ClientError.dataNotFound("Failed to retrieve dummy data.")
}
let file = ByteStream.data(data)
let objectKey = UUID().uuidString.split(separator: "-").first!.lowercased()
let putObjectInput = PutObjectInput(body: file, bucket: bucketName, key: objectKey)
_ = try await client.putObject(input: putObjectInput)
let retrievedData = try await client.getObject(input: GetObjectInput(
bucket: bucketName, key: objectKey
)).body?.readData()
XCTAssertEqual(data, retrievedData)
}
}

0 comments on commit ee90d27

Please sign in to comment.