Skip to content

Commit

Permalink
Stop sending if events fail to be removed from the event store
Browse files Browse the repository at this point in the history
  • Loading branch information
matus-tomlein committed Jul 3, 2024
1 parent 90c62a0 commit 0c326c6
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 12 deletions.
6 changes: 5 additions & 1 deletion Sources/Core/Emitter/Emitter.swift
Original file line number Diff line number Diff line change
Expand Up @@ -350,7 +350,7 @@ class Emitter: EmitterEventProcessing {
}
let allFailureCount = failedWillRetryCount + failedWontRetryCount

_ = self.eventStore.removeEvents(withIds: removableEvents)
let eventsRemoved = removableEvents.isEmpty ? true : self.eventStore.removeEvents(withIds: removableEvents)

logDebug(message: String(format: "Success Count: %d", successCount))
logDebug(message: String(format: "Failure Count: %d", allFailureCount))
Expand All @@ -366,6 +366,10 @@ class Emitter: EmitterEventProcessing {
if failedWillRetryCount > 0 && successCount == 0 {
logDebug(message: "Ending emitter run as all requests failed.")

self.scheduleStopSending()
} else if !eventsRemoved {
logDebug(message: "Ending emitter run as failed to remove events from event store.")

self.scheduleStopSending()
} else {
self.attemptEmit()
Expand Down
19 changes: 17 additions & 2 deletions Tests/TestEmitter.swift
Original file line number Diff line number Diff line change
Expand Up @@ -395,11 +395,26 @@ class TestEmitter: XCTestCase {

flush(emitter)
}

func testPausesEmitIfFailedToRemoveFromEventStore() {
let networkConnection = MockNetworkConnection(requestOption: .post, statusCode: 200)
let mockStore = MockEventStore()
mockStore.failToRemoveEvents = true
let emitter = self.emitter(with: networkConnection, bufferOption: .single, eventStore: mockStore)

addPayload(generatePayloads(1).first!, emitter)
Thread.sleep(forTimeInterval: 0.5)
addPayload(generatePayloads(1).first!, emitter)
Thread.sleep(forTimeInterval: 0.5)

XCTAssertEqual(1, networkConnection.sendingCount)
XCTAssertEqual(2, mockStore.count())
}

// MARK: - Emitter builder

func emitter(with networkConnection: NetworkConnection, bufferOption: BufferOption = .single) -> Emitter {
let emitter = Emitter(networkConnection: networkConnection, namespace: "ns1", eventStore: MockEventStore()) { emitter in
func emitter(with networkConnection: NetworkConnection, bufferOption: BufferOption = .single, eventStore: EventStore = MockEventStore()) -> Emitter {
let emitter = Emitter(networkConnection: networkConnection, namespace: "ns1", eventStore: eventStore) { emitter in
emitter.bufferOption = bufferOption
emitter.emitRange = 200
emitter.byteLimitGet = 20000
Expand Down
31 changes: 22 additions & 9 deletions Tests/Utils/MockEventStore.swift
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ class MockEventStore: NSObject, EventStore {

var db: [Int64 : Payload] = [:]
var lastInsertedRow = 0
var failToRemoveEvents = false

override init() {
super.init()
Expand All @@ -32,22 +33,34 @@ class MockEventStore: NSObject, EventStore {
}

func removeEvent(withId storeId: Int64) -> Bool {
logVerbose(message: "Remove \(storeId)")
return db.removeValue(forKey: storeId) != nil
if failToRemoveEvents {
return false
} else {
logVerbose(message: "Remove \(storeId)")
return db.removeValue(forKey: storeId) != nil
}
}

func removeEvents(withIds storeIds: [Int64]) -> Bool {
let result = true
for storeId in storeIds {
db.removeValue(forKey: storeId)
if failToRemoveEvents {
return false
} else {
let result = true
for storeId in storeIds {
db.removeValue(forKey: storeId)
}
return result
}
return result
}

func removeAllEvents() -> Bool {
db.removeAll()
lastInsertedRow = -1
return true
if failToRemoveEvents {
return false
} else {
db.removeAll()
lastInsertedRow = -1
return true
}
}

func count() -> UInt {
Expand Down

0 comments on commit 0c326c6

Please sign in to comment.