mirror of
https://codeberg.org/demostf/sync.git
synced 2026-06-03 16:44:07 +02:00
add tests
This commit is contained in:
parent
b298c8d2d9
commit
474c2beab8
4 changed files with 460 additions and 32 deletions
32
Cargo.lock
generated
32
Cargo.lock
generated
|
|
@ -69,6 +69,17 @@ dependencies = [
|
|||
"generic-array 0.12.0 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "enum_dispatch"
|
||||
version = "0.1.3"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
dependencies = [
|
||||
"lazy_static 1.3.0 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
"proc-macro2 0.4.30 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
"quote 0.6.12 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
"syn 0.15.33 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "fake-simd"
|
||||
version = "0.1.2"
|
||||
|
|
@ -139,6 +150,11 @@ dependencies = [
|
|||
"winapi-build 0.1.1 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "lazy_static"
|
||||
version = "1.3.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
|
||||
[[package]]
|
||||
name = "lazycell"
|
||||
version = "1.2.1"
|
||||
|
|
@ -157,6 +173,11 @@ dependencies = [
|
|||
"cfg-if 0.1.9 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "maplit"
|
||||
version = "1.0.1"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
|
||||
[[package]]
|
||||
name = "matches"
|
||||
version = "0.1.8"
|
||||
|
|
@ -361,7 +382,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
|
|||
dependencies = [
|
||||
"proc-macro2 0.4.30 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
"quote 0.6.12 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
"syn 0.15.36 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
"syn 0.15.33 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
|
|
@ -397,7 +418,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
|
|||
|
||||
[[package]]
|
||||
name = "syn"
|
||||
version = "0.15.36"
|
||||
version = "0.15.33"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
dependencies = [
|
||||
"proc-macro2 0.4.30 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
|
|
@ -409,6 +430,8 @@ dependencies = [
|
|||
name = "sync"
|
||||
version = "0.1.0"
|
||||
dependencies = [
|
||||
"enum_dispatch 0.1.3 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
"maplit 1.0.1 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
"mio 0.6.19 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
"serde 1.0.92 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
"serde_json 1.0.39 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
|
|
@ -517,6 +540,7 @@ dependencies = [
|
|||
"checksum cfg-if 0.1.9 (registry+https://github.com/rust-lang/crates.io-index)" = "b486ce3ccf7ffd79fdeb678eac06a9e6c09fc88d33836340becb8fffe87c5e33"
|
||||
"checksum cloudabi 0.0.3 (registry+https://github.com/rust-lang/crates.io-index)" = "ddfc5b9aa5d4507acaf872de71051dfd0e309860e88966e1051e462a077aac4f"
|
||||
"checksum digest 0.8.0 (registry+https://github.com/rust-lang/crates.io-index)" = "05f47366984d3ad862010e22c7ce81a7dbcaebbdfb37241a620f8b6596ee135c"
|
||||
"checksum enum_dispatch 0.1.3 (registry+https://github.com/rust-lang/crates.io-index)" = "167fb2d67557555a091160ce42a1237cbfbc1552066d1ff724973f866762692a"
|
||||
"checksum fake-simd 0.1.2 (registry+https://github.com/rust-lang/crates.io-index)" = "e88a8acf291dafb59c2d96e8f59828f3838bb1a70398823ade51a84de6a6deed"
|
||||
"checksum fuchsia-cprng 0.1.1 (registry+https://github.com/rust-lang/crates.io-index)" = "a06f77d526c1a601b7c4cdd98f54b5eaabffc14d5f2f0296febdc7f357c6d3ba"
|
||||
"checksum fuchsia-zircon 0.3.3 (registry+https://github.com/rust-lang/crates.io-index)" = "2e9763c69ebaae630ba35f74888db465e49e259ba1bc0eda7d06f4a067615d82"
|
||||
|
|
@ -527,9 +551,11 @@ dependencies = [
|
|||
"checksum iovec 0.1.2 (registry+https://github.com/rust-lang/crates.io-index)" = "dbe6e417e7d0975db6512b90796e8ce223145ac4e33c377e4a42882a0e88bb08"
|
||||
"checksum itoa 0.4.4 (registry+https://github.com/rust-lang/crates.io-index)" = "501266b7edd0174f8530248f87f99c88fbe60ca4ef3dd486835b8d8d53136f7f"
|
||||
"checksum kernel32-sys 0.2.2 (registry+https://github.com/rust-lang/crates.io-index)" = "7507624b29483431c0ba2d82aece8ca6cdba9382bff4ddd0f7490560c056098d"
|
||||
"checksum lazy_static 1.3.0 (registry+https://github.com/rust-lang/crates.io-index)" = "bc5729f27f159ddd61f4df6228e827e86643d4d3e7c32183cb30a1c08f604a14"
|
||||
"checksum lazycell 1.2.1 (registry+https://github.com/rust-lang/crates.io-index)" = "b294d6fa9ee409a054354afc4352b0b9ef7ca222c69b8812cbea9e7d2bf3783f"
|
||||
"checksum libc 0.2.58 (registry+https://github.com/rust-lang/crates.io-index)" = "6281b86796ba5e4366000be6e9e18bf35580adf9e63fbe2294aadb587613a319"
|
||||
"checksum log 0.4.6 (registry+https://github.com/rust-lang/crates.io-index)" = "c84ec4b527950aa83a329754b01dbe3f58361d1c5efacd1f6d68c494d08a17c6"
|
||||
"checksum maplit 1.0.1 (registry+https://github.com/rust-lang/crates.io-index)" = "08cbb6b4fef96b6d77bfc40ec491b1690c779e77b05cd9f07f787ed376fd4c43"
|
||||
"checksum matches 0.1.8 (registry+https://github.com/rust-lang/crates.io-index)" = "7ffc5c5338469d4d3ea17d269fa8ea3512ad247247c30bd2df69e68309ed0a08"
|
||||
"checksum mio 0.6.19 (registry+https://github.com/rust-lang/crates.io-index)" = "83f51996a3ed004ef184e16818edc51fadffe8e7ca68be67f9dee67d84d0ff23"
|
||||
"checksum mio-extras 2.0.5 (registry+https://github.com/rust-lang/crates.io-index)" = "46e73a04c2fa6250b8d802134d56d554a9ec2922bf977777c805ea5def61ce40"
|
||||
|
|
@ -557,7 +583,7 @@ dependencies = [
|
|||
"checksum sha-1 0.8.1 (registry+https://github.com/rust-lang/crates.io-index)" = "23962131a91661d643c98940b20fcaffe62d776a823247be80a48fcb8b6fce68"
|
||||
"checksum slab 0.4.2 (registry+https://github.com/rust-lang/crates.io-index)" = "c111b5bd5695e56cffe5129854aa230b39c93a305372fdbb2668ca2394eea9f8"
|
||||
"checksum smallvec 0.6.10 (registry+https://github.com/rust-lang/crates.io-index)" = "ab606a9c5e214920bb66c458cd7be8ef094f813f20fe77a54cc7dbfff220d4b7"
|
||||
"checksum syn 0.15.36 (registry+https://github.com/rust-lang/crates.io-index)" = "8b4f551a91e2e3848aeef8751d0d4eec9489b6474c720fd4c55958d8d31a430c"
|
||||
"checksum syn 0.15.33 (registry+https://github.com/rust-lang/crates.io-index)" = "ec52cd796e5f01d0067225a5392e70084acc4c0013fa71d55166d38a8b307836"
|
||||
"checksum typenum 1.10.0 (registry+https://github.com/rust-lang/crates.io-index)" = "612d636f949607bdf9b123b4a6f6d966dedf3ff669f7f045890d3a4a73948169"
|
||||
"checksum unicode-bidi 0.3.4 (registry+https://github.com/rust-lang/crates.io-index)" = "49f2bd0c6468a8230e1db229cff8029217cf623c767ea5d60bfbd42729ea54d5"
|
||||
"checksum unicode-normalization 0.1.8 (registry+https://github.com/rust-lang/crates.io-index)" = "141339a08b982d942be2ca06ff8b076563cbe223d1befd5450716790d44e2426"
|
||||
|
|
|
|||
|
|
@ -8,4 +8,8 @@ edition = "2018"
|
|||
ws = "0.8"
|
||||
mio = "0.6.19"
|
||||
serde = { version = "1.0.92", features = ["derive"] }
|
||||
serde_json = "1.0.39"
|
||||
serde_json = "1.0.39"
|
||||
enum_dispatch = "0.1"
|
||||
|
||||
[dev-dependencies]
|
||||
maplit = "1.0"
|
||||
94
src/client.rs
Normal file
94
src/client.rs
Normal file
|
|
@ -0,0 +1,94 @@
|
|||
use crate::SyncCommand;
|
||||
use enum_dispatch::enum_dispatch;
|
||||
use mio::Token;
|
||||
use std::cell::RefCell;
|
||||
use std::rc::Rc;
|
||||
use ws::{Result, Sender};
|
||||
|
||||
#[enum_dispatch(Client)]
|
||||
pub(crate) trait ClientTrait {
|
||||
fn send(&self, msg: &str) -> Result<()>;
|
||||
|
||||
fn token(&self) -> Token;
|
||||
}
|
||||
|
||||
#[derive(PartialEq, Clone, Debug)]
|
||||
pub(crate) struct SenderClient(Sender);
|
||||
|
||||
impl ClientTrait for SenderClient {
|
||||
fn send(&self, msg: &str) -> Result<()> {
|
||||
self.0.send(msg)
|
||||
}
|
||||
|
||||
fn token(&self) -> Token {
|
||||
self.0.token()
|
||||
}
|
||||
}
|
||||
|
||||
impl From<Sender> for SenderClient {
|
||||
fn from(sender: Sender) -> Self {
|
||||
SenderClient(sender)
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Clone, Debug)]
|
||||
pub(crate) struct MockClient {
|
||||
received: Rc<RefCell<Vec<String>>>,
|
||||
token: Token,
|
||||
}
|
||||
|
||||
impl PartialEq for MockClient {
|
||||
fn eq(&self, other: &MockClient) -> bool {
|
||||
self.token == other.token
|
||||
}
|
||||
}
|
||||
|
||||
impl ClientTrait for MockClient {
|
||||
fn send(&self, msg: &str) -> Result<()> {
|
||||
self.received.borrow_mut().push(msg.into());
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn token(&self) -> Token {
|
||||
self.token
|
||||
}
|
||||
}
|
||||
|
||||
impl MockClient {
|
||||
pub fn new(token: usize) -> Self {
|
||||
MockClient {
|
||||
received: Rc::new(RefCell::new(Vec::new())),
|
||||
token: Token(token),
|
||||
}
|
||||
}
|
||||
|
||||
pub fn received(&self) -> Vec<SyncCommand> {
|
||||
RefCell::borrow(&self.received)
|
||||
.iter()
|
||||
.map(|msg| serde_json::from_str::<SyncCommand>(msg).expect("invalid message"))
|
||||
.collect()
|
||||
}
|
||||
|
||||
pub fn clear(&self) {
|
||||
self.received.borrow_mut().clear()
|
||||
}
|
||||
}
|
||||
|
||||
#[enum_dispatch]
|
||||
#[derive(PartialEq, Clone, Debug)]
|
||||
pub(crate) enum Client {
|
||||
Sender(SenderClient),
|
||||
Mock(MockClient),
|
||||
}
|
||||
|
||||
impl Client {
|
||||
pub fn mock(token: usize) -> Self {
|
||||
Client::Mock(MockClient::new(token))
|
||||
}
|
||||
}
|
||||
|
||||
impl From<Sender> for Client {
|
||||
fn from(sender: Sender) -> Self {
|
||||
Client::Sender(sender.into())
|
||||
}
|
||||
}
|
||||
360
src/main.rs
360
src/main.rs
|
|
@ -1,11 +1,16 @@
|
|||
use mio::Token;
|
||||
use serde::{Deserialize, Serialize};
|
||||
use std::cell::RefCell;
|
||||
|
||||
use std::collections::HashMap;
|
||||
use std::rc::Rc;
|
||||
use ws::{listen, CloseCode, Error, Handler, Message, Result, Sender};
|
||||
use ws::{listen, CloseCode, Error, Handler, Message, Result};
|
||||
|
||||
#[derive(Debug, Serialize, Deserialize, PartialEq)]
|
||||
mod client;
|
||||
|
||||
use client::{Client, ClientTrait};
|
||||
use std::cell::RefCell;
|
||||
|
||||
#[derive(Debug, Serialize, Deserialize, PartialEq, Clone)]
|
||||
#[serde(tag = "type")]
|
||||
#[serde(rename_all = "lowercase")]
|
||||
enum SyncCommand {
|
||||
|
|
@ -15,6 +20,7 @@ enum SyncCommand {
|
|||
Play { session: String, play: bool },
|
||||
}
|
||||
|
||||
#[derive(PartialEq, Debug)]
|
||||
struct Session {
|
||||
owner: Token,
|
||||
clients: HashMap<Token, Client>,
|
||||
|
|
@ -46,31 +52,6 @@ impl Session {
|
|||
}
|
||||
}
|
||||
|
||||
trait ClientTrait {
|
||||
fn send(&self, msg: &str) -> Result<()>;
|
||||
|
||||
fn token(&self) -> Token;
|
||||
}
|
||||
|
||||
#[derive(Clone, Debug)]
|
||||
struct Client(Sender);
|
||||
|
||||
impl ClientTrait for Client {
|
||||
fn send(&self, msg: &str) -> Result<()> {
|
||||
self.0.send(msg)
|
||||
}
|
||||
|
||||
fn token(&self) -> Token {
|
||||
self.0.token()
|
||||
}
|
||||
}
|
||||
|
||||
impl From<Sender> for Client {
|
||||
fn from(sender: Sender) -> Self {
|
||||
Client(sender)
|
||||
}
|
||||
}
|
||||
|
||||
struct Server {
|
||||
out: Client,
|
||||
sessions: Rc<RefCell<HashMap<String, Session>>>,
|
||||
|
|
@ -190,6 +171,7 @@ fn main() {
|
|||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
use maplit::hashmap;
|
||||
|
||||
#[test]
|
||||
fn test_deserialize() {
|
||||
|
|
@ -201,4 +183,326 @@ mod tests {
|
|||
serde_json::from_str(input).unwrap()
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_create() {
|
||||
let sessions: RefCell<HashMap<String, Session>> = RefCell::new(HashMap::new());
|
||||
let sender = Client::mock(1);
|
||||
let command = SyncCommand::Create {
|
||||
session: "test".into(),
|
||||
};
|
||||
|
||||
handle_command(command, &sender, &sessions);
|
||||
|
||||
assert_eq!(
|
||||
hashmap! {
|
||||
"test".into() => Session {
|
||||
owner: Token(1),
|
||||
clients: hashmap![],
|
||||
tick: 0,
|
||||
playing: false
|
||||
}
|
||||
},
|
||||
sessions.into_inner()
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_play_owner() {
|
||||
let sessions: RefCell<HashMap<String, Session>> = RefCell::new(hashmap! {
|
||||
"test".into() => Session {
|
||||
owner: Token(1),
|
||||
clients: hashmap![],
|
||||
tick: 0,
|
||||
playing: false
|
||||
}
|
||||
});
|
||||
let sender = Client::mock(1);
|
||||
let command = SyncCommand::Play {
|
||||
session: "test".into(),
|
||||
play: true,
|
||||
};
|
||||
|
||||
handle_command(command, &sender, &sessions);
|
||||
|
||||
assert_eq!(
|
||||
hashmap! {
|
||||
"test".into() => Session {
|
||||
owner: Token(1),
|
||||
clients: hashmap![],
|
||||
tick: 0,
|
||||
playing: true
|
||||
}
|
||||
},
|
||||
sessions.into_inner()
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_play_not_owner() {
|
||||
let sessions: RefCell<HashMap<String, Session>> = RefCell::new(hashmap! {
|
||||
"test".into() => Session {
|
||||
owner: Token(1),
|
||||
clients: hashmap![],
|
||||
tick: 0,
|
||||
playing: false
|
||||
}
|
||||
});
|
||||
let sender = Client::mock(2);
|
||||
let command = SyncCommand::Play {
|
||||
session: "test".into(),
|
||||
play: true,
|
||||
};
|
||||
|
||||
handle_command(command, &sender, &sessions);
|
||||
|
||||
assert_eq!(
|
||||
hashmap! {
|
||||
"test".into() => Session {
|
||||
owner: Token(1),
|
||||
clients: hashmap![],
|
||||
tick: 0,
|
||||
playing: false
|
||||
}
|
||||
},
|
||||
sessions.into_inner()
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_tick_owner() {
|
||||
let sessions: RefCell<HashMap<String, Session>> = RefCell::new(hashmap! {
|
||||
"test".into() => Session {
|
||||
owner: Token(1),
|
||||
clients: hashmap![],
|
||||
tick: 0,
|
||||
playing: false
|
||||
}
|
||||
});
|
||||
let sender = Client::mock(1);
|
||||
let command = SyncCommand::Tick {
|
||||
session: "test".into(),
|
||||
tick: 99,
|
||||
};
|
||||
|
||||
handle_command(command, &sender, &sessions);
|
||||
|
||||
assert_eq!(
|
||||
hashmap! {
|
||||
"test".into() => Session {
|
||||
owner: Token(1),
|
||||
clients: hashmap![],
|
||||
tick: 99,
|
||||
playing: false
|
||||
}
|
||||
},
|
||||
sessions.into_inner()
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_tick_not_owner() {
|
||||
let sessions: RefCell<HashMap<String, Session>> = RefCell::new(hashmap! {
|
||||
"test".into() => Session {
|
||||
owner: Token(1),
|
||||
clients: hashmap![],
|
||||
tick: 0,
|
||||
playing: false
|
||||
}
|
||||
});
|
||||
let sender = Client::mock(2);
|
||||
let command = SyncCommand::Tick {
|
||||
session: "test".into(),
|
||||
tick: 99,
|
||||
};
|
||||
|
||||
handle_command(command, &sender, &sessions);
|
||||
|
||||
assert_eq!(
|
||||
hashmap! {
|
||||
"test".into() => Session {
|
||||
owner: Token(1),
|
||||
clients: hashmap![],
|
||||
tick: 0,
|
||||
playing: false
|
||||
}
|
||||
},
|
||||
sessions.into_inner()
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_join() {
|
||||
let sessions: RefCell<HashMap<String, Session>> = RefCell::new(hashmap! {
|
||||
"test".into() => Session {
|
||||
owner: Token(1),
|
||||
clients: hashmap![],
|
||||
tick: 99,
|
||||
playing: true
|
||||
}
|
||||
});
|
||||
let sender = Client::mock(2);
|
||||
let command = SyncCommand::Join {
|
||||
session: "test".into(),
|
||||
};
|
||||
|
||||
handle_command(command, &sender, &sessions);
|
||||
|
||||
assert_eq!(
|
||||
hashmap! {
|
||||
"test".into() => Session {
|
||||
owner: Token(1),
|
||||
clients: hashmap![Token(2) => sender.clone()],
|
||||
tick: 99,
|
||||
playing: true
|
||||
}
|
||||
},
|
||||
sessions.into_inner()
|
||||
);
|
||||
|
||||
if let Client::Mock(mock) = sender {
|
||||
assert_eq!(
|
||||
vec![
|
||||
SyncCommand::Tick {
|
||||
session: "test".into(),
|
||||
tick: 99
|
||||
},
|
||||
SyncCommand::Play {
|
||||
session: "test".into(),
|
||||
play: true
|
||||
}
|
||||
],
|
||||
mock.received()
|
||||
);
|
||||
};
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_join_non_existing() {
|
||||
let sessions: RefCell<HashMap<String, Session>> = RefCell::new(hashmap! {
|
||||
"test".into() => Session {
|
||||
owner: Token(1),
|
||||
clients: hashmap![],
|
||||
tick: 0,
|
||||
playing: false
|
||||
}
|
||||
});
|
||||
let sender = Client::mock(2);
|
||||
let command = SyncCommand::Join {
|
||||
session: "test2".into(),
|
||||
};
|
||||
|
||||
handle_command(command, &sender, &sessions);
|
||||
|
||||
assert_eq!(
|
||||
hashmap! {
|
||||
"test".into() => Session {
|
||||
owner: Token(1),
|
||||
clients: hashmap![],
|
||||
tick: 0,
|
||||
playing: false
|
||||
}
|
||||
},
|
||||
sessions.into_inner()
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_forward() {
|
||||
let sessions: RefCell<HashMap<String, Session>> = RefCell::new(hashmap! {
|
||||
"test".into() => Session {
|
||||
owner: Token(1),
|
||||
clients: hashmap![],
|
||||
tick: 99,
|
||||
playing: true
|
||||
},
|
||||
"test2".into() => Session {
|
||||
owner: Token(1),
|
||||
clients: hashmap![],
|
||||
tick: 99,
|
||||
playing: true
|
||||
}
|
||||
});
|
||||
let owner = Client::mock(1);
|
||||
let sender1 = Client::mock(2);
|
||||
let sender2 = Client::mock(3);
|
||||
let command = SyncCommand::Join {
|
||||
session: "test".into(),
|
||||
};
|
||||
|
||||
handle_command(command, &sender1, &sessions);
|
||||
|
||||
let command = SyncCommand::Join {
|
||||
session: "test2".into(),
|
||||
};
|
||||
handle_command(command, &sender2, &sessions);
|
||||
|
||||
if let Client::Mock(mock) = &sender1 {
|
||||
mock.clear();
|
||||
}
|
||||
if let Client::Mock(mock) = &sender2 {
|
||||
mock.clear();
|
||||
}
|
||||
|
||||
let command = SyncCommand::Tick {
|
||||
session: "test".into(),
|
||||
tick: 999,
|
||||
};
|
||||
|
||||
handle_command(command, &owner, &sessions);
|
||||
|
||||
if let Client::Mock(mock) = sender1 {
|
||||
assert_eq!(
|
||||
vec![SyncCommand::Tick {
|
||||
session: "test".into(),
|
||||
tick: 999
|
||||
},],
|
||||
mock.received()
|
||||
);
|
||||
};
|
||||
if let Client::Mock(mock) = sender2 {
|
||||
assert_eq!(0, mock.received().len());
|
||||
};
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_forward_non_owner() {
|
||||
let sessions: RefCell<HashMap<String, Session>> = RefCell::new(hashmap! {
|
||||
"test".into() => Session {
|
||||
owner: Token(1),
|
||||
clients: hashmap![],
|
||||
tick: 99,
|
||||
playing: true
|
||||
}
|
||||
});
|
||||
let sender1 = Client::mock(2);
|
||||
let sender2 = Client::mock(3);
|
||||
let command = SyncCommand::Join {
|
||||
session: "test".into(),
|
||||
};
|
||||
|
||||
handle_command(command.clone(), &sender1, &sessions);
|
||||
handle_command(command.clone(), &sender2, &sessions);
|
||||
|
||||
if let Client::Mock(mock) = &sender1 {
|
||||
mock.clear();
|
||||
}
|
||||
if let Client::Mock(mock) = &sender2 {
|
||||
mock.clear();
|
||||
}
|
||||
|
||||
let command = SyncCommand::Tick {
|
||||
session: "test".into(),
|
||||
tick: 999,
|
||||
};
|
||||
|
||||
handle_command(command, &sender1, &sessions);
|
||||
|
||||
if let Client::Mock(mock) = sender1 {
|
||||
assert_eq!(0, mock.received().len());
|
||||
};
|
||||
if let Client::Mock(mock) = sender2 {
|
||||
assert_eq!(0, mock.received().len());
|
||||
};
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue