forked from pointfreeco/sqlite-data
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFetchSubscriptionTests.swift
More file actions
105 lines (90 loc) · 2.61 KB
/
FetchSubscriptionTests.swift
File metadata and controls
105 lines (90 loc) · 2.61 KB
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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
import DependenciesTestSupport
import Foundation
import SQLiteData
import Testing
@Suite(.dependency(\.defaultDatabase, try .database())) struct FetchSubscriptionTests {
@Dependency(\.defaultDatabase) var database
@Test func stopSubscriptionWhenTaskCancelled() async throws {
@FetchAll var records: [Record]
#expect(records.count == 0)
try await database.write { db in
try Record.insert { Record.Draft() }.execute(db)
}
try await $records.load()
#expect(records.count == 1)
let task = Task { [$records] in
try? await $records.load(Record.all).task
}
task.cancel()
await task.value
try await database.write { db in
try Record.insert { Record.Draft() }.execute(db)
}
try await $records.load()
#expect(records.count == 1)
}
@Test func completeWhenTaskExplicitlyCancelled() async throws {
@FetchAll var records: [Record]
#expect(records.count == 0)
let didComplete = LockIsolated(false)
try await database.write { db in
try Record.insert { Record.Draft() }.execute(db)
}
try await $records.load()
#expect(records.count == 1)
let subscription = try await $records.load(Record.all)
let task = Task {
try? await subscription.task
didComplete.withValue { $0 = true }
}
try await Task.sleep(for: .seconds(1))
subscription.cancel()
await task.value
#expect(didComplete.value)
}
@Test func cancellingOneFetchDoesNotCancelAnother() async throws {
@FetchAll var records1: [Record]
#expect(records1.count == 0)
let task1 = Task { [$records1] in
try? await $records1.load(Record.all).task
}
@FetchAll var records2: [Record]
#expect(records2.count == 0)
await withUnsafeContinuation { continuation in
Task { [$records2] in
let subscription = try await $records2.load(Record.all)
continuation.resume()
try await subscription.task
}
}
task1.cancel()
await task1.value
try await database.write { db in
try Record.insert { Record.Draft() }.execute(db)
}
try await $records1.load()
try await $records2.load()
#expect(records1.count == 0)
#expect(records2.count == 1)
}
}
@Table
private struct Record: Equatable {
let id: Int
}
extension DatabaseWriter where Self == DatabaseQueue {
fileprivate static func database() throws -> DatabaseQueue {
let database = try DatabaseQueue()
try database.write { db in
try #sql(
"""
CREATE TABLE "records" (
"id" INTEGER PRIMARY KEY AUTOINCREMENT
)
"""
)
.execute(db)
}
return database
}
}