Skip to content

Commit 2ba6ed1

Browse files
yasirfolio3jaeopt
authored andcommitted
Fixes for improving code coverage keeping coveralls coverage report in mind. (#217)
* Test-cases implemented to increase coverage.
1 parent 5271ddc commit 2ba6ed1

26 files changed

+291
-174
lines changed

OptimizelySDK/Customization/DefaultEventDispatcher.swift

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

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

3939
lazy var logger = OPTLoggerFactory.getLogger()
40-
var backingStore:DataStoreType
41-
var backingStoreName:String
40+
var backingStore: DataStoreType
41+
var backingStoreName: String
4242

4343
// for dispatching events
4444
let dispatcher = DispatchQueue(label: "DefaultEventDispatcherQueue")
@@ -47,7 +47,7 @@ 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, backingStore:DataStoreType = .file, dataStoreName:String = "OPTEventQueue", timerInterval:TimeInterval = 60*1 ) {
50+
public init(batchSize: Int = 10, backingStore: DataStoreType = .file, dataStoreName: String = "OPTEventQueue", timerInterval: TimeInterval = 60*1 ) {
5151
self.batchSize = batchSize > 0 ? batchSize : 1
5252
self.backingStore = backingStore
5353
self.backingStoreName = dataStoreName
@@ -240,12 +240,11 @@ open class DefaultEventDispatcher: BackgroundingCallbacks, OPTEventDispatcher {
240240
self.timer.property = Timer.scheduledTimer(withTimeInterval: self.timerInterval, repeats: true) { (timer) in
241241
self.dispatcher.async {
242242
if self.dataStore.count == 0 {
243-
self.timer.performAtomic() { (timer) in
243+
self.timer.performAtomic {(timer) in
244244
timer.invalidate()
245245
}
246246
self.timer.property = nil
247-
}
248-
else {
247+
} else {
249248
self.flushEvents()
250249
}
251250
}

OptimizelySDK/Extensions/Array+Extension.swift

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -67,11 +67,13 @@ extension Array where Element == ThrowableCondition {
6767
throw OptimizelyError.conditionInvalidFormat("NOT with empty items")
6868
}
6969

70+
var error: OptimizelyError!
7071
do {
7172
let result = try eval()
7273
return !result
73-
} catch let error as OptimizelyError {
74-
throw OptimizelyError.conditionInvalidFormat("NOT with invalid items [\(error.reason)]")
74+
} catch let err as OptimizelyError {
75+
error = OptimizelyError.conditionInvalidFormat("NOT with invalid items [\(err.reason)]")
7576
}
77+
throw error
7678
}
7779
}

OptimizelySDK/Extensions/ArrayEventForDispatch+Extension.swift

Lines changed: 22 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -18,11 +18,7 @@ import Foundation
1818

1919
extension Array where Element == EventForDispatch {
2020
func batch() -> EventForDispatch? {
21-
if count == 0 {
22-
return nil
23-
}
24-
25-
if count == 1 {
21+
if count < 2 {
2622
return first
2723
}
2824

@@ -31,27 +27,19 @@ extension Array where Element == EventForDispatch {
3127
var projectId: String?
3228

3329
let checkUrl = { (event: EventForDispatch) -> Bool in
34-
if let url = url {
35-
if url != event.url {
36-
return false
37-
}
38-
} else {
30+
if url == nil {
3931
url = event.url
32+
return true
4033
}
41-
42-
return true
34+
return url == event.url
4335
}
4436

4537
let checkProjectId = { (batchEvent: BatchEvent) -> Bool in
46-
if let projectId = projectId {
47-
if projectId != batchEvent.projectID {
48-
return false
49-
}
50-
} else {
38+
if projectId == nil {
5139
projectId = batchEvent.projectID
40+
return true
5241
}
53-
54-
return true
42+
return projectId == batchEvent.projectID
5543
}
5644

5745
var firstBatchEvent: BatchEvent?
@@ -69,19 +57,21 @@ extension Array where Element == EventForDispatch {
6957
}
7058
}
7159

72-
if let first = firstBatchEvent {
73-
let batchEvent = BatchEvent(revision: first.revision,
74-
accountID: first.accountID,
75-
clientVersion: first.clientVersion,
76-
visitors: visitors,
77-
projectID: first.projectID,
78-
clientName: first.clientName,
79-
anonymizeIP: first.anonymizeIP,
80-
enrichDecisions: true)
81-
82-
if let data = try? JSONEncoder().encode(batchEvent), let url = url {
83-
return EventForDispatch(url: url, body: data)
84-
}
60+
guard let first = firstBatchEvent, let tmpUrl = url else {
61+
return nil
62+
}
63+
64+
let batchEvent = BatchEvent(revision: first.revision,
65+
accountID: first.accountID,
66+
clientVersion: first.clientVersion,
67+
visitors: visitors,
68+
projectID: first.projectID,
69+
clientName: first.clientName,
70+
anonymizeIP: first.anonymizeIP,
71+
enrichDecisions: true)
72+
73+
if let data = try? JSONEncoder().encode(batchEvent) {
74+
return EventForDispatch(url: tmpUrl, body: data)
8575
}
8676
return nil
8777
}

OptimizelySDK/Implementation/Datastore/DataStoreFile.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ import Foundation
1818

1919
/// Implementation of OPTDataStore as a generic for per type storeage in a flat file.
2020
/// This class should be used as a singleton per storeName and type (T)
21-
public class DataStoreFile<T> : OPTDataStore where T: Codable {
21+
public class DataStoreFile<T>: OPTDataStore where T: Codable {
2222
let dataStoreName: String
2323
let lock: DispatchQueue
2424
let url: URL

OptimizelySDK/Implementation/Datastore/DataStoreMemory.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ import UIKit
2020
/// Implementation of OPTDataStore as a generic for per type storeage in memory. On background and foreground
2121
/// the file is saved.
2222
/// This class should be used as a singleton per storeName and type (T)
23-
public class DataStoreMemory<T> : BackgroundingCallbacks, OPTDataStore where T: Codable {
23+
public class DataStoreMemory<T>: BackgroundingCallbacks, OPTDataStore where T: Codable {
2424
let dataStoreName: String
2525
let lock: DispatchQueue
2626
let url: URL
@@ -79,7 +79,7 @@ public class DataStoreMemory<T> : BackgroundingCallbacks, OPTDataStore where T:
7979
}
8080
}
8181

82-
private func save(forKey: String, value: Any) {
82+
func save(forKey: String, value: Any) {
8383
lock.async {
8484
do {
8585
if let value = value as? T {

OptimizelySDK/Implementation/Datastore/DataStoreQueueStackImpl.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
import Foundation
1818

1919
/// Implementation of DataStoreQueueStack that takes a DataStore as a instance variable allowing for different datastore imlementations. It is a generic and will work with any data type that supports Codable. Keep in mind that whichever data store you use, this class stores it as Array<Data>. Instances of DataStoreQueueStackImpl should be singletons per queueStackName (which usually corrolates to the data type as well depending on producer/consumer).
20-
public class DataStoreQueueStackImpl<T> : DataStoreQueueStack where T: Codable {
20+
public class DataStoreQueueStackImpl<T>: DataStoreQueueStack where T: Codable {
2121
let queueStackName: String
2222
let lock: DispatchQueue
2323
let dataStore: OPTDataStore

0 commit comments

Comments
 (0)