Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix SchedulerTypeTime distance(to:) #104

Merged
merged 2 commits into from
Feb 3, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Sources/CXFoundation/OperationQueue.swift
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ extension CXWrappers.OperationQueue: CombineX.Scheduler {
/// - Returns: The time interval between this time and the provided time.
public func distance(to other: SchedulerTimeType) -> SchedulerTimeType.Stride {

return SchedulerTimeType.Stride(floatLiteral: date.timeIntervalSince(other.date))
return SchedulerTimeType.Stride(floatLiteral: other.date.timeIntervalSince(date))
}

/// Returns a operation queue scheduler time calculated by advancing this instance’s time by the given interval.
Expand Down
2 changes: 1 addition & 1 deletion Sources/CXFoundation/RunLoop.swift
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ extension CXWrappers.RunLoop: CombineX.Scheduler {
/// - Parameter other: Another dispatch queue time.
/// - Returns: The time interval between this time and the provided time.
public func distance(to other: SchedulerTimeType) -> SchedulerTimeType.Stride {
return Stride(floatLiteral: date.timeIntervalSince(other.date))
return Stride(floatLiteral: other.date.timeIntervalSince(date))
}

/// Returns a run loop scheduler time calculated by advancing this instance’s time by the given interval.
Expand Down
22 changes: 22 additions & 0 deletions Tests/CXFoundationTests/SchedulerSpec.swift
Original file line number Diff line number Diff line change
Expand Up @@ -35,5 +35,27 @@ class SchedulerSpec: QuickSpec {
}.to(throwAssertion())
#endif
}

// MARK: 3.1 should compute time intervals correctly.
it("should compute time intervals correctly") {
typealias RunLoopSchedulerTimeType = CXWrappers.RunLoop.SchedulerTimeType

let earlyDate = Date(timeIntervalSinceReferenceDate: 69)
let lateDate = Date(timeIntervalSinceReferenceDate: 420)

let earlyRLSTT = RunLoopSchedulerTimeType(earlyDate)
let lateRLSTT = RunLoopSchedulerTimeType(lateDate)
let rlDistance = earlyRLSTT.distance(to: lateRLSTT)
expect(rlDistance) > .seconds(0)
expect(lateRLSTT) == earlyRLSTT.advanced(by: rlDistance)

typealias OperationQueueSchedulerTimeType = CXWrappers.OperationQueue.SchedulerTimeType

let earlyOQSTT = OperationQueueSchedulerTimeType(earlyDate)
let lateOQSTT = OperationQueueSchedulerTimeType(lateDate)
let oqDistance = earlyOQSTT.distance(to: lateOQSTT)
expect(oqDistance.timeInterval > 0).to(beTrue())
expect(lateOQSTT) == earlyOQSTT.advanced(by: oqDistance)
}
}
}