Skip to content

Commit

Permalink
clear pingData before writing new data (#11)
Browse files Browse the repository at this point in the history
* clear pingData before writing new data

* Add test
  • Loading branch information
adam-fowler authored Jan 5, 2025
1 parent 06e5e25 commit b15960a
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 0 deletions.
1 change: 1 addition & 0 deletions Sources/WSCore/WebSocketStateMachine.swift
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,7 @@ struct WebSocketStateMachine {
}
// creating random payload
let random = (0..<Self.pingDataSize).map { _ in UInt8.random(in: 0...255) }
state.pingData.clear()
state.pingData.writeBytes(random)
state.lastPingTime = .now
self.state = .open(state)
Expand Down
30 changes: 30 additions & 0 deletions Tests/WebSocketTests/WebSocketStateMachineTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -96,4 +96,34 @@ final class WebSocketStateMachineTests: XCTestCase {
return
}
}

// Verify ping buffer size doesnt grow
func testPingBufferSize() async throws {
var stateMachine = WebSocketStateMachine(autoPingSetup: .enabled(timePeriod: .milliseconds(1)))
var currentBuffer = ByteBuffer()
var count = 0
while true {
switch stateMachine.sendPing() {
case .sendPing(let buffer):
XCTAssertEqual(buffer.readableBytes, 16)
currentBuffer = buffer
count += 1
if count > 4 {
return
}

case .wait(let time):
try await Task.sleep(for: time)
stateMachine.receivedPong(frameData: currentBuffer)

case .closeConnection:
XCTFail("Should not timeout")
return

case .stop:
XCTFail("Should not stop")
return
}
}
}
}

0 comments on commit b15960a

Please sign in to comment.