Skip to content

Commit 1575232

Browse files
authored
Merge pull request #187 from optimizely/fixForEventDispatcher
(fix): fix for event dispatcher batch size
2 parents 8dca46e + 2415b91 commit 1575232

File tree

2 files changed

+46
-7
lines changed

2 files changed

+46
-7
lines changed

OptimizelySDK/Customization/DefaultEventDispatcher.swift

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -28,13 +28,13 @@ open class DefaultEventDispatcher : BackgroundingCallbacks, OPTEventDispatcher {
2828
static let MAX_FAILURE_COUNT = 3
2929

3030
// default timerInterval
31-
open var timerInterval:TimeInterval = 60 * 5 // every five minutes
31+
var timerInterval:TimeInterval = 60 * 5 // every five minutes
3232
// default batchSize.
3333
// attempt to send events in batches with batchSize number of events combined
34-
open var batchSize:Int = 10
34+
var batchSize:Int = 10
3535
// start trimming the front of the queue when we get to over maxQueueSize
3636
// TODO: implement
37-
open var maxQueueSize:Int = 3000
37+
var maxQueueSize:Int = 30000
3838

3939
lazy var logger = HandlerRegistryService.shared.injectLogger()
4040
var backingStore:DataStoreType = .file
@@ -47,9 +47,8 @@ open class DefaultEventDispatcher : BackgroundingCallbacks, OPTEventDispatcher {
4747
// timer as a atomic property.
4848
var timer:AtomicProperty<Timer> = AtomicProperty<Timer>()
4949

50-
public init(batchSize:Int = 10, maxQueueSize:Int = 3000, backingStore:DataStoreType = .file, dataStoreName:String = "OPTEventQueue", timerInterval:TimeInterval = 60*5 ) {
51-
self.batchSize = batchSize
52-
self.maxQueueSize = maxQueueSize
50+
public init(batchSize:Int = 10, backingStore:DataStoreType = .file, dataStoreName:String = "OPTEventQueue", timerInterval:TimeInterval = 60*5 ) {
51+
self.batchSize = batchSize > 0 ? batchSize : 1
5352
self.backingStore = backingStore
5453
self.backingStoreName = dataStoreName
5554
self.timerInterval = timerInterval
@@ -112,9 +111,17 @@ open class DefaultEventDispatcher : BackgroundingCallbacks, OPTEventDispatcher {
112111

113112
}
114113
while let eventsToSend:[EventForDispatch] = self.dataStore.getFirstItems(count:self.batchSize) {
114+
let actualEventsSize = eventsToSend.count
115115
var eventToSend = eventsToSend.batch()
116116
if let _ = eventToSend {
117117
// 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+
}
118125
}
119126
else {
120127
failedBatch()
@@ -182,7 +189,7 @@ open class DefaultEventDispatcher : BackgroundingCallbacks, OPTEventDispatcher {
182189

183190
}
184191

185-
func sendEvent(event: EventForDispatch, completionHandler: @escaping DispatchCompletionHandler) {
192+
open func sendEvent(event: EventForDispatch, completionHandler: @escaping DispatchCompletionHandler) {
186193
let config = URLSessionConfiguration.ephemeral
187194
let session = URLSession(configuration: config)
188195
var request = URLRequest(url: event.url)

OptimizelySDK/OptimizelyTests/OptimizelyTests-Common/EventDispatcherTests.swift

Lines changed: 32 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)
@@ -238,6 +264,12 @@ class EventDispatcherTests: XCTestCase {
238264

239265
}
240266

267+
func testDispatcherZeroBatchSize() {
268+
let eventDispatcher = DefaultEventDispatcher(batchSize: 0, backingStore: .userDefaults, dataStoreName: "DoNothing", timerInterval: 0)
269+
270+
XCTAssert(eventDispatcher.batchSize > 0)
271+
}
272+
241273
func testDataStoreQueue() {
242274
let queue = DataStoreQueueStackImpl<EventForDispatch>(queueStackName: "OPTEventQueue", dataStore: DataStoreMemory<Array<Data>>(storeName: "backingStoreName"))
243275

0 commit comments

Comments
 (0)