@@ -12,7 +12,7 @@ import Logging
12
12
import NIOCore
13
13
14
14
/// A mock function context for testing.
15
- public struct MockContext < E > : RuntimeContext , EnvironmentValueProvider {
15
+ public struct MockContext < EnvironmentVariable > : RuntimeContext , EnvironmentValueProvider {
16
16
public var requestID : String
17
17
public var traceID : String
18
18
public var invokedFunctionARN : String
@@ -23,8 +23,13 @@ public struct MockContext<E>: RuntimeContext, EnvironmentValueProvider {
23
23
public var eventLoop : EventLoop
24
24
public var allocator : ByteBufferAllocator
25
25
26
+ /// A closure returning a `TimeAmount` from a given `DispatchWallTime`.
27
+ ///
28
+ /// This is used to return the remaining time until the context's ``deadline``.
29
+ public var remainingTimeProvider : @Sendable ( DispatchWallTime ) -> TimeAmount
30
+
26
31
/// A closure returning the value of the given environment variable.
27
- public var environmentValueProvider : @Sendable ( E ) throws -> String
32
+ public var environmentValueProvider : @Sendable ( EnvironmentVariable ) throws -> String
28
33
29
34
/// Creates a new instance.
30
35
///
@@ -38,6 +43,7 @@ public struct MockContext<E>: RuntimeContext, EnvironmentValueProvider {
38
43
/// - logger: The logger.
39
44
/// - eventLoop: The event loop.
40
45
/// - allocator: The byte buffer allocator.
46
+ /// - remainingTimeProvider: A closure returning a `TimeAmount` from a given `DispatchWallTime`.
41
47
/// - environmentValueProvider: A closure returning the value of the given environment
42
48
/// variable.
43
49
public init (
@@ -50,7 +56,8 @@ public struct MockContext<E>: RuntimeContext, EnvironmentValueProvider {
50
56
logger: Logger ,
51
57
eventLoop: EventLoop ,
52
58
allocator: ByteBufferAllocator ,
53
- environmentValueProvider: @escaping @Sendable ( E) throws -> String
59
+ remainingTimeProvider: @escaping @Sendable ( DispatchWallTime ) -> TimeAmount ,
60
+ environmentValueProvider: @escaping @Sendable ( EnvironmentVariable) throws -> String
54
61
) {
55
62
self . requestID = requestID
56
63
self . traceID = traceID
@@ -61,45 +68,104 @@ public struct MockContext<E>: RuntimeContext, EnvironmentValueProvider {
61
68
self . logger = logger
62
69
self . eventLoop = eventLoop
63
70
self . allocator = allocator
71
+ self . remainingTimeProvider = remainingTimeProvider
64
72
self . environmentValueProvider = environmentValueProvider
65
73
}
66
74
67
- public func value( for environmentVariable: E ) throws -> String {
75
+ public func getRemainingTime( ) -> TimeAmount {
76
+ remainingTimeProvider ( deadline)
77
+ }
78
+
79
+ public func value( for environmentVariable: EnvironmentVariable ) throws -> String {
68
80
try environmentValueProvider ( environmentVariable)
69
81
}
70
82
}
71
83
72
84
public extension MockContext {
85
+
86
+ /// Configuration data for ``MockContext``.
87
+ struct Configuration {
88
+ /// The request ID, which identifies the request that triggered the function invocation.
89
+ public var requestID : String
90
+
91
+ /// The AWS X-Ray tracing header.
92
+ public var traceID : String
93
+
94
+ /// The ARN of the Lambda function, version, or alias that's specified in the invocation.
95
+ public var invokedFunctionARN : String
96
+
97
+ /// The time interval before the context's deadline.
98
+ public var timeout : DispatchTimeInterval
99
+
100
+ /// For invocations from the AWS Mobile SDK, data about the Amazon Cognito identity provider.
101
+ public var cognitoIdentity : String ?
102
+
103
+ /// For invocations from the AWS Mobile SDK, data about the client application and device.
104
+ public var clientContext : String ?
105
+
106
+ /// Creates an instance.
107
+ ///
108
+ /// - Parameters:
109
+ /// - requestID: The request ID.
110
+ /// - traceID: The AWS X-Ray tracing header.
111
+ /// - invokedFunctionARN: The ARN of the Lambda function.
112
+ /// - timeout: The time interval before the context's deadline.
113
+ /// - cognitoIdentity: Data about the Amazon Cognito identity provider.
114
+ /// - clientContext: Data about the client application and device.
115
+ public init (
116
+ requestID: String = " \( DispatchTime . now ( ) . uptimeNanoseconds) " ,
117
+ traceID: String = " Root= \( DispatchTime . now ( ) . uptimeNanoseconds) ;Parent= \( DispatchTime . now ( ) . uptimeNanoseconds) ;Sampled=1 " ,
118
+ invokedFunctionARN: String = " arn:aws:lambda:us-east-1: \( DispatchTime . now ( ) . uptimeNanoseconds) :function:custom-runtime " ,
119
+ timeout: DispatchTimeInterval = . seconds( 5 ) ,
120
+ cognitoIdentity: String ? = nil ,
121
+ clientContext: String ? = nil
122
+ ) {
123
+ self . requestID = requestID
124
+ self . traceID = traceID
125
+ self . invokedFunctionARN = invokedFunctionARN
126
+ self . timeout = timeout
127
+ self . cognitoIdentity = cognitoIdentity
128
+ self . clientContext = clientContext
129
+ }
130
+ }
131
+
132
+ /// Returns the time interval between a given point in time and the current time.
133
+ ///
134
+ /// - Parameter deadline: The time with which to compare now.
135
+ /// - Returns: The time interval between the given deadline and now.
136
+ @Sendable
137
+ static func timeAmountUntil( _ deadline: DispatchWallTime ) -> TimeAmount {
138
+ . milliseconds( deadline. millisecondsSinceEpoch - DispatchWallTime. now ( ) . millisecondsSinceEpoch)
139
+ }
140
+
73
141
/// Creates a new instance.
74
142
///
75
143
/// - Parameters:
76
- /// - timeout: The time interval at which the function will time out.
77
- /// - requestID: The request ID.
78
- /// - traceID: The tracing header.
79
- /// - invokedFunctionARN: The ARN of the Lambda function.
80
144
/// - eventLoop: The event loop.
145
+ /// - configuration: The context configuration.
146
+ /// - logger: The logger.
81
147
/// - allocator: The byte buffer allocator.
148
+ /// - remainingTimeProvider:
82
149
/// - environmentValueProvider: A closure returning the value of the given environment
83
150
/// variable.
84
151
init (
85
- timeout: DispatchTimeInterval = . seconds( 3 ) ,
86
- requestID: String = UUID ( ) . uuidString,
87
- traceID: String = " abc123 " ,
88
- invokedFunctionARN: String = " aws:arn: " ,
89
152
eventLoop: EventLoop ,
153
+ configuration: Configuration = . init( ) ,
154
+ logger: Logger = . mock,
90
155
allocator: ByteBufferAllocator = . init( ) ,
91
- environmentValueProvider: @escaping @Sendable ( E) throws -> String
156
+ remainingTimeProvider: @escaping @Sendable ( DispatchWallTime ) -> TimeAmount = Self . timeAmountUntil,
157
+ environmentValueProvider: @escaping @Sendable ( EnvironmentVariable) throws -> String
92
158
) {
93
- self . requestID = requestID
94
- self . traceID = traceID
95
- self . invokedFunctionARN = invokedFunctionARN
96
- self . deadline = . now( ) + timeout
97
- self . logger = Logger (
98
- label: " mock-logger " ,
99
- factory: { _ in StreamLogHandler . standardOutput ( label: " mock-logger " ) }
100
- )
159
+ self . requestID = configuration. requestID
160
+ self . traceID = configuration. traceID
161
+ self . invokedFunctionARN = configuration. invokedFunctionARN
162
+ self . deadline = . now( ) + configuration. timeout
163
+ self . cognitoIdentity = configuration. cognitoIdentity
164
+ self . clientContext = configuration. clientContext
165
+ self . logger = logger
101
166
self . eventLoop = eventLoop
102
167
self . allocator = allocator
168
+ self . remainingTimeProvider = remainingTimeProvider
103
169
self . environmentValueProvider = environmentValueProvider
104
170
}
105
171
}
0 commit comments