Skip to content

Commit 223c3ba

Browse files
authored
Don't use hardcoded port for MockServer (#350)
1 parent e147ecc commit 223c3ba

File tree

4 files changed

+95
-30
lines changed

4 files changed

+95
-30
lines changed

Tests/AWSLambdaRuntimeCoreTests/LambdaHandlerTest.swift

Lines changed: 75 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,19 @@ 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(
38+
lifecycle: .init(maxTimes: maxTimes),
39+
runtimeEngine: .init(address: "127.0.0.1:\(port)")
40+
)
3641
let result = Lambda.run(configuration: configuration, handlerType: TestBootstrapHandler.self)
3742
assertLambdaRuntimeResult(result, shouldHaveRun: maxTimes)
3843
}
3944

4045
func testBootstrapSimpleInit() {
4146
let server = MockLambdaServer(behavior: Behavior())
42-
XCTAssertNoThrow(try server.start().wait())
47+
var port: Int?
48+
XCTAssertNoThrow(port = try server.start().wait())
49+
guard let port else { return XCTFail("Expected the server to have started") }
4350
defer { XCTAssertNoThrow(try server.stop().wait()) }
4451

4552
struct TestBootstrapHandler: SimpleLambdaHandler {
@@ -56,7 +63,10 @@ class LambdaHandlerTest: XCTestCase {
5663
}
5764

5865
let maxTimes = Int.random(in: 10...20)
59-
let configuration = LambdaConfiguration(lifecycle: .init(maxTimes: maxTimes))
66+
let configuration = LambdaConfiguration(
67+
lifecycle: .init(maxTimes: maxTimes),
68+
runtimeEngine: .init(address: "127.0.0.1:\(port)")
69+
)
6070
let result = Lambda.run(configuration: configuration, handlerType: TestBootstrapHandler.self)
6171
assertLambdaRuntimeResult(result, shouldHaveRun: maxTimes)
6272
}
@@ -65,7 +75,9 @@ class LambdaHandlerTest: XCTestCase {
6575

6676
func testBootstrapSuccess() {
6777
let server = MockLambdaServer(behavior: Behavior())
68-
XCTAssertNoThrow(try server.start().wait())
78+
var port: Int?
79+
XCTAssertNoThrow(port = try server.start().wait())
80+
guard let port else { return XCTFail("Expected the server to have started") }
6981
defer { XCTAssertNoThrow(try server.stop().wait()) }
7082

7183
struct TestBootstrapHandler: LambdaHandler {
@@ -83,14 +95,19 @@ class LambdaHandlerTest: XCTestCase {
8395
}
8496

8597
let maxTimes = Int.random(in: 10...20)
86-
let configuration = LambdaConfiguration(lifecycle: .init(maxTimes: maxTimes))
98+
let configuration = LambdaConfiguration(
99+
lifecycle: .init(maxTimes: maxTimes),
100+
runtimeEngine: .init(address: "127.0.0.1:\(port)")
101+
)
87102
let result = Lambda.run(configuration: configuration, handlerType: TestBootstrapHandler.self)
88103
assertLambdaRuntimeResult(result, shouldHaveRun: maxTimes)
89104
}
90105

91106
func testBootstrapFailure() {
92107
let server = MockLambdaServer(behavior: FailedBootstrapBehavior())
93-
XCTAssertNoThrow(try server.start().wait())
108+
var port: Int?
109+
XCTAssertNoThrow(port = try server.start().wait())
110+
guard let port else { return XCTFail("Expected the server to have started") }
94111
defer { XCTAssertNoThrow(try server.stop().wait()) }
95112

96113
struct TestBootstrapHandler: LambdaHandler {
@@ -108,14 +125,19 @@ class LambdaHandlerTest: XCTestCase {
108125
}
109126

110127
let maxTimes = Int.random(in: 10...20)
111-
let configuration = LambdaConfiguration(lifecycle: .init(maxTimes: maxTimes))
128+
let configuration = LambdaConfiguration(
129+
lifecycle: .init(maxTimes: maxTimes),
130+
runtimeEngine: .init(address: "127.0.0.1:\(port)")
131+
)
112132
let result = Lambda.run(configuration: configuration, handlerType: TestBootstrapHandler.self)
113133
assertLambdaRuntimeResult(result, shouldFailWithError: TestError("kaboom"))
114134
}
115135

116136
func testHandlerSuccess() {
117137
let server = MockLambdaServer(behavior: Behavior())
118-
XCTAssertNoThrow(try server.start().wait())
138+
var port: Int?
139+
XCTAssertNoThrow(port = try server.start().wait())
140+
guard let port else { return XCTFail("Expected the server to have started") }
119141
defer { XCTAssertNoThrow(try server.stop().wait()) }
120142

121143
struct Handler: SimpleLambdaHandler {
@@ -125,30 +147,40 @@ class LambdaHandlerTest: XCTestCase {
125147
}
126148

127149
let maxTimes = Int.random(in: 1...10)
128-
let configuration = LambdaConfiguration(lifecycle: .init(maxTimes: maxTimes))
150+
let configuration = LambdaConfiguration(
151+
lifecycle: .init(maxTimes: maxTimes),
152+
runtimeEngine: .init(address: "127.0.0.1:\(port)")
153+
)
129154
let result = Lambda.run(configuration: configuration, handlerType: Handler.self)
130155
assertLambdaRuntimeResult(result, shouldHaveRun: maxTimes)
131156
}
132157

133158
func testVoidHandlerSuccess() {
134159
let server = MockLambdaServer(behavior: Behavior(result: .success(nil)))
135-
XCTAssertNoThrow(try server.start().wait())
160+
var port: Int?
161+
XCTAssertNoThrow(port = try server.start().wait())
162+
guard let port else { return XCTFail("Expected the server to have started") }
136163
defer { XCTAssertNoThrow(try server.stop().wait()) }
137164

138165
struct Handler: SimpleLambdaHandler {
139166
func handle(_ event: String, context: LambdaContext) async throws {}
140167
}
141168

142169
let maxTimes = Int.random(in: 1...10)
143-
let configuration = LambdaConfiguration(lifecycle: .init(maxTimes: maxTimes))
170+
let configuration = LambdaConfiguration(
171+
lifecycle: .init(maxTimes: maxTimes),
172+
runtimeEngine: .init(address: "127.0.0.1:\(port)")
173+
)
144174

145175
let result = Lambda.run(configuration: configuration, handlerType: Handler.self)
146176
assertLambdaRuntimeResult(result, shouldHaveRun: maxTimes)
147177
}
148178

149179
func testHandlerFailure() {
150180
let server = MockLambdaServer(behavior: Behavior(result: .failure(TestError("boom"))))
151-
XCTAssertNoThrow(try server.start().wait())
181+
var port: Int?
182+
XCTAssertNoThrow(port = try server.start().wait())
183+
guard let port else { return XCTFail("Expected the server to have started") }
152184
defer { XCTAssertNoThrow(try server.stop().wait()) }
153185

154186
struct Handler: SimpleLambdaHandler {
@@ -158,7 +190,10 @@ class LambdaHandlerTest: XCTestCase {
158190
}
159191

160192
let maxTimes = Int.random(in: 1...10)
161-
let configuration = LambdaConfiguration(lifecycle: .init(maxTimes: maxTimes))
193+
let configuration = LambdaConfiguration(
194+
lifecycle: .init(maxTimes: maxTimes),
195+
runtimeEngine: .init(address: "127.0.0.1:\(port)")
196+
)
162197
let result = Lambda.run(configuration: configuration, handlerType: Handler.self)
163198
assertLambdaRuntimeResult(result, shouldHaveRun: maxTimes)
164199
}
@@ -167,7 +202,9 @@ class LambdaHandlerTest: XCTestCase {
167202

168203
func testEventLoopSuccess() {
169204
let server = MockLambdaServer(behavior: Behavior())
170-
XCTAssertNoThrow(try server.start().wait())
205+
var port: Int?
206+
XCTAssertNoThrow(port = try server.start().wait())
207+
guard let port else { return XCTFail("Expected the server to have started") }
171208
defer { XCTAssertNoThrow(try server.stop().wait()) }
172209

173210
struct Handler: EventLoopLambdaHandler {
@@ -181,14 +218,19 @@ class LambdaHandlerTest: XCTestCase {
181218
}
182219

183220
let maxTimes = Int.random(in: 1...10)
184-
let configuration = LambdaConfiguration(lifecycle: .init(maxTimes: maxTimes))
221+
let configuration = LambdaConfiguration(
222+
lifecycle: .init(maxTimes: maxTimes),
223+
runtimeEngine: .init(address: "127.0.0.1:\(port)")
224+
)
185225
let result = Lambda.run(configuration: configuration, handlerType: Handler.self)
186226
assertLambdaRuntimeResult(result, shouldHaveRun: maxTimes)
187227
}
188228

189229
func testVoidEventLoopSuccess() {
190230
let server = MockLambdaServer(behavior: Behavior(result: .success(nil)))
191-
XCTAssertNoThrow(try server.start().wait())
231+
var port: Int?
232+
XCTAssertNoThrow(port = try server.start().wait())
233+
guard let port else { return XCTFail("Expected the server to have started") }
192234
defer { XCTAssertNoThrow(try server.stop().wait()) }
193235

194236
struct Handler: EventLoopLambdaHandler {
@@ -202,14 +244,19 @@ class LambdaHandlerTest: XCTestCase {
202244
}
203245

204246
let maxTimes = Int.random(in: 1...10)
205-
let configuration = LambdaConfiguration(lifecycle: .init(maxTimes: maxTimes))
247+
let configuration = LambdaConfiguration(
248+
lifecycle: .init(maxTimes: maxTimes),
249+
runtimeEngine: .init(address: "127.0.0.1:\(port)")
250+
)
206251
let result = Lambda.run(configuration: configuration, handlerType: Handler.self)
207252
assertLambdaRuntimeResult(result, shouldHaveRun: maxTimes)
208253
}
209254

210255
func testEventLoopFailure() {
211256
let server = MockLambdaServer(behavior: Behavior(result: .failure(TestError("boom"))))
212-
XCTAssertNoThrow(try server.start().wait())
257+
var port: Int?
258+
XCTAssertNoThrow(port = try server.start().wait())
259+
guard let port else { return XCTFail("Expected the server to have started") }
213260
defer { XCTAssertNoThrow(try server.stop().wait()) }
214261

215262
struct Handler: EventLoopLambdaHandler {
@@ -223,14 +270,19 @@ class LambdaHandlerTest: XCTestCase {
223270
}
224271

225272
let maxTimes = Int.random(in: 1...10)
226-
let configuration = LambdaConfiguration(lifecycle: .init(maxTimes: maxTimes))
273+
let configuration = LambdaConfiguration(
274+
lifecycle: .init(maxTimes: maxTimes),
275+
runtimeEngine: .init(address: "127.0.0.1:\(port)")
276+
)
227277
let result = Lambda.run(configuration: configuration, handlerType: Handler.self)
228278
assertLambdaRuntimeResult(result, shouldHaveRun: maxTimes)
229279
}
230280

231281
func testEventLoopBootstrapFailure() {
232282
let server = MockLambdaServer(behavior: FailedBootstrapBehavior())
233-
XCTAssertNoThrow(try server.start().wait())
283+
var port: Int?
284+
XCTAssertNoThrow(port = try server.start().wait())
285+
guard let port else { return XCTFail("Expected the server to have started") }
234286
defer { XCTAssertNoThrow(try server.stop().wait()) }
235287

236288
struct Handler: EventLoopLambdaHandler {
@@ -244,7 +296,8 @@ class LambdaHandlerTest: XCTestCase {
244296
}
245297
}
246298

247-
let result = Lambda.run(configuration: .init(), handlerType: Handler.self)
299+
let configuration = LambdaConfiguration(runtimeEngine: .init(address: "127.0.0.1:\(port)"))
300+
let result = Lambda.run(configuration: configuration, handlerType: Handler.self)
248301
assertLambdaRuntimeResult(result, shouldFailWithError: TestError("kaboom"))
249302
}
250303
}

Tests/AWSLambdaRuntimeCoreTests/LambdaTest.swift

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -272,11 +272,16 @@ 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(
283+
runtimeEngine: .init(address: "127.0.0.1:\(port)", requestTimeout: .milliseconds(100))
284+
)
280285

281286
let handler1 = Handler()
282287
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: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -82,10 +82,13 @@ 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(
88+
runtimeEngine: .init(address: "127.0.0.1:\(port)", requestTimeout: .milliseconds(100))
89+
)
8690
let terminator = LambdaTerminator()
8791
let runner = LambdaRunner(eventLoop: eventLoopGroup.next(), configuration: configuration)
88-
let server = try MockLambdaServer(behavior: behavior).start().wait()
8992
defer { XCTAssertNoThrow(try server.stop().wait()) }
9093
try runner.initialize(handlerProvider: handlerProvider, logger: logger, terminator: terminator).flatMap { handler in
9194
runner.run(handler: handler, logger: logger)

0 commit comments

Comments
 (0)