@@ -12,7 +12,7 @@ import Logging
1212import NIOCore
1313
1414/// A mock function context for testing.
15- public struct MockContext < E > : RuntimeContext , EnvironmentValueProvider {
15+ public struct MockContext < EnvironmentVariable > : RuntimeContext , EnvironmentValueProvider {
1616 public var requestID : String
1717 public var traceID : String
1818 public var invokedFunctionARN : String
@@ -23,8 +23,13 @@ public struct MockContext<E>: RuntimeContext, EnvironmentValueProvider {
2323 public var eventLoop : EventLoop
2424 public var allocator : ByteBufferAllocator
2525
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+
2631 /// 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
2833
2934 /// Creates a new instance.
3035 ///
@@ -38,6 +43,7 @@ public struct MockContext<E>: RuntimeContext, EnvironmentValueProvider {
3843 /// - logger: The logger.
3944 /// - eventLoop: The event loop.
4045 /// - allocator: The byte buffer allocator.
46+ /// - remainingTimeProvider: A closure returning a `TimeAmount` from a given `DispatchWallTime`.
4147 /// - environmentValueProvider: A closure returning the value of the given environment
4248 /// variable.
4349 public init (
@@ -50,7 +56,8 @@ public struct MockContext<E>: RuntimeContext, EnvironmentValueProvider {
5056 logger: Logger ,
5157 eventLoop: EventLoop ,
5258 allocator: ByteBufferAllocator ,
53- environmentValueProvider: @escaping @Sendable ( E) throws -> String
59+ remainingTimeProvider: @escaping @Sendable ( DispatchWallTime ) -> TimeAmount ,
60+ environmentValueProvider: @escaping @Sendable ( EnvironmentVariable) throws -> String
5461 ) {
5562 self . requestID = requestID
5663 self . traceID = traceID
@@ -61,45 +68,104 @@ public struct MockContext<E>: RuntimeContext, EnvironmentValueProvider {
6168 self . logger = logger
6269 self . eventLoop = eventLoop
6370 self . allocator = allocator
71+ self . remainingTimeProvider = remainingTimeProvider
6472 self . environmentValueProvider = environmentValueProvider
6573 }
6674
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 {
6880 try environmentValueProvider ( environmentVariable)
6981 }
7082}
7183
7284public 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+
73141 /// Creates a new instance.
74142 ///
75143 /// - 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.
80144 /// - eventLoop: The event loop.
145+ /// - configuration: The context configuration.
146+ /// - logger: The logger.
81147 /// - allocator: The byte buffer allocator.
148+ /// - remainingTimeProvider:
82149 /// - environmentValueProvider: A closure returning the value of the given environment
83150 /// variable.
84151 init (
85- timeout: DispatchTimeInterval = . seconds( 3 ) ,
86- requestID: String = UUID ( ) . uuidString,
87- traceID: String = " abc123 " ,
88- invokedFunctionARN: String = " aws:arn: " ,
89152 eventLoop: EventLoop ,
153+ configuration: Configuration = . init( ) ,
154+ logger: Logger = . mock,
90155 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
92158 ) {
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
101166 self . eventLoop = eventLoop
102167 self . allocator = allocator
168+ self . remainingTimeProvider = remainingTimeProvider
103169 self . environmentValueProvider = environmentValueProvider
104170 }
105171}
0 commit comments