diff --git a/Sources/NIO/EventLoop.swift b/Sources/NIO/EventLoop.swift index de42274673..e3e179cc13 100644 --- a/Sources/NIO/EventLoop.swift +++ b/Sources/NIO/EventLoop.swift @@ -394,7 +394,7 @@ extension NIODeadline: CustomStringConvertible { extension NIODeadline { public static func - (lhs: NIODeadline, rhs: NIODeadline) -> TimeAmount { - return .nanoseconds(TimeAmount.Value(lhs.uptimeNanoseconds - rhs.uptimeNanoseconds)) + return .nanoseconds(TimeAmount.Value(lhs.uptimeNanoseconds) - TimeAmount.Value(rhs.uptimeNanoseconds)) } public static func + (lhs: NIODeadline, rhs: TimeAmount) -> NIODeadline { diff --git a/Tests/NIOTests/EventLoopTest+XCTest.swift b/Tests/NIOTests/EventLoopTest+XCTest.swift index ce29fa339a..ab4dcba16b 100644 --- a/Tests/NIOTests/EventLoopTest+XCTest.swift +++ b/Tests/NIOTests/EventLoopTest+XCTest.swift @@ -54,6 +54,7 @@ extension EventLoopTest { ("testAndAllSucceedWithZeroFutures", testAndAllSucceedWithZeroFutures), ("testCancelledScheduledTasksDoNotHoldOnToRunClosure", testCancelledScheduledTasksDoNotHoldOnToRunClosure), ("testIllegalCloseOfEventLoopFails", testIllegalCloseOfEventLoopFails), + ("testSubtractingDeadlineFromPastAndFuturesDeadlinesWorks", testSubtractingDeadlineFromPastAndFuturesDeadlinesWorks), ] } } diff --git a/Tests/NIOTests/EventLoopTest.swift b/Tests/NIOTests/EventLoopTest.swift index 45d32a8ba8..8a53e742c9 100644 --- a/Tests/NIOTests/EventLoopTest.swift +++ b/Tests/NIOTests/EventLoopTest.swift @@ -795,4 +795,13 @@ public final class EventLoopTest : XCTestCase { } } } + + func testSubtractingDeadlineFromPastAndFuturesDeadlinesWorks() { + let older = NIODeadline.now() + Thread.sleep(until: Date().addingTimeInterval(0.02)) + let newer = NIODeadline.now() + + XCTAssertLessThan(older - newer, .nanoseconds(0)) + XCTAssertGreaterThan(newer - older, .nanoseconds(0)) + } }