Skip to content

Commit d699f36

Browse files
committed
fix remianing time
1 parent 02a54df commit d699f36

File tree

2 files changed

+23
-1
lines changed

2 files changed

+23
-1
lines changed

Sources/AWSLambdaRuntime/LambdaContext.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -173,7 +173,7 @@ public struct LambdaContext: CustomDebugStringConvertible, Sendable {
173173

174174
public func getRemainingTime() -> Duration {
175175
let deadline = self.deadline
176-
return deadline.duration(to: LambdaClock().now)
176+
return LambdaClock().now.duration(to: deadline)
177177
}
178178

179179
public var debugDescription: String {

Tests/AWSLambdaRuntimeTests/LambdaContextTests.swift

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
//===----------------------------------------------------------------------===//
1414

1515
import Foundation
16+
import Logging
1617
import Testing
1718

1819
@testable import AWSLambdaRuntime
@@ -111,4 +112,25 @@ struct LambdaContextTests {
111112
#expect(environment["platform"] == "Android")
112113
#expect(environment["platform_version"] == "10")
113114
}
115+
116+
@Test("getRemainingTime returns positive duration for future deadline")
117+
func getRemainingTimeReturnsPositiveDurationForFutureDeadline() {
118+
119+
// Create context with deadline 30 seconds in the future
120+
let context = LambdaContext.__forTestsOnly(
121+
requestID: "test-request",
122+
traceID: "test-trace",
123+
invokedFunctionARN: "test-arn",
124+
timeout: .seconds(30),
125+
logger: Logger(label: "test")
126+
)
127+
128+
// Get remaining time - should be positive since deadline is in future
129+
let remainingTime = context.getRemainingTime()
130+
131+
// Verify Duration can be negative (not absolute value)
132+
#expect(remainingTime > .zero, "getRemainingTime() should return positive duration when deadline is in future")
133+
#expect(remainingTime <= Duration.seconds(31), "Remaining time should be approximately 30 seconds")
134+
#expect(remainingTime >= Duration.seconds(-29), "Remaining time should be approximately -30 seconds")
135+
}
114136
}

0 commit comments

Comments
 (0)