Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

clear pingData before writing new data #11

Merged
merged 2 commits into from
Jan 5, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
}
}
}
}
Loading