Skip to content
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
25 changes: 10 additions & 15 deletions LeanCloudTests/BaseTestCase.swift
Original file line number Diff line number Diff line change
Expand Up @@ -25,22 +25,26 @@ class BaseTestCase: XCTestCase {
let id: String
let key: String
let serverURL: String
let testableServerURL: String
}

static let cnApp = AppInfo(
id: "S5vDI3IeCk1NLLiM1aFg3262-gzGzoHsz",
key: "7g5pPsI55piz2PRLPWK5MPz0",
serverURL: "https://s5vdi3ie.lc-cn-n1-shared.com")
serverURL: "https://s5vdi3ie.lc-cn-n1-shared.com",
testableServerURL: "https://beta.leancloud.cn")

static let ceApp = AppInfo(
id: "skhiVsqIk7NLVdtHaUiWn0No-9Nh9j0Va",
key: "T3TEAIcL8Ls5XGPsGz41B1bz",
serverURL: "https://skhivsqi.lc-cn-e1-shared.com")
serverURL: "https://skhivsqi.lc-cn-e1-shared.com",
testableServerURL: "https://beta-tab.leancloud.cn")

static let usApp = AppInfo(
id: "jenSt9nvWtuJtmurdE28eg5M-MdYXbMMI",
key: "8VLPsDlskJi8KsKppED4xKS0",
serverURL: "")
serverURL: "",
testableServerURL: "https://beta-us.leancloud.cn")

