Skip to content

[FME-6993] - Pausing - E2E Test 5 #710

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

Merged
merged 5 commits into from
Jul 29, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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: 0 additions & 1 deletion Split/Storage/Splits/SplitsStorage.swift
Original file line number Diff line number Diff line change
Expand Up @@ -169,7 +169,6 @@ class DefaultSplitsStorage: SplitsStorage {
segmentsInUse += 1
} else if inMemorySplits.value(forKey: splitName) != nil && !active { // If known Split and archived
segmentsInUse -= 1

}
}

Expand Down
91 changes: 91 additions & 0 deletions SplitTests/Integration/streaming/MySegmentUpdateTest.swift
Original file line number Diff line number Diff line change
Expand Up @@ -385,6 +385,97 @@ class MySegmentUpdateTest: XCTestCase {
destroy(client)
}
}

func testSdkRestartMembershipsSyncIfNewFlag() throws {

var sdkReadyFired = false
var cacheReadyFired = true
let sdkReady = XCTestExpectation(description: "SDK should be ready")
let cacheReadyExp = XCTestExpectation(description: "Cache should be ready")
let segmentsHit = XCTestExpectation(description: "/memberships should be hit at least once")
var membershipsHit = 0

var json = IntegrationHelper.loadSplitChangeFileJson(name: "splitschanges_no_segments", sourceClass: IntegrationHelper()) // no Segments

// 1. Configure dispatcher
let dispatcher: HttpClientTestDispatcher = { request in
if request.url.absoluteString.contains("/splitChanges") {
return TestDispatcherResponse(code: 200, data: Data(json!.utf8))
}

if request.url.absoluteString.contains("/memberships") {
segmentsHit.fulfill()
membershipsHit += 1
return TestDispatcherResponse(code: 200, data: Data(IntegrationHelper.emptyMySegments.utf8))
}

return TestDispatcherResponse(code: 200)
}

// 2. Setup Factory, Network & Client
let splitConfig: SplitClientConfig = SplitClientConfig()
splitConfig.featuresRefreshRate = 5
splitConfig.segmentsRefreshRate = 5
splitConfig.impressionRefreshRate = 30
splitConfig.sdkReadyTimeOut = 60000
splitConfig.eventsPerPush = 10
splitConfig.streamingEnabled = false
splitConfig.eventsQueueSize = 100
splitConfig.eventsPushRate = 999999
splitConfig.eventsFirstPushWindow = 999
splitConfig.impressionsMode = "DEBUG"
splitConfig.serviceEndpoints = ServiceEndpoints.builder()
.set(sdkEndpoint: "localhost").set(eventsEndpoint: "localhost").build()

let splitDatabase = TestingHelper.createTestDatabase(name: "ready_from_cache_test")
splitDatabase.generalInfoDao.update(info: .flagsSpec, stringValue: "1.3")

let userKey = "test-user-key"
let key: Key = Key(matchingKey: userKey, bucketingKey: nil)
let session = HttpSessionMock()
let reqManager = HttpRequestManagerTestDispatcher(dispatcher: dispatcher, streamingHandler: buildStreamingHandler())
httpClient = DefaultHttpClient(session: session, requestManager: reqManager)
let builder = DefaultSplitFactoryBuilder()

_ = builder.setTestDatabase(splitDatabase)
_ = builder.setHttpClient(httpClient)
var factory = builder.setApiKey(apiKey).setKey(key).setConfig(splitConfig).build()
let client = factory?.client

client?.on(event: .sdkReady) {
sdkReadyFired = true
sdkReady.fulfill()
}

client?.on(event: .sdkReadyFromCache) {
cacheReadyExp.fulfill()
cacheReadyFired = true
}

wait(for: [segmentsHit], timeout: 3)
XCTAssertEqual(sdkReadyFired, false)

wait(for: [cacheReadyExp, sdkReady], timeout: 4)

// MARK: Key part
var waitExp = XCTestExpectation(description: "Just waiting")
waitExp.isInverted = true // Inverted expectation
wait(for: [waitExp], timeout: 10)
XCTAssertEqual(membershipsHit, 1, "After some time, if segments are not used, SDK shouldn't hit /memberships")

// MARK: Key part 2
json = IntegrationHelper.loadSplitChangeFileJson(name: "splitchanges_1", sourceClass: IntegrationHelper()) // splitChanges, now WITH Segments

waitExp = XCTestExpectation(description: "Just waiting")
waitExp.isInverted = true // Inverted expectation
wait(for: [waitExp], timeout: 15)
XCTAssertGreaterThan(membershipsHit, 2, "If new flags with segments arrive, the mechanism should be restarted and SDK should hit /memberships many times again")

// Cleanup
if let client = client {
destroy(client)
}
}

func testMySegmentsUpdateBounded() throws {
try mySegmentsUpdateBoundedTest(type: .mySegmentsUpdate)
Expand Down
Loading
Loading