Fix exclusive access issue

This commit is contained in:
Erik Little 2018-03-01 10:15:28 -05:00
parent 9b10cab925
commit d811b194eb
No known key found for this signature in database
GPG Key ID: 62F837E56F4E9320
3 changed files with 18 additions and 5 deletions

View File

@ -58,20 +58,20 @@ private struct SocketAck : Hashable {
}
}
struct SocketAckManager {
class SocketAckManager {
private var acks = Set<SocketAck>(minimumCapacity: 1)
mutating func addAck(_ ack: Int, callback: @escaping AckCallback) {
func addAck(_ ack: Int, callback: @escaping AckCallback) {
acks.insert(SocketAck(ack: ack, callback: callback))
}
/// Should be called on handle queue
mutating func executeAck(_ ack: Int, with items: [Any]) {
func executeAck(_ ack: Int, with items: [Any]) {
acks.remove(SocketAck(ack: ack))?.callback(items)
}
/// Should be called on handle queue
mutating func timeoutAck(_ ack: Int) {
func timeoutAck(_ ack: Int) {
acks.remove(SocketAck(ack: ack))?.callback?([SocketAckStatus.noAck.rawValue])
}
}

View File

@ -75,7 +75,7 @@ open class SocketIOClient : NSObject, SocketIOClientSpec {
}
}
var ackHandlers = SocketAckManager()
let ackHandlers = SocketAckManager()
private(set) var currentAck = -1

View File

@ -38,6 +38,19 @@ class SocketSideEffectTest: XCTestCase {
waitForExpectations(timeout: 3, handler: nil)
}
func testHandleAckWithAckEmit() {
let expect = expectation(description: "handled ack")
socket.emitWithAck("test").timingOut(after: 0) {data in
XCTAssertEqual(data[0] as? String, "hello world")
self.socket.emitWithAck("test").timingOut(after: 0) {data in}
expect.fulfill()
}
manager.parseEngineMessage("30[\"hello world\"]")
waitForExpectations(timeout: 3, handler: nil)
}
func testHandleAck2() {
let expect = expectation(description: "handled ack2")
socket.emitWithAck("test").timingOut(after: 0) {data in