Skip to content

Commit 90219fe

Browse files
authored
Merge pull request #373 from zapcannon87/developer
feat(rtm): throttling command and omit peer-id
2 parents 6148bc5 + f34029f commit 90219fe

File tree

7 files changed

+535
-240
lines changed

7 files changed

+535
-240
lines changed

LeanCloudTests/BaseTestCase.swift

Lines changed: 10 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -25,22 +25,26 @@ class BaseTestCase: XCTestCase {
2525
let id: String
2626
let key: String
2727
let serverURL: String
28+
let testableServerURL: String
2829
}
2930

3031
static let cnApp = AppInfo(
3132
id: "S5vDI3IeCk1NLLiM1aFg3262-gzGzoHsz",
3233
key: "7g5pPsI55piz2PRLPWK5MPz0",
33-
serverURL: "https://s5vdi3ie.lc-cn-n1-shared.com")
34+
serverURL: "https://s5vdi3ie.lc-cn-n1-shared.com",
35+
testableServerURL: "https://beta.leancloud.cn")
3436

3537
static let ceApp = AppInfo(
3638
id: "skhiVsqIk7NLVdtHaUiWn0No-9Nh9j0Va",
3739
key: "T3TEAIcL8Ls5XGPsGz41B1bz",
38-
serverURL: "https://skhivsqi.lc-cn-e1-shared.com")
40+
serverURL: "https://skhivsqi.lc-cn-e1-shared.com",
41+
testableServerURL: "https://beta-tab.leancloud.cn")
3942

4043
static let usApp = AppInfo(
4144
id: "jenSt9nvWtuJtmurdE28eg5M-MdYXbMMI",
4245
key: "8VLPsDlskJi8KsKppED4xKS0",
43-
serverURL: "")
46+
serverURL: "",
47+
testableServerURL: "https://beta-us.leancloud.cn")
4448

4549
static var config: LCApplication.Configuration {
4650
var config = LCApplication.Configuration()
@@ -120,28 +124,19 @@ extension BaseTestCase {
120124
func expecting(
121125
description: String? = nil,
122126
count expectedFulfillmentCount: Int = 1,
127+
timeout: TimeInterval = BaseTestCase.timeout,
123128
testcase: (XCTestExpectation) -> Void)
124129
{
125130
let exp = self.expectation(description: description ?? "default expectation")
126131
exp.expectedFulfillmentCount = expectedFulfillmentCount
127132
self.expecting(
128-
timeout: BaseTestCase.timeout,
133+
timeout: timeout,
129134
expectation: { exp },
130135
testcase: testcase)
131136
}
132137

133138
func expecting(
134-
expectation: @escaping () -> XCTestExpectation,
135-
testcase: (XCTestExpectation) -> Void)
136-
{
137-
self.expecting(
138-
timeout: BaseTestCase.timeout,
139-
expectation: expectation,
140-
testcase: testcase)
141-
}
142-
143-
func expecting(
144-
timeout: TimeInterval,
139+
timeout: TimeInterval = BaseTestCase.timeout,
145140
expectation: () -> XCTestExpectation,
146141
testcase: (XCTestExpectation) -> Void)
147142
{

LeanCloudTests/IMConversationTestCase.swift

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1839,11 +1839,6 @@ class IMConversationTestCase: RTMBaseTestCase {
18391839
XCTAssertTrue(Thread.isMainThread)
18401840
XCTAssertTrue(result.isSuccess)
18411841
XCTAssertNil(result.error)
1842-
if let value = result.value {
1843-
XCTAssertNotNil(value)
1844-
XCTAssertEqual(value?.role, .manager)
1845-
}
1846-
XCTAssertEqual(convB?.memberInfoTable?[clientB.ID]?.role, .manager)
18471842
exp.fulfill()
18481843
})
18491844
}

LeanCloudTests/RTMConnectionTestCase.swift

Lines changed: 136 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -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

314431
extension 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
}

Sources/Foundation/Runtime.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ class Runtime {
5252
if let varChars: UnsafeMutablePointer<Int8> = property_copyAttributeValue(property, "V") {
5353

5454
defer {
55-
let utf8Str = String(validatingUTF8: varChars)!
55+
let _ = String(validatingUTF8: varChars)
5656
varChars.deallocate()
5757
}
5858

@@ -115,7 +115,7 @@ class Runtime {
115115
}
116116

117117
defer {
118-
let utf8Str = String(validatingUTF8: varChars)!
118+
let _ = String(validatingUTF8: varChars)
119119
varChars.deallocate()
120120
}
121121

0 commit comments

Comments
 (0)