Merge branch 'development'

* development:
  Make timeOut(after:) Take a double for finer control of timeouts
  increase timeouts for travis
  document connect data item
  Add test for namespace in connect
  Tell users what namespace was connected to
  Clean up SocketPacket methods a bit
  Use guard
This commit is contained in:
Erik 2017-07-04 11:26:53 -04:00
commit 0013e7fdb6
No known key found for this signature in database
GPG Key ID: 4930B7C5FBC1A69D
6 changed files with 105 additions and 71 deletions

View File

@ -226,11 +226,11 @@ class SocketSideEffectTest: XCTestCase {
socket.setTestStatus(.notConnected)
socket.connect(timeoutAfter: 1, withHandler: {
socket.connect(timeoutAfter: 0.5, withHandler: {
expect.fulfill()
})
waitForExpectations(timeout: 2)
waitForExpectations(timeout: 0.8)
}
func testConnectDoesNotTimeOutIfConnected() {
@ -238,22 +238,52 @@ class SocketSideEffectTest: XCTestCase {
socket.setTestStatus(.notConnected)
socket.connect(timeoutAfter: 1, withHandler: {
socket.connect(timeoutAfter: 0.5, withHandler: {
XCTFail("Should not call timeout handler if status is connected")
})
DispatchQueue.main.asyncAfter(deadline: DispatchTime.now() + 0.2) {
DispatchQueue.main.asyncAfter(deadline: DispatchTime.now() + 0.1) {
// Fake connecting
self.socket.setTestStatus(.connected)
}
DispatchQueue.main.asyncAfter(deadline: DispatchTime.now() + 1.1) {
DispatchQueue.main.asyncAfter(deadline: DispatchTime.now() + 0.7) {
expect.fulfill()
}
waitForExpectations(timeout: 2)
}
func testConnectIsCalledWithNamepsace() {
let expect = expectation(description: "The client should not call the timeout function")
let nspString = "/swift"
socket.setTestStatus(.notConnected)
socket.on(clientEvent: .connect) {data, ack in
guard let nsp = data[0] as? String else {
XCTFail("Connect should be called with a namespace")
return
}
XCTAssertEqual(nspString, nsp, "It should connect with the correct namespace")
expect.fulfill()
}
socket.connect(timeoutAfter: 0.3, withHandler: {
XCTFail("Should not call timeout handler if status is connected")
})
DispatchQueue.main.asyncAfter(deadline: DispatchTime.now() + 0.1) {
// Fake connecting
self.socket.parseEngineMessage("0/swift")
}
waitForExpectations(timeout: 2)
}
func testErrorInCustomSocketDataCallsErrorHandler() {
let expect = expectation(description: "The client should call the error handler for emit errors because of " +
"custom data")
@ -289,7 +319,7 @@ class SocketSideEffectTest: XCTestCase {
expect.fulfill()
}
socket.emitWithAck("myEvent", ThrowingData()).timingOut(after: 1, callback: {_ in
socket.emitWithAck("myEvent", ThrowingData()).timingOut(after: 0.8, callback: {_ in
XCTFail("Ack callback should not be called")
})

View File

@ -104,7 +104,7 @@ public final class OnAckCallback : NSObject {
/// - parameter after: The number of seconds before this emit times out if an ack hasn't been received.
/// - parameter callback: The callback called when an ack is received, or when a timeout happens.
/// To check for timeout, use `SocketAckStatus`'s `noAck` case.
public func timingOut(after seconds: Int, callback: @escaping AckCallback) {
public func timingOut(after seconds: Double, callback: @escaping AckCallback) {
guard let socket = self.socket, ackNumber != -1 else { return }
socket.ackHandlers.addAck(ackNumber, callback: callback)
@ -112,7 +112,7 @@ public final class OnAckCallback : NSObject {
guard seconds != 0 else { return }
socket.handleQueue.asyncAfter(deadline: DispatchTime.now() + Double(seconds)) {
socket.handleQueue.asyncAfter(deadline: DispatchTime.now() + seconds) {
socket.ackHandlers.timeoutAck(self.ackNumber, onQueue: socket.handleQueue)
}
}

View File

@ -203,12 +203,12 @@ open class SocketIOClient : NSObject, SocketIOClientSpec, SocketEngineClient, So
return OnAckCallback(ackNumber: currentAck, items: items, socket: self)
}
func didConnect() {
func didConnect(toNamespace namespace: String) {
DefaultSocketLogger.Logger.log("Socket connected", type: logType)
status = .connected
handleClientEvent(.connect, data: [])
handleClientEvent(.connect, data: [namespace])
}
func didDisconnect(reason: String) {

View File

@ -29,7 +29,7 @@ protocol SocketIOClientSpec : class {
var nsp: String { get set }
var waitingPackets: [SocketPacket] { get set }
func didConnect()
func didConnect(toNamespace namespace: String)
func didDisconnect(reason: String)
func didError(reason: String)
func handleAck(_ ack: Int, data: [Any])
@ -48,7 +48,15 @@ extension SocketIOClientSpec {
/// The set of events that are generated by the client.
public enum SocketClientEvent : String {
/// Emitted when the client connects. This is also called on a successful reconnection.
/// Emitted when the client connects. This is also called on a successful reconnection. A connect event gets one
/// data item: the namespace that was connected to.
///
/// ```swift
/// socket.on(clientEvent: .connect) {data, ack in
/// guard let nsp = data[0] as? String else { return }
/// // Some logic using the nsp
/// }
/// ```
case connect
/// Called when the socket has disconnected and will not attempt to try to reconnect.

View File

@ -87,10 +87,7 @@ struct SocketPacket {
}
private func completeMessage(_ message: String) -> String {
if data.count == 0 {
return message + "[]"
}
guard data.count != 0 else { return message + "[]" }
guard let jsonSend = try? data.toJSON(), let jsonString = String(data: jsonSend, encoding: .utf8) else {
DefaultSocketLogger.Logger.error("Error creating JSON object in SocketPacket.completeMessage",
type: SocketPacket.logType)
@ -164,10 +161,9 @@ extension SocketPacket {
static func packetFromEmit(_ items: [Any], id: Int, nsp: String, ack: Bool) -> SocketPacket {
let (parsedData, binary) = deconstructData(items)
let packet = SocketPacket(type: findType(binary.count, ack: ack), data: parsedData,
id: id, nsp: nsp, binary: binary)
return packet
return SocketPacket(type: findType(binary.count, ack: ack), data: parsedData, id: id, nsp: nsp,
binary: binary)
}
}
@ -201,6 +197,6 @@ private extension SocketPacket {
static func deconstructData(_ data: [Any]) -> ([Any], [Data]) {
var binary = [Data]()
return (data.map({shred($0, binary: &binary)}), binary)
return (data.map({ shred($0, binary: &binary) }), binary)
}
}

View File

@ -36,7 +36,7 @@ extension SocketParsable where Self: SocketIOClientSpec {
if packetNamespace == "/" && nsp != "/" {
joinNamespace(nsp)
} else {
didConnect()
didConnect(toNamespace: packetNamespace)
}
}