Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

RolloutCacheManager integration tests #610

Merged
merged 28 commits into from
Jan 27, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
28 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .github/workflows/test_all.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ on:
branches:
- master
- development
- "*_baseline"

jobs:
test:
Expand Down
26 changes: 26 additions & 0 deletions SplitPushManagerUT.xctestplan
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,13 @@
"FlushTests",
"FolderApiKeyTests",
"GeneralInfoDaoTest",
"GeneralInfoStorageTest",
"GeneralInfoStorageTest\/testGetRolloutCacheLastClearTimestampGetsValueFromDao()",
"GeneralInfoStorageTest\/testGetRolloutCacheLastClearTimestampReturnsZeroIfEntityIsNil()",
"GeneralInfoStorageTest\/testGetUpdateTimestampGetsValueFromDao()",
"GeneralInfoStorageTest\/testGetUpdateTimestampReturnsZeroIfEntityIsNil()",
"GeneralInfoStorageTest\/testSetRolloutCacheLastClearTimestampSetsValueOnDao()",
"GeneralInfoStorageTest\/testSetUpdateTimestampSetsValueOnDao()",
"GreaterThanOrEqualToSemverMatcherTest",
"HashedImpressionDaoTest",
"HashedImpressionsStorageTests",
Expand Down Expand Up @@ -163,6 +170,25 @@
"ReconnectBackoffCounterTest",
"RecorderFlusherCheckerTests",
"RegexTest",
"RolloutCacheConfigurationTest",
"RolloutCacheConfigurationTest\/testClearOnInitIsCorrectlySet()",
"RolloutCacheConfigurationTest\/testDefaultValues()",
"RolloutCacheConfigurationTest\/testExpirationIsCorrectlySet()",
"RolloutCacheConfigurationTest\/testNegativeExpirationIsSetToDefault()",
"RolloutCacheManagerIntegrationTest",
"RolloutCacheManagerIntegrationTest\/testClearOnInitClearCacheOnStartup()",
"RolloutCacheManagerIntegrationTest\/testExpirationPeriodIsUsed()",
"RolloutCacheManagerIntegrationTest\/testRepeatedInitWithClearOnInitSetToTrueDoesNotClearIfMinDaysHasNotElapsed()",
"RolloutCacheManagerTest",
"RolloutCacheManagerTest\/testDefaultValueForLastClearTimestampClearsCacheWhenClearOnInitIsTrue()",
"RolloutCacheManagerTest\/testDefaultValueForUpdateTimestampDoesNotClearCache()",
"RolloutCacheManagerTest\/testValidateCacheCallsClearOnStorageOnlyOnceWhenExecutedConsecutively()",
"RolloutCacheManagerTest\/testValidateCacheCallsClearOnStoragesWhenExpirationIsNotSurpassedAndClearOnInitIsTrue()",
"RolloutCacheManagerTest\/testValidateCacheCallsClearOnStoragesWhenExpirationIsSurpassed()",
"RolloutCacheManagerTest\/testValidateCacheCallsListener()",
"RolloutCacheManagerTest\/testValidateCacheDoesNotCallClearOnStoragesWhenExpirationIsNotSurpassedAndClearOnInitIsFalse()",
"RolloutCacheManagerTest\/testValidateCacheDoesNotUpdateLastClearTimestampWhenStoragesAreNotCleared()",
"RolloutCacheManagerTest\/testValidateCacheUpdatesLastClearTimestampWhenStoragesAreCleared()",
"SdkUpdateStreamingTest",
"SemverIntegrationTest",
"SemverTest",
Expand Down
20 changes: 20 additions & 0 deletions SplitStreamingUT.xctestplan
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@
"FlushTests",
"FolderApiKeyTests",
"GeneralInfoDaoTest",
"GeneralInfoStorageTest",
"GreaterThanOrEqualToSemverMatcherTest",
"HashedImpressionDaoTest",
"HashedImpressionsStorageTests",
Expand Down Expand Up @@ -155,6 +156,25 @@
"ReconnectBackoffCounterTest",
"RecorderFlusherCheckerTests",
"RegexTest",
"RolloutCacheConfigurationTest",
"RolloutCacheConfigurationTest\/testClearOnInitIsCorrectlySet()",
"RolloutCacheConfigurationTest\/testDefaultValues()",
"RolloutCacheConfigurationTest\/testExpirationIsCorrectlySet()",
"RolloutCacheConfigurationTest\/testNegativeExpirationIsSetToDefault()",
"RolloutCacheManagerIntegrationTest",
"RolloutCacheManagerIntegrationTest\/testClearOnInitClearCacheOnStartup()",
"RolloutCacheManagerIntegrationTest\/testExpirationPeriodIsUsed()",
"RolloutCacheManagerIntegrationTest\/testRepeatedInitWithClearOnInitSetToTrueDoesNotClearIfMinDaysHasNotElapsed()",
"RolloutCacheManagerTest",
"RolloutCacheManagerTest\/testDefaultValueForLastClearTimestampClearsCacheWhenClearOnInitIsTrue()",
"RolloutCacheManagerTest\/testDefaultValueForUpdateTimestampDoesNotClearCache()",
"RolloutCacheManagerTest\/testValidateCacheCallsClearOnStorageOnlyOnceWhenExecutedConsecutively()",
"RolloutCacheManagerTest\/testValidateCacheCallsClearOnStoragesWhenExpirationIsNotSurpassedAndClearOnInitIsTrue()",
"RolloutCacheManagerTest\/testValidateCacheCallsClearOnStoragesWhenExpirationIsSurpassed()",
"RolloutCacheManagerTest\/testValidateCacheCallsListener()",
"RolloutCacheManagerTest\/testValidateCacheDoesNotCallClearOnStoragesWhenExpirationIsNotSurpassedAndClearOnInitIsFalse()",
"RolloutCacheManagerTest\/testValidateCacheDoesNotUpdateLastClearTimestampWhenStoragesAreNotCleared()",
"RolloutCacheManagerTest\/testValidateCacheUpdatesLastClearTimestampWhenStoragesAreCleared()",
"SdkUpdateStreamingTest",
"SemverIntegrationTest",
"SemverTest",
Expand Down
5 changes: 0 additions & 5 deletions SplitTests/Impressions/ImpressionsToggleTest.swift
Original file line number Diff line number Diff line change
Expand Up @@ -158,10 +158,6 @@ class ImpressionsToggleTest: XCTestCase {
sdkReadyExpectation.fulfill()
}

