-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add fastwebsockets test and fix pre-handshake buffer issue
- Loading branch information
Showing
6 changed files
with
204 additions
and
16 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
use std::num::NonZeroUsize; | ||
|
||
use fastwebsockets::Frame; | ||
use fastwebsockets::OpCode; | ||
use fastwebsockets::Payload; | ||
use fastwebsockets::Role; | ||
use fastwebsockets::WebSocket; | ||
use rstest::rstest; | ||
|
||
const LARGE_PAYLOAD: [u8; 48 * 1024] = [0xff; 48 * 1024]; | ||
const SMALL_PAYLOAD: [u8; 16] = [0xff; 16]; | ||
|
||
#[rstest] | ||
#[case(false, false, false)] | ||
#[case(false, false, true)] | ||
#[case(false, true, false)] | ||
#[case(false, true, true)] | ||
#[case(true, false, false)] | ||
#[case(true, false, true)] | ||
#[case(true, true, false)] | ||
#[case(true, true, true)] | ||
#[tokio::test] | ||
async fn test_fastwebsockets( | ||
#[case] handshake: bool, | ||
#[case] buffer_limit: bool, | ||
#[case] large_payload: bool, | ||
) { | ||
let payload = if large_payload { | ||
LARGE_PAYLOAD.as_slice() | ||
} else { | ||
SMALL_PAYLOAD.as_slice() | ||
}; | ||
let (mut client, mut server) = if buffer_limit { | ||
crate::tests::tls_pair_buffer_size(Some( | ||
NonZeroUsize::try_from(1024).unwrap(), | ||
)) | ||
.await | ||
} else { | ||
crate::tests::tls_pair().await | ||
}; | ||
if handshake { | ||
client.handshake().await.expect("failed handshake"); | ||
server.handshake().await.expect("failed handshake"); | ||
} | ||
|
||
let a = tokio::spawn(async { | ||
let mut ws = WebSocket::after_handshake(server, Role::Server); | ||
ws.set_auto_close(true); | ||
for _ in 0..1000 { | ||
ws.write_frame(Frame::binary(Payload::Borrowed(payload))) | ||
.await | ||
.expect("failed to write"); | ||
} | ||
let frame = ws.read_frame().await.expect("failed to read"); | ||
assert_eq!(frame.payload.len(), payload.len()); | ||
ws.write_frame(Frame::close(1000, &[])) | ||
.await | ||
.expect("failed to close"); | ||
let frame = ws.read_frame().await.expect("failed to read"); | ||
assert_eq!(frame.opcode, OpCode::Close); | ||
}); | ||
let b = tokio::spawn(async { | ||
let mut ws = WebSocket::after_handshake(client, Role::Client); | ||
ws.set_auto_close(true); | ||
for _ in 0..1000 { | ||
let frame = ws.read_frame().await.expect("failed to read"); | ||
assert_eq!(frame.payload.len(), payload.len()); | ||
} | ||
ws.write_frame(Frame::binary(Payload::Borrowed(payload))) | ||
.await | ||
.expect("failed to write"); | ||
let frame = ws.read_frame().await.expect("failed to read"); | ||
assert_eq!(frame.opcode, OpCode::Close); | ||
}); | ||
|
||
a.await.expect("failed to join"); | ||
b.await.expect("failed to join"); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
mod fastwebsockets; |