Browse Source

Migrate RoomJoinRequestHandler.

main
Titouan Rigoudy 2 years ago
parent
commit
c5d0d9558e
3 changed files with 73 additions and 235 deletions
  1. +0
    -2
      client/src/handlers/mod.rs
  2. +0
    -161
      client/src/handlers/room_join_request_handler.rs
  3. +73
    -72
      client/src/room/event.rs

+ 0
- 2
client/src/handlers/mod.rs View File

@ -2,7 +2,6 @@ mod login_status_request_handler;
mod peer_address_response_handler;
mod peer_connect_request_handler;
mod privileged_users_response_handler;
mod room_join_request_handler;
mod room_join_response_handler;
mod room_list_request_handler;
mod room_list_response_handler;
@ -14,7 +13,6 @@ pub use login_status_request_handler::LoginStatusRequestHandler;
pub use peer_address_response_handler::PeerAddressResponseHandler;
pub use peer_connect_request_handler::PeerConnectRequestHandler;
pub use privileged_users_response_handler::PrivilegedUsersResponseHandler;
pub use room_join_request_handler::RoomJoinRequestHandler;
pub use room_join_response_handler::RoomJoinResponseHandler;
pub use room_list_request_handler::RoomListRequestHandler;
pub use room_list_response_handler::RoomListResponseHandler;


+ 0
- 161
client/src/handlers/room_join_request_handler.rs View File

@ -1,161 +0,0 @@
use anyhow::Context as AnyhowContext;
use log::error;
use solstice_proto::server::RoomJoinRequest;
use solstice_proto::ServerRequest;
use crate::context::Context;
use crate::control;
use crate::message_handler::MessageHandler;
#[derive(Debug, Default)]
pub struct RoomJoinRequestHandler;
fn start_joining(context: &mut Context, room_name: &str) -> anyhow::Result<()> {
let room = context.state.rooms.get_mut_strict(room_name)?;
room.start_joining().map_err(|err| {
let response =
control::Response::RoomJoinResponse(control::RoomJoinResponse {
room_name: room_name.to_string(),
room: room.clone_state(),
});
if let Err(err) = context.control_response_tx.blocking_send(response) {
error!(
"Failed to send RoomJoinResponse for room {}: {}",
room_name, err
);
}
err.into()
})
}
impl MessageHandler for RoomJoinRequestHandler {
type Message = String;
fn run(
self,
context: &mut Context,
room_name: &Self::Message,
) -> anyhow::Result<()> {
start_joining(context, room_name).context("joining room")?;
context
.server_request_tx
.blocking_send(ServerRequest::RoomJoinRequest(RoomJoinRequest {
room_name: room_name.clone(),
}))
.context("sending server request")?;
Ok(())
}
fn name() -> String {
"RoomJoinRequestHandler".to_string()
}
}
#[cfg(test)]
mod tests {
use anyhow::Context;
use solstice_proto::server::RoomJoinRequest;
use solstice_proto::ServerRequest;
use crate::context::{ContextBundle, ContextOptions};
use crate::control;
use crate::message_handler::MessageHandler;
use crate::room::{RoomMembership, RoomState, RoomVisibility};
use super::RoomJoinRequestHandler;
#[test]
fn run_failure() -> anyhow::Result<()> {
let mut bundle = ContextBundle::default();
RoomJoinRequestHandler::default()
.run(&mut bundle.context, &"bleep".to_string())
.unwrap_err();
// Room state has not changed.
assert_eq!(bundle.context.state.rooms.get_room_list(), vec![]);
// Close the channel, so we can observe it was empty without hanging.
drop(bundle.context.server_request_tx);
assert_eq!(bundle.server_request_rx.blocking_recv(), None);
Ok(())
}
#[test]
fn run_already_joined_responds_immediately() -> anyhow::Result<()> {
let mut room = RoomState::new(RoomVisibility::Public, 3);
room.membership = RoomMembership::Member;
let mut options = ContextOptions::default();
options
.initial_state
.rooms
.insert("bleep".to_string(), room.clone());
let mut bundle = ContextBundle::new(options);
RoomJoinRequestHandler::default()
.run(&mut bundle.context, &"bleep".to_string())
.unwrap_err();
// Room state has not changed.
assert_eq!(
bundle.context.state.rooms.get_room_list(),
vec![("bleep".to_string(), room.clone())]
);
assert_eq!(
bundle.control_response_rx.blocking_recv(),
Some(control::Response::RoomJoinResponse(
control::RoomJoinResponse {
room_name: "bleep".to_string(),
room,
}
))
);
Ok(())
}
#[test]
fn run_success() -> anyhow::Result<()> {
let mut options = ContextOptions::default();
options.initial_state.rooms.insert(
"bleep".to_string(),
RoomState::new(RoomVisibility::Public, 3),
);
let mut bundle = ContextBundle::new(options);
RoomJoinRequestHandler::default()
.run(&mut bundle.context, &"bleep".to_string())
.context("running handler")?;
let request = bundle.server_request_rx.blocking_recv().unwrap();
// Room state has been altered to reflect the request.
assert_eq!(
bundle
.context
.state
.rooms
.get_strict("bleep")
.context("getting room")?
.clone_state()
.membership,
RoomMembership::Joining
);
// The request is forwarded onwards.
assert_eq!(
request,
ServerRequest::RoomJoinRequest(RoomJoinRequest {
room_name: "bleep".to_string(),
})
);
Ok(())
}
}