static var config: LCApplication.Configuration {
var config = LCApplication.Configuration()
Expand Down Expand Up @@ -120,28 +124,19 @@ extension BaseTestCase {
func expecting(
description: String? = nil,
count expectedFulfillmentCount: Int = 1,
timeout: TimeInterval = BaseTestCase.timeout,
testcase: (XCTestExpectation) -> Void)
{
let exp = self.expectation(description: description ?? "default expectation")
exp.expectedFulfillmentCount = expectedFulfillmentCount
self.expecting(
timeout: BaseTestCase.timeout,
timeout: timeout,
expectation: { exp },
testcase: testcase)
}

func expecting(
expectation: @escaping () -> XCTestExpectation,
testcase: (XCTestExpectation) -> Void)
{
self.expecting(
timeout: BaseTestCase.timeout,
expectation: expectation,
testcase: testcase)
}

func expecting(
timeout: TimeInterval,
timeout: TimeInterval = BaseTestCase.timeout,
expectation: () -> XCTestExpectation,
testcase: (XCTestExpectation) -> Void)
{
Expand Down
5 changes: 0 additions & 5 deletions LeanCloudTests/IMConversationTestCase.swift
Original file line number Diff line number Diff line change
Expand Up @@ -1839,11 +1839,6 @@ class IMConversationTestCase: RTMBaseTestCase {
XCTAssertTrue(Thread.isMainThread)
XCTAssertTrue(result.isSuccess)
XCTAssertNil(result.error)
if let value = result.value {
XCTAssertNotNil(value)
XCTAssertEqual(value?.role, .manager)
}
XCTAssertEqual(convB?.memberInfoTable?[clientB.ID]?.role, .manager)
exp.fulfill()
})
}
Expand Down
150 changes: 136 additions & 14 deletions LeanCloudTests/RTMConnectionTestCase.swift
Original file line number Diff line number Diff line change
Expand Up @@ -107,11 +107,16 @@ class RTMConnectionTestCase: RTMBaseTestCase {
var connection: RTMConnection? = tuple?.0

let deinitExp = expectation(description: "deinit")
let commandCallback = RTMConnection.CommandCallback(timeoutInterval: 30, callingQueue: .main, closure: { (result) in
let commandCallback = RTMConnection.CommandCallback(
timeoutInterval: 30,
peerID: peerID,
command: IMGenericCommand(),
callingQueue: .main)
{ (result) in
XCTAssertTrue(Thread.isMainThread)
XCTAssertEqual(result.error?.code, LCError.InternalErrorCode.connectionLost.rawValue)
deinitExp.fulfill()
})
}
connection?.serialQueue.async {
connection?.timer?.insert(commandCallback: commandCallback, index: 0)
RTMConnectionManager.default.unregister(
Expand All @@ -133,15 +138,22 @@ class RTMConnectionTestCase: RTMBaseTestCase {

let timeoutExp = expectation(description: "command callback timeout")
let start = Date().timeIntervalSince1970
let commandCallback = RTMConnection.CommandCallback(timeoutInterval: commandTimeoutInterval, callingQueue: .main) { (result) in
let commandCallback = RTMConnection.CommandCallback(
timeoutInterval: commandTimeoutInterval,
peerID: peerID,
command: IMGenericCommand(),
callingQueue: .main)
{ (result) in
XCTAssertTrue(Thread.isMainThread)
XCTAssertEqual(result.error?.code, LCError.InternalErrorCode.commandTimeout.rawValue)
let interval = Date().timeIntervalSince1970 - start
XCTAssertTrue(((commandTimeoutInterval - 3)...(commandTimeoutInterval + 3)) ~= interval)
timeoutExp.fulfill()
}
connection.serialQueue.async {
connection.timer?.insert(commandCallback: commandCallback, index: connection.underlyingSerialIndex)
connection.timer?.insert(
commandCallback: commandCallback,
index: connection.timer?.nextIndex() ?? 999)
}
wait(for: [timeoutExp], timeout: commandTimeoutInterval * 2)

Expand Down Expand Up @@ -226,30 +238,40 @@ class RTMConnectionTestCase: RTMBaseTestCase {
let connection = tuple.0
let delegator = tuple.1

XCTAssertEqual(connection.underlyingSerialIndex, 1)
XCTAssertEqual(connection.timer?.index, 0)

let sendExp1 = expectation(description: "send command")
connection.send(command: testableCommand(peerID: peerID), callingQueue: .main) { (result) in
connection.send(
command: testableCommand(peerID: peerID),
service: .instantMessaging,
peerID: peerID,
callingQueue: .main)
{ (result) in
XCTAssertTrue(Thread.isMainThread)
XCTAssertNotNil(result.command)
XCTAssertNil(result.error)
sendExp1.fulfill()
}
wait(for: [sendExp1], timeout: timeout)

XCTAssertEqual(connection.underlyingSerialIndex, 2)
XCTAssertEqual(connection.timer?.index, 1)

let sendExp2 = expectation(description: "send command with big size")
var largeCommand = testableCommand(peerID: peerID)
largeCommand.peerID = String(repeating: "a", count: 5000)
connection.send(command: largeCommand, callingQueue: .main) { (result) in
largeCommand.peerID = String(repeating: "a", count: 6000)
connection.send(
command: largeCommand,
service: .instantMessaging,
peerID: peerID,
callingQueue: .main)
{ (result) in
XCTAssertTrue(Thread.isMainThread)
XCTAssertEqual(result.error?.code, LCError.InternalErrorCode.commandDataLengthTooLong.rawValue)
sendExp2.fulfill()
}
wait(for: [sendExp2], timeout: timeout)

XCTAssertEqual(connection.underlyingSerialIndex, 3)
XCTAssertEqual(connection.timer?.index, 2)

let disconnectExp = expectation(description: "disconnect")
delegator.didDisconnect = { _, _ in
Expand All @@ -259,15 +281,20 @@ class RTMConnectionTestCase: RTMBaseTestCase {
wait(for: [disconnectExp], timeout: timeout)

let sendExp3 = expectation(description: "send command when connection lost")
connection.send(command: testableCommand(peerID: peerID), callingQueue: .main) { (result) in
connection.send(
command: testableCommand(peerID: peerID),
service: .instantMessaging,
peerID: peerID,
callingQueue: .main)
{ (result) in
XCTAssertTrue(Thread.isMainThread)
XCTAssertNil(result.command)
XCTAssertEqual(result.error?.code, LCError.InternalErrorCode.connectionLost.rawValue)
sendExp3.fulfill()
}
wait(for: [sendExp3], timeout: timeout)

XCTAssertEqual(connection.underlyingSerialIndex, 3)
XCTAssertNil(connection.timer)
}

func testGoaway() {
Expand Down Expand Up @@ -309,6 +336,96 @@ class RTMConnectionTestCase: RTMBaseTestCase {
XCTAssertGreaterThan(connection.rtmRouter!.table!.createdTimestamp, oldDate)
}

func testThrottlingOutCommand() {
let peerID = uuid
let tuple = connectedConnection()
let connection = tuple.0
expecting(
description: "Throttling Out Command",
count: 3)
{ (exp) in
var index: Int32?
for _ in 0...2 {
var outCommand = IMGenericCommand()
outCommand.cmd = .session
outCommand.op = .open
outCommand.appID = LCApplication.default.id
outCommand.peerID = peerID
var sessionCommand = IMSessionCommand()
sessionCommand.ua = LCApplication.default.httpClient.configuration.userAgent
outCommand.sessionMessage = sessionCommand
connection.send(
command: outCommand,
service: .instantMessaging,
peerID: peerID,
callingQueue: .main)
{ (result) in
XCTAssertTrue(Thread.isMainThread)
if index == nil {
index = result.command?.i
}
XCTAssertNotNil(result.command)
XCTAssertNil(result.error)
XCTAssertEqual(result.command?.i, index)
exp.fulfill()
}
}
}
var conversationID: String!
expecting { (exp) in
var outCommand = IMGenericCommand()
outCommand.cmd = .conv
outCommand.op = .start
var convCommand = IMConvCommand()
convCommand.m = [peerID]
outCommand.convMessage = convCommand
connection.send(
command: outCommand,
service: .instantMessaging,
peerID: peerID,
callingQueue: .main)
{ (result) in
XCTAssertNotNil(result.command)
XCTAssertNil(result.error)
conversationID = result.command?.convMessage.cid
exp.fulfill()
}
}
if let conversationID = conversationID,
!conversationID.isEmpty {
var inCommandIndexSet = Set<Int32>()
let count = 3
expecting(
description: "NOT Throttling Direct Command",
count: count)
{ (exp) in
for _ in 0..<count {
var outCommand = IMGenericCommand()
outCommand.cmd = .direct
var directCommand = IMDirectCommand()
directCommand.cid = conversationID
directCommand.msg = peerID
outCommand.directMessage = directCommand
connection.send(
command: outCommand,
service: .instantMessaging,
peerID: peerID,
callingQueue: .main)
{ (result) in
if let i = result.command?.i {
inCommandIndexSet.insert(i)
}
XCTAssertNotNil(result.command)
XCTAssertNil(result.error)
exp.fulfill()
}
}
}
XCTAssertEqual(inCommandIndexSet.count, count)
} else {
XCTFail()
}
}
}

extension RTMConnectionTestCase {
Expand Down Expand Up @@ -353,6 +470,13 @@ extension RTMConnectionTestCase {

class Delegator: RTMConnectionDelegate {

func reset() {
inConnecting = nil
didConnect = nil
didDisconnect = nil
didReceiveCommand = nil
}

var inConnecting: ((RTMConnection) -> Void)?
func connection(inConnecting connection: RTMConnection) {
inConnecting?(connection)
Expand All @@ -372,7 +496,5 @@ extension RTMConnectionTestCase {
func connection(_ connection: RTMConnection, didReceiveCommand inCommand: IMGenericCommand) {
didReceiveCommand?(connection, inCommand)
}

}

}
4 changes: 2 additions & 2 deletions Sources/Foundation/Runtime.swift
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ class Runtime {
if let varChars: UnsafeMutablePointer<Int8> = property_copyAttributeValue(property, "V") {

defer {
let utf8Str = String(validatingUTF8: varChars)!
let _ = String(validatingUTF8: varChars)
varChars.deallocate()
}

Expand Down Expand Up @@ -115,7 +115,7 @@ class Runtime {
}

defer {
let utf8Str = String(validatingUTF8: varChars)!
let _ = String(validatingUTF8: varChars)
varChars.deallocate()
}

Expand Down
Loading