Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: send empty events packages if all non statisfying condition #268

Merged
merged 4 commits into from
Jan 25, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/workflows/format.yml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
name: Format
name: SwiftFormat
on:
push:
branches: [master]
Expand Down
5 changes: 5 additions & 0 deletions Sources/InstantSearchInsights/Logic/EventProcessor.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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 }
Expand Down
32 changes: 32 additions & 0 deletions Tests/InstantSearchInsightsTests/Unit/EventsProcessorTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -218,4 +218,36 @@ class EventsProcessorTests: XCTestCase {

waitForExpectations(timeout: 10, handler: nil)
}

func testEventsFilteringException() throws {
let mockService = MockEventService<Int>()
let packageCapacity = 10
let queue = DispatchQueue(label: "test queue")

let storage = TestPackageStorage<Int>()
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)
}
}