+ 73
- 72
client/src/room/event.rs View File

@ -132,12 +132,15 @@ pub mod testing {
#[cfg(test)]
mod tests {
use crate::context::ContextBundle;
use solstice_proto::server;
use crate::context::{ContextBundle, ContextOptions};
use crate::room::{RoomMembership, RoomState, RoomVisibility};
use super::*;
#[test]
fn run_failure() -> anyhow::Result<()> {
fn handle_room_join_request_failure() {
let mut bundle = ContextBundle::default();
RoomEventHandler
@ -153,83 +156,81 @@ mod tests {
// Close the channel, so we can observe it was empty without hanging.
drop(bundle.context.server_request_tx);
assert_eq!(bundle.server_request_rx.blocking_recv(), None);
Ok(())
}
/*
#[test]
fn run_already_joined_responds_immediately() -> anyhow::Result<()> {
let mut room = RoomState::new(RoomVisibility::Public, 3);
room.membership = RoomMembership::Member;
let mut options = ContextOptions::default();
options
.initial_state
.rooms
.insert("bleep".to_string(), room.clone());
let mut bundle = ContextBundle::new(options);
#[test]
fn handle_room_join_request_already_joined() {
let mut room = RoomState::new(RoomVisibility::Public, 3);
room.membership = RoomMembership::Member;
RoomJoinRequestHandler::default()
.run(&mut bundle.context, &"bleep".to_string())
.unwrap_err();
let mut options = ContextOptions::default();
options
.initial_state
.rooms
.insert("bleep".to_string(), room.clone());
let mut bundle = ContextBundle::new(options);
// Room state has not changed.
assert_eq!(
bundle.context.state.rooms.get_room_list(),
vec![("bleep".to_string(), room.clone())]
);
RoomEventHandler
.handle(
&mut bundle.context,
RoomEvent::JoinRequest("bleep".to_string()),
)
.unwrap_err();
assert_eq!(
bundle.control_response_rx.blocking_recv(),
Some(control::Response::RoomJoinResponse(
control::RoomJoinResponse {
room_name: "bleep".to_string(),
room,
}
))
);
// Room state has not changed.
assert_eq!(
bundle.context.state.rooms.get_room_list(),
vec![("bleep".to_string(), room.clone())]
);
assert_eq!(
bundle.control_response_rx.blocking_recv(),
Some(control::Response::RoomJoinResponse(
control::RoomJoinResponse {
room_name: "bleep".to_string(),
room,
}
))
);
}
Ok(())
}
#[test]
fn handle_room_join_request_success() {
let mut options = ContextOptions::default();
options.initial_state.rooms.insert(
"bleep".to_string(),
RoomState::new(RoomVisibility::Public, 3),
);
let mut bundle = ContextBundle::new(options);
#[test]
fn run_success() -> anyhow::Result<()> {
let mut options = ContextOptions::default();
options.initial_state.rooms.insert(
"bleep".to_string(),
RoomState::new(RoomVisibility::Public, 3),
);
let mut bundle = ContextBundle::new(options);
RoomJoinRequestHandler::default()
.run(&mut bundle.context, &"bleep".to_string())
.context("running handler")?;
let request = bundle.server_request_rx.blocking_recv().unwrap();
// Room state has been altered to reflect the request.
assert_eq!(
bundle
.context
.state
.rooms
.get_strict("bleep")
.context("getting room")?
.clone_state()
.membership,
RoomMembership::Joining
);
RoomEventHandler
.handle(
&mut bundle.context,
RoomEvent::JoinRequest("bleep".to_string()),
)
.expect("handling request");
// The request is forwarded onwards.
assert_eq!(
request,
ServerRequest::RoomJoinRequest(RoomJoinRequest {
room_name: "bleep".to_string(),
})
);
let request = bundle.server_request_rx.blocking_recv().unwrap();
Ok(())
}
*/
// Room state has been altered to reflect the request.
assert_eq!(
bundle
.context
.state
.rooms
.get_strict("bleep")
.expect("getting room")
.clone_state()
.membership,
RoomMembership::Joining
);
// The request is forwarded onwards.
assert_eq!(
request,
ServerRequest::RoomJoinRequest(server::RoomJoinRequest {
room_name: "bleep".to_string(),
})
);
}
}

Loading…
Cancel
Save