Skip to content

Commit

Permalink
Merge pull request #92 from amzn/mock_and_mock_throwing_implementations
Browse files Browse the repository at this point in the history
Mock and mock throwing implementations
  • Loading branch information
tachyonics authored Jul 2, 2021
2 parents dabe4f6 + 8fbb919 commit 31a1cd5
Show file tree
Hide file tree
Showing 8 changed files with 734 additions and 11 deletions.
8 changes: 4 additions & 4 deletions Package.resolved
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@
"repositoryURL": "https://github.com/amzn/smoke-http.git",
"state": {
"branch": null,
"revision": "8411ef45e7b683ea80b568bb30c3c53b532dcbed",
"version": "2.8.4"
"revision": "0f0ac49e96208b709a58a5e5d59d05bab44957aa",
"version": "2.8.5"
}
},
{
Expand Down Expand Up @@ -51,8 +51,8 @@
"repositoryURL": "https://github.com/apple/swift-nio.git",
"state": {
"branch": null,
"revision": "d161bf658780b209c185994528e7e24376cf7283",
"version": "2.29.0"
"revision": "d79e33308b0ac83326b0ead0ea6446e604b8162d",
"version": "2.30.0"
}
},
{
Expand Down
2 changes: 1 addition & 1 deletion Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@ let package = Package(
.package(url: "https://github.com/apple/swift-log", from: "1.0.0"),
.package(url: "https://github.com/apple/swift-metrics.git", "1.0.0"..<"3.0.0"),
.package(url: "https://github.com/LiveUI/XMLCoding.git", from: "0.4.1"),
.package(url: "https://github.com/amzn/smoke-http.git", from: "2.8.0"),
.package(url: "https://github.com/amzn/smoke-http.git", from: "2.8.5"),
.package(url: "https://github.com/apple/swift-crypto.git", from: "1.0.0"),
],
targets: [
Expand Down
135 changes: 135 additions & 0 deletions Sources/SmokeAWSHttp/MockClientProtocol.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,135 @@
//
// MockClientProtocol.swift
//

import NIO

public protocol MockClientProtocol {

}

/**
Implementations for a mock service client.

A function override directly returning a result and/or an EventLoopFuture override returning an
`EventLoopFuture` that will provide a result at a later time can be provided.

If the function override is provided, the implementation will return - via a future - the result
provided by this override or will throw any error thrown by the override.

Otherwise, if the `EventLoopFuture` override is provided, the implementation will return the result
provided by the `EventLoopFuture` or will throw any error that fails the future. This override is ignored if the first
function override is provided.

Otherwise, the implementation will return the default value provided.
*/
public extension MockClientProtocol {

func mockEventLoopFutureExecuteWithInputWithOutput<InputType, OutputType>(
input: InputType,
defaultResult: OutputType,
eventLoop: EventLoop,
functionOverride: ((InputType) throws -> OutputType)?,
eventLoopFutureFunctionOverride: ((InputType) -> EventLoopFuture<OutputType>)?) -> EventLoopFuture<OutputType> {
let promise = eventLoop.makePromise(of: OutputType.self)

if let functionOverride = functionOverride {
do {
let overrideResult = try functionOverride(input)
promise.succeed(overrideResult)
} catch {
promise.fail(error)
}

return promise.futureResult
}

if let eventLoopFutureFunctionOverride = eventLoopFutureFunctionOverride {
return eventLoopFutureFunctionOverride(input)
}

promise.succeed(defaultResult)

return promise.futureResult
}

func mockEventLoopFutureExecuteWithInputWithoutOutput<InputType>(
input: InputType,
eventLoop: EventLoop,
functionOverride: ((InputType) throws -> ())?,
eventLoopFutureFunctionOverride: ((InputType) -> EventLoopFuture<Void>)?) -> EventLoopFuture<Void> {
let promise = eventLoop.makePromise(of: Void.self)

if let functionOverride = functionOverride {
do {
try functionOverride(input)
promise.succeed(())
} catch {
promise.fail(error)
}

return promise.futureResult
}

if let eventLoopFutureFunctionOverride = eventLoopFutureFunctionOverride {
return eventLoopFutureFunctionOverride(input)
}

promise.succeed(())

return promise.futureResult
}

func mockEventLoopFutureExecuteWithoutInputWithOutput<OutputType>(
defaultResult: OutputType,
eventLoop: EventLoop,
functionOverride: (() throws -> OutputType)?,
eventLoopFutureFunctionOverride: (() -> EventLoopFuture<OutputType>)?) -> EventLoopFuture<OutputType> {
let promise = eventLoop.makePromise(of: OutputType.self)

if let functionOverride = functionOverride {
do {
let overrideResult = try functionOverride()
promise.succeed(overrideResult)
} catch {
promise.fail(error)
}

return promise.futureResult
}

if let eventLoopFutureFunctionOverride = eventLoopFutureFunctionOverride {
return eventLoopFutureFunctionOverride()
}

promise.succeed(defaultResult)

return promise.futureResult
}

func mockEventLoopFutureExecuteWithoutInputWithoutOutput(
eventLoop: EventLoop,
functionOverride: (() throws -> ())?,
eventLoopFutureFunctionOverride: (() -> EventLoopFuture<Void>)?) -> EventLoopFuture<Void> {
let promise = eventLoop.makePromise(of: Void.self)

if let functionOverride = functionOverride {
do {
try functionOverride()
promise.succeed(())
} catch {
promise.fail(error)
}

return promise.futureResult
}

if let eventLoopFutureFunctionOverride = eventLoopFutureFunctionOverride {
return eventLoopFutureFunctionOverride()
}

promise.succeed(())

return promise.futureResult
}
}
138 changes: 138 additions & 0 deletions Sources/SmokeAWSHttp/MockThrowingClientProtocol.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,138 @@
//
// MockThrowingClientProtocol.swift
//

import NIO
import NIOHTTP1

public protocol MockThrowingClientProtocol {

}

/**
Implementations for a mock service client.

A function override directly returning a result and/or an EventLoopFuture override returning an
`EventLoopFuture` that will provide a result at a later time can be provided.

If the function override is provided, the implementation will return - via a future - the result
provided by this override or will throw any error thrown by the override.

Otherwise, if the `EventLoopFuture` override is provided, the implementation will return the result
provided by the `EventLoopFuture` or will throw any error that fails the future. This override is ignored if the first
function override is provided.

Otherwise, the implementation will throw the error provided.
*/
public extension MockThrowingClientProtocol {

func mockThrowingEventLoopFutureExecuteWithInputWithOutput<InputType, OutputType>(
input: InputType,
defaultError: Error,
eventLoop: EventLoop,
functionOverride: ((InputType) throws -> OutputType)?,
eventLoopFutureFunctionOverride: ((InputType) -> EventLoopFuture<OutputType>)?) -> EventLoopFuture<OutputType> {
let promise = eventLoop.makePromise(of: OutputType.self)

if let functionOverride = functionOverride {
do {
let overrideResult = try functionOverride(input)
promise.succeed(overrideResult)
} catch {
promise.fail(error)
}

return promise.futureResult
}

if let eventLoopFutureFunctionOverride = eventLoopFutureFunctionOverride {
return eventLoopFutureFunctionOverride(input)
}

promise.fail(defaultError)

return promise.futureResult
}

func mockThrowingEventLoopFutureExecuteWithInputWithoutOutput<InputType>(
input: InputType,
defaultError: Error,
eventLoop: EventLoop,
functionOverride: ((InputType) throws -> ())?,
eventLoopFutureFunctionOverride: ((InputType) -> EventLoopFuture<Void>)?) -> EventLoopFuture<Void> {
let promise = eventLoop.makePromise(of: Void.self)

if let functionOverride = functionOverride {
do {
try functionOverride(input)
promise.succeed(())
} catch {
promise.fail(error)
}

return promise.futureResult
}

if let eventLoopFutureFunctionOverride = eventLoopFutureFunctionOverride {
return eventLoopFutureFunctionOverride(input)
}

promise.fail(defaultError)

return promise.futureResult
}

func mockThrowingEventLoopFutureExecuteWithoutInputWithOutput<OutputType>(
defaultError: Error,
eventLoop: EventLoop,
functionOverride: (() throws -> OutputType)?,
eventLoopFutureFunctionOverride: (() -> EventLoopFuture<OutputType>)?) -> EventLoopFuture<OutputType> {
let promise = eventLoop.makePromise(of: OutputType.self)

if let functionOverride = functionOverride {
do {
let overrideResult = try functionOverride()
promise.succeed(overrideResult)
} catch {
promise.fail(error)
}

return promise.futureResult
}

if let eventLoopFutureFunctionOverride = eventLoopFutureFunctionOverride {
return eventLoopFutureFunctionOverride()
}

promise.fail(defaultError)

return promise.futureResult
}

func mockThrowingEventLoopFutureExecuteWithoutInputWithoutOutput(
defaultError: Error,
eventLoop: EventLoop,
functionOverride: (() throws -> ())?,
eventLoopFutureFunctionOverride: (() -> EventLoopFuture<Void>)?) -> EventLoopFuture<Void> {
let promise = eventLoop.makePromise(of: Void.self)

if let functionOverride = functionOverride {
do {
try functionOverride()
promise.succeed(())
} catch {
promise.fail(error)
}

return promise.futureResult
}

if let eventLoopFutureFunctionOverride = eventLoopFutureFunctionOverride {
return eventLoopFutureFunctionOverride()
}

promise.fail(defaultError)

return promise.futureResult
}
}
6 changes: 3 additions & 3 deletions Sources/_SmokeAWSHttpConcurrency/AWSClientProtocol.swift
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
// AWSClientProtocol.swift
//

#if compiler(>=5.5) && $AsyncAwait
#if compiler(>=5.5)

import SmokeHTTPClient
import SmokeAWSCore
Expand All @@ -14,7 +14,7 @@ import SmokeAWSHttp
import _SmokeHTTPClientConcurrency

public extension AWSClientProtocol {
@available(macOS 9999, iOS 9999, watchOS 9999, tvOS 9999, *)
@available(macOS 12, iOS 15, tvOS 15, watchOS 8, *)
func executeWithoutOutput<InvocationReportingType: HTTPClientInvocationReporting,
InputType: HTTPRequestInputProtocol, ErrorType: ConvertableError>(
httpClient: HTTPOperationsClient,
Expand Down Expand Up @@ -50,7 +50,7 @@ public extension AWSClientProtocol {
}
}

@available(macOS 9999, iOS 9999, watchOS 9999, tvOS 9999, *)
@available(macOS 12, iOS 15, tvOS 15, watchOS 8, *)
func executeWithOutput<OutputType: HTTPResponseOutputProtocol, InvocationReportingType: HTTPClientInvocationReporting,
InputType: HTTPRequestInputProtocol, ErrorType: ConvertableError>(
httpClient: HTTPOperationsClient,
Expand Down
6 changes: 3 additions & 3 deletions Sources/_SmokeAWSHttpConcurrency/AWSQueryClientProtocol.swift
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
// AWSClientProtocol.swift
//

#if compiler(>=5.5) && $AsyncAwait
#if compiler(>=5.5)

import SmokeHTTPClient
import SmokeAWSCore
Expand All @@ -11,7 +11,7 @@ import SmokeAWSHttp
import _SmokeHTTPClientConcurrency

public extension AWSQueryClientProtocol {
@available(macOS 9999, iOS 9999, watchOS 9999, tvOS 9999, *)
@available(macOS 12, iOS 15, tvOS 15, watchOS 8, *)
func executeWithoutOutput<InvocationReportingType: HTTPClientInvocationReporting,
WrappedInputType: HTTPRequestInputProtocol, ErrorType: ConvertableError>(
httpClient: HTTPOperationsClient,
Expand Down Expand Up @@ -47,7 +47,7 @@ public extension AWSQueryClientProtocol {
}
}

@available(macOS 9999, iOS 9999, watchOS 9999, tvOS 9999, *)
@available(macOS 12, iOS 15, tvOS 15, watchOS 8, *)
func executeWithOutput<OutputType: HTTPResponseOutputProtocol, InvocationReportingType: HTTPClientInvocationReporting,
WrappedInputType: HTTPRequestInputProtocol, ErrorType: ConvertableError>(
httpClient: HTTPOperationsClient,
Expand Down
Loading

0 comments on commit 31a1cd5

Please sign in to comment.