Fix race condition leading to intermittent issues with ndb streaming and related tests

A race condition was identified where notes would get dropped if they
get indexed in the time window between when a query is made and the subscription is made.

The issue was fixed by making the subscribe call before making the query
call, to ensure we get all notes from that time when we perform the
query.

This dropped the failure rate for ndb subscription tests from about 20%
down to about 4%.

Local relay model issue was not publicly released, which is why the
changelog entry is "none".

Changelog-None
Signed-off-by: Daniel D’Aquino <daniel@daquino.me>
This commit is contained in:
Daniel D’Aquino
2025-10-24 18:37:55 -07:00
parent 7cf9a07099
commit 58e6a49bcf
2 changed files with 14 additions and 7 deletions

View File

@@ -129,6 +129,7 @@ class NostrNetworkManagerTests: XCTestCase {
var count = 0
var receivedIds = Set<NoteId>()
let subscribeExpectation = XCTestExpectation(description: "Should receive all events and EOSE")
let atLeastXNotes = XCTestExpectation(description: "Should get at least the expected amount of notes")
Task {
do {
@@ -142,6 +143,9 @@ class NostrNetworkManagerTests: XCTestCase {
receivedIds.insert(note.id)
}
}
if count >= expectedCount {
atLeastXNotes.fulfill()
}
case .eose:
// End of stored events
subscribeExpectation.fulfill()
@@ -153,7 +157,7 @@ class NostrNetworkManagerTests: XCTestCase {
}
}
await fulfillment(of: [subscribeExpectation], timeout: 10.0)
await fulfillment(of: [subscribeExpectation, atLeastXNotes], timeout: 10.0)
// Verify we received exactly the expected number of unique events
XCTAssertEqual(count, expectedCount, "Should receive all \(expectedCount) events")