forked from feather-rs/feather
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtestframework.rs
More file actions
571 lines (488 loc) · 16.7 KB
/
testframework.rs
File metadata and controls
571 lines (488 loc) · 16.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
//! Helper framework for writing unit tests.
use std::net::TcpListener;
use std::sync::atomic::AtomicUsize;
use std::sync::Arc;
use glm::DVec3;
use mio_extras::channel::{channel, Receiver, Sender};
use rand::Rng;
use shrev::EventChannel;
use specs::{Builder, Dispatcher, DispatcherBuilder, Entity, ReaderId, System, World, WorldExt};
use uuid::Uuid;
use feather_core::level::LevelData;
use feather_core::network::packet::{Packet, PacketType};
use feather_core::world::block::Block;
use feather_core::world::chunk::Chunk;
use feather_core::world::{BlockPosition, ChunkMap, ChunkPosition, Position};
use feather_core::Gamemode;
use crate::chunk_logic::{ChunkHolders, ChunkLoadSystem};
use crate::config::Config;
use crate::entity::metadata::{self, Metadata};
use crate::entity::{
ChunkEntities, EntityDestroyEvent, EntitySpawnEvent, EntityType, ItemComponent, NamedComponent,
PlayerComponent, PositionComponent, VelocityComponent,
};
use crate::io::ServerToWorkerMessage;
use crate::network::{NetworkComponent, PacketQueue};
use crate::player::{InventoryComponent, PlayerDisconnectEvent};
use crate::util::BroadcasterSystem;
use crate::worldgen::{EmptyWorldGenerator, WorldGenerator};
use crate::PlayerCount;
/// Initializes a Specs world and dispatcher
/// using default configuration options and an
/// available server port.
pub fn init_world<'a, 'b>() -> (World, Dispatcher<'a, 'b>) {
let mut config = Config::default();
config.server.port = find_open_port().unwrap();
let config = Arc::new(config);
let player_count = Arc::new(PlayerCount(AtomicUsize::new(0)));
let server_icon = Arc::new(None);
let ioman = super::init_io_manager(
Arc::clone(&config),
Arc::clone(&player_count),
Arc::clone(&server_icon),
);
let level = LevelData::default();
super::init_world(config, player_count, ioman, level)
}
pub struct Player {
pub entity: Entity,
pub network_sender: Sender<ServerToWorkerMessage>,
pub network_receiver: Receiver<ServerToWorkerMessage>,
}
/// Adds a player to the world, inserting
/// all the necessary components. Returns
/// a number of useful channels.
///
/// # Notes
/// * A `ChunkHolders` and `ChunkEntities` entry
/// is created for the player. If this behavior is not
/// desired, use `add_player_without_holder`.
pub fn add_player(world: &mut World) -> Player {
let player = add_player_without_holder(world);
let mut chunk_holders = world.fetch_mut::<ChunkHolders>();
let view_distance = i32::from(world.fetch::<Arc<Config>>().server.view_distance);
for x in -view_distance..=view_distance {
for z in -view_distance..=view_distance {
chunk_holders.insert_holder(ChunkPosition::new(x, z), player.entity);
}
}
let mut chunk_entities = world.fetch_mut::<ChunkEntities>();
chunk_entities.add_to_chunk(ChunkPosition::new(0, 0), player.entity);
player
}
/// Adds a player to the world without adding the `ChunkHolders`
/// and `ChunkEntities` entries.
pub fn add_player_without_holder(world: &mut World) -> Player {
let (ns1, nr1) = channel();
let (ns2, nr2) = channel();
let entity = world
.create_entity()
.with(NetworkComponent::new(ns1, nr2))
.with(PlayerComponent {
gamemode: Gamemode::Creative,
profile_properties: vec![],
})
.with(PositionComponent {
current: position!(0.0, 0.0, 0.0),
previous: position!(0.0, 0.0, 0.0),
})
.with(NamedComponent {
display_name: "".to_string(),
uuid: Uuid::new_v4(),
})
.with(InventoryComponent::default())
.with(Metadata::Player(metadata::Player::default()))
.with(EntityType::Player)
.build();
Player {
entity,
network_sender: ns2,
network_receiver: nr1,
}
}
/// Asserts that the given player has received
/// a packet of the given type, returning the packet.
pub fn assert_packet_received(player: &Player, ty: PacketType) -> Box<dyn Packet> {
while let Ok(msg) = player.network_receiver.try_recv() {
if let ServerToWorkerMessage::SendPacket(packet) = msg {
if packet.ty() == ty {
return packet;
}
}
}
panic!();
}
/// Asserts that a player did not receive
/// any packets of the given type.
/// Panics if not.
pub fn assert_packet_not_received(player: &Player, ty: PacketType) {
while let Ok(msg) = player.network_receiver.try_recv() {
if let ServerToWorkerMessage::SendPacket(packet) = msg {
assert_ne!(packet.ty(), ty);
}
}
}
/// Retrieves up to `cap` packets sent to a player, if any.
/// If `cap` is set to `None`, all packets will be read.
///
/// Note that this function consumes messages in
/// the network channel until enough packets have been read.
pub fn received_packets(player: &Player, cap: Option<usize>) -> Vec<Box<dyn Packet>> {
let mut result = vec![];
while let Ok(msg) = player.network_receiver.try_recv() {
if let ServerToWorkerMessage::SendPacket(pack) = msg {
result.push(pack);
}
if let Some(cap) = cap.as_ref() {
if result.len() >= *cap {
break;
}
}
}
result
}
/// Adds a received packet to the packet queue
/// for a given player.
pub fn receive_packet<P: Packet + 'static>(player: &Player, world: &World, packet: P) {
let queue = world.fetch_mut::<PacketQueue>();
queue.add_for_packet(player.entity, Box::new(packet));
}
/// Attempts to find an available port.
fn find_open_port() -> Option<u16> {
let start = rand::thread_rng().gen_range(10000, 30000);
(start..60000).find(|port| TcpListener::bind(("127.0.0.1", *port)).is_ok())
}
/// Asserts that a player was disconnected, panicking if not.
pub fn assert_disconnected(player: &Player) {
let mut disconnected = false;
for packet in received_packets(player, None) {
if packet.ty() == PacketType::DisconnectPlay {
disconnected = true;
}
}
assert!(disconnected);
}
/// Asserts that a player was not disconnected, panicking
/// if they were.
pub fn assert_not_disconnected(player: &Player) {
let mut disconnected = false;
for packet in received_packets(player, None) {
if packet.ty() == PacketType::DisconnectPlay {
disconnected = true;
}
}
assert!(!disconnected);
}
/// Sends a packet to the player.
pub fn send_packet<P: Packet + 'static>(player: &Player, packet: P) {
player
.network_sender
.send(ServerToWorkerMessage::NotifyPacketReceived(Box::new(
packet,
)))
.unwrap();
}
/// Registers a reader for events of the given type.
pub fn reader<E: Send + Sync>(w: &World) -> ReaderId<E> {
let mut channel = w.fetch_mut::<EventChannel<E>>();
channel.register_reader()
}
/// Triggers the given event, writing it to
/// the corresponding `EventChannel`.
pub fn trigger_event<E: Send + Sync + 'static>(world: &World, event: E) {
let mut channel = world.fetch_mut::<EventChannel<E>>();
channel.single_write(event);
}
/// Returns all triggered events of a given type.
pub fn triggered_events<E: Send + Sync + Clone + 'static>(
world: &World,
reader: &mut ReaderId<E>,
) -> Vec<E> {
let channel = world.fetch::<EventChannel<E>>();
channel.read(reader).cloned().collect()
}
/// Creates an entity at the origin with zero
/// velocity.
///
///
/// # Notes
/// * A `ChunkHolders` and `ChunkEntities` entry
/// is created for the entity. If this behavior is not
/// desired, use `add_entity_without_holder`.
pub fn add_entity(world: &mut World, ty: EntityType, trigger_spawn_event: bool) -> Entity {
add_entity_with_pos(world, ty, Position::default(), trigger_spawn_event)
}
/// Creates an entity at the origin with zero velocity, without
/// adding a chunk holder or chunk entities entry for it.
pub fn add_entity_without_holder(
world: &mut World,
ty: EntityType,
trigger_spawn_event: bool,
) -> Entity {
add_entity_without_holder_with_pos(world, ty, Position::default(), trigger_spawn_event)
}
/// Creates an entity with the given position
/// and zero velocity.
pub fn add_entity_with_pos(
world: &mut World,
ty: EntityType,
pos: Position,
trigger_spawn_event: bool,
) -> Entity {
add_entity_with_pos_and_vel(
world,
ty,
pos,
glm::vec3(0.0, 0.0, 0.0),
trigger_spawn_event,
)
}
pub fn add_entity_without_holder_with_pos(
world: &mut World,
ty: EntityType,
pos: Position,
trigger_spawn_event: bool,
) -> Entity {
add_entity_without_holder_with_pos_and_vel(
world,
ty,
pos,
glm::vec3(0.0, 0.0, 0.0),
trigger_spawn_event,
)
}
/// Creates an entity with the given position and velocity.
pub fn add_entity_with_pos_and_vel(
world: &mut World,
ty: EntityType,
pos: Position,
vel: DVec3,
trigger_spawn_event: bool,
) -> Entity {
let entity =
add_entity_without_holder_with_pos_and_vel(world, ty, pos, vel, trigger_spawn_event);
let mut chunk_entities = world.fetch_mut::<ChunkEntities>();
chunk_entities.add_to_chunk(pos.chunk_pos(), entity);
entity
}
pub fn add_entity_without_holder_with_pos_and_vel(
world: &mut World,
ty: EntityType,
pos: Position,
vel: DVec3,
trigger_spawn_event: bool,
) -> Entity {
let entity = world
.create_entity()
.with(PositionComponent {
current: pos,
previous: pos,
})
.with(VelocityComponent(vel))
.with(NamedComponent {
uuid: Uuid::new_v4(),
display_name: "bla".to_string(),
})
.with(ty)
.with(Metadata::Entity(metadata::Entity::default()))
.build();
if ty == EntityType::Item {
world
.write_component::<ItemComponent>()
.insert(entity, ItemComponent { collectable_at: 20 })
.unwrap();
}
if trigger_spawn_event {
let event = EntitySpawnEvent { entity, ty };
trigger_event(&world, event);
}
entity
}
/// Populates a 15x15 area of chunks around the origin
/// with air.
pub fn populate_with_air(world: &mut World) {
for x in -15..=15 {
for z in -15..=15 {
let chunk = Chunk::new(ChunkPosition::new(x, z));
world
.fetch_mut::<ChunkMap>()
.set_chunk_at(chunk.position(), chunk);
}
}
}
/// Asserts that an entity was not removed.
pub fn assert_not_removed(world: &World, entity: Entity) {
assert!(world.entities().is_alive(entity));
}
/// Asserts that an entity was removed.
pub fn assert_removed(world: &World, entity: Entity) {
assert!(!world.entities().is_alive(entity));
}
/// Retrieves the position of an entity.
pub fn entity_pos(world: &World, entity: Entity) -> Position {
world
.read_component::<PositionComponent>()
.get(entity)
.unwrap()
.current
}
/// Retrieves the velocity of an entity.
pub fn entity_vel(world: &World, entity: Entity) -> Option<DVec3> {
if let Some(comp) = world.read_component::<VelocityComponent>().get(entity) {
Some(comp.0)
} else {
None
}
}
/// Sets an entity's position.
pub fn set_entity_pos(world: &World, entity: Entity, pos: Position) {
let mut storage = world.write_component::<PositionComponent>();
storage.get_mut(entity).unwrap().current = pos;
}
/// Sets an entity's velocity.
pub fn set_entity_velocity(world: &World, entity: Entity, vel: DVec3) {
let mut storage = world.write_component::<VelocityComponent>();
storage.get_mut(entity).unwrap().0 = vel;
}
/// Sets the block at the given position in the world.
pub fn set_block(x: i32, y: i32, z: i32, block: Block, world: &World) {
let mut chunk_map = world.fetch_mut::<ChunkMap>();
chunk_map
.set_block_at(BlockPosition::new(x, y, z), block)
.unwrap();
}
/// A dispatcher builder for isolating tests.
pub struct TestBuilder<'a, 'b> {
world: World,
dispatcher: DispatcherBuilder<'a, 'b>,
}
impl<'a, 'b> TestBuilder<'a, 'b> {
pub fn with<S>(mut self, system: S, name: &'static str) -> Self
where
S: for<'c> System<'c> + Send + 'a,
{
self.dispatcher.add(system, name, &[]);
self
}
pub fn with_dep<S>(mut self, system: S, name: &'static str, deps: &[&'static str]) -> Self
where
S: for<'c> System<'c> + Send + 'a,
{
self.dispatcher.add(system, name, deps);
self
}
pub fn build(mut self) -> (World, Dispatcher<'a, 'b>) {
self.world
.insert(Arc::new(PlayerCount(AtomicUsize::new(0))));
self.world
.insert(EventChannel::<PlayerDisconnectEvent>::new());
self.world.insert(EventChannel::<EntityDestroyEvent>::new());
self.world.insert(EventChannel::<EntitySpawnEvent>::new());
self.world.insert(crate::time::Time(0));
self.world.insert(ChunkHolders::default());
self.world.insert(ChunkEntities::default());
self.world.insert(Arc::new(Config::default()));
let generator: Arc<dyn WorldGenerator> = Arc::new(EmptyWorldGenerator {});
self.world.insert(generator);
let mut chunk_system = ChunkLoadSystem {};
chunk_system.setup(&mut self.world);
// Insert the broadcaster system, since it is so commonly
// used that it should be used for all tests.
self.dispatcher.add_barrier();
self.dispatcher.add(BroadcasterSystem, "", &[]);
let mut dispatcher = self.dispatcher.build();
dispatcher.setup(&mut self.world);
self.world.register::<VelocityComponent>();
self.world.register::<PositionComponent>();
self.world.register::<NamedComponent>();
self.world.register::<Metadata>();
self.world.register::<EntityType>();
self.world.register::<ItemComponent>();
self.world.register::<PlayerComponent>();
self.world.register::<InventoryComponent>();
self.world.register::<NetworkComponent>();
(self.world, dispatcher)
}
}
pub fn builder<'a, 'b>() -> TestBuilder<'a, 'b> {
TestBuilder {
world: World::new(),
dispatcher: DispatcherBuilder::new(),
}
}
/// Heh... tests for the testing framework.
/// Not sure what the point of this is, since
/// all other tests would fail if the testing
/// framework didn't work.
mod tests {
use feather_core::network::packet::implementation::{DisconnectPlay, LoginStart};
use crate::entity::{PlayerComponent, PositionComponent};
use crate::network::{send_packet_to_player, NetworkComponent};
use super::*;
#[test]
fn test_find_open_port() {
let port = find_open_port().unwrap();
println!("Found open port: {}", port);
assert!(TcpListener::bind(("127.0.0.1", port)).is_ok());
}
#[test]
fn test_init_world() {
// Check that initializing the world doesn't cause
// a panic.
let (w, mut d) = init_world();
// Check that running the dispatcher works fine
d.dispatch(&w);
}
#[test]
fn test_add_player() {
let (mut w, _) = init_world();
let entity = add_player(&mut w).entity;
assert!(w.read_component::<PlayerComponent>().get(entity).is_some());
assert!(w
.read_component::<PositionComponent>()
.get(entity)
.is_some());
assert!(w.read_component::<NetworkComponent>().get(entity).is_some());
}
#[test]
fn test_received_packets() {
let (mut w, _) = init_world();
let player = add_player(&mut w);
let cap = 1;
send_packet_to_player(
w.read_component().get(player.entity).unwrap(),
LoginStart::new("".to_string()),
);
send_packet_to_player(
w.read_component().get(player.entity).unwrap(),
LoginStart::new("".to_string()),
);
let packets = received_packets(&player, Some(cap));
assert_eq!(packets.len(), 1);
let packets = received_packets(&player, Some(cap));
assert_eq!(packets.len(), 1);
}
#[test]
#[should_panic]
fn test_assert_packet_received() {
let (mut w, _) = init_world();
let player = add_player(&mut w);
assert_packet_received(&player, PacketType::Handshake);
}
#[test]
#[should_panic]
fn test_assert_disconnected() {
let (mut w, _) = init_world();
let player = add_player(&mut w);
assert_disconnected(&player);
}
#[test]
#[should_panic]
fn test_assert_not_disconnected() {
let (mut w, _) = init_world();
let disconnect = DisconnectPlay::new("bla".to_string());
let player = add_player(&mut w);
send_packet_to_player(w.read_component().get(player.entity).unwrap(), disconnect);
assert_not_disconnected(&player);
}
}