@@ -671,6 +671,43 @@ class DevCycleClientTest: XCTestCase {
671671 . build ( )
672672 await client. close ( )
673673 }
674+
675+ func testFailedConfigFetch( ) {
676+ let expectation = XCTestExpectation ( description: " Config fetch fails " )
677+ let failedService = MockFailedConnectionService ( )
678+ let client = try ! self . builder. user ( self . user) . sdkKey ( " dvc_mobile_my_sdk_key " ) . service ( failedService) . build ( onInitialized: nil )
679+
680+ client. setup ( service: failedService)
681+ client. initialize ( callback: { error in
682+ XCTAssertNotNil ( error)
683+ // Test that the client's state is initialized
684+ XCTAssertTrue ( client. initialized)
685+
686+ // Test that functions that depend on a config fetch behave appropriately even if it fails to get
687+ let variable = client. variable ( key: " some_non_existent_variable " , defaultValue: false )
688+ XCTAssertTrue ( variable. isDefaulted)
689+ XCTAssertFalse ( variable. value)
690+
691+ _ = client. allFeatures ( )
692+ _ = client. allVariables ( )
693+
694+ do {
695+ let user = try DevCycleUser . builder ( ) . userId ( " user1 " ) . build ( )
696+
697+ try client. identifyUser ( user: user)
698+ try client. resetUser ( )
699+
700+ client. track ( DevCycleEvent ( type: nil , target: nil , clientDate: nil , value: nil , metaData: nil ) )
701+ client. flushEvents ( )
702+ } catch {
703+
704+ }
705+ expectation. fulfill ( )
706+ } )
707+
708+ wait ( for: [ expectation] , timeout: 1.0 )
709+ client. close ( callback: nil )
710+ }
674711}
675712
676713extension DevCycleClientTest {
@@ -723,6 +760,49 @@ extension DevCycleClientTest {
723760 }
724761 }
725762 }
763+
764+ private class MockFailedConnectionService : DevCycleServiceProtocol {
765+ public var userConfig : UserConfig ?
766+
767+ init ( userConfig: UserConfig ? = nil ) {
768+ self . userConfig = userConfig
769+ }
770+
771+ func getConfig(
772+ user: DevCycleUser ,
773+ enableEdgeDB: Bool ,
774+ extraParams: RequestParams ? ,
775+ completion: @escaping ConfigCompletionHandler
776+ ) {
777+ // Simulate a failed config fetch by returning an error
778+ let error = NSError ( domain: " MockFailedConnectionService " , code: 1 , userInfo: [ NSLocalizedDescriptionKey: " Failed to fetch config " ] )
779+ DispatchQueue . main. async {
780+ completion ( ( nil , error) )
781+ }
782+ }
783+
784+ func publishEvents(
785+ events: [ DevCycleEvent ] , user: DevCycleUser ,
786+ completion: @escaping PublishEventsCompletionHandler
787+ ) {
788+ XCTAssert ( true )
789+ DispatchQueue . main. asyncAfter ( deadline: . now( ) + 0.1 ) {
790+ completion ( ( data: nil , urlResponse: nil , error: nil ) )
791+ }
792+ }
793+
794+ func saveEntity( user: DevCycleUser , completion: @escaping SaveEntityCompletionHandler ) {
795+ DispatchQueue . main. async {
796+ completion ( ( data: nil , urlResponse: nil , error: nil ) )
797+ }
798+ }
799+
800+ func makeRequest( request: URLRequest , completion: @escaping DevCycle . CompletionHandler ) {
801+ DispatchQueue . main. async {
802+ completion ( ( data: nil , urlResponse: nil , error: nil ) )
803+ }
804+ }
805+ }
726806
727807 private class MockSSEConnection : SSEConnectionProtocol {
728808 var connected : Bool
0 commit comments