-
Notifications
You must be signed in to change notification settings - Fork 113
/
Copy pathLambdaRuntimeTests.swift
83 lines (67 loc) · 2.32 KB
/
LambdaRuntimeTests.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
//===----------------------------------------------------------------------===//
//
// This source file is part of the SwiftAWSLambdaRuntime open source project
//
// Copyright (c) 2025 Apple Inc. and the SwiftAWSLambdaRuntime project authors
// Licensed under Apache License v2.0
//
// See LICENSE.txt for license information
// See CONTRIBUTORS.txt for the list of SwiftAWSLambdaRuntime project authors
//
// SPDX-License-Identifier: Apache-2.0
//
//===----------------------------------------------------------------------===//
import Foundation
import Logging
import NIOCore
import Synchronization
import Testing
@testable import AWSLambdaRuntime
@Suite("LambdaRuntimeTests")
final class LambdaRuntimeTests {
@Test("LambdaRuntime can only be run once")
func testLambdaRuntimerunOnce() async throws {
// First runtime
let runtime1 = LambdaRuntime(
handler: MockHandler(),
eventLoop: Lambda.defaultEventLoop,
logger: Logger(label: "Runtime1")
)
// Second runtime
let runtime2 = LambdaRuntime(
handler: MockHandler(),
eventLoop: Lambda.defaultEventLoop,
logger: Logger(label: "Runtime1")
)
// start the first runtime
let task1 = Task {
try await runtime1.run()
}
// wait a small amount to ensure runtime1 task is started
try await Task.sleep(for: .seconds(1))
// Running the second runtime should trigger LambdaRuntimeError
await #expect(throws: LambdaRuntimeError.self) {
try await runtime2.run()
}
// cancel runtime 1 / task 1
print("--- cancelling ---")
task1.cancel()
// Running the second runtime should work now
await #expect(throws: Never.self) {
let nonReturningTask = Task.detached(priority: .userInitiated) {
try await runtime2.run()
}
// Set timeout and cancel the runtime 2
try await Task.sleep(for: .seconds(2))
nonReturningTask.cancel()
}
}
}
struct MockHandler: StreamingLambdaHandler {
mutating func handle(
_ event: NIOCore.ByteBuffer,
responseWriter: some AWSLambdaRuntime.LambdaResponseStreamWriter,
context: AWSLambdaRuntime.LambdaContext
) async throws {
}
}