diff options
| author | salmonik <salmonik@tuta.io> | 2026-10-28 22:38:54 +0100 |
|---|---|---|
| committer | salmonik <salmonik@tuta.io> | 2026-10-28 22:38:54 +0100 |
| commit | ef2e13df274aaf0f2e50c03c63db33d65d9a0275 (patch) | |
| tree | 991fa12e70ec11e84e8f6c6e7bc0a60b0a150759 /tests | |
| parent | v0.2.5: fix terminal width, ping timing and output formatting (diff) | |
| download | zagan-bet-ef2e13df274aaf0f2e50c03c63db33d65d9a0275.tar.gz zagan-bet-ef2e13df274aaf0f2e50c03c63db33d65d9a0275.zip | |
v0.2.6: preserve buffered lines and add request read timeouts
Diffstat (limited to 'tests')
| -rw-r--r-- | tests/protocol.rs | 78 |
1 files changed, 78 insertions, 0 deletions
diff --git a/tests/protocol.rs b/tests/protocol.rs new file mode 100644 index 0000000..2962574 --- /dev/null +++ b/tests/protocol.rs | |||
| @@ -0,0 +1,78 @@ | |||
| 1 | use std::io::{Cursor, Error, ErrorKind}; | ||
| 2 | |||
| 3 | use zagan_bet::protocol::{io_failure_message, LineBuffer}; | ||
| 4 | |||
| 5 | #[test] | ||
| 6 | fn keeps_data_after_the_first_newline() { | ||
| 7 | let mut buffer = LineBuffer::new(); | ||
| 8 | buffer.push(b"PONG\nGAME_RESULT session 60 10 990 OSS two\n"); | ||
| 9 | |||
| 10 | assert_eq!(buffer.next_line().as_deref(), Some("PONG")); | ||
| 11 | assert_eq!( | ||
| 12 | buffer.next_line().as_deref(), | ||
| 13 | Some("GAME_RESULT session 60 10 990 OSS two") | ||
| 14 | ); | ||
| 15 | assert_eq!(buffer.next_line(), None); | ||
| 16 | } | ||
| 17 | |||
| 18 | #[test] | ||
| 19 | fn handles_crlf_and_empty_lines() { | ||
| 20 | let mut buffer = LineBuffer::new(); | ||
| 21 | buffer.push(b"PONG\r\n\r\n"); | ||
| 22 | |||
| 23 | assert_eq!(buffer.next_line().as_deref(), Some("PONG")); | ||
| 24 | assert_eq!(buffer.next_line().as_deref(), Some("")); | ||
| 25 | assert_eq!(buffer.next_line(), None); | ||
| 26 | } | ||
| 27 | |||
| 28 | #[test] | ||
| 29 | fn waits_for_a_split_line() { | ||
| 30 | let mut buffer = LineBuffer::new(); | ||
| 31 | buffer.push(b"PO"); | ||
| 32 | assert_eq!(buffer.next_line(), None); | ||
| 33 | |||
| 34 | buffer.push(b"NG\n"); | ||
| 35 | assert_eq!(buffer.next_line().as_deref(), Some("PONG")); | ||
| 36 | } | ||
| 37 | |||
| 38 | #[test] | ||
| 39 | fn reads_multiple_lines_from_a_reader() { | ||
| 40 | let mut buffer = LineBuffer::new(); | ||
| 41 | let mut reader = Cursor::new(b"one\ntwo\n".to_vec()); | ||
| 42 | |||
| 43 | assert_eq!(buffer.read_line(&mut reader).unwrap().as_deref(), Some("one")); | ||
| 44 | assert_eq!(buffer.read_line(&mut reader).unwrap().as_deref(), Some("two")); | ||
| 45 | assert_eq!(buffer.read_line(&mut reader).unwrap(), None); | ||
| 46 | } | ||
| 47 | |||
| 48 | #[test] | ||
| 49 | fn returns_a_partial_line_at_eof() { | ||
| 50 | let mut buffer = LineBuffer::new(); | ||
| 51 | let mut reader = Cursor::new(b"partial".to_vec()); | ||
| 52 | |||
| 53 | assert_eq!( | ||
| 54 | buffer.read_line(&mut reader).unwrap().as_deref(), | ||
| 55 | Some("partial") | ||
| 56 | ); | ||
| 57 | assert_eq!(buffer.read_line(&mut reader).unwrap(), None); | ||
| 58 | } | ||
| 59 | |||
| 60 | #[test] | ||
| 61 | fn timeout_errors_get_a_timeout_message() { | ||
| 62 | assert_eq!( | ||
| 63 | io_failure_message(&Error::from(ErrorKind::TimedOut)), | ||
| 64 | "ERROR Request timed out" | ||
| 65 | ); | ||
| 66 | assert_eq!( | ||
| 67 | io_failure_message(&Error::from(ErrorKind::WouldBlock)), | ||
| 68 | "ERROR Request timed out" | ||
| 69 | ); | ||
| 70 | assert_eq!( | ||
| 71 | io_failure_message(&Error::from(ErrorKind::UnexpectedEof)), | ||
| 72 | "ERROR Connection lost" | ||
| 73 | ); | ||
| 74 | assert_eq!( | ||
| 75 | io_failure_message(&Error::from(ErrorKind::ConnectionReset)), | ||
| 76 | "ERROR Connection lost" | ||
| 77 | ); | ||
| 78 | } | ||
