@@ -107,11 +107,16 @@ class RTMConnectionTestCase: RTMBaseTestCase {
107107 var connection : RTMConnection ? = tuple? . 0
108108
109109 let deinitExp = expectation ( description: " deinit " )
110- let commandCallback = RTMConnection . CommandCallback ( timeoutInterval: 30 , callingQueue: . main, closure: { ( result) in
110+ let commandCallback = RTMConnection . CommandCallback (
111+ timeoutInterval: 30 ,
112+ peerID: peerID,
113+ command: IMGenericCommand ( ) ,
114+ callingQueue: . main)
115+ { ( result) in
111116 XCTAssertTrue ( Thread . isMainThread)
112117 XCTAssertEqual ( result. error? . code, LCError . InternalErrorCode. connectionLost. rawValue)
113118 deinitExp. fulfill ( )
114- } )
119+ }
115120 connection? . serialQueue. async {
116121 connection? . timer? . insert ( commandCallback: commandCallback, index: 0 )
117122 RTMConnectionManager . default. unregister (
@@ -133,15 +138,22 @@ class RTMConnectionTestCase: RTMBaseTestCase {
133138
134139 let timeoutExp = expectation ( description: " command callback timeout " )
135140 let start = Date ( ) . timeIntervalSince1970
136- let commandCallback = RTMConnection . CommandCallback ( timeoutInterval: commandTimeoutInterval, callingQueue: . main) { ( result) in
141+ let commandCallback = RTMConnection . CommandCallback (
142+ timeoutInterval: commandTimeoutInterval,
143+ peerID: peerID,
144+ command: IMGenericCommand ( ) ,
145+ callingQueue: . main)
146+ { ( result) in
137147 XCTAssertTrue ( Thread . isMainThread)
138148 XCTAssertEqual ( result. error? . code, LCError . InternalErrorCode. commandTimeout. rawValue)
139149 let interval = Date ( ) . timeIntervalSince1970 - start
140150 XCTAssertTrue ( ( ( commandTimeoutInterval - 3 ) ... ( commandTimeoutInterval + 3 ) ) ~= interval)
141151 timeoutExp. fulfill ( )
142152 }
143153 connection. serialQueue. async {
144- connection. timer? . insert ( commandCallback: commandCallback, index: connection. underlyingSerialIndex)
154+ connection. timer? . insert (
155+ commandCallback: commandCallback,
156+ index: connection. timer? . nextIndex ( ) ?? 999 )
145157 }
146158 wait ( for: [ timeoutExp] , timeout: commandTimeoutInterval * 2 )
147159
@@ -226,30 +238,40 @@ class RTMConnectionTestCase: RTMBaseTestCase {
226238 let connection = tuple. 0
227239 let delegator = tuple. 1
228240
229- XCTAssertEqual ( connection. underlyingSerialIndex , 1 )
241+ XCTAssertEqual ( connection. timer ? . index , 0 )
230242
231243 let sendExp1 = expectation ( description: " send command " )
232- connection. send ( command: testableCommand ( peerID: peerID) , callingQueue: . main) { ( result) in
244+ connection. send (
245+ command: testableCommand ( peerID: peerID) ,
246+ service: . instantMessaging,
247+ peerID: peerID,
248+ callingQueue: . main)
249+ { ( result) in
233250 XCTAssertTrue ( Thread . isMainThread)
234251 XCTAssertNotNil ( result. command)
235252 XCTAssertNil ( result. error)
236253 sendExp1. fulfill ( )
237254 }
238255 wait ( for: [ sendExp1] , timeout: timeout)
239256
240- XCTAssertEqual ( connection. underlyingSerialIndex , 2 )
257+ XCTAssertEqual ( connection. timer ? . index , 1 )
241258
242259 let sendExp2 = expectation ( description: " send command with big size " )
243260 var largeCommand = testableCommand ( peerID: peerID)
244- largeCommand. peerID = String ( repeating: " a " , count: 5000 )
245- connection. send ( command: largeCommand, callingQueue: . main) { ( result) in
261+ largeCommand. peerID = String ( repeating: " a " , count: 6000 )
262+ connection. send (
263+ command: largeCommand,
264+ service: . instantMessaging,
265+ peerID: peerID,
266+ callingQueue: . main)
267+ { ( result) in
246268 XCTAssertTrue ( Thread . isMainThread)
247269 XCTAssertEqual ( result. error? . code, LCError . InternalErrorCode. commandDataLengthTooLong. rawValue)
248270 sendExp2. fulfill ( )
249271 }
250272 wait ( for: [ sendExp2] , timeout: timeout)
251273
252- XCTAssertEqual ( connection. underlyingSerialIndex , 3 )
274+ XCTAssertEqual ( connection. timer ? . index , 2 )
253275
254276 let disconnectExp = expectation ( description: " disconnect " )
255277 delegator. didDisconnect = { _, _ in
@@ -259,15 +281,20 @@ class RTMConnectionTestCase: RTMBaseTestCase {
259281 wait ( for: [ disconnectExp] , timeout: timeout)
260282
261283 let sendExp3 = expectation ( description: " send command when connection lost " )
262- connection. send ( command: testableCommand ( peerID: peerID) , callingQueue: . main) { ( result) in
284+ connection. send (
285+ command: testableCommand ( peerID: peerID) ,
286+ service: . instantMessaging,
287+ peerID: peerID,
288+ callingQueue: . main)
289+ { ( result) in
263290 XCTAssertTrue ( Thread . isMainThread)
264291 XCTAssertNil ( result. command)
265292 XCTAssertEqual ( result. error? . code, LCError . InternalErrorCode. connectionLost. rawValue)
266293 sendExp3. fulfill ( )
267294 }
268295 wait ( for: [ sendExp3] , timeout: timeout)
269296
270- XCTAssertEqual ( connection. underlyingSerialIndex , 3 )
297+ XCTAssertNil ( connection. timer )
271298 }
272299
273300 func testGoaway( ) {
@@ -309,6 +336,96 @@ class RTMConnectionTestCase: RTMBaseTestCase {
309336 XCTAssertGreaterThan ( connection. rtmRouter!. table!. createdTimestamp, oldDate)
310337 }
311338
339+ func testThrottlingOutCommand( ) {
340+ let peerID = uuid
341+ let tuple = connectedConnection ( )
342+ let connection = tuple. 0
343+ expecting (
344+ description: " Throttling Out Command " ,
345+ count: 3 )
346+ { ( exp) in
347+ var index : Int32 ?
348+ for _ in 0 ... 2 {
349+ var outCommand = IMGenericCommand ( )
350+ outCommand. cmd = . session
351+ outCommand. op = . open
352+ outCommand. appID = LCApplication . default. id
353+ outCommand. peerID = peerID
354+ var sessionCommand = IMSessionCommand ( )
355+ sessionCommand. ua = LCApplication . default. httpClient. configuration. userAgent
356+ outCommand. sessionMessage = sessionCommand
357+ connection. send (
358+ command: outCommand,
359+ service: . instantMessaging,
360+ peerID: peerID,
361+ callingQueue: . main)
362+ { ( result) in
363+ XCTAssertTrue ( Thread . isMainThread)
364+ if index == nil {
365+ index = result. command? . i
366+ }
367+ XCTAssertNotNil ( result. command)
368+ XCTAssertNil ( result. error)
369+ XCTAssertEqual ( result. command? . i, index)
370+ exp. fulfill ( )
371+ }
372+ }
373+ }
374+ var conversationID : String !
375+ expecting { ( exp) in
376+ var outCommand = IMGenericCommand ( )
377+ outCommand. cmd = . conv
378+ outCommand. op = . start
379+ var convCommand = IMConvCommand ( )
380+ convCommand. m = [ peerID]
381+ outCommand. convMessage = convCommand
382+ connection. send (
383+ command: outCommand,
384+ service: . instantMessaging,
385+ peerID: peerID,
386+ callingQueue: . main)
387+ { ( result) in
388+ XCTAssertNotNil ( result. command)
389+ XCTAssertNil ( result. error)
390+ conversationID = result. command? . convMessage. cid
391+ exp. fulfill ( )
392+ }
393+ }
394+ if let conversationID = conversationID,
395+ !conversationID. isEmpty {
396+ var inCommandIndexSet = Set < Int32 > ( )
397+ let count = 3
398+ expecting (
399+ description: " NOT Throttling Direct Command " ,
400+ count: count)
401+ { ( exp) in
402+ for _ in 0 ..< count {
403+ var outCommand = IMGenericCommand ( )
404+ outCommand. cmd = . direct
405+ var directCommand = IMDirectCommand ( )
406+ directCommand. cid = conversationID
407+ directCommand. msg = peerID
408+ outCommand. directMessage = directCommand
409+ connection. send (
410+ command: outCommand,
411+ service: . instantMessaging,
412+ peerID: peerID,
413+ callingQueue: . main)
414+ { ( result) in
415+ if let i = result. command? . i {
416+ inCommandIndexSet. insert ( i)
417+ }
418+ XCTAssertNotNil ( result. command)
419+ XCTAssertNil ( result. error)
420+ exp. fulfill ( )
421+ }
422+ }
423+ }
424+ XCTAssertEqual ( inCommandIndexSet. count, count)
425+ } else {
426+ XCTFail ( )
427+ }
428+ }
312429}
313430
314431extension RTMConnectionTestCase {
@@ -353,6 +470,13 @@ extension RTMConnectionTestCase {
353470
354471 class Delegator : RTMConnectionDelegate {
355472
473+ func reset( ) {
474+ inConnecting = nil
475+ didConnect = nil
476+ didDisconnect = nil
477+ didReceiveCommand = nil
478+ }
479+
356480 var inConnecting : ( ( RTMConnection ) -> Void ) ?
357481 func connection( inConnecting connection: RTMConnection ) {
358482 inConnecting ? ( connection)
@@ -372,7 +496,5 @@ extension RTMConnectionTestCase {
372496 func connection( _ connection: RTMConnection , didReceiveCommand inCommand: IMGenericCommand ) {
373497 didReceiveCommand ? ( connection, inCommand)
374498 }
375-
376499 }
377-
378500}
0 commit comments