Skip to content

Commit ec83c87

Browse files
committed
remove odp events reset on update config
1 parent b0d7fcd commit ec83c87

File tree

5 files changed

+107
-14
lines changed

5 files changed

+107
-14
lines changed

OptimizelySwiftSDK.xcodeproj/project.pbxproj

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1814,6 +1814,8 @@
18141814
846414B7289B2D0700C45EE2 /* odp_integrated_no_segments.json in Resources */ = {isa = PBXBuildFile; fileRef = 846414A0289B2D0700C45EE2 /* odp_integrated_no_segments.json */; };
18151815
846414B8289B2D0700C45EE2 /* odp_integrated_no_segments.json in Resources */ = {isa = PBXBuildFile; fileRef = 846414A0289B2D0700C45EE2 /* odp_integrated_no_segments.json */; };
18161816
846414B9289B2D0700C45EE2 /* odp_integrated_no_segments.json in Resources */ = {isa = PBXBuildFile; fileRef = 846414A0289B2D0700C45EE2 /* odp_integrated_no_segments.json */; };
1817+
84644AB328F0D2B3003FB9CB /* OptimizelyUserContextTests_ODP_2.swift in Sources */ = {isa = PBXBuildFile; fileRef = 84644AB228F0D2B3003FB9CB /* OptimizelyUserContextTests_ODP_2.swift */; };
1818+
84644AB428F0D2B3003FB9CB /* OptimizelyUserContextTests_ODP_2.swift in Sources */ = {isa = PBXBuildFile; fileRef = 84644AB228F0D2B3003FB9CB /* OptimizelyUserContextTests_ODP_2.swift */; };
18171819
848617C82863DC2700B7F41B /* OdpSegmentManager.swift in Sources */ = {isa = PBXBuildFile; fileRef = 848617C72863DC2700B7F41B /* OdpSegmentManager.swift */; };
18181820
848617C92863DC2700B7F41B /* OdpSegmentManager.swift in Sources */ = {isa = PBXBuildFile; fileRef = 848617C72863DC2700B7F41B /* OdpSegmentManager.swift */; };
18191821
848617CA2863DC2700B7F41B /* OdpSegmentManager.swift in Sources */ = {isa = PBXBuildFile; fileRef = 848617C72863DC2700B7F41B /* OdpSegmentManager.swift */; };
@@ -2393,6 +2395,7 @@
23932395
84640880281320F000CCF97D /* IntegrationTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = IntegrationTests.swift; sourceTree = "<group>"; };
23942396
8464149F289B2D0700C45EE2 /* decide_audience_segments.json */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text.json; path = decide_audience_segments.json; sourceTree = "<group>"; };
23952397
846414A0289B2D0700C45EE2 /* odp_integrated_no_segments.json */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text.json; path = odp_integrated_no_segments.json; sourceTree = "<group>"; };
2398+
84644AB228F0D2B3003FB9CB /* OptimizelyUserContextTests_ODP_2.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = OptimizelyUserContextTests_ODP_2.swift; sourceTree = "<group>"; };
23962399
848617C72863DC2700B7F41B /* OdpSegmentManager.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = OdpSegmentManager.swift; sourceTree = "<group>"; };
23972400
848617D82863E21200B7F41B /* OdpSegmentApiManager.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = OdpSegmentApiManager.swift; sourceTree = "<group>"; };
23982401
848617D92863E21200B7F41B /* OdpEventApiManager.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = OdpEventApiManager.swift; sourceTree = "<group>"; };
@@ -2944,6 +2947,7 @@
29442947
6EB97BCC24C89DFB00068883 /* OptimizelyUserContextTests_Decide_Legacy.swift */,
29452948
6E0A72D326C5B9AE00FF92B5 /* OptimizelyUserContextTests_ForcedDecisions.swift */,
29462949
84F6BAB227FCC5CF004BE62A /* OptimizelyUserContextTests_ODP.swift */,
2950+
84644AB228F0D2B3003FB9CB /* OptimizelyUserContextTests_ODP_2.swift */,
29472951
84F6BADC27FD011B004BE62A /* OptimizelyUserContextTests_ODP_Decide.swift */,
29482952
84958C5D280F22FE008655C7 /* OptimizelyUserContextTests_Performance.swift */,
29492953
6E86CEB224FF20DE005DAFED /* OptimizelyUserContextTests_Objc.m */,
@@ -4840,6 +4844,7 @@
48404844
84E2E97F2855875E001114AB /* OdpEventManager.swift in Sources */,
48414845
6E9B116722C5487100C22D81 /* BatchEventBuilderTests_Events.swift in Sources */,
48424846
6E75181922C520D400B2B157 /* DataStoreQueueStackImpl.swift in Sources */,
4847+
84644AB428F0D2B3003FB9CB /* OptimizelyUserContextTests_ODP_2.swift in Sources */,
48434848
6E75186D22C520D400B2B157 /* Rollout.swift in Sources */,
48444849
6E4544B7270E67C800F2CEBC /* NetworkReachability.swift in Sources */,
48454850
848617D52863DC2700B7F41B /* OdpSegmentManager.swift in Sources */,
@@ -5115,6 +5120,7 @@
51155120
6E7517A522C520D400B2B157 /* Array+Extension.swift in Sources */,
51165121
6E9B115122C5486E00C22D81 /* NotificationCenterTests.swift in Sources */,
51175122
6E75184322C520D400B2B157 /* Event.swift in Sources */,
5123+
84644AB328F0D2B3003FB9CB /* OptimizelyUserContextTests_ODP_2.swift in Sources */,
51185124
6E75193F22C520D500B2B157 /* OPTDecisionService.swift in Sources */,
51195125
84E7ABC027D2A1F100447CAE /* ThreadSafeLogger.swift in Sources */,
51205126
6E7516CD22C520D400B2B157 /* OPTLogger.swift in Sources */,

