@@ -58,6 +58,56 @@ class ClientTests: XCTestCase {
5858 waitForExpectationsWithTimeout ( expectationTimeout, handler: nil )
5959 }
6060
61+ func testKeepAlive( ) {
62+ // Setup index
63+ let expectationAdd = expectationWithDescription ( " testAdd " )
64+ let object = [ " city " : " San Francisco " ]
65+ index. addObject ( object, completionHandler: { ( content, error) -> Void in
66+ XCTAssertNil ( error, " Error during addObject: \( error? . description) " )
67+ if content != nil {
68+ self . index. waitTask ( content![ " taskID " ] as! Int ) {
69+ ( content, error) -> Void in
70+ if let error = error {
71+ XCTFail ( " Error during waitTask: \( error) " )
72+ } else {
73+ XCTAssertEqual ( ( content![ " status " ] as! String ) , " published " , " Wait task failed " )
74+ }
75+ expectationAdd. fulfill ( )
76+ }
77+ }
78+ } )
79+ waitForExpectationsWithTimeout ( expectationTimeout, handler: nil )
80+
81+ // Reset client to avoid reuse of connection (Keep-Alive)
82+ let appID = NSProcessInfo . processInfo ( ) . environment [ " ALGOLIA_APPLICATION_ID " ] ?? APP_ID
83+ let apiKey = NSProcessInfo . processInfo ( ) . environment [ " ALGOLIA_API_KEY " ] ?? API_KEY
84+ let testClient = AlgoliaSearch . Client ( appID: appID, apiKey: apiKey)
85+ let testIndex = testClient. getIndex ( safeIndexName ( " algol?à-swift " ) )
86+
87+ let bench = 10
88+ var mesuredTime = [ Double] ( )
89+ mesuredTime. reserveCapacity ( bench)
90+
91+ for _ in 1 ... bench {
92+ let expectation = expectationWithDescription ( " testKeepAlive " )
93+
94+ let startTime = CACurrentMediaTime ( )
95+ testIndex. search ( Query ( query: " " ) , completionHandler: { ( content, error) -> Void in
96+ XCTAssertNil ( error, " Error during search: \( error? . description) " )
97+ expectation. fulfill ( )
98+ } )
99+ waitForExpectationsWithTimeout ( expectationTimeout, handler: nil )
100+ mesuredTime. append ( CACurrentMediaTime ( ) - startTime)
101+ }
102+
103+ let avgTime = average ( Array ( mesuredTime. dropFirst ( ) ) )
104+ let ratio = ( mesuredTime [ 0 ] - avgTime) / max( mesuredTime [ 0 ] , avgTime)
105+
106+ if ratio < 0.5 {
107+ XCTFail ( " Maybe keep-alive doesn't work " )
108+ }
109+ }
110+
61111 func testListIndexes( ) {
62112 let expectation = expectationWithDescription ( " testListIndexes " )
63113 let object = [ " city " : " San Francisco " , " objectID " : " a/go/?à " ]
0 commit comments