Skip to content

Commit 1cb97c1

Browse files
committed
add tests for event-batching
1 parent dbb4aa9 commit 1cb97c1

File tree

7 files changed

+537
-106
lines changed

7 files changed

+537
-106
lines changed

OptimizelySDK/Customization/DefaultEventDispatcher.swift

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -73,10 +73,11 @@ open class DefaultEventDispatcher : BackgroundingCallbacks, OPTEventDispatcher {
7373
unsubscribe()
7474
}
7575

76-
open func dispatchEvent(event: EventForDispatch, completionHandler: @escaping DispatchCompletionHandler) {
77-
76+
open func dispatchEvent(event: EventForDispatch, completionHandler: DispatchCompletionHandler?) {
7877
dataStore.save(item: event)
7978

79+
// TODO: use or clean up completionHandler
80+
8081
setTimer()
8182
}
8283

@@ -229,6 +230,9 @@ open class DefaultEventDispatcher : BackgroundingCallbacks, OPTEventDispatcher {
229230

230231
if #available(iOS 10.0, tvOS 10.0, *) {
231232
DispatchQueue.main.async {
233+
// should check here again
234+
guard self.timer.property == nil else { return }
235+
232236
self.timer.property = Timer.scheduledTimer(withTimeInterval: self.timerInterval, repeats: true) { (timer) in
233237
if self.dataStore.count == 0 {
234238
self.timer.performAtomic() { (timer) in

OptimizelySDK/Customization/Protocols/OPTEventDispatcher.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ public protocol OPTEventDispatcher {
3535
- Parameter event: EventForDispatch object which contains the url to send to and the body.
3636
- Parameter completionHandler: Called when the event has been sent or if an error occured. This may not be called in the case where the dispatcher is doing batch events. It is up to the implementor of the protocol.
3737
*/
38-
func dispatchEvent(event:EventForDispatch, completionHandler: @escaping DispatchCompletionHandler)
38+
func dispatchEvent(event:EventForDispatch, completionHandler: DispatchCompletionHandler?)
3939

4040
/// Attempts to flush the event queue if there are any events to process.
4141
func flushEvents()

OptimizelySDK/Data Model/DispatchEvents/BatchEvent.swift

Lines changed: 26 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -9,14 +9,14 @@
99
import Foundation
1010

1111
struct BatchEvent: Codable, Equatable {
12-
let revision: String
13-
let accountID: String
14-
let clientVersion: String
15-
let visitors: [Visitor]
16-
let projectID: String
17-
let clientName: String
18-
let anonymizeIP: Bool
19-
let enrichDecisions: Bool
12+
var revision: String
13+
var accountID: String
14+
var clientVersion: String
15+
var visitors: [Visitor]
16+
var projectID: String
17+
var clientName: String
18+
var anonymizeIP: Bool
19+
var enrichDecisions: Bool
2020

2121
enum CodingKeys: String, CodingKey {
2222
case revision
@@ -31,9 +31,9 @@ struct BatchEvent: Codable, Equatable {
3131
}
3232

3333
struct Visitor: Codable, Equatable {
34-
let attributes: [EventAttribute]
35-
let snapshots: [Snapshot]
36-
let visitorID: String
34+
var attributes: [EventAttribute]
35+
var snapshots: [Snapshot]
36+
var visitorID: String
3737

3838
enum CodingKeys: String, CodingKey {
3939
case attributes
@@ -43,10 +43,10 @@ struct Visitor: Codable, Equatable {
4343
}
4444

4545
struct EventAttribute: Codable, Equatable {
46-
let value: AttributeValue
47-
let key: String
48-
let type: String
49-
let entityID: String
46+
var value: AttributeValue
47+
var key: String
48+
var type: String
49+
var entityID: String
5050

5151
enum CodingKeys: String, CodingKey {
5252
case value
@@ -57,12 +57,12 @@ struct EventAttribute: Codable, Equatable {
5757
}
5858

5959
struct Snapshot: Codable, Equatable {
60-
let decisions: [Decision]?
61-
let events: [DispatchEvent]
60+
var decisions: [Decision]?
61+
var events: [DispatchEvent]
6262
}
6363

6464
struct Decision: Codable, Equatable {
65-
let variationID, campaignID, experimentID: String
65+
var variationID, campaignID, experimentID: String
6666

6767
enum CodingKeys: String, CodingKey {
6868
case variationID = "variation_id"
@@ -77,14 +77,18 @@ struct DispatchEvent: Codable, Equatable {
7777
static let activateEventKey = "campaign_activated"
7878

7979
// entityID is the layer id for impression events.
80-
let entityID: String
81-
let key: String
82-
let timestamp: Int64
83-
let uuid: String
80+
var entityID: String
81+
var key: String
82+
var timestamp: Int64
83+
var uuid: String
8484
var tags: [String: AttributeValue]?
8585
var revenue: AttributeValue?
8686
var value: AttributeValue?
8787

88+
// TODO: [Tom] these two in spec, not used here. is it ok?
89+
// quantity: MISSING
90+
// type: MISSING
91+
8892
enum CodingKeys: String, CodingKey {
8993
case entityID = "entity_id"
9094
case key

OptimizelySDK/OptimizelyTests/OptimizelyTests-APIs/OptimizelyManagerTests_Threading.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -289,7 +289,7 @@ class OptimizelyManagerTests_Threading: XCTestCase {
289289

290290
dispatchGroup.wait()
291291

292-
XCTAssertTrue(atomicBackground.property!.count == 101, "Count should be 101 but was \(atomicBackground.property!.count)")
292+
XCTAssertTrue(atomicBackground.property!.count == 101)
293293

294294
for index in 0...100 where index < atomicBackground.property!.count {
295295
XCTAssertTrue(atomicBackground.property![index].enabled!)
@@ -337,7 +337,7 @@ class OptimizelyManagerTests_Threading: XCTestCase {
337337

338338
func makeEventHandler() -> OPTEventDispatcher {
339339
class NoOpEventHandler : OPTEventDispatcher {
340-
func dispatchEvent(event: EventForDispatch, completionHandler: @escaping DispatchCompletionHandler) {
340+
func dispatchEvent(event: EventForDispatch, completionHandler: DispatchCompletionHandler?) {
341341

342342
}
343343

0 commit comments

Comments
 (0)