forked from ryanashcraft/BadAccessPlayground
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBadAccessPlaygroundTests.swift
45 lines (38 loc) · 1.17 KB
/
BadAccessPlaygroundTests.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
import XCTest
import Combine
class Item {
deinit {
// Shouldn't ever be called due to Store being a global singleton, but can get called randomly by Future.deinit
// If it ever should be called, shouldn't it only be called by Store.deinit?
print("item deinit")
}
}
class Store {
static let shared = Store()
private let item: Item = Item()
private let queue = DispatchQueue(label: "Store.queue")
func fetchItem(completion: @escaping (Item) -> Void) {
queue.async {
completion(self.item)
}
}
}
final class BadAccessPlaygroundTests: XCTestCase {
var store = Store()
func testBadAccess() {
let expectation = expectation(description: "expectation")
var task: AnyCancellable?
task = Future<Item, Never> { promise in
self.store.fetchItem() { result in
promise(.success(result))
}
}
.sink(
receiveCompletion: { _ in
expectation.fulfill()
task?.cancel()
}, receiveValue: { _ in }
)
wait(for: [expectation])
}
}