client.on(event: SplitEvent.sdkReadyTimedOut) {
sdkReadyExpectation.fulfill()
}

exps.append(sseExp)
wait(for: exps, timeout: 5)

Expand Down Expand Up @@ -240,7 +236,6 @@ class ImpressionsToggleTest: XCTestCase {
return IntegrationHelper.emptySplitChanges(since: 99999, till: 99999)
}
return splitJson

}

private func buildStreamingHandler() -> TestStreamResponseBindingHandler {
Expand Down
6 changes: 2 additions & 4 deletions SplitTests/Integration/Cache/InitialCacheTest.swift
Original file line number Diff line number Diff line change
Expand Up @@ -353,10 +353,10 @@ class InitialCacheTest: XCTestCase {
var treatmentCache = ""
var treatmentReady = ""

var readyCacheNotFired = false
var readyCacheNotFired = true
client.on(event: SplitEvent.sdkReadyFromCache) {
treatmentCache = client.getTreatment(self.splitName)
readyCacheNotFired = true
readyCacheNotFired = false
}

client.on(event: SplitEvent.sdkReady) {
Expand All @@ -377,8 +377,6 @@ class InitialCacheTest: XCTestCase {
XCTAssertFalse(readyCacheNotFired)
}



private func getChanges(for hitNumber: Int) -> Data {
if hitNumber < jsonChanges.count {
return Data(self.jsonChanges[hitNumber].utf8)
Expand Down
4 changes: 2 additions & 2 deletions SplitTests/Integration/Cache/ReadyFromCacheTest.swift
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,7 @@ class ReadyFromCacheTest: XCTestCase {
}

func testNotExistingSplitsAndConnectionOk() {
// When NO feature flags and connection available, ready from cache should NOT be fired and Ready should be fired
// When NO feature flags and connection available, ready from cache should be fired alongside Ready
let splitDatabase = TestingHelper.createTestDatabase(name: "ready_from_cache_test", queue: dbqueue)
let session = HttpSessionMock()
let reqManager = HttpRequestManagerTestDispatcher(dispatcher: buildTestDispatcher(),
Expand Down Expand Up @@ -201,7 +201,7 @@ class ReadyFromCacheTest: XCTestCase {
wait(for: [readyExp], timeout: 3)
let treatmentReady = client.getTreatment(splitName)

XCTAssertFalse(cacheReadyFired)
XCTAssertTrue(cacheReadyFired)
XCTAssertTrue(readyFired)
XCTAssertFalse(timeoutFired)
XCTAssertEqual("control", treatmentCache)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,252 @@
import XCTest
@testable import Split

final class RolloutCacheManagerIntegrationTest: XCTestCase {

override func setUpWithError() throws {
private var httpClient: HttpClient!
private let apiKey = IntegrationHelper.dummyApiKey
private let userKey = "key"
private var firstSplitHit: Bool!
private var streamingBinding: TestStreamResponseBinding?
private var notificationHelper: NotificationHelperStub?
private var testDb: SplitDatabase!
private var enableRequest: Bool!
private let lock = NSLock()

override func setUp() {
let session = HttpSessionMock()
let reqManager = HttpRequestManagerTestDispatcher(dispatcher: buildTestDispatcher(),
streamingHandler: buildStreamingHandler())
httpClient = DefaultHttpClient(session: session, requestManager: reqManager)
notificationHelper = NotificationHelperStub()
testDb = TestingHelper.createTestDatabase(name: "test")
firstSplitHit = true
enableRequest = false
}

func testExpirationPeriodIsUsed() {
test(timestampDaysAgo: getTimestampDaysAgo(days: 1), configBuilder: RolloutCacheConfiguration.builder().set(expirationDays: 1))
}

func testClearOnInitClearCacheOnStartup() {
test(timestampDaysAgo: getTimestampDaysAgo(days: 0), configBuilder: RolloutCacheConfiguration.builder().set(clearOnInit: true))
}

func testRepeatedInitWithClearOnInitSetToTrueDoesNotClearIfMinDaysHasNotElapsed() {
// preload DB with update timestamp of now
preloadDB(updateTimestamp: getTimestampDaysAgo(days: 0), lastClearTimestamp: 0, changeNumber: 8000)

// Track initial values
let initialFlags = testDb.splitDao.getAll()
let initialSegments = testDb.mySegmentsDao.getBy(userKey: userKey)
let initialLargeSegments = testDb.myLargeSegmentsDao.getBy(userKey: userKey)
let initialChangeNumber = testDb.generalInfoDao.longValue(info: .splitsChangeNumber)

// Initialize SDK
let factory = getFactory(rolloutConfig: RolloutCacheConfiguration.builder().set(clearOnInit: true).build())
sleep(1)

// Track intermediate values
let intermediateFlags = testDb.splitDao.getAll()
let intermediateSegments = testDb.mySegmentsDao.getBy(userKey: userKey)
let intermediateLargeSegments = testDb.myLargeSegmentsDao.getBy(userKey: userKey)
let intermediateChangeNumber = testDb.generalInfoDao.longValue(info: .splitsChangeNumber)

// Resume server responses after tracking DB values
lock.lock()
enableRequest = true
lock.unlock()

// Wait for ready
let readyExp = XCTestExpectation(description: "SDK READY Expectation")
factory.client.on(event: SplitEvent.sdkReady) {
readyExp.fulfill()
}
wait(for: [readyExp], timeout: 15)

// Destroy factory
let destroyExp = XCTestExpectation(description: "Destroy Expectation")
factory.client.destroy(completion: {
destroyExp.fulfill()
})

wait(for: [destroyExp], timeout: 15)

// Initialize SDK again
lock.lock()
enableRequest = false
lock.unlock()

preloadDB(updateTimestamp: nil, lastClearTimestamp: nil, changeNumber: nil)
let factory2 = getFactory(rolloutConfig: RolloutCacheConfiguration.builder().set(clearOnInit: true).build())

Check warning on line 81 in SplitTests/Integration/Cache/RolloutCacheManagerIntegrationTest.swift

View workflow job for this annotation

GitHub Actions / test (SplitiOSUnit_3) / test

initialization of immutable value 'factory2' was never used; consider replacing with assignment to '_' or removing it

Check warning on line 81 in SplitTests/Integration/Cache/RolloutCacheManagerIntegrationTest.swift

View workflow job for this annotation

GitHub Actions / test (SplitiOSUnit) / test

initialization of immutable value 'factory2' was never used; consider replacing with assignment to '_' or removing it

Check warning on line 81 in SplitTests/Integration/Cache/RolloutCacheManagerIntegrationTest.swift

View workflow job for this annotation

GitHub Actions / test (SplitiOSStreaming) / test

initialization of immutable value 'factory2' was never used; consider replacing with assignment to '_' or removing it

Check warning on line 81 in SplitTests/Integration/Cache/RolloutCacheManagerIntegrationTest.swift

View workflow job for this annotation

GitHub Actions / test (SplitiOSUnit_2) / test

initialization of immutable value 'factory2' was never used; consider replacing with assignment to '_' or removing it
sleep(1)

// Track intermediate values
let factory2Flags = testDb.splitDao.getAll()
let factory2Segments = testDb.mySegmentsDao.getBy(userKey: userKey)
let factory2LargeSegments = testDb.myLargeSegmentsDao.getBy(userKey: userKey)
let factory2ChangeNumber = testDb.generalInfoDao.longValue(info: .splitsChangeNumber)

// initial values
XCTAssertEqual(1, initialFlags.count)
XCTAssertEqual(2, initialSegments?.segments.count ?? 0)
XCTAssertFalse(initialSegments?.segments.isEmpty ?? true)
XCTAssertFalse(initialLargeSegments?.segments.isEmpty ?? true)
XCTAssertEqual(8000, initialChangeNumber)

// values after clear
XCTAssertEqual(0, intermediateFlags.count)
XCTAssertTrue(intermediateSegments?.segments.isEmpty ?? true)
XCTAssertTrue(intermediateLargeSegments?.segments.isEmpty ?? true)
XCTAssertEqual(-1, intermediateChangeNumber)

// values after second init (values were reinserted into DB); no clear should have happened
XCTAssertEqual(1, factory2Flags.count)
XCTAssertEqual(2, factory2Segments?.segments.count ?? 0)
XCTAssertFalse(factory2Segments?.segments.isEmpty ?? true)
XCTAssertFalse(factory2LargeSegments?.segments.isEmpty ?? true)
XCTAssertEqual(99999, factory2ChangeNumber)
XCTAssertTrue(0 < testDb.generalInfoDao.longValue(info: .rolloutCacheLastClearTimestamp) ?? -1)
}

private func test(timestampDaysAgo: Int64, configBuilder: RolloutCacheConfiguration.Builder) {
let oldTimestamp = timestampDaysAgo
preloadDB(updateTimestamp: oldTimestamp, lastClearTimestamp: 0, changeNumber: 8000)
// Track initial values
let initialFlags = testDb.splitDao.getAll()
let initialSegments = testDb.mySegmentsDao.getBy(userKey: userKey)
let initialLargeSegments = testDb.myLargeSegmentsDao.getBy(userKey: userKey)
let initialChangeNumber = testDb.generalInfoDao.longValue(info: .splitsChangeNumber)

// Initialize SDK
let factory = getFactory(rolloutConfig: configBuilder.build())

let readyExp = XCTestExpectation(description: "SDK READY Expectation")
factory.client.on(event: SplitEvent.sdkReady) {
readyExp.fulfill()
}

// Track final values
verify(factory: factory, readyExp: readyExp, initialFlags: initialFlags, initialSegments: initialSegments, initialLargeSegments: initialLargeSegments, initialChangeNumber: initialChangeNumber)
}

private func preloadDB(updateTimestamp: Int64?, lastClearTimestamp: Int64?, changeNumber: Int64?) {
let split = TestingHelper.buildSplit(name: "test_split", treatment: "test_treatment")
testDb.splitDao.insertOrUpdate(split: split)
if let updateTimestamp = updateTimestamp {
testDb.generalInfoDao.update(info: .splitsUpdateTimestamp, longValue: updateTimestamp)
}
if let lastClearTimestamp = lastClearTimestamp {
testDb.generalInfoDao.update(info: .rolloutCacheLastClearTimestamp, longValue: lastClearTimestamp)
}
if let changeNumber = changeNumber {
testDb.generalInfoDao.update(info: .splitsChangeNumber, longValue: changeNumber)
}
testDb.mySegmentsDao.update(userKey: userKey, change: SegmentChange(segments: ["s1", "s2"], changeNumber: nil))
testDb.myLargeSegmentsDao.update(userKey: userKey, change: SegmentChange(segments: ["l1", "l2"], changeNumber: nil))
}

private func loadSplitsChangeFile() -> String {
guard let splitJson = FileHelper.readDataFromFile(sourceClass: self, name: "splitchanges_1", type: "json") else {
return IntegrationHelper.emptySplitChanges(since: 99999, till: 99999)
}
return splitJson
}

private func verify(factory: SplitFactory, readyExp: XCTestExpectation, initialFlags: [Split], initialSegments: SegmentChange?, initialLargeSegments: SegmentChange?, initialChangeNumber: Int64?) {
let finalFlags = testDb.splitDao.getAll()
let finalSegments = testDb.mySegmentsDao.getBy(userKey: userKey)
let finalLargeSegments = testDb.myLargeSegmentsDao.getBy(userKey: userKey)
let finalChangeNumber = testDb.generalInfoDao.longValue(info: .splitsChangeNumber)

// Resume responses after tracking DB Values
lock.lock()
enableRequest = true
lock.unlock()

// Wait for ready
wait(for: [readyExp], timeout: 15)

// Verify
XCTAssertEqual(1, initialFlags.count)
XCTAssertEqual(2, initialSegments?.segments.count ?? 0)
XCTAssertFalse(initialSegments?.segments.isEmpty ?? true)
XCTAssertFalse(initialLargeSegments?.segments.isEmpty ?? true)
XCTAssertEqual(8000, initialChangeNumber)
XCTAssertEqual(0, finalFlags.count)
XCTAssertTrue(finalSegments?.segments.isEmpty ?? true)
XCTAssertTrue(finalLargeSegments?.segments.isEmpty ?? true)
XCTAssertEqual(-1, finalChangeNumber)
XCTAssertTrue(0 < testDb.generalInfoDao.longValue(info: .rolloutCacheLastClearTimestamp) ?? -1)
}

private func getTimestampDaysAgo(days: Int) -> Int64 {
return Date.secondsToDays(seconds: Date.now() - Int64(days * 86400))
}

private func getFactory(rolloutConfig: RolloutCacheConfiguration) -> SplitFactory {
let splitConfig: SplitClientConfig = SplitClientConfig()
splitConfig.logLevel = .verbose
splitConfig.streamingEnabled = false
splitConfig.featuresRefreshRate = 1

let key: Key = Key(matchingKey: userKey)
let builder = DefaultSplitFactoryBuilder()
_ = builder.setHttpClient(httpClient)
_ = builder.setReachabilityChecker(ReachabilityMock())
_ = builder.setTestDatabase(testDb)
_ = builder.setNotificationHelper(notificationHelper!)
let factory = builder.setApiKey(apiKey).setKey(key)
.setConfig(splitConfig).build()!

return factory
}

private func buildTestDispatcher() -> HttpClientTestDispatcher {
return { request in
self.lock.lock()
while !self.enableRequest {
self.lock.unlock()
Thread.sleep(forTimeInterval: 0.5)
self.lock.lock()
}
self.lock.unlock()

if request.isSplitEndpoint() {
if self.firstSplitHit {
self.firstSplitHit = false
return TestDispatcherResponse(code: 200, data: Data(IntegrationHelper.emptySplitChanges(since: -1, till: 99999).utf8))
}
return TestDispatcherResponse(code: 200, data: Data(IntegrationHelper.emptySplitChanges(since: 99999, till: 99999).utf8))
}

if request.isMySegmentsEndpoint() {
return TestDispatcherResponse(code: 200, data: Data(IntegrationHelper.emptyMySegments.utf8))
}

if request.isAuthEndpoint() {
return TestDispatcherResponse(code: 200, data: Data(IntegrationHelper.dummySseResponse().utf8))
}

if request.isImpressionsEndpoint() {
return TestDispatcherResponse(code: 200)
}

if request.isImpressionsCountEndpoint() {
return TestDispatcherResponse(code: 200)
}

if request.isUniqueKeysEndpoint() {
return TestDispatcherResponse(code: 200)
}
return TestDispatcherResponse(code: 404)
}
}

private func buildStreamingHandler() -> TestStreamResponseBindingHandler {
return { request in
self.streamingBinding = TestStreamResponseBinding.createFor(request: request, code: 200)
return self.streamingBinding!
}
}
}
Loading
Loading