-
Notifications
You must be signed in to change notification settings - Fork 80
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'main' into jbe/sendable_document_and_streams
- Loading branch information
Showing
3 changed files
with
116 additions
and
0 deletions.
There are no files selected for viewing
49 changes: 49 additions & 0 deletions
49
IntegrationTests/AWSIntegrationTestUtils/ConcurrentTestHelper.swift
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 |
---|---|---|
@@ -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() | ||
} | ||
} |
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
57 changes: 57 additions & 0 deletions
57
IntegrationTests/Services/AWSS3IntegrationTests/S3ConcurrentTests.swift
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 |
---|---|---|
@@ -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) | ||
} | ||
} |