Sources/ODP/OdpManager.swift

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -127,14 +127,13 @@ class OdpManager {
127127
// If it fails to flush all the old events here (network error), remaning events will be discarded.
128128
eventManager.flush()
129129

130-
let configChanged = odpConfig.update(apiKey: apiKey, apiHost: apiHost, segmentsToCheck: segmentsToCheck)
131-
guard configChanged else { return }
132-
133-
// reset segments cache when odp integration or segmentsToCheck are changed
134-
segmentManager.reset()
135-
136-
// reset all remaining events for old config
137-
eventManager.reset()
130+
let configChanged = odpConfig.update(apiKey: apiKey,
131+
apiHost: apiHost,
132+
segmentsToCheck: segmentsToCheck)
133+
if configChanged {
134+
// reset segments cache when odp integration or segmentsToCheck are changed
135+
segmentManager.reset()
136+
}
138137
}
139138

140139
}

Tests/OptimizelyTests-Common/OdpManagerTests.swift

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -189,7 +189,12 @@ class OdpManagerTests: XCTestCase {
189189

190190
// MARK: - updateConfig
191191

192-
func testUpdateOdpConfig_resetCalled() {
192+
func testUpdateOdpConfig_segmentResetCalled() {
193+
// initially
194+
// - apiKey = nil
195+
// - apiHost = nil
196+
// - segmentsToCheck = []
197+
193198
manager.updateOdpConfig(apiKey: "key-1", apiHost: "host-1", segmentsToCheck: [])
194199
XCTAssertTrue(segmentManager.resetCalled)
195200

@@ -235,34 +240,35 @@ class OdpManagerTests: XCTestCase {
235240
}
236241

237242
func testUpdateOdpConfig_flushCalled() {
243+
// initially
244+
// - apiKey = nil
245+
// - apiHost = nil
246+
// - segmentsToCheck = []
247+
238248
manager.updateOdpConfig(apiKey: "key-1", apiHost: "host-1", segmentsToCheck: [])
239249
XCTAssertEqual(eventManager.flushApiKeys.count, 1, "flush called before")
240250
XCTAssertEqual(eventManager.flushApiKeys[0], nil)
241-
XCTAssertTrue(eventManager.resetCalled, "reset called after to discard remaining events")
242251

243252
eventManager.flushApiKeys.removeAll()
244253
eventManager.resetCalled = false
245254

246255
manager.updateOdpConfig(apiKey: "key-2", apiHost: "host-1", segmentsToCheck: [])
247256
XCTAssertEqual(eventManager.flushApiKeys.count, 1)
248257
XCTAssertEqual(eventManager.flushApiKeys[0], "key-1", "old events must be flushed with the old odp key")
249-
XCTAssertTrue(eventManager.resetCalled)
250258

251259
eventManager.flushApiKeys.removeAll()
252260
eventManager.resetCalled = false
253261

254262
manager.updateOdpConfig(apiKey: "key-2", apiHost: "host-1", segmentsToCheck: [])
255263
XCTAssertEqual(eventManager.flushApiKeys.count, 1)
256264
XCTAssertEqual(eventManager.flushApiKeys[0], "key-2")
257-
XCTAssertFalse(eventManager.resetCalled, "reset should not be called if config not changed")
258265

259266
eventManager.flushApiKeys.removeAll()
260267
eventManager.resetCalled = false
261268

262269
manager.updateOdpConfig(apiKey: nil, apiHost: nil, segmentsToCheck: [])
263270
XCTAssertEqual(eventManager.flushApiKeys.count, 1)
264271
XCTAssertEqual(eventManager.flushApiKeys[0], "key-2")
265-
XCTAssertTrue(eventManager.resetCalled, "reset called after to discard remaining events")
266272
}
267273

268274
func testUpdateOdpConfig_odpConfigPropagatedProperly() {

Tests/OptimizelyTests-Common/OptimizelyUserContextTests_ODP.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -251,7 +251,7 @@ class MockOdpManager: OdpManager {
251251
self.options = options
252252
super.fetchQualifiedSegments(userId: userId, options: options, completionHandler: completionHandler)
253253
}
254-
254+
255255
override func identifyUser(userId: String) {
256256
self.userId = userId
257257
self.identifyCalled = true
Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
//
2+
// Copyright 2022, Optimizely, Inc. and contributors
3+
//
4+
// Licensed under the Apache License, Version 2.0 (the "License");
5+
// you may not use this file except in compliance with the License.
6+
// You may obtain a copy of the License at
7+
//
8+
// http://www.apache.org/licenses/LICENSE-2.0
9+
//
10+
// Unless required by applicable law or agreed to in writing, software
11+
// distributed under the License is distributed on an "AS IS" BASIS,
12+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
// See the License for the specific language governing permissions and
14+
// limitations under the License.
15+
//
16+
17+
import XCTest
18+
19+
class OptimizelyUserContextTests_ODP_2: XCTestCase {
20+
21+
let datafile = OTUtils.loadJSONDatafile("decide_audience_segments")!
22+
23+
func testOdpEvents_earlyEventsDispatched() {
24+
// use a different sdkKey to avoid events conflict
25+
let sdkKey = OTUtils.randomSdkKey
26+
27+
// odp disabled to avoid initial noise
28+
29+
let optimizely = OptimizelyClient(sdkKey: sdkKey,
30+
settings: OptimizelySdkSettings(disableOdp: true))
31+
32+
// override with a custom enabled odpManager.
33+
// - client_inializatied event will be sent automatically
34+
// - will wait in the queue until project config is ready
35+
36+
let odpEventApiManager = MockOdpEventApiManager()
37+
optimizely.odpManager = OdpManager(sdkKey: sdkKey,
38+
disable: false,
39+
cacheSize: 10,
40+
cacheTimeoutInSecs: 10,
41+
eventManager: OdpEventManager(sdkKey: sdkKey,
42+
odpConfig: nil,
43+
apiManager: odpEventApiManager))
44+
45+
// identified event will sent but wait in the queue until project config is ready
46+
_ = optimizely.createUserContext(userId: "tester")
47+
48+
sleep(1)
49+
XCTAssertEqual(odpEventApiManager.dispatchedEvents.count, 0, "wait until project config is ready")
50+
51+
// project config gets ready
52+
try! optimizely.start(datafile: datafile)
53+
54+
// identified event will sent
55+
_ = optimizely.createUserContext(userId: "tester")
56+
57+
sleep(1)
58+
XCTAssertEqual(odpEventApiManager.dispatchedEvents.count, 3, "client_initialized and 2 x identified events")
59+
60+
for i in 0..<100 {
61+
_ = optimizely.createUserContext(userId: "tester-\(i % 10)")
62+
}
63+
64+
sleep(1)
65+
XCTAssertEqual(odpEventApiManager.dispatchedEvents.count, 103, "100 more identified events")
66+
}
67+
68+
// MARK: - Utils
69+
70+
class MockOdpEventApiManager: OdpEventApiManager {
71+
var dispatchedEvents = [OdpEvent]()
72+
73+
override func sendOdpEvents(apiKey: String,
74+
apiHost: String,
75+
events: [OdpEvent],
76+
completionHandler: @escaping (OptimizelyError?) -> Void) {
77+
self.dispatchedEvents.append(contentsOf: events)
78+
completionHandler(nil)
79+
}
80+
}
81+
82+
}

0 commit comments

Comments
 (0)