Skip to content

Commit 03f169f

Browse files
authored
Merge pull request #2651 from Frizlab/swift-5.1-branch
[5.1] Backport of #2543 for Swift 5.1 branch
2 parents 70f8af9 + ed7e0af commit 03f169f

File tree

2 files changed

+25
-1
lines changed

2 files changed

+25
-1
lines changed

Foundation/Operation.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -347,7 +347,7 @@ open class Operation : NSObject {
347347
_state = .executing
348348
Operation.observeValue(forKeyPath: _NSOperationIsExecuting, ofObject: self)
349349

350-
_queue?._execute(self)
350+
_queue?._execute(self) ?? main()
351351
}
352352

353353
if __NSOperationState.executing == _state {

TestFoundation/TestOperationQueue.swift

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ class TestOperationQueue : XCTestCase {
1515
("test_OperationPriorities", test_OperationPriorities),
1616
("test_OperationCount", test_OperationCount),
1717
("test_AsyncOperation", test_AsyncOperation),
18+
("test_SyncOperationWithoutAQueue", test_SyncOperationWithoutAQueue),
1819
("test_isExecutingWorks", test_isExecutingWorks),
1920
("test_MainQueueGetter", test_MainQueueGetter),
2021
("test_CancelOneOperation", test_CancelOneOperation),
@@ -104,6 +105,18 @@ class TestOperationQueue : XCTestCase {
104105
XCTAssertTrue(operation.isFinished)
105106
}
106107

108+
func test_SyncOperationWithoutAQueue() {
109+
let operation = SyncOperation()
110+
XCTAssertFalse(operation.isExecuting)
111+
XCTAssertFalse(operation.isFinished)
112+
113+
operation.start()
114+
115+
XCTAssertFalse(operation.isExecuting)
116+
XCTAssertTrue(operation.isFinished)
117+
XCTAssertTrue(operation.hasRun)
118+
}
119+
107120
func test_MainQueueGetter() {
108121
XCTAssertTrue(OperationQueue.main === OperationQueue.main)
109122

@@ -330,3 +343,14 @@ class AsyncOperation: Operation {
330343
}
331344

332345
}
346+
347+
class SyncOperation: Operation {
348+
349+
var hasRun = false
350+
351+
override func main() {
352+
Thread.sleep(forTimeInterval: 1)
353+
hasRun = true
354+
}
355+
356+
}

0 commit comments

Comments
 (0)