Allen George | 8b96bfb | 2016-11-02 08:01:08 -0400 | [diff] [blame] | 1 | // Licensed to the Apache Software Foundation (ASF) under one |
| 2 | // or more contributor license agreements. See the NOTICE file |
| 3 | // distributed with this work for additional information |
| 4 | // regarding copyright ownership. The ASF licenses this file |
| 5 | // to you under the Apache License, Version 2.0 (the |
| 6 | // "License"); you may not use this file except in compliance |
| 7 | // with the License. You may obtain a copy of the License at |
| 8 | // |
| 9 | // http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | // |
| 11 | // Unless required by applicable law or agreed to in writing, |
| 12 | // software distributed under the License is distributed on an |
| 13 | // "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 14 | // KIND, either express or implied. See the License for the |
| 15 | // specific language governing permissions and limitations |
| 16 | // under the License. |
| 17 | |
| 18 | use byteorder::{BigEndian, ReadBytesExt, WriteBytesExt}; |
Allen George | 8b96bfb | 2016-11-02 08:01:08 -0400 | [diff] [blame] | 19 | use std::cmp; |
| 20 | use std::io; |
Allen George | b7084cb | 2017-12-13 07:34:49 -0500 | [diff] [blame] | 21 | use std::io::{Read, Write}; |
Allen George | 8b96bfb | 2016-11-02 08:01:08 -0400 | [diff] [blame] | 22 | |
Allen George | 0e22c36 | 2017-01-30 07:15:00 -0500 | [diff] [blame] | 23 | use super::{TReadTransport, TReadTransportFactory, TWriteTransport, TWriteTransportFactory}; |
Hasnain Lakhani | 42d0b71 | 2025-07-17 19:57:05 -0700 | [diff] [blame^] | 24 | use crate::TConfiguration; |
Allen George | 8b96bfb | 2016-11-02 08:01:08 -0400 | [diff] [blame] | 25 | |
| 26 | /// Default capacity of the read buffer in bytes. |
Allen George | 0e22c36 | 2017-01-30 07:15:00 -0500 | [diff] [blame] | 27 | const READ_CAPACITY: usize = 4096; |
Allen George | 8b96bfb | 2016-11-02 08:01:08 -0400 | [diff] [blame] | 28 | |
Allen George | 0e22c36 | 2017-01-30 07:15:00 -0500 | [diff] [blame] | 29 | /// Default capacity of the write buffer in bytes. |
| 30 | const WRITE_CAPACITY: usize = 4096; |
Allen George | 8b96bfb | 2016-11-02 08:01:08 -0400 | [diff] [blame] | 31 | |
Allen George | 0e22c36 | 2017-01-30 07:15:00 -0500 | [diff] [blame] | 32 | /// Transport that reads framed messages. |
Allen George | 8b96bfb | 2016-11-02 08:01:08 -0400 | [diff] [blame] | 33 | /// |
Allen George | 0e22c36 | 2017-01-30 07:15:00 -0500 | [diff] [blame] | 34 | /// A `TFramedReadTransport` maintains a fixed-size internal read buffer. |
| 35 | /// On a call to `TFramedReadTransport::read(...)` one full message - both |
| 36 | /// fixed-length header and bytes - is read from the wrapped channel and |
| 37 | /// buffered. Subsequent read calls are serviced from the internal buffer |
| 38 | /// until it is exhausted, at which point the next full message is read |
| 39 | /// from the wrapped channel. |
Allen George | 8b96bfb | 2016-11-02 08:01:08 -0400 | [diff] [blame] | 40 | /// |
| 41 | /// # Examples |
| 42 | /// |
Allen George | 0e22c36 | 2017-01-30 07:15:00 -0500 | [diff] [blame] | 43 | /// Create and use a `TFramedReadTransport`. |
Allen George | 8b96bfb | 2016-11-02 08:01:08 -0400 | [diff] [blame] | 44 | /// |
| 45 | /// ```no_run |
Allen George | 0e22c36 | 2017-01-30 07:15:00 -0500 | [diff] [blame] | 46 | /// use std::io::Read; |
| 47 | /// use thrift::transport::{TFramedReadTransport, TTcpChannel}; |
Allen George | 8b96bfb | 2016-11-02 08:01:08 -0400 | [diff] [blame] | 48 | /// |
Allen George | 0e22c36 | 2017-01-30 07:15:00 -0500 | [diff] [blame] | 49 | /// let mut c = TTcpChannel::new(); |
| 50 | /// c.open("localhost:9090").unwrap(); |
Allen George | 8b96bfb | 2016-11-02 08:01:08 -0400 | [diff] [blame] | 51 | /// |
Allen George | 0e22c36 | 2017-01-30 07:15:00 -0500 | [diff] [blame] | 52 | /// let mut t = TFramedReadTransport::new(c); |
Allen George | 8b96bfb | 2016-11-02 08:01:08 -0400 | [diff] [blame] | 53 | /// |
Allen George | 8b96bfb | 2016-11-02 08:01:08 -0400 | [diff] [blame] | 54 | /// t.read(&mut vec![0u8; 1]).unwrap(); |
Allen George | 8b96bfb | 2016-11-02 08:01:08 -0400 | [diff] [blame] | 55 | /// ``` |
Allen George | 0e22c36 | 2017-01-30 07:15:00 -0500 | [diff] [blame] | 56 | #[derive(Debug)] |
| 57 | pub struct TFramedReadTransport<C> |
| 58 | where |
| 59 | C: Read, |
| 60 | { |
Allen George | b7084cb | 2017-12-13 07:34:49 -0500 | [diff] [blame] | 61 | buf: Vec<u8>, |
Allen George | 0e22c36 | 2017-01-30 07:15:00 -0500 | [diff] [blame] | 62 | pos: usize, |
| 63 | cap: usize, |
| 64 | chan: C, |
Hasnain Lakhani | 42d0b71 | 2025-07-17 19:57:05 -0700 | [diff] [blame^] | 65 | config: TConfiguration, |
Allen George | 8b96bfb | 2016-11-02 08:01:08 -0400 | [diff] [blame] | 66 | } |
| 67 | |
Allen George | 0e22c36 | 2017-01-30 07:15:00 -0500 | [diff] [blame] | 68 | impl<C> TFramedReadTransport<C> |
| 69 | where |
| 70 | C: Read, |
| 71 | { |
Allen George | b7084cb | 2017-12-13 07:34:49 -0500 | [diff] [blame] | 72 | /// Create a `TFramedReadTransport` with a default-sized |
| 73 | /// internal read buffer that wraps the given `TIoChannel`. |
Allen George | 0e22c36 | 2017-01-30 07:15:00 -0500 | [diff] [blame] | 74 | pub fn new(channel: C) -> TFramedReadTransport<C> { |
| 75 | TFramedReadTransport::with_capacity(READ_CAPACITY, channel) |
Allen George | 8b96bfb | 2016-11-02 08:01:08 -0400 | [diff] [blame] | 76 | } |
| 77 | |
Allen George | b7084cb | 2017-12-13 07:34:49 -0500 | [diff] [blame] | 78 | /// Create a `TFramedTransport` with an internal read buffer |
| 79 | /// of size `read_capacity` that wraps the given `TIoChannel`. |
Allen George | 0e22c36 | 2017-01-30 07:15:00 -0500 | [diff] [blame] | 80 | pub fn with_capacity(read_capacity: usize, channel: C) -> TFramedReadTransport<C> { |
| 81 | TFramedReadTransport { |
Allen George | b7084cb | 2017-12-13 07:34:49 -0500 | [diff] [blame] | 82 | buf: vec![0; read_capacity], // FIXME: do I actually have to do this? |
Allen George | 0e22c36 | 2017-01-30 07:15:00 -0500 | [diff] [blame] | 83 | pos: 0, |
| 84 | cap: 0, |
| 85 | chan: channel, |
Hasnain Lakhani | 42d0b71 | 2025-07-17 19:57:05 -0700 | [diff] [blame^] | 86 | config: TConfiguration::default(), |
Allen George | 8b96bfb | 2016-11-02 08:01:08 -0400 | [diff] [blame] | 87 | } |
| 88 | } |
| 89 | } |
| 90 | |
Allen George | 0e22c36 | 2017-01-30 07:15:00 -0500 | [diff] [blame] | 91 | impl<C> Read for TFramedReadTransport<C> |
| 92 | where |
| 93 | C: Read, |
| 94 | { |
Allen George | 8b96bfb | 2016-11-02 08:01:08 -0400 | [diff] [blame] | 95 | fn read(&mut self, b: &mut [u8]) -> io::Result<usize> { |
Allen George | 0e22c36 | 2017-01-30 07:15:00 -0500 | [diff] [blame] | 96 | if self.cap - self.pos == 0 { |
Hasnain Lakhani | 42d0b71 | 2025-07-17 19:57:05 -0700 | [diff] [blame^] | 97 | let frame_size_bytes = self.chan.read_i32::<BigEndian>()?; |
| 98 | |
| 99 | if frame_size_bytes < 0 { |
| 100 | return Err(io::Error::new( |
| 101 | io::ErrorKind::InvalidData, |
| 102 | format!("Negative frame size: {}", frame_size_bytes), |
| 103 | )); |
| 104 | } |
| 105 | |
| 106 | let message_size = frame_size_bytes as usize; |
| 107 | |
| 108 | if let Some(max_frame) = self.config.max_frame_size() { |
| 109 | if message_size > max_frame { |
| 110 | return Err(io::Error::new( |
| 111 | io::ErrorKind::InvalidData, |
| 112 | format!( |
| 113 | "Frame size {} exceeds maximum allowed size of {}", |
| 114 | message_size, max_frame |
| 115 | ), |
| 116 | )); |
| 117 | } |
| 118 | } |
Allen George | b7084cb | 2017-12-13 07:34:49 -0500 | [diff] [blame] | 119 | |
| 120 | let buf_capacity = cmp::max(message_size, READ_CAPACITY); |
| 121 | self.buf.resize(buf_capacity, 0); |
| 122 | |
Allen George | 0e22c36 | 2017-01-30 07:15:00 -0500 | [diff] [blame] | 123 | self.chan.read_exact(&mut self.buf[..message_size])?; |
Cameron Martin | da54fc8 | 2025-01-12 08:55:45 +0000 | [diff] [blame] | 124 | self.cap = message_size; |
Allen George | b7084cb | 2017-12-13 07:34:49 -0500 | [diff] [blame] | 125 | self.pos = 0; |
Allen George | 8b96bfb | 2016-11-02 08:01:08 -0400 | [diff] [blame] | 126 | } |
| 127 | |
Allen George | 0e22c36 | 2017-01-30 07:15:00 -0500 | [diff] [blame] | 128 | let nread = cmp::min(b.len(), self.cap - self.pos); |
| 129 | b[..nread].clone_from_slice(&self.buf[self.pos..self.pos + nread]); |
| 130 | self.pos += nread; |
Allen George | 8b96bfb | 2016-11-02 08:01:08 -0400 | [diff] [blame] | 131 | |
| 132 | Ok(nread) |
| 133 | } |
| 134 | } |
| 135 | |
Allen George | 0e22c36 | 2017-01-30 07:15:00 -0500 | [diff] [blame] | 136 | /// Factory for creating instances of `TFramedReadTransport`. |
| 137 | #[derive(Default)] |
| 138 | pub struct TFramedReadTransportFactory; |
| 139 | |
| 140 | impl TFramedReadTransportFactory { |
| 141 | pub fn new() -> TFramedReadTransportFactory { |
| 142 | TFramedReadTransportFactory {} |
| 143 | } |
| 144 | } |
| 145 | |
| 146 | impl TReadTransportFactory for TFramedReadTransportFactory { |
| 147 | /// Create a `TFramedReadTransport`. |
Danny Browning | 77d96c1 | 2019-08-21 13:41:07 -0600 | [diff] [blame] | 148 | fn create(&self, channel: Box<dyn Read + Send>) -> Box<dyn TReadTransport + Send> { |
Allen George | 0e22c36 | 2017-01-30 07:15:00 -0500 | [diff] [blame] | 149 | Box::new(TFramedReadTransport::new(channel)) |
| 150 | } |
| 151 | } |
Allen George | 0e22c36 | 2017-01-30 07:15:00 -0500 | [diff] [blame] | 152 | /// Transport that writes framed messages. |
| 153 | /// |
| 154 | /// A `TFramedWriteTransport` maintains a fixed-size internal write buffer. All |
| 155 | /// writes are made to this buffer and are sent to the wrapped channel only |
| 156 | /// when `TFramedWriteTransport::flush()` is called. On a flush a fixed-length |
| 157 | /// header with a count of the buffered bytes is written, followed by the bytes |
| 158 | /// themselves. |
| 159 | /// |
| 160 | /// # Examples |
| 161 | /// |
| 162 | /// Create and use a `TFramedWriteTransport`. |
| 163 | /// |
| 164 | /// ```no_run |
| 165 | /// use std::io::Write; |
| 166 | /// use thrift::transport::{TFramedWriteTransport, TTcpChannel}; |
| 167 | /// |
| 168 | /// let mut c = TTcpChannel::new(); |
| 169 | /// c.open("localhost:9090").unwrap(); |
| 170 | /// |
| 171 | /// let mut t = TFramedWriteTransport::new(c); |
| 172 | /// |
| 173 | /// t.write(&[0x00]).unwrap(); |
| 174 | /// t.flush().unwrap(); |
| 175 | /// ``` |
| 176 | #[derive(Debug)] |
| 177 | pub struct TFramedWriteTransport<C> |
| 178 | where |
| 179 | C: Write, |
| 180 | { |
Allen George | b7084cb | 2017-12-13 07:34:49 -0500 | [diff] [blame] | 181 | buf: Vec<u8>, |
Allen George | 0e22c36 | 2017-01-30 07:15:00 -0500 | [diff] [blame] | 182 | channel: C, |
| 183 | } |
| 184 | |
| 185 | impl<C> TFramedWriteTransport<C> |
| 186 | where |
| 187 | C: Write, |
| 188 | { |
Allen George | b7084cb | 2017-12-13 07:34:49 -0500 | [diff] [blame] | 189 | /// Create a `TFramedWriteTransport` with default-sized internal |
| 190 | /// write buffer that wraps the given `TIoChannel`. |
Allen George | 0e22c36 | 2017-01-30 07:15:00 -0500 | [diff] [blame] | 191 | pub fn new(channel: C) -> TFramedWriteTransport<C> { |
| 192 | TFramedWriteTransport::with_capacity(WRITE_CAPACITY, channel) |
| 193 | } |
| 194 | |
Allen George | b7084cb | 2017-12-13 07:34:49 -0500 | [diff] [blame] | 195 | /// Create a `TFramedWriteTransport` with an internal write buffer |
| 196 | /// of size `write_capacity` that wraps the given `TIoChannel`. |
Allen George | 0e22c36 | 2017-01-30 07:15:00 -0500 | [diff] [blame] | 197 | pub fn with_capacity(write_capacity: usize, channel: C) -> TFramedWriteTransport<C> { |
| 198 | TFramedWriteTransport { |
Allen George | b7084cb | 2017-12-13 07:34:49 -0500 | [diff] [blame] | 199 | buf: Vec::with_capacity(write_capacity), |
| 200 | channel, |
Allen George | 0e22c36 | 2017-01-30 07:15:00 -0500 | [diff] [blame] | 201 | } |
| 202 | } |
| 203 | } |
| 204 | |
| 205 | impl<C> Write for TFramedWriteTransport<C> |
| 206 | where |
| 207 | C: Write, |
| 208 | { |
Allen George | 8b96bfb | 2016-11-02 08:01:08 -0400 | [diff] [blame] | 209 | fn write(&mut self, b: &[u8]) -> io::Result<usize> { |
Allen George | b7084cb | 2017-12-13 07:34:49 -0500 | [diff] [blame] | 210 | let current_capacity = self.buf.capacity(); |
| 211 | let available_space = current_capacity - self.buf.len(); |
| 212 | if b.len() > available_space { |
| 213 | let additional_space = cmp::max(b.len() - available_space, current_capacity); |
| 214 | self.buf.reserve(additional_space); |
Allen George | 8b96bfb | 2016-11-02 08:01:08 -0400 | [diff] [blame] | 215 | } |
| 216 | |
Allen George | b7084cb | 2017-12-13 07:34:49 -0500 | [diff] [blame] | 217 | self.buf.extend_from_slice(b); |
| 218 | Ok(b.len()) |
Allen George | 8b96bfb | 2016-11-02 08:01:08 -0400 | [diff] [blame] | 219 | } |
| 220 | |
| 221 | fn flush(&mut self) -> io::Result<()> { |
Allen George | b7084cb | 2017-12-13 07:34:49 -0500 | [diff] [blame] | 222 | let message_size = self.buf.len(); |
Allen George | 8b96bfb | 2016-11-02 08:01:08 -0400 | [diff] [blame] | 223 | |
| 224 | if let 0 = message_size { |
| 225 | return Ok(()); |
| 226 | } else { |
Allen George | ef7a189 | 2018-12-16 18:01:37 -0500 | [diff] [blame] | 227 | self.channel.write_i32::<BigEndian>(message_size as i32)?; |
Allen George | 8b96bfb | 2016-11-02 08:01:08 -0400 | [diff] [blame] | 228 | } |
| 229 | |
Allen George | b7084cb | 2017-12-13 07:34:49 -0500 | [diff] [blame] | 230 | // will spin if the underlying channel can't be written to |
Allen George | 8b96bfb | 2016-11-02 08:01:08 -0400 | [diff] [blame] | 231 | let mut byte_index = 0; |
Allen George | b7084cb | 2017-12-13 07:34:49 -0500 | [diff] [blame] | 232 | while byte_index < message_size { |
| 233 | let nwrite = self.channel.write(&self.buf[byte_index..message_size])?; |
| 234 | byte_index = cmp::min(byte_index + nwrite, message_size); |
Allen George | 8b96bfb | 2016-11-02 08:01:08 -0400 | [diff] [blame] | 235 | } |
| 236 | |
Allen George | b7084cb | 2017-12-13 07:34:49 -0500 | [diff] [blame] | 237 | let buf_capacity = cmp::min(self.buf.capacity(), WRITE_CAPACITY); |
| 238 | self.buf.resize(buf_capacity, 0); |
| 239 | self.buf.clear(); |
| 240 | |
Allen George | 0e22c36 | 2017-01-30 07:15:00 -0500 | [diff] [blame] | 241 | self.channel.flush() |
Allen George | 8b96bfb | 2016-11-02 08:01:08 -0400 | [diff] [blame] | 242 | } |
| 243 | } |
| 244 | |
Allen George | 0e22c36 | 2017-01-30 07:15:00 -0500 | [diff] [blame] | 245 | /// Factory for creating instances of `TFramedWriteTransport`. |
Allen George | 8b96bfb | 2016-11-02 08:01:08 -0400 | [diff] [blame] | 246 | #[derive(Default)] |
Allen George | 0e22c36 | 2017-01-30 07:15:00 -0500 | [diff] [blame] | 247 | pub struct TFramedWriteTransportFactory; |
Allen George | 8b96bfb | 2016-11-02 08:01:08 -0400 | [diff] [blame] | 248 | |
Allen George | 0e22c36 | 2017-01-30 07:15:00 -0500 | [diff] [blame] | 249 | impl TFramedWriteTransportFactory { |
| 250 | pub fn new() -> TFramedWriteTransportFactory { |
| 251 | TFramedWriteTransportFactory {} |
Allen George | 8b96bfb | 2016-11-02 08:01:08 -0400 | [diff] [blame] | 252 | } |
| 253 | } |
| 254 | |
Allen George | 0e22c36 | 2017-01-30 07:15:00 -0500 | [diff] [blame] | 255 | impl TWriteTransportFactory for TFramedWriteTransportFactory { |
| 256 | /// Create a `TFramedWriteTransport`. |
Danny Browning | 77d96c1 | 2019-08-21 13:41:07 -0600 | [diff] [blame] | 257 | fn create(&self, channel: Box<dyn Write + Send>) -> Box<dyn TWriteTransport + Send> { |
Allen George | 0e22c36 | 2017-01-30 07:15:00 -0500 | [diff] [blame] | 258 | Box::new(TFramedWriteTransport::new(channel)) |
Allen George | 8b96bfb | 2016-11-02 08:01:08 -0400 | [diff] [blame] | 259 | } |
| 260 | } |
| 261 | |
| 262 | #[cfg(test)] |
| 263 | mod tests { |
Allen George | b7084cb | 2017-12-13 07:34:49 -0500 | [diff] [blame] | 264 | use super::*; |
Allen George | b0d1413 | 2020-03-29 11:48:55 -0400 | [diff] [blame] | 265 | use crate::transport::mem::TBufferChannel; |
Allen George | b7084cb | 2017-12-13 07:34:49 -0500 | [diff] [blame] | 266 | |
| 267 | // FIXME: test a forced reserve |
| 268 | |
| 269 | #[test] |
| 270 | fn must_read_message_smaller_than_initial_buffer_size() { |
| 271 | let c = TBufferChannel::with_capacity(10, 10); |
| 272 | let mut t = TFramedReadTransport::with_capacity(8, c); |
| 273 | |
Allen George | ef7a189 | 2018-12-16 18:01:37 -0500 | [diff] [blame] | 274 | t.chan.set_readable_bytes(&[ |
| 275 | 0x00, 0x00, 0x00, 0x04, /* message size */ |
| 276 | 0x00, 0x01, 0x02, 0x03, /* message body */ |
| 277 | ]); |
Allen George | b7084cb | 2017-12-13 07:34:49 -0500 | [diff] [blame] | 278 | |
| 279 | let mut buf = vec![0; 8]; |
| 280 | |
| 281 | // we've read exactly 4 bytes |
| 282 | assert_eq!(t.read(&mut buf).unwrap(), 4); |
| 283 | assert_eq!(&buf[..4], &[0x00, 0x01, 0x02, 0x03]); |
| 284 | } |
| 285 | |
| 286 | #[test] |
| 287 | fn must_read_message_greater_than_initial_buffer_size() { |
| 288 | let c = TBufferChannel::with_capacity(10, 10); |
| 289 | let mut t = TFramedReadTransport::with_capacity(2, c); |
| 290 | |
Allen George | ef7a189 | 2018-12-16 18:01:37 -0500 | [diff] [blame] | 291 | t.chan.set_readable_bytes(&[ |
| 292 | 0x00, 0x00, 0x00, 0x04, /* message size */ |
| 293 | 0x00, 0x01, 0x02, 0x03, /* message body */ |
| 294 | ]); |
Allen George | b7084cb | 2017-12-13 07:34:49 -0500 | [diff] [blame] | 295 | |
| 296 | let mut buf = vec![0; 8]; |
| 297 | |
| 298 | // we've read exactly 4 bytes |
| 299 | assert_eq!(t.read(&mut buf).unwrap(), 4); |
| 300 | assert_eq!(&buf[..4], &[0x00, 0x01, 0x02, 0x03]); |
| 301 | } |
| 302 | |
| 303 | #[test] |
| 304 | fn must_read_multiple_messages_in_sequence_correctly() { |
| 305 | let c = TBufferChannel::with_capacity(10, 10); |
| 306 | let mut t = TFramedReadTransport::with_capacity(2, c); |
| 307 | |
| 308 | // |
| 309 | // 1st message |
| 310 | // |
| 311 | |
Allen George | ef7a189 | 2018-12-16 18:01:37 -0500 | [diff] [blame] | 312 | t.chan.set_readable_bytes(&[ |
| 313 | 0x00, 0x00, 0x00, 0x04, /* message size */ |
| 314 | 0x00, 0x01, 0x02, 0x03, /* message body */ |
| 315 | ]); |
Allen George | b7084cb | 2017-12-13 07:34:49 -0500 | [diff] [blame] | 316 | |
| 317 | let mut buf = vec![0; 8]; |
| 318 | |
| 319 | // we've read exactly 4 bytes |
| 320 | assert_eq!(t.read(&mut buf).unwrap(), 4); |
| 321 | assert_eq!(&buf, &[0x00, 0x01, 0x02, 0x03, 0x00, 0x00, 0x00, 0x00]); |
| 322 | |
| 323 | // |
| 324 | // 2nd message |
| 325 | // |
| 326 | |
Allen George | ef7a189 | 2018-12-16 18:01:37 -0500 | [diff] [blame] | 327 | t.chan.set_readable_bytes(&[ |
| 328 | 0x00, 0x00, 0x00, 0x01, /* message size */ |
| 329 | 0x04, /* message body */ |
| 330 | ]); |
Allen George | b7084cb | 2017-12-13 07:34:49 -0500 | [diff] [blame] | 331 | |
| 332 | let mut buf = vec![0; 8]; |
| 333 | |
| 334 | // we've read exactly 1 byte |
| 335 | assert_eq!(t.read(&mut buf).unwrap(), 1); |
| 336 | assert_eq!(&buf, &[0x04, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00]); |
| 337 | } |
| 338 | |
| 339 | #[test] |
| 340 | fn must_write_message_smaller_than_buffer_size() { |
| 341 | let mem = TBufferChannel::with_capacity(0, 0); |
| 342 | let mut t = TFramedWriteTransport::with_capacity(20, mem); |
| 343 | |
| 344 | let b = vec![0; 10]; |
| 345 | |
| 346 | // should have written 10 bytes |
| 347 | assert_eq!(t.write(&b).unwrap(), 10); |
| 348 | } |
| 349 | |
| 350 | #[test] |
| 351 | fn must_return_zero_if_caller_calls_write_with_empty_buffer() { |
| 352 | let mem = TBufferChannel::with_capacity(0, 10); |
| 353 | let mut t = TFramedWriteTransport::with_capacity(10, mem); |
| 354 | |
| 355 | let expected: [u8; 0] = []; |
| 356 | |
| 357 | assert_eq!(t.write(&[]).unwrap(), 0); |
| 358 | assert_eq_transport_written_bytes!(t, expected); |
| 359 | } |
| 360 | |
| 361 | #[test] |
| 362 | fn must_write_to_inner_transport_on_flush() { |
| 363 | let mem = TBufferChannel::with_capacity(10, 10); |
| 364 | let mut t = TFramedWriteTransport::new(mem); |
| 365 | |
| 366 | let b: [u8; 5] = [0x00, 0x01, 0x02, 0x03, 0x04]; |
| 367 | assert_eq!(t.write(&b).unwrap(), 5); |
| 368 | assert_eq_transport_num_written_bytes!(t, 0); |
| 369 | |
| 370 | assert!(t.flush().is_ok()); |
| 371 | |
| 372 | let expected_bytes = [ |
Allen George | ef7a189 | 2018-12-16 18:01:37 -0500 | [diff] [blame] | 373 | 0x00, 0x00, 0x00, 0x05, /* message size */ |
| 374 | 0x00, 0x01, 0x02, 0x03, 0x04, /* message body */ |
Allen George | b7084cb | 2017-12-13 07:34:49 -0500 | [diff] [blame] | 375 | ]; |
| 376 | |
| 377 | assert_eq_transport_written_bytes!(t, expected_bytes); |
| 378 | } |
| 379 | |
| 380 | #[test] |
| 381 | fn must_write_message_greater_than_buffer_size_00() { |
| 382 | let mem = TBufferChannel::with_capacity(0, 10); |
| 383 | |
| 384 | // IMPORTANT: DO **NOT** CHANGE THE WRITE_CAPACITY OR THE NUMBER OF BYTES TO BE WRITTEN! |
| 385 | // these lengths were chosen to be just long enough |
| 386 | // that doubling the capacity is a **worse** choice than |
| 387 | // simply resizing the buffer to b.len() |
| 388 | |
| 389 | let mut t = TFramedWriteTransport::with_capacity(1, mem); |
| 390 | let b = [0x00, 0x01, 0x02]; |
| 391 | |
| 392 | // should have written 3 bytes |
| 393 | assert_eq!(t.write(&b).unwrap(), 3); |
| 394 | assert_eq_transport_num_written_bytes!(t, 0); |
| 395 | |
| 396 | assert!(t.flush().is_ok()); |
| 397 | |
| 398 | let expected_bytes = [ |
Allen George | ef7a189 | 2018-12-16 18:01:37 -0500 | [diff] [blame] | 399 | 0x00, 0x00, 0x00, 0x03, /* message size */ |
| 400 | 0x00, 0x01, 0x02, /* message body */ |
Allen George | b7084cb | 2017-12-13 07:34:49 -0500 | [diff] [blame] | 401 | ]; |
| 402 | |
| 403 | assert_eq_transport_written_bytes!(t, expected_bytes); |
| 404 | } |
| 405 | |
| 406 | #[test] |
| 407 | fn must_write_message_greater_than_buffer_size_01() { |
| 408 | let mem = TBufferChannel::with_capacity(0, 10); |
| 409 | |
| 410 | // IMPORTANT: DO **NOT** CHANGE THE WRITE_CAPACITY OR THE NUMBER OF BYTES TO BE WRITTEN! |
| 411 | // these lengths were chosen to be just long enough |
| 412 | // that doubling the capacity is a **better** choice than |
| 413 | // simply resizing the buffer to b.len() |
| 414 | |
| 415 | let mut t = TFramedWriteTransport::with_capacity(2, mem); |
| 416 | let b = [0x00, 0x01, 0x02]; |
| 417 | |
| 418 | // should have written 3 bytes |
| 419 | assert_eq!(t.write(&b).unwrap(), 3); |
| 420 | assert_eq_transport_num_written_bytes!(t, 0); |
| 421 | |
| 422 | assert!(t.flush().is_ok()); |
| 423 | |
| 424 | let expected_bytes = [ |
Allen George | ef7a189 | 2018-12-16 18:01:37 -0500 | [diff] [blame] | 425 | 0x00, 0x00, 0x00, 0x03, /* message size */ |
| 426 | 0x00, 0x01, 0x02, /* message body */ |
Allen George | b7084cb | 2017-12-13 07:34:49 -0500 | [diff] [blame] | 427 | ]; |
| 428 | |
| 429 | assert_eq_transport_written_bytes!(t, expected_bytes); |
| 430 | } |
| 431 | |
| 432 | #[test] |
| 433 | fn must_return_error_if_nothing_can_be_written_to_inner_transport_on_flush() { |
| 434 | let mem = TBufferChannel::with_capacity(0, 0); |
| 435 | let mut t = TFramedWriteTransport::with_capacity(1, mem); |
| 436 | |
| 437 | let b = vec![0; 10]; |
| 438 | |
| 439 | // should have written 10 bytes |
| 440 | assert_eq!(t.write(&b).unwrap(), 10); |
| 441 | |
| 442 | // let's flush |
| 443 | let r = t.flush(); |
| 444 | |
| 445 | // this time we'll error out because the flush can't write to the underlying channel |
| 446 | assert!(r.is_err()); |
| 447 | } |
| 448 | |
| 449 | #[test] |
| 450 | fn must_write_successfully_after_flush() { |
| 451 | // IMPORTANT: write capacity *MUST* be greater |
| 452 | // than message sizes used in this test + 4-byte frame header |
| 453 | let mem = TBufferChannel::with_capacity(0, 10); |
| 454 | let mut t = TFramedWriteTransport::with_capacity(5, mem); |
| 455 | |
| 456 | // write and flush |
| 457 | let first_message: [u8; 5] = [0x00, 0x01, 0x02, 0x03, 0x04]; |
| 458 | assert_eq!(t.write(&first_message).unwrap(), 5); |
| 459 | assert!(t.flush().is_ok()); |
| 460 | |
| 461 | let mut expected = Vec::new(); |
| 462 | expected.write_all(&[0x00, 0x00, 0x00, 0x05]).unwrap(); // message size |
| 463 | expected.extend_from_slice(&first_message); |
| 464 | |
| 465 | // check the flushed bytes |
| 466 | assert_eq!(t.channel.write_bytes(), expected); |
| 467 | |
| 468 | // reset our underlying transport |
| 469 | t.channel.empty_write_buffer(); |
| 470 | |
| 471 | let second_message: [u8; 3] = [0x05, 0x06, 0x07]; |
| 472 | assert_eq!(t.write(&second_message).unwrap(), 3); |
| 473 | assert!(t.flush().is_ok()); |
| 474 | |
| 475 | expected.clear(); |
| 476 | expected.write_all(&[0x00, 0x00, 0x00, 0x03]).unwrap(); // message size |
| 477 | expected.extend_from_slice(&second_message); |
| 478 | |
| 479 | // check the flushed bytes |
| 480 | assert_eq!(t.channel.write_bytes(), expected); |
| 481 | } |
Allen George | 8b96bfb | 2016-11-02 08:01:08 -0400 | [diff] [blame] | 482 | } |