Skip to content

Commit

Permalink
fix subtracting NIODeadlines
Browse files Browse the repository at this point in the history
Motivation:

Subtracting deadlines should just work, no matter if the result is
positive or negative.

Modifications:

Don't crash on earlierDeadline - laterDeadline.

Result:

- fewer crashes
- fixes swift-server/async-http-client#71
  • Loading branch information
weissi committed Jul 17, 2019
1 parent 6b20fa5 commit 0251aa8
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 1 deletion.
2 changes: 1 addition & 1 deletion Sources/NIO/EventLoop.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
1 change: 1 addition & 0 deletions Tests/NIOTests/EventLoopTest+XCTest.swift
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ extension EventLoopTest {
("testAndAllSucceedWithZeroFutures", testAndAllSucceedWithZeroFutures),
("testCancelledScheduledTasksDoNotHoldOnToRunClosure", testCancelledScheduledTasksDoNotHoldOnToRunClosure),
("testIllegalCloseOfEventLoopFails", testIllegalCloseOfEventLoopFails),
("testSubtractingDeadlineFromPastAndFuturesDeadlinesWorks", testSubtractingDeadlineFromPastAndFuturesDeadlinesWorks),
]
}
}
Expand Down
9 changes: 9 additions & 0 deletions Tests/NIOTests/EventLoopTest.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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))
}
}

0 comments on commit 0251aa8

Please # to comment.