Skip to content

Contributing test-cases for NSOperation/NSOperationQueue. #617

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

Closed
wants to merge 1 commit into from
Closed
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
110 changes: 105 additions & 5 deletions TestFoundation/TestNSOperationQueue.swift
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,12 @@ class TestNSOperationQueue : XCTestCase {
static var allTests: [(String, (TestNSOperationQueue) -> () throws -> Void)] {
return [
("test_OperationPriorities", test_OperationPriorities),
("test_OperationCount", test_OperationCount)
("test_OperationCount", test_OperationCount),
("test_OperationAddRemoveDependency",test_OperationAddRemoveDependency),
("test_OperationExecutionWithDependency", test_OperationExecutionWithDependency),
("test_OperationStates", test_OperationStates),
("test_synchronousOperations", test_synchronousOperations),
("test_completionBlock", test_completionBlock),
]
}

Expand All @@ -36,17 +41,21 @@ class TestNSOperationQueue : XCTestCase {

func test_OperationPriorities() {
var msgOperations = [String]()
let operation1 : BlockOperation = BlockOperation(block: {
let operation1 = BlockOperation(block: {
msgOperations.append("Operation1 executed")
sleep(1)
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added time-lag to resolve the test-case failures seen sometimes on TestFoundation.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry - sleep cannot be a fix for issues like this. Instead we should use a semaphore to signal completion of whatever work we are waiting on.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok. Will modify to use semaphores. Thank you.

})
let operation2 : BlockOperation = BlockOperation(block: {
let operation2 = BlockOperation(block: {
msgOperations.append("Operation2 executed")
sleep(1)
})
let operation3 : BlockOperation = BlockOperation(block: {
let operation3 = BlockOperation(block: {
msgOperations.append("Operation3 executed")
sleep(1)
})
let operation4: BlockOperation = BlockOperation(block: {
let operation4 = BlockOperation(block: {
msgOperations.append("Operation4 executed")
sleep(1)
})
operation4.queuePriority = .veryLow
operation3.queuePriority = .veryHigh
Expand All @@ -65,4 +74,95 @@ class TestNSOperationQueue : XCTestCase {
XCTAssertEqual(msgOperations[2], "Operation2 executed")
XCTAssertEqual(msgOperations[3], "Operation4 executed")
}

func test_OperationAddRemoveDependency() {
let operation1 = BlockOperation(block: {
})
operation1.name = "Operation1"
let operation2 = BlockOperation(block: {
})
operation2.name = "Operation2"
operation1.addDependency(operation2)
XCTAssertEqual(1, operation1.dependencies.count)
for dependentOperation in operation1.dependencies {
XCTAssertEqual("Operation2", dependentOperation.name)
}
operation1.removeDependency(operation2)
operation2.removeDependency(operation1)
XCTAssertEqual(0, operation1.dependencies.count)
}

func test_OperationExecutionWithDependency() {
var finalMsg: String = "init"
let operation1 = BlockOperation(block: {
finalMsg = "Execution Order: Operation1"
})
let operation2 = BlockOperation(block: {
finalMsg = finalMsg + ", Operation2"
})
let operation3 = BlockOperation(block: {
finalMsg = finalMsg + ", Operation3"
})
let myOpQueue = OperationQueue()
operation3.addDependency(operation2)
operation2.addDependency(operation1)
myOpQueue.addOperation(operation1)
myOpQueue.addOperation(operation2)
myOpQueue.addOperation(operation3)
myOpQueue.waitUntilAllOperationsAreFinished()
XCTAssertEqual(finalMsg, "Execution Order: Operation1, Operation2, Operation3")
}

func test_OperationStates() {
var total: Int = 0
let operation1 = BlockOperation(block: {
sleep(1)
total += 1
})
let operation2 = BlockOperation(block: {
sleep(1)
total += 1
})
operation1.cancel()
XCTAssertTrue(operation1.isCancelled)
let queue = OperationQueue()
queue.addOperation(operation1)
queue.addOperation(operation2)
queue.waitUntilAllOperationsAreFinished()
XCTAssertEqual(total, 1)
XCTAssertTrue(operation1.isCancelled)
XCTAssertTrue(operation2.isFinished)
}

func test_synchronousOperations() {
var finalMsg: String = "init"
let operation1 = BlockOperation(block: {
finalMsg = "Operation1"
})
let operation2 = BlockOperation(block: {
finalMsg = "Operation2"
})
let operation3 = BlockOperation(block: {
finalMsg = "Operation3"
})
operation2.start()
operation3.start()
operation1.start()
XCTAssertEqual(finalMsg, "Operation1")
}

func test_completionBlock() {
var message = ""
let operation = BlockOperation(block: {
message.append("Operation done.")
})
operation.completionBlock = {
message.append("completionBlock invoked.")
}
operation.start()
repeat {
sleep(1)
} while(!operation.isFinished)
XCTAssertEqual(message, "Operation done.completionBlock invoked.")
}
}