Skip to content

Commit 8543004

Browse files
committed
Merge rust-bitcoin/rust-bitcoin#1042: Refactor serve_tcp code
abfeb32 Remove unnecessary local variable (Tobin C. Harding) 04b09a4 Remove unused loop (Tobin C. Harding) 380e001 Use write_all instead of write (Tobin C. Harding) Pull request description: Done while clearing clippy warnings, done as a separate PR because its not a simple glance to review like the others. Remove 2 clippy warnings and remove unnecessary local variable. ACKs for top commit: apoelstra: ACK abfeb32 Kixunil: ACK abfeb32 Tree-SHA512: 965708999c067dd8c156bbc54b711f608d524fab7051a0e56066f53b5c8d7bea1c233f04e77873b2624cd22e26a58f1d22f47870d2afe4347aa85335c3142245
2 parents 7d18457 + 9245304 commit 8543004

File tree

1 file changed

+10
-15
lines changed

1 file changed

+10
-15
lines changed

src/network/stream_reader.rs

Lines changed: 10 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -203,18 +203,15 @@ mod test {
203203
// 2. Spawning thread that will be writing our messages to the TCP Stream at the server side
204204
// in async mode
205205
let handle = thread::spawn(move || {
206-
for ostream in listener.incoming() {
207-
let mut ostream = ostream.unwrap();
208-
209-
for piece in pieces {
210-
ostream.write(&piece[..]).unwrap();
211-
ostream.flush().unwrap();
212-
thread::sleep(Duration::from_secs(1));
213-
}
214-
215-
ostream.shutdown(Shutdown::Both).unwrap();
216-
break;
206+
// We only simulate a single connection.
207+
let mut ostream = listener.incoming().next().unwrap().unwrap();
208+
for piece in pieces {
209+
ostream.write_all(&piece[..]).unwrap();
210+
ostream.flush().unwrap();
211+
thread::sleep(Duration::from_secs(1));
217212
}
213+
214+
ostream.shutdown(Shutdown::Both).unwrap();
218215
});
219216

220217
// 3. Creating client side of the TCP socket connection
@@ -228,11 +225,10 @@ mod test {
228225
#[test]
229226
fn read_multipartmsg_test() {
230227
// Setting up TCP connection emulation
231-
let (handle, istream) = serve_tcp(vec![
228+
let (handle, stream) = serve_tcp(vec![
232229
// single message split in two parts to emulate real network conditions
233230
MSG_VERSION[..24].to_vec(), MSG_VERSION[24..].to_vec()
234231
]);
235-
let stream = istream;
236232
let mut reader = StreamReader::new(stream, None);
237233

238234
// Reading and checking the whole message back
@@ -246,13 +242,12 @@ mod test {
246242
#[test]
247243
fn read_sequencemsg_test() {
248244
// Setting up TCP connection emulation
249-
let (handle, istream) = serve_tcp(vec![
245+
let (handle, stream) = serve_tcp(vec![
250246
// Real-world Bitcoin core communication case for /Satoshi:0.17.1/
251247
MSG_VERSION[..23].to_vec(), MSG_VERSION[23..].to_vec(),
252248
MSG_VERACK.to_vec(),
253249
MSG_ALERT[..24].to_vec(), MSG_ALERT[24..].to_vec()
254250
]);
255-
let stream = istream;
256251
let mut reader = StreamReader::new(stream, None);
257252

258253
// Reading and checking the first message (Version)

0 commit comments

Comments
 (0)