Skip to content

Commit f6631ea

Browse files
authored
Merge pull request #370 from zapcannon87/developer
fix(im): message ack error handling not right
2 parents e338d85 + 2a1ad00 commit f6631ea

File tree

10 files changed

+119
-77
lines changed

10 files changed

+119
-77
lines changed

LeanCloud.xcodeproj/xcshareddata/xcschemes/LeanCloud.xcscheme

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -76,13 +76,6 @@
7676
ReferencedContainer = "container:LeanCloud.xcodeproj">
7777
</BuildableReference>
7878
</MacroExpansion>
79-
<EnvironmentVariables>
80-
<EnvironmentVariable
81-
key = "OS_ACTIVITY_MODE"
82-
value = "disable"
83-
isEnabled = "YES">
84-
</EnvironmentVariable>
85-
</EnvironmentVariables>
8679
<AdditionalOptions>
8780
<AdditionalOption
8881
key = "NSZombieEnabled"

LeanCloud.xcodeproj/xcshareddata/xcschemes/LeanCloudTests.xcscheme

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,6 @@
5252
buildConfiguration = "Debug"
5353
selectedDebuggerIdentifier = "Xcode.DebuggerFoundation.Debugger.LLDB"
5454
selectedLauncherIdentifier = "Xcode.DebuggerFoundation.Launcher.LLDB"
55-
enableThreadSanitizer = "YES"
5655
launchStyle = "0"
5756
useCustomWorkingDirectory = "NO"
5857
ignoresPersistentStateOnLaunch = "NO"

LeanCloudTests/BaseTestCase.swift

Lines changed: 38 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -11,15 +11,13 @@ import XCTest
1111

