Skip to content

Commit

Permalink
add MockClientHelper to reduce repetition
Browse files Browse the repository at this point in the history
  • Loading branch information
dyc3 committed Jan 26, 2023
1 parent 5049c6c commit aa1e01d
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 11 deletions.
7 changes: 2 additions & 5 deletions crates/valence_new/src/unit_test/example.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,18 +41,15 @@ mod tests {
let server = app.world.resource::<Server>();
let permit = server.force_aquire_owned();
let info = gen_client_info("test");
let (client, mut send, recv) = create_mock_client(permit, info);
let (client, mut client_helper) = create_mock_client(permit, info);
let client_ent = app.world.spawn(client).id();

// Send a packet as the client to the server.
let packet = valence_protocol::packets::c2s::play::SetPlayerPosition {
position: [12.0, 64.0, 0.0],
on_ground: true,
};
let mut buffer = Vec::<u8>::new();
valence_protocol::encode_packet(&mut buffer, &packet).expect("Failed to encode packet");
send.try_send(BytesMut::from(buffer.as_slice()))
.expect("Failed to send packet");
client_helper.send_packet(packet);

// Process the packet.
app.update();
Expand Down
38 changes: 32 additions & 6 deletions crates/valence_new/src/unit_test/util.rs
Original file line number Diff line number Diff line change
@@ -1,24 +1,24 @@
use bytes::BytesMut;
use tokio::sync::OwnedSemaphorePermit;
use valence_protocol::Username;
use valence_protocol::{EncodePacket, Username};

use crate::client::Client;
use crate::server::byte_channel::{ByteReceiver, ByteSender};
use crate::server::{NewClientInfo, PlayPacketReceiver, PlayPacketSender};

/// Creates a mock client that can be used for unit testing.
///
/// Returns the client, a `ByteSender` to inject packets to be read and acted
/// upon by the server, and a `ByteReceiver` to receive packets and make
/// assertions about what the server sent.
/// Returns the client, and a helper to inject packets as if the client sent
/// them and receive packets as if the client received them.
pub fn create_mock_client(
permit: OwnedSemaphorePermit,
client_info: NewClientInfo,
) -> (Client, ByteSender, ByteReceiver) {
) -> (Client, MockClientHelper) {
let (pkt_send, recv) = PlayPacketSender::new_injectable();
let (pkt_recv, send) = PlayPacketReceiver::new_injectable();

let client = Client::new(pkt_send, pkt_recv, permit, client_info);
(client, send, recv)
(client, MockClientHelper::new(send, recv))
}

/// Creates a `NewClientInfo` with the given username and a random UUID.
Expand All @@ -31,3 +31,29 @@ pub fn gen_client_info(username: &str) -> NewClientInfo {
properties: vec![],
}
}

pub struct MockClientHelper {
/// Used to pretend the client sent bytes.
send: ByteSender,
/// Used to pretend the client received bytes.
recv: ByteReceiver,
}

/// Has a `ByteSender` to inject packets to be read and acted
/// upon by the server, and a `ByteReceiver` to receive packets and make
/// assertions about what the server sent.
impl MockClientHelper {
fn new(send: ByteSender, recv: ByteReceiver) -> Self {
Self { send, recv }
}

/// Inject a packet to be parsed by the server. Panics if the packet cannot
/// be sent.
pub fn send_packet(&mut self, packet: impl EncodePacket) {
let mut buffer = Vec::<u8>::new();
valence_protocol::encode_packet(&mut buffer, &packet).expect("Failed to encode packet");
self.send
.try_send(BytesMut::from(buffer.as_slice()))
.expect("Failed to send packet");
}
}

0 comments on commit aa1e01d

Please sign in to comment.