diff --git a/.github/workflows/format.yml b/.github/workflows/format.yml index 088f277d..69d6d5b0 100644 --- a/.github/workflows/format.yml +++ b/.github/workflows/format.yml @@ -1,4 +1,4 @@ -name: Format +name: SwiftFormat on: push: branches: [master] diff --git a/Sources/InstantSearchInsights/Logic/EventProcessor.swift b/Sources/InstantSearchInsights/Logic/EventProcessor.swift index 7a2e1944..5b4a4f4b 100644 --- a/Sources/InstantSearchInsights/Logic/EventProcessor.swift +++ b/Sources/InstantSearchInsights/Logic/EventProcessor.swift @@ -159,6 +159,11 @@ private extension EventProcessor { let eligibleEvents = eventsPackage.items.filter(acceptEvent) + guard !eligibleEvents.isEmpty else { + logger.info("all events in package were filtered out by the acceptance condition, no event will be sent") + return + } + service.sendEvents(eligibleEvents) { [weak self] result in guard let processor = self else { return } diff --git a/Tests/InstantSearchInsightsTests/Unit/EventsProcessorTests.swift b/Tests/InstantSearchInsightsTests/Unit/EventsProcessorTests.swift index d202eade..aac5dd42 100644 --- a/Tests/InstantSearchInsightsTests/Unit/EventsProcessorTests.swift +++ b/Tests/InstantSearchInsightsTests/Unit/EventsProcessorTests.swift @@ -218,4 +218,36 @@ class EventsProcessorTests: XCTestCase { waitForExpectations(timeout: 10, handler: nil) } + + func testEventsFilteringException() throws { + let mockService = MockEventService() + let packageCapacity = 10 + let queue = DispatchQueue(label: "test queue") + + let storage = TestPackageStorage() + storage.store([try .init(items: [1, 2], capacity: 2), try .init(items: [3, 4], capacity: 2)]) + + let acceptEvent: (Int) -> Bool = { _ in false } + + let eventsProcessor = EventProcessor(service: mockService, + storage: storage, + packageCapacity: packageCapacity, + flushNotificationName: nil, + flushDelay: 1000, + acceptEvent: acceptEvent, + logger: Logger(label: #function), + dispatchQueue: queue) + + let exp = expectation(description: "send events") + exp.isInverted = true + + mockService.didSendEvents = { events in + XCTAssertTrue(events.allSatisfy(acceptEvent)) + exp.fulfill() + } + + eventsProcessor.flush() + + waitForExpectations(timeout: 10, handler: nil) + } }