Skip to content

Commit 508803a

Browse files
committed
refactor: extract function
1 parent 1f258c1 commit 508803a

File tree

1 file changed

+15
-32
lines changed

1 file changed

+15
-32
lines changed

tests/udp.rs

Lines changed: 15 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -240,6 +240,19 @@ mod udp_tracker_server {
240240
assert!(is_connect_response(&response, TransactionId(123)));
241241
}
242242

243+
async fn send_connection_request(transaction_id: TransactionId, client: &UdpTrackerClient) -> ConnectionId {
244+
let connect_request = ConnectRequest { transaction_id };
245+
246+
client.send(connect_request.into()).await;
247+
248+
let response = client.receive().await;
249+
250+
match response {
251+
Response::Connect(connect_response) => connect_response.connection_id,
252+
_ => panic!("error connecting to udp server {:?}", response),
253+
}
254+
}
255+
243256
#[tokio::test]
244257
async fn should_return_an_announce_response_when_the_client_sends_an_announce_request() {
245258
let configuration = tracker_configuration();
@@ -248,22 +261,7 @@ mod udp_tracker_server {
248261

249262
let client = new_connected_udp_tracker_client(&udp_server.bind_address.unwrap()).await;
250263

251-
// todo: extract client.connect() -> ConnectionId
252-
253-
// Get connection id before sending the announce request
254-
255-
let connect_request = ConnectRequest {
256-
transaction_id: TransactionId(123),
257-
};
258-
259-
client.send(connect_request.into()).await;
260-
261-
let response = client.receive().await;
262-
263-
let connection_id = match response {
264-
Response::Connect(connect_response) => connect_response.connection_id,
265-
_ => panic!("error connecting to udp server {:?}", response),
266-
};
264+
let connection_id = send_connection_request(TransactionId(123), &client).await;
267265

268266
// Send announce request
269267

@@ -297,22 +295,7 @@ mod udp_tracker_server {
297295

298296
let client = new_connected_udp_tracker_client(&udp_server.bind_address.unwrap()).await;
299297

300-
// todo: extract client.connect() -> ConnectionId
301-
302-
// Get connection id before sending the announce request
303-
304-
let connect_request = ConnectRequest {
305-
transaction_id: TransactionId(123i32),
306-
};
307-
308-
client.send(connect_request.into()).await;
309-
310-
let response = client.receive().await;
311-
312-
let connection_id = match response {
313-
Response::Connect(connect_response) => connect_response.connection_id,
314-
_ => panic!("error connecting to udp server {:?}", response),
315-
};
298+
let connection_id = send_connection_request(TransactionId(123), &client).await;
316299

317300
// Send scrape request
318301

0 commit comments

Comments
 (0)