Skip to content

Commit

Permalink
Merge pull request #33 from grahamburgsma/websocket-retain-cycle
Browse files Browse the repository at this point in the history
Fixed websocket reference cycle
  • Loading branch information
tanner0101 authored Mar 26, 2019
2 parents 21eb477 + bb2b082 commit d85e5b6
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 3 deletions.
6 changes: 3 additions & 3 deletions Sources/WebSocket/WebSocket.swift
Original file line number Diff line number Diff line change
Expand Up @@ -83,21 +83,21 @@ public final class WebSocket: BasicWorker {
self.onErrorCallback = { _, _ in }
self.onCloseCodeCallback = { _ in }

self.onTextCallback = { _, text in
self.onTextCallback = { [unowned self] _, text in
guard self.textBuffer != nil else {
self.textBuffer = [text]
return
}
self.textBuffer?.append(text)
}
self.onBinaryCallback = { _, binary in
self.onBinaryCallback = { [unowned self] _, binary in
guard self.binaryBuffer != nil else {
self.binaryBuffer = [binary]
return
}
self.binaryBuffer?.append(binary)
}
self.onErrorCallback = { _, error in
self.onErrorCallback = { [unowned self] _, error in
guard self.errorBuffer != nil else {
self.errorBuffer = [error]
return
Expand Down
11 changes: 11 additions & 0 deletions Tests/WebSocketTests/WebSocketTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -112,11 +112,22 @@ class WebSocketTests: XCTestCase {
try server.onClose.wait()
}

func testDeallocation() throws {
let worker = MultiThreadedEventLoopGroup(numberOfThreads: 1)

weak var ws = try HTTPClient.webSocket(hostname: "echo.websocket.org", on: worker).wait()
ws?.close()
try ws?.onClose.wait()

XCTAssertNil(ws, "Websocket not deallocated")
}

static let allTests = [
("testClient", testClient),
("testClientTLS", testClientTLS),
("testServer", testServer),
("testServerContinuation", testServerContinuation),
("testDeallocation", testDeallocation),
]
}

Expand Down

0 comments on commit d85e5b6

Please sign in to comment.