-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Fix BlockOperation with multiple execution blocks #2938
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
Conversation
@swift-ci test |
@lxbndr I ran I modified it as follows and it ran correctly multiple times, I suggest updating the test-case. It also suggests that Darwin may run the blocks differently to Linux. diff --git a/Tests/Foundation/Tests/TestOperationQueue.swift b/Tests/Foundation/Tests/TestOperationQueue.swift
index 4e55e939..d944f090 100644
--- a/Tests/Foundation/Tests/TestOperationQueue.swift
+++ b/Tests/Foundation/Tests/TestOperationQueue.swift
@@ -702,12 +702,17 @@ class TestOperationQueue : XCTestCase {
}
func test_BlockOperationAddExecutionBlock() {
- var msgOperations = [String]()
+ let lock = NSLock()
+ var msgOperations = Set<String>()
let blockOperation = BlockOperation {
- msgOperations.append("block1 executed")
+ lock.lock()
+ msgOperations.insert("block1 executed")
+ lock.unlock()
}
blockOperation.addExecutionBlock {
- msgOperations.append("block2 executed")
+ lock.lock()
+ msgOperations.insert("block2 executed")
+ lock.unlock()
}
XCTAssert(blockOperation.executionBlocks.count == 2)
let queue = OperationQueue() |
@spevans you're right, there is concurrency issue. Didn't noticed that first, as my run of Darwin compatibility tests was lucky to pass. As we are modifying original request code anyway, would you mind if I change test to use XCTest expectations instead of manual locking? |
I dont mind as long as the test runs ok on Darwin. I had to run it quite a few times to see the failures. |
122933f
to
a9664c9
Compare
@spevans done. Thanks for pointing the issue! |
@swift-ci test |
Steps: 1. Create BlockOperation with an init block. 2. Add another one with addExecutionBlock. 3. Access the executionBlocks property, it contains 3 blocks. Co-authored-by: Danny Liu <[email protected]>
a9664c9
to
30ef6af
Compare
@swift-ci test |
1 similar comment
@swift-ci test |
@spevans should I port it to release branches? |
If its a bug fix then yes its fine to port to the release branches. |
This is rebased version of #2497. Original request seems abandoned, but changes are useful. All credit goes to @dannliu.
Latest Foundation still has this issue: BlockOperation stores first execution block into the internal array along with a second execution block. As the result:
executionBlocks
Also
@convention(block)
specifier inexecutionBlocks
makes dynamic cast crash on return. Looks like it could be safely removed.