Skip to content

Commit 7099c48

Browse files
committed
Don't use hardcoded port for MockServer
1 parent e147ecc commit 7099c48

File tree

4 files changed

+61
-30
lines changed

4 files changed

+61
-30
lines changed

Tests/AWSLambdaRuntimeCoreTests/LambdaHandlerTest.swift

Lines changed: 45 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,9 @@ class LambdaHandlerTest: XCTestCase {
2222

2323
func testBootstrapSimpleNoInit() {
2424
let server = MockLambdaServer(behavior: Behavior())
25-
XCTAssertNoThrow(try server.start().wait())
25+
var port: Int?
26+
XCTAssertNoThrow(port = try server.start().wait())
27+
guard let port else { return XCTFail("Expected the server to have started") }
2628
defer { XCTAssertNoThrow(try server.stop().wait()) }
2729

2830
struct TestBootstrapHandler: SimpleLambdaHandler {
@@ -32,14 +34,16 @@ class LambdaHandlerTest: XCTestCase {
3234
}
3335

3436
let maxTimes = Int.random(in: 10...20)
35-
let configuration = LambdaConfiguration(lifecycle: .init(maxTimes: maxTimes))
37+
let configuration = LambdaConfiguration(lifecycle: .init(maxTimes: maxTimes), runtimeEngine: .init(address: "127.0.0.1:\(port)"))
3638
let result = Lambda.run(configuration: configuration, handlerType: TestBootstrapHandler.self)
3739
assertLambdaRuntimeResult(result, shouldHaveRun: maxTimes)
3840
}
3941

4042
func testBootstrapSimpleInit() {
4143
let server = MockLambdaServer(behavior: Behavior())
42-
XCTAssertNoThrow(try server.start().wait())
44+
var port: Int?
45+
XCTAssertNoThrow(port = try server.start().wait())
46+
guard let port else { return XCTFail("Expected the server to have started") }
4347
defer { XCTAssertNoThrow(try server.stop().wait()) }
4448

4549
struct TestBootstrapHandler: SimpleLambdaHandler {
@@ -56,7 +60,7 @@ class LambdaHandlerTest: XCTestCase {
5660
}
5761

5862
let maxTimes = Int.random(in: 10...20)
59-
let configuration = LambdaConfiguration(lifecycle: .init(maxTimes: maxTimes))
63+
let configuration = LambdaConfiguration(lifecycle: .init(maxTimes: maxTimes), runtimeEngine: .init(address: "127.0.0.1:\(port)"))
6064
let result = Lambda.run(configuration: configuration, handlerType: TestBootstrapHandler.self)
6165
assertLambdaRuntimeResult(result, shouldHaveRun: maxTimes)
6266
}
@@ -65,7 +69,9 @@ class LambdaHandlerTest: XCTestCase {
6569

6670
func testBootstrapSuccess() {
6771
let server = MockLambdaServer(behavior: Behavior())
68-
XCTAssertNoThrow(try server.start().wait())
72+
var port: Int?
73+
XCTAssertNoThrow(port = try server.start().wait())
74+
guard let port else { return XCTFail("Expected the server to have started") }
6975
defer { XCTAssertNoThrow(try server.stop().wait()) }
7076

7177
struct TestBootstrapHandler: LambdaHandler {
@@ -83,14 +89,16 @@ class LambdaHandlerTest: XCTestCase {
8389
}
8490

8591
let maxTimes = Int.random(in: 10...20)
86-
let configuration = LambdaConfiguration(lifecycle: .init(maxTimes: maxTimes))
92+
let configuration = LambdaConfiguration(lifecycle: .init(maxTimes: maxTimes), runtimeEngine: .init(address: "127.0.0.1:\(port)"))
8793
let result = Lambda.run(configuration: configuration, handlerType: TestBootstrapHandler.self)
8894
assertLambdaRuntimeResult(result, shouldHaveRun: maxTimes)
8995
}
9096

9197
func testBootstrapFailure() {
9298
let server = MockLambdaServer(behavior: FailedBootstrapBehavior())
93-
XCTAssertNoThrow(try server.start().wait())
99+
var port: Int?
100+
XCTAssertNoThrow(port = try server.start().wait())
101+
guard let port else { return XCTFail("Expected the server to have started") }
94102
defer { XCTAssertNoThrow(try server.stop().wait()) }
95103

96104
struct TestBootstrapHandler: LambdaHandler {
@@ -108,14 +116,16 @@ class LambdaHandlerTest: XCTestCase {
108116
}
109117

110118
let maxTimes = Int.random(in: 10...20)
111-
let configuration = LambdaConfiguration(lifecycle: .init(maxTimes: maxTimes))
119+
let configuration = LambdaConfiguration(lifecycle: .init(maxTimes: maxTimes), runtimeEngine: .init(address: "127.0.0.1:\(port)"))
112120
let result = Lambda.run(configuration: configuration, handlerType: TestBootstrapHandler.self)
113121
assertLambdaRuntimeResult(result, shouldFailWithError: TestError("kaboom"))
114122
}
115123

116124
func testHandlerSuccess() {
117125
let server = MockLambdaServer(behavior: Behavior())
118-
XCTAssertNoThrow(try server.start().wait())
126+
var port: Int?
127+
XCTAssertNoThrow(port = try server.start().wait())
128+
guard let port else { return XCTFail("Expected the server to have started") }
119129
defer { XCTAssertNoThrow(try server.stop().wait()) }
120130

121131
struct Handler: SimpleLambdaHandler {
@@ -125,30 +135,34 @@ class LambdaHandlerTest: XCTestCase {
125135
}
126136

127137
let maxTimes = Int.random(in: 1...10)
128-
let configuration = LambdaConfiguration(lifecycle: .init(maxTimes: maxTimes))
138+
let configuration = LambdaConfiguration(lifecycle: .init(maxTimes: maxTimes), runtimeEngine: .init(address: "127.0.0.1:\(port)"))
129139
let result = Lambda.run(configuration: configuration, handlerType: Handler.self)
130140
assertLambdaRuntimeResult(result, shouldHaveRun: maxTimes)
131141
}
132142

133143
func testVoidHandlerSuccess() {
134144
let server = MockLambdaServer(behavior: Behavior(result: .success(nil)))
135-
XCTAssertNoThrow(try server.start().wait())
145+
var port: Int?
146+
XCTAssertNoThrow(port = try server.start().wait())
147+
guard let port else { return XCTFail("Expected the server to have started") }
136148
defer { XCTAssertNoThrow(try server.stop().wait()) }
137149

138150
struct Handler: SimpleLambdaHandler {
139151
func handle(_ event: String, context: LambdaContext) async throws {}
140152
}
141153

142154
let maxTimes = Int.random(in: 1...10)
143-
let configuration = LambdaConfiguration(lifecycle: .init(maxTimes: maxTimes))
155+
let configuration = LambdaConfiguration(lifecycle: .init(maxTimes: maxTimes), runtimeEngine: .init(address: "127.0.0.1:\(port)"))
144156

145157
let result = Lambda.run(configuration: configuration, handlerType: Handler.self)
146158
assertLambdaRuntimeResult(result, shouldHaveRun: maxTimes)
147159
}
148160

149161
func testHandlerFailure() {
150162
let server = MockLambdaServer(behavior: Behavior(result: .failure(TestError("boom"))))
151-
XCTAssertNoThrow(try server.start().wait())
163+
var port: Int?
164+
XCTAssertNoThrow(port = try server.start().wait())
165+
guard let port else { return XCTFail("Expected the server to have started") }
152166
defer { XCTAssertNoThrow(try server.stop().wait()) }
153167

154168
struct Handler: SimpleLambdaHandler {
@@ -158,7 +172,7 @@ class LambdaHandlerTest: XCTestCase {
158172
}
159173

160174
let maxTimes = Int.random(in: 1...10)
161-
let configuration = LambdaConfiguration(lifecycle: .init(maxTimes: maxTimes))
175+
let configuration = LambdaConfiguration(lifecycle: .init(maxTimes: maxTimes), runtimeEngine: .init(address: "127.0.0.1:\(port)"))
162176
let result = Lambda.run(configuration: configuration, handlerType: Handler.self)
163177
assertLambdaRuntimeResult(result, shouldHaveRun: maxTimes)
164178
}
@@ -167,7 +181,9 @@ class LambdaHandlerTest: XCTestCase {
167181

168182
func testEventLoopSuccess() {
169183
let server = MockLambdaServer(behavior: Behavior())
170-
XCTAssertNoThrow(try server.start().wait())
184+
var port: Int?
185+
XCTAssertNoThrow(port = try server.start().wait())
186+
guard let port else { return XCTFail("Expected the server to have started") }
171187
defer { XCTAssertNoThrow(try server.stop().wait()) }
172188

173189
struct Handler: EventLoopLambdaHandler {
@@ -181,14 +197,16 @@ class LambdaHandlerTest: XCTestCase {
181197
}
182198

183199
let maxTimes = Int.random(in: 1...10)
184-
let configuration = LambdaConfiguration(lifecycle: .init(maxTimes: maxTimes))
200+
let configuration = LambdaConfiguration(lifecycle: .init(maxTimes: maxTimes), runtimeEngine: .init(address: "127.0.0.1:\(port)"))
185201
let result = Lambda.run(configuration: configuration, handlerType: Handler.self)
186202
assertLambdaRuntimeResult(result, shouldHaveRun: maxTimes)
187203
}
188204

189205
func testVoidEventLoopSuccess() {
190206
let server = MockLambdaServer(behavior: Behavior(result: .success(nil)))
191-
XCTAssertNoThrow(try server.start().wait())
207+
var port: Int?
208+
XCTAssertNoThrow(port = try server.start().wait())
209+
guard let port else { return XCTFail("Expected the server to have started") }
192210
defer { XCTAssertNoThrow(try server.stop().wait()) }
193211

194212
struct Handler: EventLoopLambdaHandler {
@@ -202,14 +220,16 @@ class LambdaHandlerTest: XCTestCase {
202220
}
203221

204222
let maxTimes = Int.random(in: 1...10)
205-
let configuration = LambdaConfiguration(lifecycle: .init(maxTimes: maxTimes))
223+
let configuration = LambdaConfiguration(lifecycle: .init(maxTimes: maxTimes), runtimeEngine: .init(address: "127.0.0.1:\(port)"))
206224
let result = Lambda.run(configuration: configuration, handlerType: Handler.self)
207225
assertLambdaRuntimeResult(result, shouldHaveRun: maxTimes)
208226
}
209227

210228
func testEventLoopFailure() {
211229
let server = MockLambdaServer(behavior: Behavior(result: .failure(TestError("boom"))))
212-
XCTAssertNoThrow(try server.start().wait())
230+
var port: Int?
231+
XCTAssertNoThrow(port = try server.start().wait())
232+
guard let port else { return XCTFail("Expected the server to have started") }
213233
defer { XCTAssertNoThrow(try server.stop().wait()) }
214234

215235
struct Handler: EventLoopLambdaHandler {
@@ -223,14 +243,16 @@ class LambdaHandlerTest: XCTestCase {
223243
}
224244

225245
let maxTimes = Int.random(in: 1...10)
226-
let configuration = LambdaConfiguration(lifecycle: .init(maxTimes: maxTimes))
246+
let configuration = LambdaConfiguration(lifecycle: .init(maxTimes: maxTimes), runtimeEngine: .init(address: "127.0.0.1:\(port)"))
227247
let result = Lambda.run(configuration: configuration, handlerType: Handler.self)
228248
assertLambdaRuntimeResult(result, shouldHaveRun: maxTimes)
229249
}
230250

231251
func testEventLoopBootstrapFailure() {
232252
let server = MockLambdaServer(behavior: FailedBootstrapBehavior())
233-
XCTAssertNoThrow(try server.start().wait())
253+
var port: Int?
254+
XCTAssertNoThrow(port = try server.start().wait())
255+
guard let port else { return XCTFail("Expected the server to have started") }
234256
defer { XCTAssertNoThrow(try server.stop().wait()) }
235257

236258
struct Handler: EventLoopLambdaHandler {
@@ -244,7 +266,8 @@ class LambdaHandlerTest: XCTestCase {
244266
}
245267
}
246268

247-
let result = Lambda.run(configuration: .init(), handlerType: Handler.self)
269+
let configuration = LambdaConfiguration(runtimeEngine: .init(address: "127.0.0.1:\(port)"))
270+
let result = Lambda.run(configuration: configuration, handlerType: Handler.self)
248271
assertLambdaRuntimeResult(result, shouldFailWithError: TestError("kaboom"))
249272
}
250273
}

Tests/AWSLambdaRuntimeCoreTests/LambdaTest.swift

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -272,11 +272,14 @@ class LambdaTest: XCTestCase {
272272
let eventLoopGroup = MultiThreadedEventLoopGroup(numberOfThreads: 1)
273273
defer { XCTAssertNoThrow(try eventLoopGroup.syncShutdownGracefully()) }
274274

275-
let server = try await MockLambdaServer(behavior: Behavior()).start().get()
275+
let server = MockLambdaServer(behavior: Behavior(), port: 0)
276+
var port: Int?
277+
XCTAssertNoThrow(port = try server.start().wait())
278+
guard let port else { return XCTFail("Expected the server to have started") }
276279
defer { XCTAssertNoThrow(try server.stop().wait()) }
277280

278281
let logger = Logger(label: "TestLogger")
279-
let configuration = LambdaConfiguration(runtimeEngine: .init(requestTimeout: .milliseconds(100)))
282+
let configuration = LambdaConfiguration(runtimeEngine: .init(address: "127.0.0.1:\(port)", requestTimeout: .milliseconds(100)))
280283

281284
let handler1 = Handler()
282285
let task = Task.detached {

Tests/AWSLambdaRuntimeCoreTests/MockLambdaServer.swift

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -43,14 +43,18 @@ final class MockLambdaServer {
4343
assert(shutdown)
4444
}
4545

46-
func start() -> EventLoopFuture<MockLambdaServer> {
46+
func start() -> EventLoopFuture<Int> {
4747
let bootstrap = ServerBootstrap(group: group)
4848
.serverChannelOption(ChannelOptions.socket(SocketOptionLevel(SOL_SOCKET), SO_REUSEADDR), value: 1)
4949
.childChannelInitializer { channel in
50-
channel.pipeline.configureHTTPServerPipeline(withErrorHandling: true).flatMap { _ in
51-
channel.pipeline.addHandler(
50+
do {
51+
try channel.pipeline.syncOperations.configureHTTPServerPipeline(withErrorHandling: true)
52+
try channel.pipeline.syncOperations.addHandler(
5253
HTTPHandler(logger: self.logger, keepAlive: self.keepAlive, behavior: self.behavior)
5354
)
55+
return channel.eventLoop.makeSucceededVoidFuture()
56+
} catch {
57+
return channel.eventLoop.makeFailedFuture(error)
5458
}
5559
}
5660
return bootstrap.bind(host: self.host, port: self.port).flatMap { channel in
@@ -59,7 +63,7 @@ final class MockLambdaServer {
5963
return channel.eventLoop.makeFailedFuture(ServerError.cantBind)
6064
}
6165
self.logger.info("\(self) started and listening on \(localAddress)")
62-
return channel.eventLoop.makeSucceededFuture(self)
66+
return channel.eventLoop.makeSucceededFuture(localAddress.port!)
6367
}
6468
}
6569

Tests/AWSLambdaRuntimeCoreTests/Utils.swift

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -82,10 +82,11 @@ func runLambda(
8282
let eventLoopGroup = MultiThreadedEventLoopGroup(numberOfThreads: 1)
8383
defer { XCTAssertNoThrow(try eventLoopGroup.syncShutdownGracefully()) }
8484
let logger = Logger(label: "TestLogger")
85-
let configuration = LambdaConfiguration(runtimeEngine: .init(requestTimeout: .milliseconds(100)))
85+
let server = MockLambdaServer(behavior: behavior, port: 0)
86+
let port = try server.start().wait()
87+
let configuration = LambdaConfiguration(runtimeEngine: .init(address: "127.0.0.1:\(port)", requestTimeout: .milliseconds(100)))
8688
let terminator = LambdaTerminator()
8789
let runner = LambdaRunner(eventLoop: eventLoopGroup.next(), configuration: configuration)
88-
let server = try MockLambdaServer(behavior: behavior).start().wait()
8990
defer { XCTAssertNoThrow(try server.stop().wait()) }
9091
try runner.initialize(handlerProvider: handlerProvider, logger: logger, terminator: terminator).flatMap { handler in
9192
runner.run(handler: handler, logger: logger)

0 commit comments

Comments
 (0)