Skip to content

Instantly share code, notes, and snippets.

@sisoje
Last active February 15, 2019 10:48
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sisoje/4a73e36ffbe917a25d14deacd814bfc9 to your computer and use it in GitHub Desktop.
Save sisoje/4a73e36ffbe917a25d14deacd814bfc9 to your computer and use it in GitHub Desktop.
import XCTest
import Foundation
class TestFulfill: XCTestCase {
class FulfillOnRelease {
let ex: XCTestExpectation
init(_ ex: XCTestExpectation) {
self.ex = ex
}
deinit {
print("Fullfiling: \(ex.description)")
ex.fulfill()
}
}
func testFulfillWithDispatchOk() {
let queue = DispatchQueue(label: #function)
(0..<3).forEach { index in
let ex = expectation(description: "\(#function) \(index)")
let fulfillOnRelease = FulfillOnRelease(ex)
queue.async {
print("Retaining: \(fulfillOnRelease.ex.description)")
}
}
waitForExpectations(timeout: 1, handler: nil)
}
func testFulfillWithOperationQueueBad() {
let queue = OperationQueue()
(0..<3).forEach { index in
let ex = expectation(description: "\(#function) \(index)")
let fulfillOnRelease = FulfillOnRelease(ex)
queue.addOperation {
print("Retaining: \(fulfillOnRelease.ex.description)")
}
}
waitForExpectations(timeout: 1, handler: nil)
}
func testFulfillWithOperationQueueOk() {
let queue = OperationQueue()
(0..<3).forEach { index in
let ex = expectation(description: "\(#function) \(index)")
let fulfillOnRelease = FulfillOnRelease(ex)
let blockOperation = BlockOperation {
print("Retaining: \(fulfillOnRelease.ex.description)")
}
queue.addOperation(blockOperation)
}
waitForExpectations(timeout: 1, handler: nil)
}
}
@millenomi
Copy link

Operations are under many conditions autoreleased on completion, and you cannot assume here that an object is alive or not in ObjC because they may still be in the autorelease pool. Can you try again by emptying the pool with autoreleasepool { … } and see if it's still true? We have tried and it does not reproduce in correct conditions (checking with the pool).

@sisoje
Copy link
Author

sisoje commented Feb 15, 2019

works with autoreleasepool, thanks

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment