Skip to content

Commit 3b3efe0

Browse files
committed
add a unit test
1 parent 82b8584 commit 3b3efe0

File tree

1 file changed

+84
-0
lines changed

1 file changed

+84
-0
lines changed

crates/ott-balancer/src/balancer.rs

+84
Original file line numberDiff line numberDiff line change
@@ -696,3 +696,87 @@ pub async fn dispatch_monolith_message(
696696

697697
Ok(())
698698
}
699+
700+
#[cfg(test)]
701+
mod test {
702+
use std::net::Ipv4Addr;
703+
704+
use crate::discovery::{HostOrIp, MonolithConnectionConfig};
705+
706+
use super::*;
707+
708+
#[tokio::test]
709+
async fn test_clients_add_remove() {
710+
// a bunch of setup
711+
let room_name = RoomName::from("test");
712+
let mut ctx = BalancerContext::new();
713+
let (monolith_outbound_tx, _monolith_outbound_rx) = tokio::sync::mpsc::channel(100);
714+
let monolith_outbound_tx = Arc::new(monolith_outbound_tx);
715+
let (client_inbound_tx, _client_inbound_rx) = tokio::sync::mpsc::channel(100);
716+
let (client_unicast_tx, _client_unicast_rx) = tokio::sync::mpsc::channel(100);
717+
let monolith_id = uuid::Uuid::new_v4().into();
718+
let monolith = BalancerMonolith::new(
719+
NewMonolith {
720+
id: monolith_id,
721+
region: "unknown".into(),
722+
config: MonolithConnectionConfig {
723+
host: HostOrIp::Ip(Ipv4Addr::LOCALHOST.into()),
724+
port: 3002,
725+
},
726+
proxy_port: 3000,
727+
},
728+
monolith_outbound_tx,
729+
client_inbound_tx,
730+
);
731+
let client_id = uuid::Uuid::new_v4().into();
732+
let client = BalancerClient::new(
733+
NewClient {
734+
id: client_id,
735+
room: room_name.clone(),
736+
token: "test".into(),
737+
},
738+
client_unicast_tx,
739+
);
740+
ctx.add_monolith(monolith);
741+
ctx.add_room(room_name.clone(), RoomLocator::new(monolith_id, 0))
742+
.expect("failed to add room");
743+
744+
// add a client
745+
ctx.add_client(client, monolith_id)
746+
.await
747+
.expect("failed to add client");
748+
749+
// make sure the client is in the context
750+
assert!(ctx.clients.contains_key(&client_id));
751+
752+
// make sure the client is in the monolith
753+
assert!(ctx
754+
.monoliths
755+
.get(&monolith_id)
756+
.unwrap()
757+
.rooms()
758+
.get(&room_name)
759+
.unwrap()
760+
.clients()
761+
.contains(&client_id));
762+
763+
// remove the client
764+
ctx.remove_client(client_id)
765+
.await
766+
.expect("failed to remove client");
767+
768+
// make sure the client is not in the context
769+
assert!(!ctx.clients.contains_key(&client_id));
770+
771+
// make sure the client is not in the monolith
772+
assert!(!ctx
773+
.monoliths
774+
.get(&monolith_id)
775+
.unwrap()
776+
.rooms()
777+
.get(&room_name)
778+
.unwrap()
779+
.clients()
780+
.contains(&client_id));
781+
}
782+
}

0 commit comments

Comments
 (0)