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
import XCTest
@testable import OldFella
@MainActor
final class PostLoadTests: XCTestCase {
func testConcurrentThreadLoadSurvivesFirstCallerCancellation() async throws {
let suite = "PostLoadTests-\(UUID().uuidString)"
let defaults = try XCTUnwrap(UserDefaults(suiteName: suite))
defer { defaults.removePersistentDomain(forName: suite) }
let started = expectation(description: "Thread request started")
let service = SharedThreadTestService(started: started)
let model = AppModel(service: service, feedDefaults: defaults, composerOnly: true)
await model.signIn(handle: "demo", appPassword: "test-only")
let first = Task { await model.loadPost(id: "post-1") }
await fulfillment(of: [started], timeout: 2)
let secondStarted = expectation(description: "Second caller started")
let second = Task {
secondStarted.fulfill()
await model.loadPost(id: "post-1")
}
await fulfillment(of: [secondStarted], timeout: 2)
first.cancel()
let posts = try await service.base.loadPost(uri: "post-1")
service.finish(ThreadResult(posts: posts, mutedPostIDs: []))
await first.value
await second.value
XCTAssertEqual(service.threadRequestCount, 1)
XCTAssertNotNil(model.threadContexts["post-1"])
XCTAssertNotNil(model.post(id: "post-1"))
XCTAssertFalse(model.loadingPostIDs.contains("post-1"))
XCTAssertFalse(model.failedPostIDs.contains("post-1"))
}
}
@MainActor
private final class SharedThreadTestService: BlueskyService {
let base = MockBlueskyService()
private let started: XCTestExpectation
private var result: CheckedContinuation<ThreadResult, Never>?
private(set) var threadRequestCount = 0
init(started: XCTestExpectation) { self.started = started }
func loadThread(uri: String) async throws -> ThreadResult {
threadRequestCount += 1
return await withCheckedContinuation {
result = $0
started.fulfill()
}
}
func finish(_ thread: ThreadResult) {
result?.resume(returning: thread)
result = nil
}
func restoreAccount() async throws -> Account? { try await base.restoreAccount() }
func signIn(handle: String, appPassword: String, authFactorToken: String?) async throws -> Account {
try await base.signIn(handle: handle, appPassword: appPassword, authFactorToken: authFactorToken)
}
func signOut() async throws { try await base.signOut() }
func loadModerationPreferences() async throws -> ModerationPolicy.Preferences {
try await base.loadModerationPreferences()
}
func loadFollowingFeed(cursor: String?) async throws -> FeedPage {
try await base.loadFollowingFeed(cursor: cursor)
}
func loadNotifications(cursor: String?) async throws -> NotificationPage {
try await base.loadNotifications(cursor: cursor)
}
func markNotificationsRead(seenAt: Date) async throws { try await base.markNotificationsRead(seenAt: seenAt) }
func loadPost(uri: String) async throws -> [Post] { try await base.loadPost(uri: uri) }
func loadProfile(actorID: String) async throws -> Profile? { try await base.loadProfile(actorID: actorID) }
func loadAuthorPosts(actorID: String, cursor: String?) async throws -> FeedPage {
try await base.loadAuthorPosts(actorID: actorID, cursor: cursor)
}
func toggleLike(post: Post, liked: Bool) async throws -> String? {
try await base.toggleLike(post: post, liked: liked)
}
func toggleRepost(post: Post, reposted: Bool) async throws -> String? {
try await base.toggleRepost(post: post, reposted: reposted)
}
func publish(text: String, replyTo: Post?, quotePost: Post?, images: [ImageAttachment]) async throws -> Post {
try await base.publish(text: text, replyTo: replyTo, quotePost: quotePost, images: images)
}
func setFollowing(_ following: Bool, actorID: String) async throws -> String? {
try await base.setFollowing(following, actorID: actorID)
}
}