1212
class BaseTestCase: XCTestCase {
1313

14-
let timeout: TimeInterval = 60.0
15-
1614
static let timeout: TimeInterval = 60.0
15+
let timeout: TimeInterval = 60.0
1716

18-
var uuid: String {
17+
static var uuid: String {
1918
return UUID().uuidString.replacingOccurrences(of: "-", with: "")
2019
}
21-
22-
static var uuid: String {
20+
var uuid: String {
2321
return UUID().uuidString.replacingOccurrences(of: "-", with: "")
2422
}
2523

@@ -32,28 +30,34 @@ class BaseTestCase: XCTestCase {
3230
static let cnApp = AppInfo(
3331
id: "S5vDI3IeCk1NLLiM1aFg3262-gzGzoHsz",
3432
key: "7g5pPsI55piz2PRLPWK5MPz0",
35-
serverURL: "https://s5vdi3ie.lc-cn-n1-shared.com"
36-
)
33+
serverURL: "https://s5vdi3ie.lc-cn-n1-shared.com")
34+
35+
static let ceApp = AppInfo(
36+
id: "skhiVsqIk7NLVdtHaUiWn0No-9Nh9j0Va",
37+
key: "T3TEAIcL8Ls5XGPsGz41B1bz",
38+
serverURL: "https://skhivsqi.lc-cn-e1-shared.com")
39+
40+
static let usApp = AppInfo(
41+
id: "jenSt9nvWtuJtmurdE28eg5M-MdYXbMMI",
42+
key: "8VLPsDlskJi8KsKppED4xKS0",
43+
serverURL: "")
44+
45+
static var config: LCApplication.Configuration {
46+
var config = LCApplication.Configuration()
47+
config.RTMCustomServerURL = RTMBaseTestCase.testableRTMURL
48+
return config
49+
}
3750

3851
override class func setUp() {
3952
super.setUp()
40-
53+
let app = BaseTestCase.cnApp
4154
TestObject.register()
42-
4355
LCApplication.logLevel = .all
44-
4556
try! LCApplication.default.set(
46-
id: BaseTestCase.cnApp.id,
47-
key: BaseTestCase.cnApp.key,
48-
serverURL: BaseTestCase.cnApp.serverURL)
49-
50-
// var config = LCApplication.Configuration()
51-
// config.RTMCustomServerURL = RTMBaseTestCase.testableRTMURL
52-
// try! LCApplication.default.set(
53-
// id: BaseTestCase.cnApp.id,
54-
// key: BaseTestCase.cnApp.key,
55-
// serverURL: BaseTestCase.cnApp.serverURL,
56-
// configuration: config)
57+
id: app.id,
58+
key: app.key,
59+
serverURL: app.serverURL.isEmpty ? nil : app.serverURL,
60+
configuration: BaseTestCase.config)
5761
}
5862

5963
override class func tearDown() {
@@ -161,24 +165,29 @@ extension BaseTestCase {
161165
extension LCApplication {
162166

163167
var masterKey: String {
168+
let key: String
164169
switch self.id {
165-
case "S5vDI3IeCk1NLLiM1aFg3262-gzGzoHsz":
166-
return "Q26gTodbyi1Ki7lM9vtncF6U,master"
170+
case BaseTestCase.cnApp.id:
171+
key = "Q26gTodbyi1Ki7lM9vtncF6U"
172+
case BaseTestCase.ceApp.id:
173+
key = "FTPdEcG7vLKxNqKxYhTFdK4g"
174+
case BaseTestCase.usApp.id:
175+
key = "fasiJXz8jvSwn3G2B2QeraRe"
167176
default:
168177
fatalError()
169178
}
179+
return key + ",master"
170180
}
171181

172182
var v2router: AppRouter {
173183
return AppRouter(
174184
application: self,
175-
configuration: AppRouter.Configuration(apiVersion: "1.2")
176-
)
185+
configuration: AppRouter.Configuration(apiVersion: "1.2"))
177186
}
178187

179188
var applicationSupportDirectoryURL: URL {
180-
return (
181-
try! FileManager.default.url(
189+
return (try!
190+
FileManager.default.url(
182191
for: .applicationSupportDirectory,
183192
in: .userDomainMask,
184193
appropriateFor: nil,
@@ -192,8 +201,8 @@ extension LCApplication {
192201
}
193202

194203
var cachesDirectoryURL: URL {
195-
return (
196-
try! FileManager.default.url(
204+
return (try!
205+
FileManager.default.url(
197206
for: .cachesDirectory,
198207
in: .userDomainMask,
199208
appropriateFor: nil,

LeanCloudTests/IMConversationTestCase.swift

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2313,11 +2313,12 @@ extension IMConversationTestCase {
23132313
var objectID: String?
23142314
var loop = true
23152315
_ = LCApplication.default.httpClient.request(
2316-
url: LCApplication.default.v2router.route(path: "/rtm/service-conversations", module: .api)!,
2316+
url: LCApplication.default.v2router.route(
2317+
path: "/rtm/service-conversations", module: .api)!,
23172318
method: .post,
23182319
parameters: ["name": uuid],
23192320
headers: nil,
2320-
completionDispatchQueue: .main)
2321+
completionQueue: .main)
23212322
{ (response) in
23222323
objectID = response["objectId"]
23232324
loop.toggle()
@@ -2337,7 +2338,7 @@ extension IMConversationTestCase {
23372338
method: .post,
23382339
parameters: ["client_id": clientID],
23392340
headers: ["X-LC-Key": LCApplication.default.masterKey],
2340-
completionDispatchQueue: .main)
2341+
completionQueue: .main)
23412342
{ (response) in
23422343
success = response.isSuccess
23432344
loop.toggle()
@@ -2357,7 +2358,7 @@ extension IMConversationTestCase {
23572358
method: .post,
23582359
parameters: ["from_client": "master", "message": content],
23592360
headers: ["X-LC-Key": LCApplication.default.masterKey],
2360-
completionDispatchQueue: .main)
2361+
completionQueue: .main)
23612362
{ (response) in
23622363
if let result: [String: Any] = response["result"],
23632364
let messageID: String = result["msg-id"] as? String,

LeanCloudTests/IMMessageTestCase.swift

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -146,6 +146,27 @@ class IMMessageTestCase: RTMBaseTestCase {
146146
conversationA.lastMessage?.sentTimestamp,
147147
conversationB.lastMessage?.sentTimestamp
148148
)
149+
150+
expecting { (exp) in
151+
try! conversationA.leave(completion: { (result) in
152+
XCTAssertTrue(result.isSuccess)
153+
XCTAssertNil(result.error)
154+
exp.fulfill()
155+
})
156+
}
157+
158+
expecting { (exp) in
159+
let message = IMTextMessage()
160+
message.text = uuid
161+
try! conversationA.send(message: message, completion: { (result) in
162+
XCTAssertTrue(result.isFailure)
163+
XCTAssertNotNil(result.error)
164+
XCTAssertNotNil(message.ID)
165+
XCTAssertNotNil(message.sentTimestamp)
166+
XCTAssertEqual(message.status, .failed)
167+
exp.fulfill()
168+
})
169+
}
149170
}
150171

151172
func testMessageContinuousSendingAndReceiving() {

LeanCloudTests/LCFileTestCase.swift

Lines changed: 16 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -290,17 +290,22 @@ extension LCFileTestCase {
290290
XCTAssertNotNil(file2.createdAt)
291291
XCTAssertNotNil(file2.save().error)
292292

293-
var file3: LCFile! = LCFile(
294-
application: application,
295-
url: file2.url!)
296-
XCTAssertTrue(file3.save().isSuccess)
297-
XCTAssertNotNil(file3.mimeType)
298-
XCTAssertNotNil(file3.name)
299-
XCTAssertEqual(file3.metaData?.__source as? LCString, LCString("external"))
300-
XCTAssertNotNil(file3.url)
301-
XCTAssertNotNil(file3.objectId)
302-
XCTAssertNotNil(file3.createdAt)
303-
XCTAssertNotNil(file3.save().error)
293+
var file3: LCFile!
294+
if let file2url = file2.url {
295+
file3 = LCFile(
296+
application: application,
297+
url: file2url)
298+
XCTAssertTrue(file3.save().isSuccess)
299+
XCTAssertNotNil(file3.mimeType)
300+
XCTAssertNotNil(file3.name)
301+
XCTAssertEqual(file3.metaData?.__source as? LCString, LCString("external"))
302+
XCTAssertNotNil(file3.url)
303+
XCTAssertNotNil(file3.objectId)
304+
XCTAssertNotNil(file3.createdAt)
305+
XCTAssertNotNil(file3.save().error)
306+
} else {
307+
XCTFail()
308+
}
304309

305310
delay()
306311

LeanCloudTests/LCRouterTestCase.swift

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,8 @@ import XCTest
1212
class LCRouterTestCase: BaseTestCase {
1313

1414
static let usApplication = try! LCApplication(
15-
id: "jenSt9nvWtuJtmurdE28eg5M-MdYXbMMI",
16-
key: "8VLPsDlskJi8KsKppED4xKS0"
17-
)
15+
id: BaseTestCase.usApp.id,
16+
key: BaseTestCase.usApp.key)
1817

1918
var appRouter: AppRouter {
2019
return LCRouterTestCase.usApplication.appRouter

LeanCloudTests/RTMBaseTestCase.swift

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,5 +11,8 @@ import XCTest
1111

1212
class RTMBaseTestCase: BaseTestCase {
1313

14-
static let testableRTMURL: URL = URL(string: "wss://rtm51.leancloud.cn")!
14+
static let useTestableRTMURL = false
15+
static let testableRTMURL = RTMBaseTestCase.useTestableRTMURL
16+
? URL(string: "wss://cn-n1-core-k8s-cell-12.leancloud.cn")!
17+
: nil
1518
}

Sources/RTM/IMConversation.swift

Lines changed: 18 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -735,13 +735,24 @@ extension IMConversation {
735735
switch result {
736736
case .inCommand(let inCommand):
737737
assert(client.specificAssertion)
738-
if let ackCommand = (inCommand.hasAckMessage ? inCommand.ackMessage : nil),
739-
let messageID = (ackCommand.hasUid ? ackCommand.uid : nil),
740-
let timestamp = (ackCommand.hasT ? ackCommand.t : nil) {
741-
message.update(status: .sent, ID: messageID, timestamp: timestamp)
742-
self.safeUpdatingLastMessage(newMessage: message, client: client)
743-
client.eventQueue.async {
744-
completion(.success)
738+
if let ackCommand = (inCommand.hasAckMessage ? inCommand.ackMessage : nil) {
739+
if let messageID = (ackCommand.hasUid ? ackCommand.uid : nil),
740+
let timestamp = (ackCommand.hasT ? ackCommand.t : nil) {
741+
message.update(
742+
status: .sent,
743+
ID: messageID,
744+
timestamp: timestamp)
745+
}
746+
if let error = ackCommand.lcError {
747+
message.update(status: .failed)
748+
client.eventQueue.async {
749+
completion(.failure(error: error))
750+
}
751+
} else {
752+
self.safeUpdatingLastMessage(newMessage: message, client: client)
753+
client.eventQueue.async {
754+
completion(.success)
755+
}
745756
}
746757
} else {
747758
message.update(status: .failed)

Sources/RTM/RTMConnection.swift

Lines changed: 15 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -221,20 +221,21 @@ class RTMConnection {
221221
private(set) var lastPongReceivedTimestamp: TimeInterval = 0
222222

223223
#if DEBUG
224-
private(set) var specificKey: DispatchSpecificKey<Int>? = nil
225-
private(set) var specificValue: Int? = nil
224+
private(set) var specificKey: DispatchSpecificKey<Int>?
225+
private(set) var specificValue: Int?
226+
#endif
226227
private var specificAssertion: Bool {
227-
if let key = self.specificKey, let value = self.specificValue {
228+
#if DEBUG
229+
if let key = self.specificKey,
230+
let value = self.specificValue {
228231
return DispatchQueue.getSpecific(key: key) == value
229232
} else {
230233
return false
231234
}
232-
}
233-
#else
234-
private var specificAssertion: Bool {
235+
#else
235236
return true
237+
#endif
236238
}
237-
#endif
238239

239240
init(connection: RTMConnection, socket: WebSocketClient) {
240241
#if DEBUG
@@ -422,14 +423,14 @@ class RTMConnection {
422423
#if DEBUG
423424
let specificKey = DispatchSpecificKey<Int>()
424425
let specificValue: Int = Int.random(in: 100...999) // whatever random int is OK.
426+
#endif
425427
private var specificAssertion: Bool {
428+
#if DEBUG
426429
return self.specificValue == DispatchQueue.getSpecific(key: self.specificKey)
427-
}
428-
#else
429-
private var specificAssertion: Bool {
430+
#else
430431
return true
432+
#endif
431433
}
432-
#endif
433434

434435
init(application: LCApplication, lcimProtocol: LCIMProtocol) throws {
435436
#if DEBUG
@@ -925,7 +926,7 @@ extension IMErrorCommand {
925926
return LCError(
926927
code: Int(self.code),
927928
reason: self.hasReason ? self.reason : nil,
928-
userInfo: userInfo)
929+
userInfo: userInfo.isEmpty ? nil : userInfo)
929930
}
930931
}
931932

@@ -947,7 +948,7 @@ extension IMSessionCommand {
947948
return LCError(
948949
code: Int(self.code),
949950
reason: self.hasReason ? self.reason : nil,
950-
userInfo: userInfo)
951+
userInfo: userInfo.isEmpty ? nil : userInfo)
951952
} else {
952953
return nil
953954
}
@@ -975,7 +976,7 @@ extension IMAckCommand {
975976
return LCError(
976977
code: Int(self.code),
977978
reason: self.hasReason ? self.reason : nil,
978-
userInfo: userInfo)
979+
userInfo: userInfo.isEmpty ? nil : userInfo)
979980
} else {
980981
return nil
981982
}

0 commit comments

Comments
 (0)