Skip to content

Commit

Permalink
Test
Browse files Browse the repository at this point in the history
  • Loading branch information
gthea committed Oct 25, 2024
1 parent 7ebbbf0 commit f855d15
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 19 deletions.
43 changes: 29 additions & 14 deletions Split/Storage/MySegments/MySegmentsStorage.swift
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,14 @@ protocol MySegmentsStorage {

class DefaultMySegmentsStorage: MySegmentsStorage {

private let synchronizationQueue = DispatchQueue(label: "split-segments-storage")
private var inMemoryMySegments: SynchronizedDictionarySet<String, String> = SynchronizedDictionarySet()
private let persistenStorage: PersistentMySegmentsStorage

var keys: Set<String> {
return inMemoryMySegments.keys
return synchronizationQueue.sync {
return inMemoryMySegments.keys
}
}

init(persistentMySegmentsStorage: PersistentMySegmentsStorage) {
Expand All @@ -48,34 +51,46 @@ class DefaultMySegmentsStorage: MySegmentsStorage {
}

func getAll(forKey key: String) -> Set<String> {
return inMemoryMySegments.values(forKey: key) ?? Set<String>()
return synchronizationQueue.sync {
return inMemoryMySegments.values(forKey: key) ?? Set<String>()
}
}

func set(_ change: SegmentChange, forKey key: String) {
let names = change.segments.compactMap { $0.name }
inMemoryMySegments.set(names.asSet(), forKey: key)
persistenStorage.set(change, forKey: key)
synchronizationQueue.sync {
let names = change.segments.compactMap { $0.name }
inMemoryMySegments.set(names.asSet(), forKey: key)
persistenStorage.set(change, forKey: key)
}
}

func clear(forKey key: String) {
inMemoryMySegments.removeValues(forKey: key)
persistenStorage.set(SegmentChange.empty(), forKey: key)
return synchronizationQueue.sync {
inMemoryMySegments.removeValues(forKey: key)
persistenStorage.set(SegmentChange.empty(), forKey: key)
}
}

func destroy() {
inMemoryMySegments.removeAll()
return synchronizationQueue.sync {
inMemoryMySegments.removeAll()
}
}

func getCount(forKey key: String) -> Int {
return inMemoryMySegments.count(forKey: key)
return synchronizationQueue.sync {
return inMemoryMySegments.count(forKey: key)
}
}

func getCount() -> Int {
let keys = inMemoryMySegments.keys
var count = 0
for key in keys {
count+=(inMemoryMySegments.values(forKey: key)?.count ?? 0)
return synchronizationQueue.sync {
let keys = inMemoryMySegments.keys
var count = 0
for key in keys {
count+=(inMemoryMySegments.values(forKey: key)?.count ?? 0)
}
return count
}
return count
}
}
14 changes: 9 additions & 5 deletions SplitTests/Integration/streaming/MySegmentUpdateTest.swift
Original file line number Diff line number Diff line change
Expand Up @@ -64,33 +64,36 @@ class MySegmentUpdateTest: XCTestCase {
}

// Wait for hitting my segments two times (sdk ready and full sync after streaming connection)
wait(for: [sdkReadyExp, sseExp], timeout: 5)
wait(for: [sdkReadyExp, sseExp], timeout: 50)

streamingBinding?.push(message: ":keepalive")

wait(for: [mySegExp], timeout: 5)
wait(for: [mySegExp], timeout: 50)

// Unbounded fetch notification should trigger my segments
// refresh on synchronizer
// Set count to 0 to start counting hits
syncSpy.forceMySegmentsCalledCount = 0
sdkUpdExp = XCTestExpectation()
pushMessage(TestingData.unboundedNotification(type: type, cn: mySegmentsCns[cnIndex()]))
wait(for: [sdkUpdExp], timeout: 5)
wait(for: [sdkUpdExp], timeout: 50)
Thread.sleep(forTimeInterval: 1.0)

// Should not trigger any fetch to my segments because
// this payload doesn't have "key1" enabled

pushMessage(TestingData.escapedBoundedNotificationZlib(type: type, cn: mySegmentsCns[cnIndex()]))

Thread.sleep(forTimeInterval: 1.0)
// Pushed key list message. Key 1 should add a segment
sdkUpdExp = XCTestExpectation()
pushMessage(TestingData.escapedKeyListNotificationGzip(type: type, cn: mySegmentsCns[cnIndex()]))
wait(for: [sdkUpdExp], timeout: 5)
wait(for: [sdkUpdExp], timeout: 50)
Thread.sleep(forTimeInterval: 1.0)

sdkUpdExp = XCTestExpectation()
pushMessage(TestingData.segmentRemovalNotification(type: type, cn: mySegmentsCns[cnIndex()]))
wait(for: [sdkUpdExp], timeout: 5)
wait(for: [sdkUpdExp], timeout: 50)

var segmentEntity: [String]!
if type == .mySegmentsUpdate {
Expand All @@ -99,6 +102,7 @@ class MySegmentUpdateTest: XCTestCase {
segmentEntity = db.myLargeSegmentsDao.getBy(userKey: testFactory.userKey)?.segments.map { $0.name } ?? []
}

Thread.sleep(forTimeInterval: 2.0)
// Hits are not asserted because tests will fail if expectations are not fulfilled
XCTAssertEqual(1, syncSpy.forceMySegmentsSyncCount[userKey] ?? 0)
XCTAssertEqual(1, segmentEntity.filter { $0 == "new_segment_added" }.count)
Expand Down

0 comments on commit f855d15

Please sign in to comment.