Skip to content
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

feat: add public initializer to WaiterOutcome #577

Merged
merged 1 commit into from
Aug 11, 2023
Merged

Conversation

dayaffe
Copy link
Contributor

@dayaffe dayaffe commented Aug 10, 2023

This PR addresses the inability to mock WaiterOutcome by providing a public initializer.

Issue #

awslabs/aws-sdk-swift#1068

Description of changes

Add a public initializer to WaiterOutcome with required parameters attempts and result. This will allow users to create an instance of WaiterOutcome in scripts that depend on smithy-swift.

I was able to reproduce the issue in #1068 using the code provided. With this change I am able to successfully build and the error "'WaiterOutcome' initializer is inaccessible due to 'internal' protection level" no longer occurs.

Example Usage

import XCTest
import AWSS3
import class Foundation.Bundle

@testable import ClientRuntime

// define a protocol with our method
protocol WaitUntilBucketExistsDefaults {
    func waitUntilBucketExists(options: WaiterOptions, input: HeadBucketInput) async throws -> WaiterOutcome<HeadBucketOutputResponse>
}

// extend the S3Client and make sure it conforms to our protocol with the method
// the below definition no longer errors on WaiterOutcome init
extension S3Client: WaitUntilBucketExistsDefaults {
    func waitUntilBucketExists(options: WaiterOptions, input: HeadBucketInput) async throws -> WaiterOutcome<HeadBucketOutputResponse> {
        let response = ClientRuntime.HttpResponse(
            statusCode: .ok
        )
        let output = try await HeadBucketOutputResponse(
            httpResponse: response
        )
        return WaiterOutcome<HeadBucketOutputResponse>(
            attempts: 1,
            result: .success(output)
        )
    }
}

// Example Function
public func createS3Bucket(bucketName: String) async -> String {
    do {
        let client = try S3Client(region: "us-east-1")
        let input = HeadBucketInput(bucket: bucketName)
        let options = WaiterOptions(maxWaitTime: 120.0, minDelay: 5.0, maxDelay: 20.0)
        let outcome = try await client.waitUntilBucketExists(options: options, input: input)
        
        switch outcome.result {
        case .success(_):
            return "SUCCESS"
        default:
            return "FAILURE"
        }
    } catch {
        print("Error occurred while waiting: \(error.localizedDescription)")
        return "ERROR"
    }
}

final class CLIToolTests: XCTestCase {
    
    public func test_createS3Bucket() async {
        let result = await createS3Bucket(bucketName: "testBucket")
        XCTAssertEqual(result, "SUCCESS")
    }
    
}

Scope

  • Bug fix (non-breaking change which fixes an issue)
  • New feature (non-breaking change which adds functionality)

By submitting this pull request, I confirm that my contribution is made under the terms of the Apache 2.0 license.

@dayaffe dayaffe self-assigned this Aug 10, 2023
Copy link
Contributor

@sichanyoo sichanyoo left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM!

@dayaffe dayaffe merged commit ed250b8 into main Aug 11, 2023
7 checks passed
@dayaffe dayaffe deleted the day/add-init-waiter branch August 11, 2023 16:21
@dayaffe dayaffe linked an issue Aug 11, 2023 that may be closed by this pull request
2 tasks
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Make waiter initializers public for mocking
3 participants