Skip to content

Commit 0013e7f

Browse files
committed
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
2 parents 630ef27 + 49961eb commit 0013e7f

6 files changed

+105
-71
lines changed

Diff for: SocketIO-MacTests/SocketSideEffectTest.swift

+36-6
Original file line numberDiff line numberDiff line change
@@ -226,31 +226,61 @@ class SocketSideEffectTest: XCTestCase {
226226

227227
socket.setTestStatus(.notConnected)
228228

229-
socket.connect(timeoutAfter: 1, withHandler: {
229+
socket.connect(timeoutAfter: 0.5, withHandler: {
230230
expect.fulfill()
231231
})
232232

233-
waitForExpectations(timeout: 2)
233+
waitForExpectations(timeout: 0.8)
234234
}
235235

236236
func testConnectDoesNotTimeOutIfConnected() {
237237
let expect = expectation(description: "The client should not call the timeout function")
238238

239239
socket.setTestStatus(.notConnected)
240240

241-
socket.connect(timeoutAfter: 1, withHandler: {
241+
socket.connect(timeoutAfter: 0.5, withHandler: {
242242
XCTFail("Should not call timeout handler if status is connected")
243243
})
244244

245-
DispatchQueue.main.asyncAfter(deadline: DispatchTime.now() + 0.2) {
245+
DispatchQueue.main.asyncAfter(deadline: DispatchTime.now() + 0.1) {
246246
// Fake connecting
247247
self.socket.setTestStatus(.connected)
248248
}
249249

250-
DispatchQueue.main.asyncAfter(deadline: DispatchTime.now() + 1.1) {
250+
DispatchQueue.main.asyncAfter(deadline: DispatchTime.now() + 0.7) {
251+
expect.fulfill()
252+
}
253+
254+
waitForExpectations(timeout: 2)
255+
}
256+
257+
func testConnectIsCalledWithNamepsace() {
258+
let expect = expectation(description: "The client should not call the timeout function")
259+
let nspString = "/swift"
260+
261+
socket.setTestStatus(.notConnected)
262+
263+
socket.on(clientEvent: .connect) {data, ack in
264+
guard let nsp = data[0] as? String else {
265+
XCTFail("Connect should be called with a namespace")
266+
267+
return
268+
}
269+
270+
XCTAssertEqual(nspString, nsp, "It should connect with the correct namespace")
271+
251272
expect.fulfill()
252273
}
253274

275+
socket.connect(timeoutAfter: 0.3, withHandler: {
276+
XCTFail("Should not call timeout handler if status is connected")
277+
})
278+
279+
DispatchQueue.main.asyncAfter(deadline: DispatchTime.now() + 0.1) {
280+
// Fake connecting
281+
self.socket.parseEngineMessage("0/swift")
282+
}
283+
254284
waitForExpectations(timeout: 2)
255285
}
256286

@@ -289,7 +319,7 @@ class SocketSideEffectTest: XCTestCase {
289319
expect.fulfill()
290320
}
291321

292-
socket.emitWithAck("myEvent", ThrowingData()).timingOut(after: 1, callback: {_ in
322+
socket.emitWithAck("myEvent", ThrowingData()).timingOut(after: 0.8, callback: {_ in
293323
XCTFail("Ack callback should not be called")
294324
})
295325

Diff for: Source/SocketAckEmitter.swift

+2-2
Original file line numberDiff line numberDiff line change
@@ -104,15 +104,15 @@ public final class OnAckCallback : NSObject {
104104
/// - parameter after: The number of seconds before this emit times out if an ack hasn't been received.
105105
/// - parameter callback: The callback called when an ack is received, or when a timeout happens.
106106
/// To check for timeout, use `SocketAckStatus`'s `noAck` case.
107-
public func timingOut(after seconds: Int, callback: @escaping AckCallback) {
107+
public func timingOut(after seconds: Double, callback: @escaping AckCallback) {
108108
guard let socket = self.socket, ackNumber != -1 else { return }
109109

110110
socket.ackHandlers.addAck(ackNumber, callback: callback)
111111
socket._emit(items, ack: ackNumber)
112112

113113
guard seconds != 0 else { return }
114114

115-
socket.handleQueue.asyncAfter(deadline: DispatchTime.now() + Double(seconds)) {
115+
socket.handleQueue.asyncAfter(deadline: DispatchTime.now() + seconds) {
116116
socket.ackHandlers.timeoutAck(self.ackNumber, onQueue: socket.handleQueue)
117117
}
118118
}

Diff for: Source/SocketIOClient.swift

+2-2
Original file line numberDiff line numberDiff line change
@@ -203,12 +203,12 @@ open class SocketIOClient : NSObject, SocketIOClientSpec, SocketEngineClient, So
203203
return OnAckCallback(ackNumber: currentAck, items: items, socket: self)
204204
}
205205

206-
func didConnect() {
206+
func didConnect(toNamespace namespace: String) {
207207
DefaultSocketLogger.Logger.log("Socket connected", type: logType)
208208

209209
status = .connected
210210

211-
handleClientEvent(.connect, data: [])
211+
handleClientEvent(.connect, data: [namespace])
212212
}
213213

214214
func didDisconnect(reason: String) {

Diff for: Source/SocketIOClientSpec.swift

+10-2
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ protocol SocketIOClientSpec : class {
2929
var nsp: String { get set }
3030
var waitingPackets: [SocketPacket] { get set }
3131

32-
func didConnect()
32+
func didConnect(toNamespace namespace: String)
3333
func didDisconnect(reason: String)
3434
func didError(reason: String)
3535
func handleAck(_ ack: Int, data: [Any])
@@ -48,7 +48,15 @@ extension SocketIOClientSpec {
4848

4949
/// The set of events that are generated by the client.
5050
public enum SocketClientEvent : String {
51-
/// Emitted when the client connects. This is also called on a successful reconnection.
51+
/// Emitted when the client connects. This is also called on a successful reconnection. A connect event gets one
52+
/// data item: the namespace that was connected to.
53+
///
54+
/// ```swift
55+
/// socket.on(clientEvent: .connect) {data, ack in
56+
/// guard let nsp = data[0] as? String else { return }
57+
/// // Some logic using the nsp
58+
/// }
59+
/// ```
5260
case connect
5361

5462
/// Called when the socket has disconnected and will not attempt to try to reconnect.

Diff for: Source/SocketPacket.swift

+31-35
Original file line numberDiff line numberDiff line change
@@ -29,15 +29,15 @@ struct SocketPacket {
2929
enum PacketType: Int {
3030
case connect, disconnect, event, ack, error, binaryEvent, binaryAck
3131
}
32-
32+
3333
private let placeholders: Int
34-
34+
3535
private static let logType = "SocketPacket"
3636

3737
let nsp: String
3838
let id: Int
3939
let type: PacketType
40-
40+
4141
var binary: [Data]
4242
var data: [Any]
4343
var args: [Any] {
@@ -47,20 +47,20 @@ struct SocketPacket {
4747
return data
4848
}
4949
}
50-
50+
5151
var description: String {
5252
return "SocketPacket {type: \(String(type.rawValue)); data: " +
5353
"\(String(describing: data)); id: \(id); placeholders: \(placeholders); nsp: \(nsp)}"
5454
}
55-
55+
5656
var event: String {
5757
return String(describing: data[0])
5858
}
59-
59+
6060
var packetString: String {
6161
return createPacketString()
6262
}
63-
63+
6464
init(type: PacketType, data: [Any] = [Any](), id: Int = -1, nsp: String, placeholders: Int = 0,
6565
binary: [Data] = [Data]()) {
6666
self.data = data
@@ -70,37 +70,34 @@ struct SocketPacket {
7070
self.placeholders = placeholders
7171
self.binary = binary
7272
}
73-
73+
7474
mutating func addData(_ data: Data) -> Bool {
7575
if placeholders == binary.count {
7676
return true
7777
}
78-
78+
7979
binary.append(data)
80-
80+
8181
if placeholders == binary.count {
8282
fillInPlaceholders()
8383
return true
8484
} else {
8585
return false
8686
}
8787
}
88-
88+
8989
private func completeMessage(_ message: String) -> String {
90-
if data.count == 0 {
91-
return message + "[]"
92-
}
93-
90+
guard data.count != 0 else { return message + "[]" }
9491
guard let jsonSend = try? data.toJSON(), let jsonString = String(data: jsonSend, encoding: .utf8) else {
9592
DefaultSocketLogger.Logger.error("Error creating JSON object in SocketPacket.completeMessage",
9693
type: SocketPacket.logType)
97-
94+
9895
return message + "[]"
9996
}
100-
97+
10198
return message + jsonString
10299
}
103-
100+
104101
private func createPacketString() -> String {
105102
let typeString = String(type.rawValue)
106103
// Binary count?
@@ -109,17 +106,17 @@ struct SocketPacket {
109106
let nspString = binaryCountString + (nsp != "/" ? "\(nsp)," : "")
110107
// Ack number?
111108
let idString = nspString + (id != -1 ? String(id) : "")
112-
109+
113110
return completeMessage(idString)
114111
}
115-
112+
116113
// Called when we have all the binary data for a packet
117114
// calls _fillInPlaceholders, which replaces placeholders with the
118115
// corresponding binary
119116
private mutating func fillInPlaceholders() {
120117
data = data.map(_fillInPlaceholders)
121118
}
122-
119+
123120
// Helper method that looks for placeholders
124121
// If object is a collection it will recurse
125122
// Returns the object if it is not a placeholder or the corresponding
@@ -132,9 +129,9 @@ struct SocketPacket {
132129
} else {
133130
return dict.reduce(JSON(), {cur, keyValue in
134131
var cur = cur
135-
132+
136133
cur[keyValue.0] = _fillInPlaceholders(keyValue.1)
137-
134+
138135
return cur
139136
})
140137
}
@@ -161,46 +158,45 @@ extension SocketPacket {
161158
return .error
162159
}
163160
}
164-
161+
165162
static func packetFromEmit(_ items: [Any], id: Int, nsp: String, ack: Bool) -> SocketPacket {
166163
let (parsedData, binary) = deconstructData(items)
167-
let packet = SocketPacket(type: findType(binary.count, ack: ack), data: parsedData,
168-
id: id, nsp: nsp, binary: binary)
169-
170-
return packet
164+
165+
return SocketPacket(type: findType(binary.count, ack: ack), data: parsedData, id: id, nsp: nsp,
166+
binary: binary)
171167
}
172168
}
173169

174170
private extension SocketPacket {
175171
// Recursive function that looks for NSData in collections
176172
static func shred(_ data: Any, binary: inout [Data]) -> Any {
177173
let placeholder = ["_placeholder": true, "num": binary.count] as JSON
178-
174+
179175
switch data {
180176
case let bin as Data:
181177
binary.append(bin)
182-
178+
183179
return placeholder
184180
case let arr as [Any]:
185181
return arr.map({shred($0, binary: &binary)})
186182
case let dict as JSON:
187183
return dict.reduce(JSON(), {cur, keyValue in
188184
var mutCur = cur
189-
185+
190186
mutCur[keyValue.0] = shred(keyValue.1, binary: &binary)
191-
187+
192188
return mutCur
193189
})
194190
default:
195191
return data
196192
}
197193
}
198-
194+
199195
// Removes binary data from emit data
200196
// Returns a type containing the de-binaryed data and the binary
201197
static func deconstructData(_ data: [Any]) -> ([Any], [Data]) {
202198
var binary = [Data]()
203-
204-
return (data.map({shred($0, binary: &binary)}), binary)
199+
200+
return (data.map({ shred($0, binary: &binary) }), binary)
205201
}
206202
}

0 commit comments

Comments
 (0)