Skip to content

Commit 16986e5

Browse files
(fix): fix registry lookup and add a unit test for optimizely change notifi… (#191)
* fix registery lookup and add a unit test for optimizely change notifications * cleanup some tests * remove all registered services in teardown.
1 parent 1575232 commit 16986e5

File tree

5 files changed

+40
-20
lines changed

5 files changed

+40
-20
lines changed

OptimizelySDK/Customization/DefaultEventDispatcher.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -175,7 +175,7 @@ open class DefaultEventDispatcher : BackgroundingCallbacks, OPTEventDispatcher {
175175
}
176176
}
177177
else {
178-
// batch worked
178+
// batch had batchSize items
179179
}
180180
}
181181
// our send is done.

OptimizelySDK/OptimizelyTests/OptimizelyTests-Common/DatafileHandlerTests.swift

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@ import XCTest
1919
class DatafileHandlerTests: XCTestCase {
2020

2121
override func setUp() {
22+
23+
HandlerRegistryService.shared.binders.removeAll()
2224
// Put setup code here. This method is called before the invocation of each test method in the class.
2325
if let url = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask).first {
2426
if (!FileManager.default.fileExists(atPath: url.path)) {
@@ -36,6 +38,7 @@ class DatafileHandlerTests: XCTestCase {
3638

3739
override func tearDown() {
3840
// Put teardown code here. This method is called after the invocation of each test method in the class.
41+
HandlerRegistryService.shared.binders.removeAll()
3942
}
4043

4144
func testDatafileHandler() {
@@ -102,6 +105,39 @@ class DatafileHandlerTests: XCTestCase {
102105

103106

104107
}
108+
109+
func testPeriodicDownloadWithOptimizlyClient() {
110+
class FakeDatafileHandler : DefaultDatafileHandler {
111+
let data = OTUtils.loadJSONDatafile("typed_audience_datafile")
112+
override func downloadDatafile(sdkKey: String, resourceTimeoutInterval: Double?, completionHandler: @escaping DatafileDownloadCompletionHandler) {
113+
completionHandler(.success(data))
114+
}
115+
}
116+
let expection = XCTestExpectation(description: "Expect 10 periodic downloads")
117+
let handler = FakeDatafileHandler()
118+
119+
HandlerRegistryService.shared.registerBinding(binder: Binder(service: OPTDatafileHandler.self).sdkKey(key: "notrealkey123").using(instance: handler).to(factory: FakeDatafileHandler.init).reInitializeStrategy(strategy: .reUse).singetlon())
120+
121+
let optimizely = OptimizelyClient(sdkKey: "notrealkey123", periodicDownloadInterval:1)
122+
123+
var count = 0
124+
125+
let _ = optimizely.notificationCenter.addDatafileChangeNotificationListener { (data) in
126+
count += 1
127+
if count == 9 {
128+
optimizely.datafileHandler.stopPeriodicUpdates()
129+
expection.fulfill()
130+
}
131+
}
132+
optimizely.start() { (result) in
133+
XCTAssert(true)
134+
}
135+
wait(for: [expection], timeout: 10)
136+
137+
XCTAssert(count == 9)
138+
139+
}
140+
105141

106142
func testPerformanceExample() {
107143
// This is an example of a performance test case.

OptimizelySDK/OptimizelyTests/OptimizelyTests-Common/DecisionServiceTests.swift

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -46,11 +46,4 @@ class DecisionServiceTests: XCTestCase {
4646

4747
}
4848

49-
func testPerformanceExample() {
50-
// This is an example of a performance test case.
51-
self.measure {
52-
// Put the code you want to measure the time of here.
53-
}
54-
}
55-
5649
}

OptimizelySDK/OptimizelyTests/OptimizelyTests-Common/EventDispatcherTests.swift

Lines changed: 2 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -48,9 +48,8 @@ class EventDispatcherTests: XCTestCase {
4848
}
4949

5050
func testDefaultDispatcher() {
51-
eventDispatcher = DefaultEventDispatcher()
51+
eventDispatcher = DefaultEventDispatcher(timerInterval:1)
5252
let pEventD:OPTEventDispatcher = eventDispatcher!
53-
eventDispatcher?.timerInterval = 1
5453

5554
pEventD.flushEvents()
5655

@@ -297,13 +296,5 @@ class EventDispatcherTests: XCTestCase {
297296
let _ = queue.removeLastItem()
298297

299298
XCTAssert(queue.count == 0)
300-
}
301-
302-
func testPerformanceExample() {
303-
// This is an example of a performance test case.
304-
self.measure {
305-
// Put the code you want to measure the time of here.
306-
}
307-
}
308-
299+
}
309300
}

OptimizelySDK/Utils/HandlerRegistryService.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ class HandlerRegistryService {
6666

6767
func lookupComponents(sdkKey:String)->[Any?]? {
6868

69-
let value = self.binders.keys.filter({$0.sdkKey == sdkKey}).map({self.injectComponent(service: self.binders[$0]!.service)!})
69+
let value = self.binders.keys.filter({$0.sdkKey == sdkKey}).map({self.injectComponent(service: self.binders[$0]!.service, sdkKey: sdkKey)!})
7070

7171
return value
7272
}

0 commit comments

Comments
 (0)