mirror of
https://github.com/permissionlesstech/bitchat.git
synced 2026-11-29 08:29:22 +00:00
test(nostr): flush debounce in ephemeral dedup persistence test (#1909)
This commit is contained in:
1 parent
bd603386d5
commit
b5eec13ba1
2 files changed
+27
-8
No files matched your search
@@ -167,6 +167,7 @@ final class MessageDeduplicationService {
|
||||
/// that don't opt in) keeps the cache purely in-memory.
|
||||
private let nostrEventStore: NostrProcessedEventStore?
|
||||
private let nostrEventCapacity: Int
|
||||
private let persistDebounceNanoseconds: UInt64
|
||||
private var persistScheduled = false
|
||||
private var persistTask: Task<Void, Never>?
|
||||
private var pendingPersistIDs: [String] = []
|
||||
@@ -178,15 +179,19 @@ final class MessageDeduplicationService {
|
||||
/// - nostrEventCapacity: Max entries for Nostr event cache
|
||||
/// - nostrEventStore: Optional disk store preloading and persisting
|
||||
/// processed Nostr event IDs across launches
|
||||
/// - persistDebounceNanoseconds: Debounce delay before flushing
|
||||
/// persisted event IDs to `nostrEventStore`
|
||||
init(
|
||||
contentCapacity: Int = TransportConfig.contentLRUCap,
|
||||
nostrEventCapacity: Int = TransportConfig.uiProcessedNostrEventsCap,
|
||||
nostrEventStore: NostrProcessedEventStore? = nil
|
||||
nostrEventStore: NostrProcessedEventStore? = nil,
|
||||
persistDebounceNanoseconds: UInt64 = 2_000_000_000
|
||||
) {
|
||||
self.contentCache = LRUDeduplicationCache(capacity: contentCapacity)
|
||||
self.nostrEventCache = LRUDeduplicationCache(capacity: nostrEventCapacity)
|
||||
self.nostrEventStore = nostrEventStore
|
||||
self.nostrEventCapacity = nostrEventCapacity
|
||||
self.persistDebounceNanoseconds = persistDebounceNanoseconds
|
||||
if let nostrEventStore {
|
||||
let generation = clearAllGeneration
|
||||
nostrEventStore.loadAsync { [weak self] eventIDs in
|
||||
@@ -272,20 +277,29 @@ final class MessageDeduplicationService {
|
||||
}
|
||||
}
|
||||
|
||||
private func flushPendingNostrEvents(to nostrEventStore: NostrProcessedEventStore) {
|
||||
let newIDs = pendingPersistIDs
|
||||
pendingPersistIDs.removeAll()
|
||||
nostrEventStore.append(newIDs, cap: nostrEventCapacity)
|
||||
}
|
||||
|
||||
/// Debounced persistence: bursts of inbound events (reconnect redelivery)
|
||||
/// collapse into one append. Append-merge rather than snapshot, so a
|
||||
/// transient in-memory clear between flushes can't shrink the disk record.
|
||||
private func schedulePersistIfNeeded() {
|
||||
guard let nostrEventStore, !persistScheduled else { return }
|
||||
let delay = persistDebounceNanoseconds
|
||||
guard delay > 0 else {
|
||||
flushPendingNostrEvents(to: nostrEventStore)
|
||||
return
|
||||
}
|
||||
persistScheduled = true
|
||||
persistTask = Task { @MainActor [weak self] in
|
||||
try? await Task.sleep(nanoseconds: 2_000_000_000)
|
||||
try? await Task.sleep(nanoseconds: delay)
|
||||
guard let self, !Task.isCancelled else { return }
|
||||
self.persistScheduled = false
|
||||
self.persistTask = nil
|
||||
let newIDs = self.pendingPersistIDs
|
||||
self.pendingPersistIDs.removeAll()
|
||||
nostrEventStore.append(newIDs, cap: self.nostrEventCapacity)
|
||||
self.flushPendingNostrEvents(to: nostrEventStore)
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -386,14 +386,19 @@ struct MessageDeduplicationServiceTests {
|
||||
let service = MessageDeduplicationService(
|
||||
contentCapacity: 100,
|
||||
nostrEventCapacity: 100,
|
||||
nostrEventStore: store
|
||||
nostrEventStore: store,
|
||||
persistDebounceNanoseconds: 0
|
||||
)
|
||||
|
||||
service.recordNostrEvent("ephemeral-geo-event", persist: false)
|
||||
|
||||
#expect(service.hasProcessedNostrEvent("ephemeral-geo-event"))
|
||||
#expect(!FileManager.default.fileExists(atPath: fileURL.path))
|
||||
#expect(store.load().isEmpty)
|
||||
#expect(!FileManager.default.fileExists(atPath: fileURL.path))
|
||||
|
||||
service.recordNostrEvent("durable-dm-event", persist: true)
|
||||
#expect(service.hasProcessedNostrEvent("durable-dm-event"))
|
||||
#expect(store.load() == ["durable-dm-event"])
|
||||
#expect(NostrProcessedEventStore(fileURL: fileURL).load() == ["durable-dm-event"])
|
||||
}
|
||||
|
||||
@Test func nostrProcessedEventStore_recoversAfterTransientReadFailure() throws {
|
||||
|
||||
Reference in new issue
Block a user