Skip to content

Commit 0685705

Browse files
authored
Merge pull request #318 from zapcannon87/developer
feat(IM): async counting members function of conversation
2 parents 82b0c5b + e0b8a17 commit 0685705

File tree

6 files changed

+601
-469
lines changed

6 files changed

+601
-469
lines changed

LeanCloudTests/BaseTestCase.swift

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -59,13 +59,22 @@ class BaseTestCase: XCTestCase {
5959
LCApplication.default.unregister()
6060
super.tearDown()
6161
}
62+
}
63+
64+
extension BaseTestCase {
65+
66+
var className: String {
67+
return "\(type(of: self))"
68+
}
6269

6370
func object(_ objectId: LCStringConvertible? = nil) -> LCObject {
64-
let className = "\(type(of: self))"
6571
if let objectId = objectId {
66-
return LCObject(className: className, objectId: objectId)
72+
return LCObject(
73+
className: self.className,
74+
objectId: objectId)
6775
} else {
68-
return LCObject(className: className)
76+
return LCObject(
77+
className: self.className)
6978
}
7079
}
7180
}

LeanCloudTests/IMClientTestCase.swift

Lines changed: 81 additions & 69 deletions
Original file line numberDiff line numberDiff line change
@@ -104,35 +104,41 @@ class IMClientTestCase: RTMBaseTestCase {
104104

105105
func testOpenWithSignature() {
106106
let user = LCUser()
107-
user.username = UUID().uuidString.lcString
108-
user.password = UUID().uuidString.lcString
109-
107+
user.username = self.uuid.lcString
108+
user.password = self.uuid.lcString
110109
XCTAssertTrue(user.signUp().isSuccess)
111110

112-
if let objectID = user.objectId?.value, let sessionToken = user.sessionToken?.value {
113-
114-
var clientWithUser: IMClient! = try! IMClient(user: user, options: [])
115-
expecting { (exp) in
116-
clientWithUser.open(completion: { (result) in
117-
XCTAssertTrue(result.isSuccess)
118-
XCTAssertNil(result.error)
119-
exp.fulfill()
120-
})
121-
}
122-
123-
clientWithUser = nil
124-
delay()
125-
126-
let signatureDelegator = SignatureDelegator()
127-
signatureDelegator.sessionToken = sessionToken
128-
let clientWithID = try! IMClient(ID: objectID, options: [], signatureDelegate: signatureDelegator)
129-
expecting { (exp) in
130-
clientWithID.open(completion: { (result) in
131-
XCTAssertTrue(result.isSuccess)
132-
XCTAssertNil(result.error)
133-
exp.fulfill()
134-
})
135-
}
111+
guard let objectID = user.objectId?.value,
112+
let sessionToken = user.sessionToken?.value else {
113+
XCTFail()
114+
return
115+
}
116+
117+
var clientFromUser: IMClient! = try! IMClient(
118+
user: user,
119+
options: [])
120+
expecting { (exp) in
121+
clientFromUser.open(completion: { (result) in
122+
XCTAssertTrue(result.isSuccess)
123+
XCTAssertNil(result.error)
124+
exp.fulfill()
125+
})
126+
}
127+
clientFromUser = nil
128+
delay()
129+
130+
let signatureDelegator = SignatureDelegator()
131+
signatureDelegator.sessionToken = sessionToken
132+
let clientFromID = try! IMClient(
133+
ID: objectID,
134+
options: [],
135+
signatureDelegate: signatureDelegator)
136+
expecting { (exp) in
137+
clientFromID.open(completion: { (result) in
138+
XCTAssertTrue(result.isSuccess)
139+
XCTAssertNil(result.error)
140+
exp.fulfill()
141+
})
136142
}
137143
}
138144

@@ -306,24 +312,29 @@ class IMClientTestCase: RTMBaseTestCase {
306312
}
307313

308314
func testSessionTokenExpired() {
309-
let client: IMClient = try! IMClient(ID: uuid, options: [])
310-
let delegator: Delegator = Delegator()
311-
client.delegate = delegator
315+
let delegator = Delegator()
316+
let client = try! IMClient(
317+
ID: self.uuid,
318+
options: [],
319+
delegate: delegator)
312320

313-
let openExp = expectation(description: "open")
314-
client.open { (result) in
315-
XCTAssertTrue(result.isSuccess)
316-
openExp.fulfill()
321+
expecting { (exp) in
322+
client.open { (result) in
323+
XCTAssertTrue(result.isSuccess)
324+
XCTAssertNil(result.error)
325+
exp.fulfill()
326+
}
317327
}
318-
wait(for: [openExp], timeout: timeout)
319328

320-
client.test_change(sessionToken: uuid, sessionTokenExpiration: Date(timeIntervalSinceNow: 36000))
329+
client.sessionToken = self.uuid
330+
client.sessionTokenExpiration = Date(timeIntervalSinceNow: 36000)
321331

322-
let exp = expectation(description: "Pause, Resume, First Reopen Then Session Token Expired and Second Reopen Success")
323-
exp.expectedFulfillmentCount = 4
324-
exp.assertForOverFulfill = true
325-
delegator.clientEvent = { c, event in
326-
if c === client {
332+
expecting(
333+
description: "Pause -> Resume -> First-Reopen Then session token expired, Final Second-Reopen success",
334+
count: 4)
335+
{ (exp) in
336+
delegator.clientEvent = { c, event in
337+
XCTAssertTrue(c === client)
327338
switch event {
328339
case .sessionDidPause(error: _):
329340
exp.fulfill()
@@ -335,22 +346,19 @@ class IMClientTestCase: RTMBaseTestCase {
335346
XCTFail()
336347
}
337348
}
349+
let _ = NotificationCenter.default.addObserver(
350+
forName: IMClient.TestSessionTokenExpiredNotification,
351+
object: client,
352+
queue: .main
353+
) { (notification) in
354+
XCTAssertEqual(
355+
(notification.userInfo?["error"] as? LCError)?.code,
356+
LCError.ServerErrorCode.sessionTokenExpired.rawValue)
357+
exp.fulfill()
358+
}
359+
client.connection.disconnect()
360+
client.connection.connect()
338361
}
339-
let _ = NotificationCenter.default.addObserver(
340-
forName: IMClient.TestSessionTokenExpiredNotification,
341-
object: client,
342-
queue: OperationQueue.main
343-
) { (notification) in
344-
let error = notification.userInfo?["error"] as? LCError
345-
XCTAssertEqual(
346-
error?.code,
347-
LCError.ServerErrorCode.sessionTokenExpired.rawValue
348-
)
349-
exp.fulfill()
350-
}
351-
client.connection.disconnect()
352-
client.connection.connect()
353-
wait(for: [exp], timeout: timeout)
354362
}
355363

356364
func testReportDeviceToken() {
@@ -624,24 +632,29 @@ extension IMClientTestCase {
624632

625633
func getOpenSignature(client: IMClient, completion: @escaping (IMSignature) -> Void) {
626634
guard let sessionToken = self.sessionToken else {
635+
XCTFail()
627636
return
628637
}
629-
let application = client.application
630-
let httpClient: HTTPClient = application.httpClient
631-
let url = application.v2router.route(path: "rtm/clients/sign", module: .api)!
632-
let parameters: [String: Any] = ["session_token": sessionToken]
633-
_ = httpClient.request(url: url, method: .get, parameters: parameters) { (response) in
634-
guard
635-
let value = response.value as? [String: Any],
638+
_ = client.application.httpClient.request(
639+
url: client.application.v2router.route(
640+
path: "rtm/clients/sign",
641+
module: .api)!,
642+
method: .get,
643+
parameters: ["session_token": sessionToken])
644+
{ (response) in
645+
guard let value = response.value as? [String: Any],
636646
let client_id = value["client_id"] as? String,
637647
client_id == client.ID,
638648
let signature = value["signature"] as? String,
639649
let timestamp = value["timestamp"] as? Int64,
640-
let nonce = value["nonce"] as? String else
641-
{
642-
return
650+
let nonce = value["nonce"] as? String else {
651+
XCTFail()
652+
return
643653
}
644-
completion(IMSignature(signature: signature, timestamp: timestamp, nonce: nonce))
654+
completion(IMSignature(
655+
signature: signature,
656+
timestamp: timestamp,
657+
nonce: nonce))
645658
}
646659
}
647660

@@ -653,9 +666,8 @@ extension IMClientTestCase {
653666
signatureHandler(client, signature)
654667
}
655668
default:
656-
break
669+
XCTFail()
657670
}
658671
}
659672
}
660-
661673
}

0 commit comments

Comments
 (0)