【发布时间】:2017-06-30 15:29:32
【问题描述】:
我们正在尝试获取 XMPPFramework 运行的最小示例。
考虑这个简单的类:
import Foundation
import XMPPFramework
class Connection: NSObject, XMPPStreamDelegate {
let stream: XMPPStream
override init() {
self.stream = XMPPStream()!
}
func connectToServer(timeout: TimeInterval) {
stream.addDelegate(self, delegateQueue: DispatchQueue.main)
stream.myJID = XMPPJID(string: "myuser")
stream.hostName = "myserver.tld"
stream.hostPort = 5222
do {
try stream.connect(withTimeout: timeout)
}
catch {
print(error)
}
}
func xmppStreamWillConnect(_ sender: XMPPStream!) {
print("will connect")
}
func xmppStreamDidConnect(_ sender: XMPPStream!) {
print("did connect")
self.stream.disconnect()
}
}
还有这个简单的测试类:
import Foundation
import XCTest
@testable import MyModule
class ConnectionTests: XCTestCase {
func testConnect() {
let connection = Connection()
print("Try to connect")
let expectation = self.expectation(description: "connect")
connection.connectToServer(timeout: 3)
self.waitForExpectations(timeout: 5)
}
}
我期望这个输出:
Try to connect
will connect
did connect
<5s timeout since I don't fulfill>
如果我的 XMPP 服务器不小心对我的请求做出了积极响应,我希望看到:
Try to connect
will connect
<some error message>
如果 XMPP 服务器没有(快速)回复,我希望:
Try to connect
will connect
<3s timeout reached>
但是,我没有得到这些,而是:
Try to connect
will connect
<5s timeout since I don't fulfill>
发生了什么事?
这是我收集的。
- XCTest 等待
DispatchQueue.main。 - 我们可以控制代表的运行位置;使用
DispatchQueue.main或DispatchQueue(label: "test", qos: .userInitiated, attributes: .concurrent)似乎并不重要。 - 我可以在
XMPPStream.connectWithTimeout中跟踪直到dispatch_sync(xmppQueue, block);的执行,其中xmppQueue是一个带有标签xmpp的新队列;该块似乎也已完全执行。
所以我不明白谁在这里阻止了谁,以及如何防止它发生。
【问题讨论】:
-
不进行身份验证连接的目的是什么?连接后需要登录,否则会关闭连接。
-
由于
xmppStreamDidConnect没有被调用,我们甚至没有走到那一步。
标签: swift unit-testing concurrency xctest xmppframework