From ac36351fa18a6254fca0824f6776e07554302de2 Mon Sep 17 00:00:00 2001 From: Vladislav Fitc Date: Tue, 24 Jan 2023 12:31:16 +0100 Subject: [PATCH 1/4] fix: send empty events packages if all non statisfying condition --- .../Logic/EventProcessor.swift | 5 +++ .../Unit/EventsProcessorTests.swift | 34 +++++++++++++++++++ 2 files changed, 39 insertions(+) diff --git a/Sources/InstantSearchInsights/Logic/EventProcessor.swift b/Sources/InstantSearchInsights/Logic/EventProcessor.swift index 7a2e1944..3b7dafda 100644 --- a/Sources/InstantSearchInsights/Logic/EventProcessor.swift +++ b/Sources/InstantSearchInsights/Logic/EventProcessor.swift @@ -158,6 +158,11 @@ private extension EventProcessor { logger.info("sending events package: \(eventsPackage.items)") 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 diff --git a/Tests/InstantSearchInsightsTests/Unit/EventsProcessorTests.swift b/Tests/InstantSearchInsightsTests/Unit/EventsProcessorTests.swift index d202eade..45b0fffc 100644 --- a/Tests/InstantSearchInsightsTests/Unit/EventsProcessorTests.swift +++ b/Tests/InstantSearchInsightsTests/Unit/EventsProcessorTests.swift @@ -218,4 +218,38 @@ 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) + } + } From 8c1575d5ee4771dbb1cf6a2cb85fadce2fca187b Mon Sep 17 00:00:00 2001 From: Vladislav Fitc Date: Tue, 24 Jan 2023 12:46:24 +0100 Subject: [PATCH 2/4] fix lint issues --- Sources/InstantSearchInsights/Logic/EventProcessor.swift | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Sources/InstantSearchInsights/Logic/EventProcessor.swift b/Sources/InstantSearchInsights/Logic/EventProcessor.swift index 3b7dafda..5b4a4f4b 100644 --- a/Sources/InstantSearchInsights/Logic/EventProcessor.swift +++ b/Sources/InstantSearchInsights/Logic/EventProcessor.swift @@ -158,7 +158,7 @@ private extension EventProcessor { logger.info("sending events package: \(eventsPackage.items)") 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 From a6a629823174944b207418a0a378bd931b4b8a58 Mon Sep 17 00:00:00 2001 From: Vladislav Fitc Date: Wed, 25 Jan 2023 16:09:05 +0100 Subject: [PATCH 3/4] apply swiftformat --- .../Unit/EventsProcessorTests.swift | 14 ++++++-------- 1 file changed, 6 insertions(+), 8 deletions(-) diff --git a/Tests/InstantSearchInsightsTests/Unit/EventsProcessorTests.swift b/Tests/InstantSearchInsightsTests/Unit/EventsProcessorTests.swift index 45b0fffc..aac5dd42 100644 --- a/Tests/InstantSearchInsightsTests/Unit/EventsProcessorTests.swift +++ b/Tests/InstantSearchInsightsTests/Unit/EventsProcessorTests.swift @@ -218,18 +218,17 @@ 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, @@ -241,15 +240,14 @@ class EventsProcessorTests: XCTestCase { 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) } - } From d9df8a7b1e770de779956f357d1c2b612c081f43 Mon Sep 17 00:00:00 2001 From: Vladislav Fitc Date: Wed, 25 Jan 2023 16:11:02 +0100 Subject: [PATCH 4/4] rename format -> swiftformat --- .github/workflows/format.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) 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]