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

Advance scheduler to specific time #36

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
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
26 changes: 26 additions & 0 deletions Sources/EntwineTest/TestScheduler/TestScheduler.swift
Original file line number Diff line number Diff line change
Expand Up @@ -196,6 +196,32 @@ public class TestScheduler {
lastTaskId += 1
return lastTaskId
}

/// Performs all the actions in the scheduler's queue until reaches to the duration that is added as virtual time from now
/// - Parameters:
/// - duration: The `VirtualTimeInterval` that are going to be performed all tasks until by adding interval from now
public func advance(by duration: VirtualTimeInterval) {
advance(to: now.advanced(by: duration))
}

/// Performs all the actions in the scheduler's queue, in time until reaches to the duration
/// - Parameters:
/// - instant: The `VirtualTime` that are going to be performed all tasks until
public func advance(to instant: VirtualTime) {
while now <= instant {
guard
let next = findNext(),
instant >= next.time
else {
currentTime = instant
return
}

currentTime = next.time
schedulerQueue.remove(next)
next.action()
}
}
}

// MARK: - TestScheduler Scheduler conformance
Expand Down
35 changes: 33 additions & 2 deletions Tests/EntwineTestTests/TestSchedulerTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,36 @@ final class TestSchedulerTests: XCTestCase {

XCTAssertEqual(times, [100, 200, 300,])
}


func testSchedulerAdvancesToTasksTime() {

let subject = TestScheduler(initialClock: 0)
var changedValue = 0

subject.schedule(after: 300) { changedValue = 300 }
subject.schedule(after: 200) { changedValue = 200 }
subject.schedule(after: 100) { changedValue = 100 }

subject.advance(to: 200)

XCTAssertEqual(changedValue, 200)
}

func testSchedulerAdvancesToTasksByTimeInterval() {

let subject = TestScheduler(initialClock: 0)
var changedValue = 0

subject.schedule(after: 300) { changedValue = 300 }
subject.schedule(after: 200) { changedValue = 200 }
subject.schedule(after: 100) { changedValue = 100 }

subject.advance(to: 100)
XCTAssertEqual(changedValue, 100)
subject.advance(by: 100)
XCTAssertEqual(changedValue, 200)
}

func testSchedulerInvokesDeferredTask() {

let subject = TestScheduler(initialClock: 0)
Expand Down Expand Up @@ -308,7 +337,9 @@ final class TestSchedulerTests: XCTestCase {
("testTrampolinesImmediatelyScheduledTasks", testTrampolinesImmediatelyScheduledTasks),
("testSchedulesRepeatingTask", testSchedulesAndCancelsRepeatingTask),
("testIgnoresTasksAfterMaxClock", testIgnoresTasksAfterMaxClock),
("testRemovesTaskCancelledWithinOwnAction", testRemovesTaskCancelledWithinOwnAction)
("testRemovesTaskCancelledWithinOwnAction", testRemovesTaskCancelledWithinOwnAction),
("testSchedulerAdvancesToTasksTime", testSchedulerAdvancesToTasksTime),
("testSchedulerAdvancesToTasksByTimeInterval", testSchedulerAdvancesToTasksByTimeInterval)
]
}

Expand Down