12
12
//
13
13
//===----------------------------------------------------------------------===//
14
14
15
+ import NIO
15
16
@testable import SwiftAwsLambda
16
17
import XCTest
17
18
18
19
class CodableLambdaTest : XCTestCase {
19
20
func testSuccess( ) {
20
- let server = MockLambdaServer ( behavior: GoodBehavior ( ) )
21
+ let server = MockLambdaServer ( behavior: Behavior ( ) )
21
22
XCTAssertNoThrow ( try server. start ( ) . wait ( ) )
22
23
defer { XCTAssertNoThrow ( try server. stop ( ) . wait ( ) ) }
23
24
25
+ struct Handler : LambdaCodableHandler {
26
+ func handle( context: Lambda . Context , payload: Request , callback: @escaping LambdaCodableCallback < Response > ) {
27
+ callback ( . success( Response ( requestId: payload. requestId) ) )
28
+ }
29
+ }
24
30
let maxTimes = Int . random ( in: 1 ... 10 )
25
31
let configuration = Lambda . Configuration ( lifecycle: . init( maxTimes: maxTimes) )
26
- let result = Lambda . run ( handler: CodableEchoHandler ( ) , configuration: configuration)
27
- assertLambdaLifecycleResult ( result: result , shoudHaveRun: maxTimes)
32
+ let result = Lambda . run ( handler: Handler ( ) , configuration: configuration)
33
+ assertLambdaLifecycleResult ( result, shoudHaveRun: maxTimes)
28
34
}
29
35
30
36
func testFailure( ) {
31
- let server = MockLambdaServer ( behavior: BadBehavior ( ) )
37
+ let server = MockLambdaServer ( behavior: Behavior ( result : . failure ( TestError ( " boom " ) ) ) )
32
38
XCTAssertNoThrow ( try server. start ( ) . wait ( ) )
33
39
defer { XCTAssertNoThrow ( try server. stop ( ) . wait ( ) ) }
34
40
35
- let result = Lambda . run ( handler: CodableEchoHandler ( ) )
36
- assertLambdaLifecycleResult ( result: result, shouldFailWithError: LambdaRuntimeClientError . badStatusCode ( . internalServerError) )
41
+ struct Handler : LambdaCodableHandler {
42
+ func handle( context: Lambda . Context , payload: Request , callback: @escaping LambdaCodableCallback < Response > ) {
43
+ callback ( . failure( TestError ( " boom " ) ) )
44
+ }
45
+ }
46
+ let maxTimes = Int . random ( in: 1 ... 10 )
47
+ let configuration = Lambda . Configuration ( lifecycle: . init( maxTimes: maxTimes) )
48
+ let result = Lambda . run ( handler: Handler ( ) , configuration: configuration)
49
+ assertLambdaLifecycleResult ( result, shoudHaveRun: maxTimes)
37
50
}
38
51
39
52
func testClosureSuccess( ) {
40
- let server = MockLambdaServer ( behavior: GoodBehavior ( ) )
53
+ let server = MockLambdaServer ( behavior: Behavior ( ) )
41
54
XCTAssertNoThrow ( try server. start ( ) . wait ( ) )
42
55
defer { XCTAssertNoThrow ( try server. stop ( ) . wait ( ) ) }
43
56
@@ -46,40 +59,34 @@ class CodableLambdaTest: XCTestCase {
46
59
let result = Lambda . run ( configuration: configuration) { ( _, payload: Request , callback) in
47
60
callback ( . success( Response ( requestId: payload. requestId) ) )
48
61
}
49
- assertLambdaLifecycleResult ( result: result , shoudHaveRun: maxTimes)
62
+ assertLambdaLifecycleResult ( result, shoudHaveRun: maxTimes)
50
63
}
51
64
52
65
func testClosureFailure( ) {
53
- let server = MockLambdaServer ( behavior: BadBehavior ( ) )
66
+ let server = MockLambdaServer ( behavior: Behavior ( result : . failure ( TestError ( " boom " ) ) ) )
54
67
XCTAssertNoThrow ( try server. start ( ) . wait ( ) )
55
68
defer { XCTAssertNoThrow ( try server. stop ( ) . wait ( ) ) }
56
69
57
- let result : LambdaLifecycleResult = Lambda . run { ( _, payload: Request , callback) in
58
- callback ( . success( Response ( requestId: payload. requestId) ) )
59
- }
60
- assertLambdaLifecycleResult ( result: result, shouldFailWithError: LambdaRuntimeClientError . badStatusCode ( . internalServerError) )
61
- }
62
- }
63
-
64
- private func assertLambdaLifecycleResult( result: LambdaLifecycleResult , shoudHaveRun: Int = 0 , shouldFailWithError: Error ? = nil ) {
65
- switch result {
66
- case . success( let count) :
67
- if shouldFailWithError != nil {
68
- XCTFail ( " should fail with \( shouldFailWithError!) " )
69
- }
70
- XCTAssertEqual ( shoudHaveRun, count, " should have run \( shoudHaveRun) times " )
71
- case . failure( let error) :
72
- if shouldFailWithError == nil {
73
- XCTFail ( " should succeed, but failed with \( error) " )
74
- break
70
+ let maxTimes = Int . random ( in: 1 ... 10 )
71
+ let configuration = Lambda . Configuration ( lifecycle: . init( maxTimes: maxTimes) )
72
+ let result : Result < Int , Error > = Lambda . run ( configuration: configuration) { ( _, _: Request , callback: ( Result < Response , Error > ) -> Void ) in
73
+ callback ( . failure( TestError ( " boom " ) ) )
75
74
}
76
- XCTAssertEqual ( shouldFailWithError ? . localizedDescription , error . localizedDescription , " expected error to mactch " )
75
+ assertLambdaLifecycleResult ( result , shoudHaveRun : maxTimes )
77
76
}
78
77
}
79
78
80
79
// TODO: taking advantage of the fact we know the serialization is json
81
- private struct GoodBehavior : LambdaServerBehavior {
82
- let requestId = UUID ( ) . uuidString
80
+ private struct Behavior : LambdaServerBehavior {
81
+ let requestId : String
82
+ let payload : String
83
+ let result : Result < String ? , TestError >
84
+
85
+ init ( requestId: String = UUID ( ) . uuidString, payload: String = " hello " , result: Result < String ? , TestError > = . success( " hello " ) ) {
86
+ self . requestId = requestId
87
+ self . payload = payload
88
+ self . result = result
89
+ }
83
90
84
91
func getWork( ) -> GetWorkResult {
85
92
guard let payload = try ? JSONEncoder ( ) . encode ( Request ( requestId: requestId) ) else {
@@ -93,48 +100,48 @@ private struct GoodBehavior: LambdaServerBehavior {
93
100
return . success( ( requestId: self . requestId, payload: payloadAsString) )
94
101
}
95
102
96
- func processResponse( requestId: String , response: String ) -> ProcessResponseResult {
97
- guard let data = response. data ( using: . utf8) else {
98
- XCTFail ( " decoding error " )
99
- return . failure( . internalServerError)
100
- }
101
- guard let response = try ? JSONDecoder ( ) . decode ( Response . self, from: data) else {
102
- XCTFail ( " decoding error " )
103
+ func processResponse( requestId: String , response: String ? ) -> Result < Void , ProcessResponseError > {
104
+ switch self . result {
105
+ case . success( let expected) where expected != nil :
106
+ guard let data = response? . data ( using: . utf8) else {
107
+ XCTFail ( " decoding error " )
108
+ return . failure( . internalServerError)
109
+ }
110
+ guard let response = try ? JSONDecoder ( ) . decode ( Response . self, from: data) else {
111
+ XCTFail ( " decoding error " )
112
+ return . failure( . internalServerError)
113
+ }
114
+ XCTAssertEqual ( self . requestId, response. requestId, " expecting requestId to match " )
115
+ return . success( ( ) )
116
+ case . success( let expected) where expected == nil :
117
+ XCTAssertNil ( response)
118
+ return . success( ( ) )
119
+ case . failure:
120
+ XCTFail ( " unexpected to fail, but succeeded with: \( response ?? " undefined " ) " )
103
121
return . failure( . internalServerError)
122
+ default :
123
+ preconditionFailure ( " invalid state " )
104
124
}
105
- XCTAssertEqual ( self . requestId, response. requestId, " expecting requestId to match " )
106
- return . success
107
125
}
108
126
109
- func processError( requestId: String , error: ErrorResponse ) -> ProcessErrorResult {
110
- XCTFail ( " should not report error " )
111
- return . failure( . internalServerError)
127
+ func processError( requestId: String , error: ErrorResponse ) -> Result < Void , ProcessErrorError > {
128
+ XCTAssertEqual ( self . requestId, requestId, " expecting requestId to match " )
129
+ switch self . result {
130
+ case . success:
131
+ XCTFail ( " unexpected to succeed, but failed with: \( error) " )
132
+ return . failure( . internalServerError)
133
+ case . failure( let expected) :
134
+ XCTAssertEqual ( expected. description, error. errorMessage, " expecting error to match " )
135
+ return . success( ( ) )
136
+ }
112
137
}
113
138
114
- func processInitError( error: ErrorResponse ) -> ProcessInitErrorResult {
139
+ func processInitError( error: ErrorResponse ) -> Result < Void , ProcessErrorError > {
115
140
XCTFail ( " should not report init error " )
116
141
return . failure( . internalServerError)
117
142
}
118
143
}
119
144
120
- private struct BadBehavior : LambdaServerBehavior {
121
- func getWork( ) -> GetWorkResult {
122
- return . failure( . internalServerError)
123
- }
124
-
125
- func processResponse( requestId: String , response: String ) -> ProcessResponseResult {
126
- return . failure( . internalServerError)
127
- }
128
-
129
- func processError( requestId: String , error: ErrorResponse ) -> ProcessErrorResult {
130
- return . failure( . internalServerError)
131
- }
132
-
133
- func processInitError( error: ErrorResponse ) -> ProcessInitErrorResult {
134
- return . failure( . internalServerError)
135
- }
136
- }
137
-
138
145
private struct Request : Codable {
139
146
let requestId : String
140
147
init ( requestId: String ) {
@@ -148,9 +155,3 @@ private struct Response: Codable {
148
155
self . requestId = requestId
149
156
}
150
157
}
151
-
152
- private struct CodableEchoHandler : LambdaCodableHandler {
153
- func handle( context: Lambda . Context , payload: Request , callback: @escaping LambdaCodableCallback < Response > ) {
154
- callback ( . success( Response ( requestId: payload. requestId) ) )
155
- }
156
- }
0 commit comments