Docs native tests: fixture servers derive Docs-Seq from the rows they send
testANewDraftAfterADocumentStartsFromItsOwnCursor failed about one run in
three under load ("bc" instead of "abc", on main 80d4989 too). Logged:
the catch-up GET for since=1 returned 18 bytes (row 2 only) with
Docs-Seq 3. SequencedServer read `document.sequence` for the header after
awaiting the merge of the rows; the actor is reentrant, so the next
peer's commit landed in between and the header claimed a row the body did
not carry. The client then (correctly, by the protocol) set its cursor to
3 and never fetched row 3.
A test-fixture bug, not a client one: the real server derives Docs-Seq
from the rows in hand, never from a second read (handlers.go
`updatesGet`, "the update this loses if that is ever simplified"). The
draft half of the test was never involved. Both fixture servers
(RealtimeSyncTests' SequencedServer and OfflineDurabilityTests'
IntegratingServer, which had the same pattern) now take the header from
the rows they send, or the sequence before a snapshot is awaited, and a
POST answers with the sequence its own commit assigned. 0 failures in 80
runs (was 8 in 25).
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2 files changed,
+28
−11
native/apple/Tests/DocsKitTests/OfflineDurabilityTests.swift+8 −3native/apple/Tests/DocsKitTests/RealtimeSyncTests.swift+20 −8
diff --git a/native/apple/Tests/DocsKitTests/OfflineDurabilityTests.swift b/native/apple/Tests/DocsKitTests/OfflineDurabilityTests.swift| index 5045e43..2c5b55d 100644 |
| --- a/native/apple/Tests/DocsKitTests/OfflineDurabilityTests.swift |
| +++ b/native/apple/Tests/DocsKitTests/OfflineDurabilityTests.swift |
| @@ -74,16 +74,21 @@ private actor IntegratingServer: NativeTransport { |
| } |
| if path.contains("/updates") { |
| let since = URLComponents(string: path)?.queryItems?.first(where: { $0.name == "since" })?.value.flatMap(Int64.init) ?? 0 |
| - let data: Data |
| + // Docs-Seq from the rows in hand, before any await (see the same |
| + // note in RealtimeSyncTests' SequencedServer). |
| + let data: Data, highest: Int64 |
| if since > 0 { |
| - let updates = rows.keys.sorted().filter { $0 > since }.compactMap { rows[$0] } |
| + let kept = rows.keys.sorted().filter { $0 > since } |
| + highest = kept.last ?? since |
| + let updates = kept.compactMap { rows[$0] } |
| data = updates.isEmpty ? Data() : try await replica.mergeUpdates(updates) |
| } else { |
| fullDownloadAttempts += 1 |
| if failingFullDownloads > 0 { failingFullDownloads -= 1; throw NativeClientError.server(status: 503) } |
| + highest = sequence |
| fullDownloads += 1; data = try await snapshot() |
| } |
| - return NativeResponse(data: data, status: 200, headers: ["Docs-Seq": String(sequence)]) |
| + return NativeResponse(data: data, status: 200, headers: ["Docs-Seq": String(highest)]) |
| } |
| let detail = DocumentDetail(sqid: "doc", title: title, guid: "offline-property", can_write: true, can_share: false) |
| return NativeResponse(data: try JSONEncoder().encode(detail), status: 200, headers: [:]) |
diff --git a/native/apple/Tests/DocsKitTests/RealtimeSyncTests.swift b/native/apple/Tests/DocsKitTests/RealtimeSyncTests.swift| index 83046c3..29d1c0d 100644 |
| --- a/native/apple/Tests/DocsKitTests/RealtimeSyncTests.swift |
| +++ b/native/apple/Tests/DocsKitTests/RealtimeSyncTests.swift |
| @@ -37,10 +37,12 @@ private actor SequencedServer: NativeTransport { |
| } |
| func poison(_ value: Bool) { poisoned = value } |
| func sequence(of sqid: String = "doc") -> Int64 { documents[sqid]?.sequence ?? 0 } |
| - func commit(_ update: Data, to sqid: String = "doc") async throws { |
| + @discardableResult func commit(_ update: Data, to sqid: String = "doc") async throws -> Int64 { |
| guard let document = documents[sqid] else { throw NativeClientError.notFound } |
| _ = try await document.replica.apply(update); document.sequence += 1; document.rows[document.sequence] = update |
| - for listener in document.listeners.values { listener.yield(document.sequence) } |
| + let assigned = document.sequence |
| + for listener in document.listeners.values { listener.yield(assigned) } |
| + return assigned |
| } |
| func text(of sqid: String = "doc") async throws -> String { |
| try await documents[sqid]!.replica.projection().children.map(\.plainText).joined(separator: "\n") |
| @@ -62,18 +64,28 @@ private actor SequencedServer: NativeTransport { |
| // A slow failure: the peer's event reaches the client while its POST waits. |
| if failNextPost { failNextPost = false; try await Task.sleep(for: .milliseconds(300)); throw NativeClientError.server(status: 503) } |
| if let stall = stallNextPost { stallNextPost = nil; try await Task.sleep(for: stall) } |
| - try await commit(body, to: sqid) |
| - return NativeResponse(data: Data(), status: 204, headers: ["Docs-Seq": String(document.sequence)]) |
| + let assigned = try await commit(body, to: sqid) |
| + return NativeResponse(data: Data(), status: 204, headers: ["Docs-Seq": String(assigned)]) |
| } |
| if path.contains("/updates") { |
| let since = URLComponents(string: path)?.queryItems?.first(where: { $0.name == "since" })?.value.flatMap(Int64.init) ?? 0 |
| - let data: Data |
| + // Docs-Seq comes from the rows in hand, as handlers.go |
| + // `updatesGet` derives it, never from a second read: this actor |
| + // is reentrant, so a POST can commit a row while the merge below |
| + // awaits, and a header read afterwards would claim a row the body |
| + // does not carry (the client then skips it for good). |
| + let data: Data, highest: Int64 |
| if path.contains("since=") { |
| catchUps += 1 |
| - let updates = document.rows.keys.sorted().filter { $0 > since }.compactMap { document.rows[$0] } |
| + let rows = document.rows.keys.sorted().filter { $0 > since } |
| + highest = rows.last ?? since |
| + let updates = rows.compactMap { document.rows[$0] } |
| data = poisoned ? Data([255, 255, 255]) : updates.isEmpty ? Data() : try await document.replica.mergeUpdates(updates) |
| - } else { data = try await document.replica.persistentState().state } |
| - return NativeResponse(data: data, status: 200, headers: ["Docs-Seq": String(document.sequence)]) |
| + } else { |
| + highest = document.sequence |
| + data = try await document.replica.persistentState().state |
| + } |
| + return NativeResponse(data: data, status: 200, headers: ["Docs-Seq": String(highest)]) |
| } |
| let detail = DocumentDetail(sqid: sqid, title: "Doc", guid: "realtime-sync", can_write: true, can_share: false) |
| return NativeResponse(data: try JSONEncoder().encode(detail), status: 200, headers: [:]) |