Skip to content

Commit

Permalink
add un/load_room to emulated monoliths
Browse files Browse the repository at this point in the history
  • Loading branch information
dyc3 committed Oct 12, 2023
1 parent 9520d8f commit 6b707ac
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 11 deletions.
12 changes: 2 additions & 10 deletions crates/harness-tests/src/routing.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,7 @@ async fn route_http_to_correct_monolith(ctx: &mut TestRunner) {
);

m.show().await;
m.send(MsgM2B::Loaded {
name: "foo".to_owned().into(),
metadata: RoomMetadata::default(),
})
.await;
m.load_room("foo".to_string()).await;

// Without this sleep, this test can trigger a race condition where the client connects to the balancer before the monolith has the room loaded.
// This will cause the other monolith to get the room loaded, and the client will connect to that monolith instead.
Expand Down Expand Up @@ -104,11 +100,7 @@ async fn route_ws_to_correct_monolith(ctx: &mut TestRunner) {

let mut m = Monolith::new(ctx).await.unwrap();
m.show().await;
m.send(MsgM2B::Loaded {
name: "foo".to_owned().into(),
metadata: RoomMetadata::default(),
})
.await;
m.load_room("foo".to_string()).await;

// Without this sleep, this test can trigger a race condition where the client connects to the balancer before the monolith has the room loaded.
// This will cause the other monolith to get the room loaded, and the client will connect to that monolith instead.
Expand Down
32 changes: 31 additions & 1 deletion crates/harness/src/monolith.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ use http_body_util::{BodyExt, Full};
use hyper::{body::Incoming as IncomingBody, Request};
use hyper::{service::Service, Response};

use ott_balancer_protocol::monolith::*;
use ott_balancer_protocol::{monolith::*, RoomName};
use tokio::{net::TcpListener, sync::Notify};
use tracing::warn;
use tungstenite::Message;
Expand Down Expand Up @@ -47,6 +47,7 @@ pub(crate) struct MonolithState {
received_http: Vec<MockRequest>,
/// A mapping from request path to response body for mocking HTTP responses.
response_mocks: HashMap<String, (MockRespParts, Bytes)>,
rooms: HashMap<RoomName, RoomMetadata>,
}

impl Monolith {
Expand Down Expand Up @@ -237,6 +238,35 @@ impl Monolith {
pub fn collect_mock_http(&self) -> Vec<MockRequest> {
self.state.lock().unwrap().received_http.clone()
}

pub async fn load_room(&mut self, room: impl Into<RoomName> + Clone) {
let room = room.into();
let meta = RoomMetadata::default();
self.state
.lock()
.unwrap()
.rooms
.insert(room.clone(), meta.clone());
if self.connected() {
self.send(MsgM2B::Loaded {
name: room,
metadata: meta,
})
.await;
}
}

pub async fn unload_room(&mut self, room: impl Into<RoomName> + Clone) {
let room = room.into();
self.state
.lock()
.unwrap()
.rooms
.remove(&room.clone().clone());
if self.connected() {
self.send(MsgM2B::Unloaded { room }).await;
}
}
}

impl Drop for Monolith {
Expand Down

0 comments on commit 6b707ac

Please sign in to comment.