Add more tests for Task
This commit is contained in:
parent
92f0c3894a
commit
11f5d1f59d
|
@ -135,7 +135,7 @@ public class Task {
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Suspensions of this task.
|
/// Suspensions of this task.
|
||||||
public var suspends: UInt64 {
|
public var suspensions: UInt64 {
|
||||||
return _lock.withLock {
|
return _lock.withLock {
|
||||||
_suspensions
|
_suspensions
|
||||||
}
|
}
|
||||||
|
@ -240,6 +240,7 @@ public class Task {
|
||||||
/// if new lifetime is shorter than its age, subtracting will fail, too.
|
/// if new lifetime is shorter than its age, subtracting will fail, too.
|
||||||
///
|
///
|
||||||
/// - Returns: `true` if set successfully, `false` if not.
|
/// - Returns: `true` if set successfully, `false` if not.
|
||||||
|
@discardableResult
|
||||||
public func subtractLifetime(_ interval: Interval) -> Bool {
|
public func subtractLifetime(_ interval: Interval) -> Bool {
|
||||||
return addLifetime(interval.opposite)
|
return addLifetime(interval.opposite)
|
||||||
}
|
}
|
||||||
|
|
|
@ -21,6 +21,30 @@ final class TaskTests: XCTestCase {
|
||||||
task.cancel()
|
task.cancel()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func testSuspendResume() {
|
||||||
|
let block = {
|
||||||
|
let task = Schedule.distantFuture.do { }
|
||||||
|
XCTAssertEqual(task.suspensions, 0)
|
||||||
|
task.suspend()
|
||||||
|
task.suspend()
|
||||||
|
task.suspend()
|
||||||
|
XCTAssertEqual(task.suspensions, 3)
|
||||||
|
task.resume()
|
||||||
|
XCTAssertEqual(task.suspensions, 2)
|
||||||
|
}
|
||||||
|
block()
|
||||||
|
|
||||||
|
let tag = UUID().uuidString
|
||||||
|
let task = Schedule.distantFuture.do { }
|
||||||
|
task.addTag(tag)
|
||||||
|
Task.suspend(byTag: tag)
|
||||||
|
XCTAssertEqual(task.suspensions, 1)
|
||||||
|
Task.resume(byTag: tag)
|
||||||
|
XCTAssertEqual(task.suspensions, 0)
|
||||||
|
Task.cancel(byTag: tag)
|
||||||
|
XCTAssertTrue(task.isCancelled)
|
||||||
|
}
|
||||||
|
|
||||||
func testAddAndRemoveActions() {
|
func testAddAndRemoveActions() {
|
||||||
let expectation = XCTestExpectation(description: "testAddAndRemoveActions")
|
let expectation = XCTestExpectation(description: "testAddAndRemoveActions")
|
||||||
let task = Schedule.after(0.5.second).do { }
|
let task = Schedule.after(0.5.second).do { }
|
||||||
|
@ -34,6 +58,9 @@ final class TaskTests: XCTestCase {
|
||||||
task.removeAction(byKey: key)
|
task.removeAction(byKey: key)
|
||||||
XCTAssertEqual(task.countOfActions, 1)
|
XCTAssertEqual(task.countOfActions, 1)
|
||||||
task.cancel()
|
task.cancel()
|
||||||
|
|
||||||
|
task.removeAllActions()
|
||||||
|
XCTAssertEqual(task.countOfActions, 0)
|
||||||
}
|
}
|
||||||
|
|
||||||
func testAddAndRemoveTags() {
|
func testAddAndRemoveTags() {
|
||||||
|
@ -84,9 +111,11 @@ final class TaskTests: XCTestCase {
|
||||||
let expectation = XCTestExpectation(description: "testLifetime")
|
let expectation = XCTestExpectation(description: "testLifetime")
|
||||||
let task = Schedule.after(1.hour).do { }
|
let task = Schedule.after(1.hour).do { }
|
||||||
task.setLifetime(1.second)
|
task.setLifetime(1.second)
|
||||||
|
XCTAssertEqual(task.lifetime, 1.second)
|
||||||
|
|
||||||
DispatchQueue.global().async(after: 0.5.second) {
|
DispatchQueue.global().async(after: 0.5.second) {
|
||||||
XCTAssertTrue(task.restOfLifetime.isAlmostEqual(to: 0.5.second, leeway: 0.1.second))
|
XCTAssertTrue(task.restOfLifetime.isAlmostEqual(to: 0.5.second, leeway: 0.1.second))
|
||||||
task.addLifetime(0.5.second)
|
task.subtractLifetime(-0.5.second)
|
||||||
}
|
}
|
||||||
DispatchQueue.global().async(after: 1.second) {
|
DispatchQueue.global().async(after: 1.second) {
|
||||||
XCTAssertFalse(task.isCancelled)
|
XCTAssertFalse(task.isCancelled)
|
||||||
|
|
Loading…
Reference in New Issue