Skip to content

Commit 764dc5d

Browse files
committed
another approach
1 parent 3f49f3b commit 764dc5d

File tree

7 files changed

+61
-23
lines changed

7 files changed

+61
-23
lines changed

Sources/SwiftAwsLambda/Lambda.swift

Lines changed: 2 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@ public enum Lambda {
8888
public let invokedFunctionArn: String
8989

9090
/// The date that the function times out in Unix time milliseconds
91-
public let deadline: Int64
91+
public let deadline: DispatchWallTime
9292

9393
/// For invocations from the AWS Mobile SDK, data about the Amazon Cognito identity provider.
9494
public let cognitoIdentity: String?
@@ -102,7 +102,7 @@ public enum Lambda {
102102
internal init(requestId: String,
103103
traceId: String,
104104
invokedFunctionArn: String,
105-
deadline: Int64,
105+
deadline: DispatchWallTime,
106106
cognitoIdentity: String? = nil,
107107
clientContext: String? = nil,
108108
logger: Logger) {
@@ -118,17 +118,6 @@ public enum Lambda {
118118
logger[metadataKey: "awsTraceId"] = .string(traceId)
119119
self.logger = logger
120120
}
121-
122-
@available(OSX 10.12, *)
123-
public func getRemainingTime() -> TimeAmount {
124-
func getTimeInMilliSeconds() -> Int64 {
125-
var ts: timespec = timespec(tv_sec: 0, tv_nsec: 0)
126-
clock_gettime(CLOCK_REALTIME, &ts)
127-
return Int64(ts.tv_sec * 1000) + Int64(ts.tv_nsec / 1_000_000)
128-
}
129-
130-
return .milliseconds(self.deadline - getTimeInMilliSeconds())
131-
}
132121
}
133122

134123
private final class Lifecycle {

Sources/SwiftAwsLambda/LambdaRunner.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -130,7 +130,7 @@ private extension Lambda.Context {
130130
self.init(requestId: invocation.requestId,
131131
traceId: invocation.traceId,
132132
invokedFunctionArn: invocation.invokedFunctionArn,
133-
deadline: invocation.deadline,
133+
deadline: DispatchWallTime(millisSinceEpoch: invocation.deadline),
134134
cognitoIdentity: invocation.cognitoIdentity,
135135
clientContext: invocation.clientContext,
136136
logger: logger)

Sources/SwiftAwsLambda/Utils.swift

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,3 +64,16 @@ internal enum Signal: Int32 {
6464
case ALRM = 14
6565
case TERM = 15
6666
}
67+
68+
internal extension DispatchWallTime {
69+
init(millisSinceEpoch: Int64) {
70+
let nanoSinceEpoch = UInt64(millisSinceEpoch) * 1_000_000
71+
let seconds = UInt64(nanoSinceEpoch / 1_000_000_000)
72+
let nanoseconds = nanoSinceEpoch - (seconds * 1_000_000_000)
73+
self.init(timespec: timespec(tv_sec: Int(seconds), tv_nsec: Int(nanoseconds)))
74+
}
75+
76+
var millisSinceEpoch: Int64 {
77+
return Int64(bitPattern: self.rawValue) / -1_000_000
78+
}
79+
}

Tests/SwiftAwsLambdaTests/LambdaTest+XCTest.swift

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ extension LambdaTest {
3636
("testBigPayload", testBigPayload),
3737
("testKeepAliveServer", testKeepAliveServer),
3838
("testNoKeepAliveServer", testNoKeepAliveServer),
39+
("testDeadline", testDeadline),
3940
]
4041
}
4142
}

Tests/SwiftAwsLambdaTests/LambdaTest.swift

Lines changed: 37 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -214,14 +214,43 @@ class LambdaTest: XCTestCase {
214214
assertLambdaLifecycleResult(result, shoudHaveRun: maxTimes)
215215
}
216216

217-
@available(OSX 10.12, *)
218-
func testGetTimeRemaining() {
219-
let deadline = round(Date(timeIntervalSinceNow: 60).timeIntervalSince1970 * 1000)
220-
let context = Lambda.Context(requestId: "abc", traceId: "abc", invokedFunctionArn: "abc", deadline: Int64(deadline), logger: Logger(label: ""))
221-
222-
let remaining = context.getRemainingTime().nanoseconds
223-
// maximal allowed time offset 2 milliseconds
224-
XCTAssertLessThanOrEqual(abs(remaining - 60 * 1_000_000_000), 2_000_000)
217+
func testDeadline() {
218+
let delta = Int.random(in: 1 ... 600)
219+
220+
let milli1 = Date(timeIntervalSinceNow: Double(delta)).millisSinceEpoch
221+
let milli2 = (DispatchWallTime.now() + .seconds(delta)).millisSinceEpoch
222+
XCTAssertEqual(Double(milli1), Double(milli2), accuracy: 2.0)
223+
224+
let now1 = DispatchWallTime.now()
225+
let now2 = DispatchWallTime(millisSinceEpoch: Date().millisSinceEpoch)
226+
XCTAssertEqual(Double(now2.rawValue), Double(now1.rawValue), accuracy: 2_000_000.0)
227+
228+
let future1 = DispatchWallTime.now() + .seconds(delta)
229+
let future2 = DispatchWallTime(millisSinceEpoch: Date(timeIntervalSinceNow: Double(delta)).millisSinceEpoch)
230+
XCTAssertEqual(Double(future1.rawValue), Double(future2.rawValue), accuracy: 2_000_000.0)
231+
232+
let past1 = DispatchWallTime.now() - .seconds(delta)
233+
let past2 = DispatchWallTime(millisSinceEpoch: Date(timeIntervalSinceNow: Double(-delta)).millisSinceEpoch)
234+
XCTAssertEqual(Double(past1.rawValue), Double(past2.rawValue), accuracy: 2_000_000.0)
235+
236+
let logger = Logger(label: "test")
237+
let context = Lambda.Context(requestId: UUID().uuidString,
238+
traceId: UUID().uuidString,
239+
invokedFunctionArn: UUID().uuidString,
240+
deadline: .now() + .seconds(1),
241+
cognitoIdentity: nil,
242+
clientContext: nil,
243+
logger: logger)
244+
XCTAssertGreaterThan(context.deadline, .now())
245+
246+
let expiredContext = Lambda.Context(requestId: UUID().uuidString,
247+
traceId: UUID().uuidString,
248+
invokedFunctionArn: UUID().uuidString,
249+
deadline: .now() - .seconds(1),
250+
cognitoIdentity: nil,
251+
clientContext: nil,
252+
logger: logger)
253+
XCTAssertLessThan(expiredContext.deadline, .now())
225254
}
226255
}
227256

Tests/SwiftAwsLambdaTests/MockLambdaServer.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -140,7 +140,7 @@ internal final class HTTPHandler: ChannelInboundHandler {
140140
}
141141
responseStatus = .ok
142142
responseBody = result
143-
let deadline = Int64(Date(timeIntervalSinceNow: 60).timeIntervalSince1970 * 1000)
143+
let deadline = Date(timeIntervalSinceNow: 60).millisSinceEpoch
144144
responseHeaders = [
145145
(AmazonHeaders.requestID, requestId),
146146
(AmazonHeaders.invokedFunctionARN, "arn:aws:lambda:us-east-1:123456789012:function:custom-runtime"),

Tests/SwiftAwsLambdaTests/Utils.swift

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,3 +93,9 @@ struct TestError: Error, Equatable, CustomStringConvertible {
9393
self.description = description
9494
}
9595
}
96+
97+
internal extension Date {
98+
var millisSinceEpoch: Int64 {
99+
return Int64(self.timeIntervalSince1970 * 1000)
100+
}
101+
}

0 commit comments

Comments
 (0)