Skip to content

Commit e619f7f

Browse files
fix for sending and recieving different events at different times when on a zero timer.
1 parent 01d622c commit e619f7f

File tree

2 files changed

+36
-2
lines changed

2 files changed

+36
-2
lines changed

OptimizelySDK/Customization/DefaultEventDispatcher.swift

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -111,9 +111,17 @@ open class DefaultEventDispatcher : BackgroundingCallbacks, OPTEventDispatcher {
111111

112112
}
113113
while let eventsToSend:[EventForDispatch] = self.dataStore.getFirstItems(count:self.batchSize) {
114+
let actualEventsSize = eventsToSend.count
114115
var eventToSend = eventsToSend.batch()
115116
if let _ = eventToSend {
116117
// we merged the event and ready for batch
118+
// if the bacth size is not equal to the actual event size,
119+
// then setup the batchSizeHolder to be the size of the event.
120+
if actualEventsSize != self.batchSize {
121+
batchSizeHolder = actualEventsSize
122+
self.batchSize = actualEventsSize
123+
sendCount = actualEventsSize - 1
124+
}
117125
}
118126
else {
119127
failedBatch()
@@ -145,7 +153,7 @@ open class DefaultEventDispatcher : BackgroundingCallbacks, OPTEventDispatcher {
145153
failureCount += 1
146154
case .success(_):
147155
// we succeeded. remove the batch size sent.
148-
if let removedItem:[EventForDispatch] = self.dataStore.removeFirstItems(count: self.batchSize) {
156+
if let removedItem:[EventForDispatch] = self.dataStore.removeFirstItems(count: failureCount > 0 ? 1 : actualEventsSize) {
149157
if self.batchSize == 1 && removedItem.first != event {
150158
self.logger?.e("Removed event different from sent event")
151159
}
@@ -181,7 +189,7 @@ open class DefaultEventDispatcher : BackgroundingCallbacks, OPTEventDispatcher {
181189

182190
}
183191

184-
func sendEvent(event: EventForDispatch, completionHandler: @escaping DispatchCompletionHandler) {
192+
open func sendEvent(event: EventForDispatch, completionHandler: @escaping DispatchCompletionHandler) {
185193
let config = URLSessionConfiguration.ephemeral
186194
let session = URLSession(configuration: config)
187195
var request = URLRequest(url: event.url)

OptimizelySDK/OptimizelyTests/OptimizelyTests-Common/EventDispatcherTests.swift

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,32 @@ class EventDispatcherTests: XCTestCase {
8080
// This is an example of a functional test case.
8181
// Use XCTAssert and related functions to verify your tests produce the correct results.
8282
}
83+
84+
func testDispatcherZeroTimeInterval() {
85+
class InnerEventDispatcher : DefaultEventDispatcher {
86+
var once = false
87+
var events:[EventForDispatch] = [EventForDispatch]()
88+
override func sendEvent(event: EventForDispatch, completionHandler: @escaping DispatchCompletionHandler) {
89+
events.append(event)
90+
if !once {
91+
self.dataStore.save(item: EventForDispatch(body: Data()))
92+
once = true
93+
}
94+
completionHandler(.success(Data()))
95+
}
96+
}
97+
98+
let dispatcher = InnerEventDispatcher(timerInterval:0)
99+
100+
// add two items.... call flush
101+
dispatcher.dataStore.save(item: EventForDispatch(body: Data()))
102+
dispatcher.flushEvents()
103+
104+
dispatcher.dispatcher.sync {
105+
}
106+
107+
XCTAssert(dispatcher.events.count == 2)
108+
}
83109

84110
func testEventDispatcherFile() {
85111
eventDispatcher = DefaultEventDispatcher( backingStore: .file)

0 commit comments

Comments
 (0)