Skip to content

Commit 1c757b6

Browse files
take out the eventImmiter queue
1 parent 4e53790 commit 1c757b6

File tree

5 files changed

+60
-95
lines changed

5 files changed

+60
-95
lines changed

OptimizelySDK/Optimizely/OptimizelyManager.swift

Lines changed: 47 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,6 @@ open class OptimizelyManager: NSObject {
5252
}
5353

5454
private let reInitLock = Dispatch.DispatchSemaphore(value: 1)
55-
let eventImitterQueue = DispatchQueue(label:"OptimizelyEventImitterQueue")
5655

5756
// MARK: - Public interfaces
5857

@@ -645,35 +644,32 @@ extension OptimizelyManager {
645644
userId: String,
646645
attributes: OptimizelyAttributes?=nil) {
647646

648-
eventImitterQueue.async {
649-
guard let config = self.config else { return }
650-
651-
guard let body = BatchEventBuilder.createImpressionEvent(config: config,
652-
experiment: experiment,
653-
varionation: variation,
654-
userId: userId,
655-
attributes: attributes) else
656-
{
657-
self.logger.e(OptimizelyError.eventBuildFailure(DispatchEvent.activateEventKey))
658-
return
659-
}
660-
661-
let event = EventForDispatch(body: body)
662-
// because we are batching events, we cannot guarantee that the completion handler will be
663-
// called. So, for now, we are queuing and calling onActivate. Maybe we should mention that
664-
// onActivate only means the event has been queued and not necessarily sent.
665-
self.eventDispatcher.dispatchEvent(event: event) { result in
666-
switch result {
667-
case .failure:
668-
break
669-
case .success( _):
670-
break
671-
}
647+
guard let config = self.config else { return }
648+
649+
guard let body = BatchEventBuilder.createImpressionEvent(config: config,
650+
experiment: experiment,
651+
varionation: variation,
652+
userId: userId,
653+
attributes: attributes) else
654+
{
655+
self.logger.e(OptimizelyError.eventBuildFailure(DispatchEvent.activateEventKey))
656+
return
657+
}
658+
659+
let event = EventForDispatch(body: body)
660+
// because we are batching events, we cannot guarantee that the completion handler will be
661+
// called. So, for now, we are queuing and calling onActivate. Maybe we should mention that
662+
// onActivate only means the event has been queued and not necessarily sent.
663+
self.eventDispatcher.dispatchEvent(event: event) { result in
664+
switch result {
665+
case .failure:
666+
break
667+
case .success( _):
668+
break
672669
}
673-
674-
self.notificationCenter.sendNotifications(type: NotificationType.Activate.rawValue, args: [experiment, userId, attributes, variation, ["url":event.url as Any, "body":event.body as Any]])
675-
676670
}
671+
672+
self.notificationCenter.sendNotifications(type: NotificationType.Activate.rawValue, args: [experiment, userId, attributes, variation, ["url":event.url as Any, "body":event.body as Any]])
677673

678674
}
679675

@@ -683,34 +679,31 @@ extension OptimizelyManager {
683679
eventTags: OptimizelyEventTags?=nil) {
684680

685681

686-
eventImitterQueue.async {
687-
guard let config = self.config else { return }
688-
689-
guard let body = BatchEventBuilder.createConversionEvent(config: config,
690-
eventKey: eventKey,
691-
userId: userId,
692-
attributes: attributes,
693-
eventTags: eventTags) else
694-
{
695-
self.logger.e(OptimizelyError.eventBuildFailure(eventKey))
696-
return
697-
}
698-
699-
let event = EventForDispatch(body: body)
700-
// because we are batching events, we cannot guarantee that the completion handler will be
701-
// called. So, for now, we are queuing and calling onTrack. Maybe we should mention that
702-
// onTrack only means the event has been queued and not necessarily sent.
703-
self.eventDispatcher.dispatchEvent(event: event) { result in
704-
switch result {
705-
case .failure:
706-
break
707-
case .success( _):
708-
break
709-
}
682+
guard let config = self.config else { return }
683+
684+
guard let body = BatchEventBuilder.createConversionEvent(config: config,
685+
eventKey: eventKey,
686+
userId: userId,
687+
attributes: attributes,
688+
eventTags: eventTags) else
689+
{
690+
self.logger.e(OptimizelyError.eventBuildFailure(eventKey))
691+
return
692+
}
693+
694+
let event = EventForDispatch(body: body)
695+
// because we are batching events, we cannot guarantee that the completion handler will be
696+
// called. So, for now, we are queuing and calling onTrack. Maybe we should mention that
697+
// onTrack only means the event has been queued and not necessarily sent.
698+
self.eventDispatcher.dispatchEvent(event: event) { result in
699+
switch result {
700+
case .failure:
701+
break
702+
case .success( _):
703+
break
710704
}
711-
self.notificationCenter.sendNotifications(type: NotificationType.Track.rawValue, args: [eventKey, userId, attributes, eventTags, ["url":event.url as Any, "body":event.body as Any]])
712-
713705
}
706+
self.notificationCenter.sendNotifications(type: NotificationType.Track.rawValue, args: [eventKey, userId, attributes, eventTags, ["url":event.url as Any, "body":event.body as Any]])
714707

715708
}
716709
}

OptimizelySDK/OptimizelyTests/OptimizelyTests-APIs/OptimizelyManagerTests_Evaluation.swift

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -226,7 +226,6 @@ class OptimizelyManagerTests_Evaluation: XCTestCase {
226226
]
227227

228228
let variationKey = try? optimizely.activate(experimentKey: experimentKey, userId: userId, attributes: attributes)
229-
optimizely.eventImitterQueue.sync {}
230229
XCTAssertNotNil(variationKey)
231230
XCTAssertNotNil(eventDispatcher.events.first)
232231
}

OptimizelySDK/OptimizelyTests/OptimizelyTests-Common/BatchEventBuilderTests_Attributes.swift

Lines changed: 0 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -53,8 +53,6 @@ class BatchEventBuilderTests_Attributes: XCTestCase {
5353
_ = try! optimizely.activate(experimentKey: experimentKey,
5454
userId: userId,
5555
attributes: attributes)
56-
optimizely?.eventImitterQueue.sync {
57-
}
5856

5957
let event = eventDispatcher.events.first!
6058
let json = JSON(event.body)
@@ -97,8 +95,6 @@ class BatchEventBuilderTests_Attributes: XCTestCase {
9795
_ = try! optimizely.activate(experimentKey: experimentKey,
9896
userId: userId,
9997
attributes: attributes)
100-
optimizely?.eventImitterQueue.sync {
101-
}
10298

10399
let event = eventDispatcher.events.first!
104100
let json = JSON(event.body)
@@ -134,9 +130,6 @@ class BatchEventBuilderTests_Attributes: XCTestCase {
134130
_ = try! optimizely.activate(experimentKey: experimentKey,
135131
userId: userId,
136132
attributes: attributes)
137-
optimizely?.eventImitterQueue.sync {
138-
}
139-
140133
let event = eventDispatcher.events.first!
141134
let json = JSON(event.body)
142135
let array = json["visitors"][0]["attributes"].arrayValue
@@ -174,8 +167,6 @@ class BatchEventBuilderTests_Attributes: XCTestCase {
174167
_ = try! optimizely.activate(experimentKey: experimentKey,
175168
userId: userId,
176169
attributes: attributes)
177-
optimizely?.eventImitterQueue.sync {
178-
}
179170

180171
let event = eventDispatcher.events.first!
181172
let json = JSON(event.body)
@@ -212,8 +203,6 @@ class BatchEventBuilderTests_Attributes: XCTestCase {
212203
userId: userId,
213204
attributes: attributes)
214205

215-
optimizely?.eventImitterQueue.sync {
216-
}
217206

218207
let event = eventDispatcher.events.first!
219208
let json = JSON(event.body)
@@ -232,8 +221,6 @@ class BatchEventBuilderTests_Attributes: XCTestCase {
232221
userId: userId,
233222
attributes: nil)
234223

235-
optimizely?.eventImitterQueue.sync {
236-
}
237224

238225
let event = eventDispatcher.events.first!
239226
let json = JSON(event.body)
@@ -255,8 +242,6 @@ class BatchEventBuilderTests_Attributes: XCTestCase {
255242
_ = try! optimizely.activate(experimentKey: experimentKey,
256243
userId: userId,
257244
attributes: attributes)
258-
optimizely?.eventImitterQueue.sync {
259-
}
260245

261246
let event = eventDispatcher.events.first!
262247
let json = JSON(event.body)
@@ -292,8 +277,6 @@ extension BatchEventBuilderTests_Attributes {
292277
eventDispatcher: eventDispatcher)
293278
_ = try! optimizely?.activate(experimentKey: "ab_running_exp_untargeted",
294279
userId: "test_user_1")
295-
optimizely?.eventImitterQueue.sync {
296-
}
297280

298281
let eventForDispatch = eventDispatcher.events.first
299282
let event: BatchEvent = try! OTUtils.model(fromData: eventForDispatch!.body)
@@ -317,9 +300,6 @@ extension BatchEventBuilderTests_Attributes {
317300
_ = try! optimizely?.activate(experimentKey: "ab_running_exp_untargeted",
318301
userId: "test_user_1")
319302

320-
optimizely?.eventImitterQueue.sync {
321-
322-
}
323303

324304
let eventForDispatch = eventDispatcher.events.first
325305
let event: BatchEvent = try! OTUtils.model(fromData: eventForDispatch!.body)
@@ -343,8 +323,6 @@ extension BatchEventBuilderTests_Attributes {
343323
_ = try! optimizely?.activate(experimentKey: "ab_running_exp_untargeted",
344324
userId: "test_user_1")
345325

346-
optimizely?.eventImitterQueue.sync {
347-
}
348326

349327
let eventForDispatch = eventDispatcher.events.first
350328
let event: BatchEvent = try! OTUtils.model(fromData: eventForDispatch!.body)

OptimizelySDK/OptimizelyTests/OptimizelyTests-Common/BatchEventBuilderTests_EventTags.swift

Lines changed: 13 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,6 @@ class BatchEventBuilderTests_EventTags: XCTestCase {
4242
"check": false]
4343

4444
try! optimizely.track(eventKey: eventKey, userId: userId, attributes: nil, eventTags: eventTags)
45-
optimizely.eventImitterQueue.sync {}
4645
let de = getDispatchEvent(dispatcher: eventDispatcher)!
4746
let tags = de["tags"] as! [String: Any]
4847

@@ -61,7 +60,7 @@ class BatchEventBuilderTests_EventTags: XCTestCase {
6160
"value": 32.5]
6261

6362
try! optimizely.track(eventKey: eventKey, userId: userId, attributes: nil, eventTags: eventTags)
64-
optimizely.eventImitterQueue.sync {}
63+
6564
let de = getDispatchEvent(dispatcher: eventDispatcher)!
6665
let tags = de["tags"] as! [String: Any]
6766

@@ -84,7 +83,7 @@ extension BatchEventBuilderTests_EventTags {
8483
"future": [1,2,3]]
8584

8685
try! optimizely.track(eventKey: eventKey, userId: userId, attributes: nil, eventTags: eventTags)
87-
optimizely.eventImitterQueue.sync {}
86+
8887
let de = getDispatchEvent(dispatcher: eventDispatcher)!
8988
let tags = de["tags"] as! [String: Any]
9089

@@ -99,7 +98,7 @@ extension BatchEventBuilderTests_EventTags {
9998
"tooBig": OTUtils.positiveTooBigValue]
10099

101100
try! optimizely.track(eventKey: eventKey, userId: userId, attributes: nil, eventTags: eventTags)
102-
optimizely.eventImitterQueue.sync {}
101+
103102
let de = getDispatchEvent(dispatcher: eventDispatcher)!
104103
let tags = de["tags"] as! [String: Any]
105104

@@ -121,7 +120,7 @@ extension BatchEventBuilderTests_EventTags {
121120
"value": "bar"]
122121

123122
try! optimizely.track(eventKey: eventKey, userId: userId, attributes: nil, eventTags: eventTags)
124-
optimizely.eventImitterQueue.sync {}
123+
125124
let de = getDispatchEvent(dispatcher: eventDispatcher)!
126125
let tags = de["tags"] as! [String: Any]
127126

@@ -139,7 +138,7 @@ extension BatchEventBuilderTests_EventTags {
139138
"value": false]
140139

141140
try! optimizely.track(eventKey: eventKey, userId: userId, attributes: nil, eventTags: eventTags)
142-
optimizely.eventImitterQueue.sync {}
141+
143142
let de = getDispatchEvent(dispatcher: eventDispatcher)!
144143
let tags = de["tags"] as! [String: Any]
145144

@@ -157,7 +156,7 @@ extension BatchEventBuilderTests_EventTags {
157156
"value": 30]
158157

159158
try! optimizely.track(eventKey: eventKey, userId: userId, attributes: nil, eventTags: eventTags)
160-
optimizely.eventImitterQueue.sync {}
159+
161160
let de = getDispatchEvent(dispatcher: eventDispatcher)!
162161
let tags = de["tags"] as! [String: Any]
163162

@@ -180,7 +179,7 @@ extension BatchEventBuilderTests_EventTags {
180179
"value": OTUtils.positiveTooBigValue]
181180

182181
try! optimizely.track(eventKey: eventKey, userId: userId, attributes: nil, eventTags: eventTags)
183-
optimizely.eventImitterQueue.sync {}
182+
184183
let de = getDispatchEvent(dispatcher: eventDispatcher)!
185184
let tags = de["tags"] as! [String: Any]
186185

@@ -198,7 +197,7 @@ extension BatchEventBuilderTests_EventTags {
198197
"value": OTUtils.negativeTooBigValue]
199198

200199
try! optimizely.track(eventKey: eventKey, userId: userId, attributes: nil, eventTags: eventTags)
201-
optimizely.eventImitterQueue.sync {}
200+
202201
let de = getDispatchEvent(dispatcher: eventDispatcher)!
203202
let tags = de["tags"] as! [String: Any]
204203

@@ -222,7 +221,7 @@ extension BatchEventBuilderTests_EventTags {
222221
"value": 0]
223222

224223
try! optimizely.track(eventKey: eventKey, userId: userId, attributes: nil, eventTags: eventTags)
225-
optimizely.eventImitterQueue.sync {}
224+
226225
let de = getDispatchEvent(dispatcher: eventDispatcher)!
227226
let tags = de["tags"] as! [String: Any]
228227

@@ -240,7 +239,7 @@ extension BatchEventBuilderTests_EventTags {
240239
"value": 1]
241240

242241
try! optimizely.track(eventKey: eventKey, userId: userId, attributes: nil, eventTags: eventTags)
243-
optimizely.eventImitterQueue.sync {}
242+
244243
let de = getDispatchEvent(dispatcher: eventDispatcher)!
245244
let tags = de["tags"] as! [String: Any]
246245

@@ -258,7 +257,7 @@ extension BatchEventBuilderTests_EventTags {
258257
"value": -1]
259258

260259
try! optimizely.track(eventKey: eventKey, userId: userId, attributes: nil, eventTags: eventTags)
261-
optimizely.eventImitterQueue.sync {}
260+
262261
let de = getDispatchEvent(dispatcher: eventDispatcher)!
263262
let tags = de["tags"] as! [String: Any]
264263

@@ -282,7 +281,7 @@ extension BatchEventBuilderTests_EventTags {
282281
"value": NSNumber(value: 3.15)]
283282

284283
try! optimizely.track(eventKey: eventKey, userId: userId, attributes: nil, eventTags: eventTags)
285-
optimizely.eventImitterQueue.sync {}
284+
286285
let de = getDispatchEvent(dispatcher: eventDispatcher)!
287286
let tags = de["tags"] as! [String: Any]
288287

@@ -304,7 +303,7 @@ extension BatchEventBuilderTests_EventTags {
304303
"value": Float(32)]
305304

306305
try! optimizely.track(eventKey: eventKey, userId: userId, attributes: nil, eventTags: eventTags)
307-
optimizely.eventImitterQueue.sync {}
306+
308307
let de = getDispatchEvent(dispatcher: eventDispatcher)!
309308
let tags = de["tags"] as! [String: Any]
310309

OptimizelySDK/OptimizelyTests/OptimizelyTests-Common/BatchEventBuilderTests_Events.swift

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -50,8 +50,6 @@ class BatchEventBuilderTests_Events: XCTestCase {
5050
_ = try! optimizely.activate(experimentKey: experimentKey,
5151
userId: userId,
5252
attributes: attributes)
53-
optimizely.eventImitterQueue.sync {
54-
}
5553

5654
let eventForDispatch = eventDispatcher.events.first!
5755
let json = JSON(eventForDispatch.body)
@@ -107,7 +105,6 @@ class BatchEventBuilderTests_Events: XCTestCase {
107105
userId: userId,
108106
attributes: attributes,
109107
eventTags: eventTags)
110-
optimizely.eventImitterQueue.sync {}
111108
let eventForDispatch = eventDispatcher.events.first!
112109
let json = JSON(eventForDispatch.body)
113110
let event = json.dictionaryValue
@@ -160,7 +157,6 @@ class BatchEventBuilderTests_Events: XCTestCase {
160157
userId: userId,
161158
attributes: nil,
162159
eventTags: eventTags)
163-
optimizely.eventImitterQueue.sync {}
164160
XCTAssert(false, "event should not be created for an invalid event key")
165161
} catch {
166162
XCTAssert(true)

0 commit comments

Comments
 (0)