Skip to content

Commit

Permalink
chore: add unit test for FlightQuery try_from
Browse files Browse the repository at this point in the history
  • Loading branch information
amsmith-pro committed Oct 3, 2023
1 parent 4c436bd commit 40506a2
Show file tree
Hide file tree
Showing 9 changed files with 142 additions and 379 deletions.
2 changes: 1 addition & 1 deletion server/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ pub struct Config {

impl Default for Config {
fn default() -> Self {
log::warn!("(Config default) Creating Config object with default values.");
log::warn!("(default) Creating Config object with default values.");
Self::new()
}
}
Expand Down
4 changes: 2 additions & 2 deletions server/src/grpc/api/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ pub fn unconfirmed_flight_plans() -> &'static Mutex<HashMap<String, flight_plan:
/// Gets itinerary from hash map of unconfirmed itineraries
pub fn get_draft_itinerary_by_id(id: &str) -> Option<Vec<String>> {
let Ok(itineraries) = unconfirmed_itineraries().lock() else {
grpc_error!("(get_draft_itinerary_by_id) mutex Lock Error getting itinerary from temp storage");
grpc_error!("(get_draft_itinerary_by_id) mutex Lock Error getting itinerary from temp storage.");
return None;
};

Expand All @@ -44,7 +44,7 @@ pub fn get_draft_itinerary_by_id(id: &str) -> Option<Vec<String>> {
/// Gets flight plan from hash map of unconfirmed flight plans
pub fn get_draft_fp_by_id(id: &str) -> Option<flight_plan::Data> {
let Ok(flight_plans) = unconfirmed_flight_plans().lock() else {
grpc_error!("(get_draft_fp_by_id) mutex Lock Error getting flight plan from temp storage");
grpc_error!("(get_draft_fp_by_id) mutex Lock Error getting flight plan from temp storage.");
return None;
};

Expand Down
394 changes: 88 additions & 306 deletions server/src/grpc/api/query_flight.rs

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion server/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ pub fn init_logger(config: &Config) {
let log_cfg: &str = config.log_config.as_str();
if let Err(e) = log4rs::init_file(log_cfg, Default::default()) {
panic!(
"(logger) could not parse log config {} found in config {:?}: {}.",
"(init_logger) could not parse log config {} found in config {:?}: {}.",
log_cfg, config, e
);
}
Expand Down
4 changes: 2 additions & 2 deletions server/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use svc_scheduler::*;
#[tokio::main]
#[cfg(not(tarpaulin_include))]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
println!("(svc-scheduler) server startup.");
println!("(main) server startup.");

// Will use default config settings if no environment vars are found.
let config = Config::try_from_env().unwrap_or_default();
Expand All @@ -19,7 +19,7 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> {
// Make sure all log message are written/ displayed before shutdown
log::logger().flush();

info!("(svc-scheduler) server shutdown.");
info!("(main) server shutdown.");

Ok(())
}
16 changes: 8 additions & 8 deletions server/src/router/flight_plan.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ impl TryFrom<flight_plan::Object> for FlightPlanSchedule {
fn try_from(flight_plan: flight_plan::Object) -> Result<Self, Self::Error> {
let Some(data) = flight_plan.data else {
router_error!(
"(get_flight_plan_schedule) Flight plan [{}] has no data.",
"(try_from) Flight plan [{}] has no data.",
flight_plan.id
);
return Err(FlightPlanError::InvalidData)
Expand All @@ -51,7 +51,7 @@ impl TryFrom<flight_plan::Object> for FlightPlanSchedule {
Some(departure_time) => departure_time.into(),
None => {
router_error!(
"(get_flight_plan_schedule) Flight plan [{}] has no scheduled departure.",
"(try_from) Flight plan [{}] has no scheduled departure.",
flight_plan.id
);
return Err(FlightPlanError::InvalidData);
Expand All @@ -62,7 +62,7 @@ impl TryFrom<flight_plan::Object> for FlightPlanSchedule {
Some(arrival_time) => arrival_time.into(),
None => {
router_error!(
"(get_flight_plan_schedule) Flight plan [{}] has no scheduled arrival.",
"(try_from) Flight plan [{}] has no scheduled arrival.",
flight_plan.id
);
return Err(FlightPlanError::InvalidData);
Expand All @@ -74,7 +74,7 @@ impl TryFrom<flight_plan::Object> for FlightPlanSchedule {
//
let Some(departure_vertiport_id) = data.departure_vertiport_id else {
router_error!(
"(get_flight_plan_schedule) Flight plan [{}] has no departure vertiport.",
"(try_from) Flight plan [{}] has no departure vertiport.",
flight_plan.id
);
return Err(FlightPlanError::InvalidData)
Expand All @@ -84,7 +84,7 @@ impl TryFrom<flight_plan::Object> for FlightPlanSchedule {
Ok(id) => id.to_string(),
Err(e) => {
router_error!(
"(get_flight_plan_schedule) Flight plan [{}] has invalid departure vertiport id: {}",
"(try_from) Flight plan [{}] has invalid departure vertiport id: {}",
flight_plan.id,
e
);
Expand All @@ -94,7 +94,7 @@ impl TryFrom<flight_plan::Object> for FlightPlanSchedule {

let Some(arrival_vertiport_id) = data.destination_vertiport_id else {
router_error!(
"(get_flight_plan_schedule) Flight plan [{}] has no arrival vertiport.",
"(try_from) Flight plan [{}] has no arrival vertiport.",
flight_plan.id
);
return Err(FlightPlanError::InvalidData)
Expand All @@ -104,7 +104,7 @@ impl TryFrom<flight_plan::Object> for FlightPlanSchedule {
Ok(id) => id.to_string(),
Err(e) => {
router_error!(
"(get_flight_plan_schedule) Flight plan [{}] has invalid arrival vertiport id: {}",
"(try_from) Flight plan [{}] has invalid arrival vertiport id: {}",
flight_plan.id,
e
);
Expand All @@ -117,7 +117,7 @@ impl TryFrom<flight_plan::Object> for FlightPlanSchedule {
//
let Ok(vehicle_id) = Uuid::parse_str(&data.vehicle_id) else {
router_error!(
"(get_flight_plan_schedule) Flight plan [{}] has no vehicle.",
"(try_from) Flight plan [{}] has no vehicle.",
flight_plan.id
);
return Err(FlightPlanError::InvalidData)
Expand Down
42 changes: 16 additions & 26 deletions server/src/router/itinerary.rs
Original file line number Diff line number Diff line change
Expand Up @@ -81,9 +81,7 @@ pub async fn get_itineraries(
Ok(itinerary) => itineraries.push(itinerary),
Err(ItineraryError::ClientError) => {
// exit immediately if svc-gis is down, don't allow new flights
router_error!(
"(get_vehicle_availability) Could not determine path; client error."
);
router_error!("(get_itineraries) Could not determine path; client error.");
return Err(ItineraryError::ClientError);
}
_ => {
Expand Down Expand Up @@ -126,7 +124,7 @@ async fn aircraft_selection(
}
Err(ItineraryError::ClientError) => {
// exit immediately if svc-gis is down, don't allow new flights
router_error!("(get_vehicle_availability) Could not determine path; client error.");
router_error!("(aircraft_selection) Could not determine path; client error.");
return Err(ItineraryError::ClientError);
}
_ => {
Expand All @@ -152,23 +150,23 @@ async fn get_itinerary(
// Must be some overlap between the flight window and the available timeslot
let Ok(overlap) = availability.timeslot.overlap(flight_window) else {
router_debug!(
"(is_aircraft_available) No overlap between flight window and available timeslot."
"(get_itinerary) No overlap between flight window and available timeslot."
);

return Err(ItineraryError::ScheduleConflict);
};

let Some(ref departure_vertiport_id) = flight_plan.departure_vertiport_id else {
router_error!(
"(get_vehicle_itinerary) Flight plan doesn't have departure_vertiport_id.",
"(get_itinerary) Flight plan doesn't have departure_vertiport_id.",
);

return Err(ItineraryError::InvalidData);
};

let Some(ref arrival_vertiport_id) = flight_plan.destination_vertiport_id else {
router_error!(
"(get_vehicle_itinerary) Flight plan doesn't have destination_vertiport_id.",
"(get_itinerary) Flight plan doesn't have destination_vertiport_id.",
);

return Err(ItineraryError::InvalidData);
Expand Down Expand Up @@ -199,7 +197,7 @@ async fn get_itinerary(
// is blocking journeys from this depart timeslot
// Break out and try the next depart timeslot
router_debug!(
"(get_vehicle_availability) No path found from vertiport {}
"(get_itinerary) No path found from vertiport {}
to vertiport {} (from {} to {}).",
availability.vertiport_id,
departure_vertiport_id,
Expand All @@ -211,7 +209,7 @@ async fn get_itinerary(
}
Err(BestPathError::ClientError) => {
// exit immediately if svc-gis is down, don't allow new flights
router_error!("(get_vehicle_availability) Could not determine path.");
router_error!("(get_itinerary) Could not determine path.");
return Err(ItineraryError::ClientError);
}
};
Expand All @@ -227,9 +225,7 @@ async fn get_itinerary(
if scheduled_arrival > availability.timeslot.time_end {
// This flight plan would end after the available timeslot
// Break out and try the next available timeslot
router_debug!(
"(get_vehicle_availability) Flight plan would end after available timeslot."
);
router_debug!("(get_itinerary) Flight plan would end after available timeslot.");

return Err(ItineraryError::ScheduleConflict);
}
Expand Down Expand Up @@ -257,9 +253,7 @@ async fn get_itinerary(
Some(last) => match &last.scheduled_arrival {
Some(s) => s.clone().into(),
None => {
router_error!(
"(get_vehicle_availability) Last flight plan has no scheduled arrival."
);
router_error!("(get_itinerary) Last flight plan has no scheduled arrival.");

return Err(ItineraryError::InvalidData);
}
Expand All @@ -272,17 +266,15 @@ async fn get_itinerary(
if scheduled_arrival > availability.timeslot.time_end {
// This flight plan would end after the available timeslot
// Break out and try the next available timeslot
router_debug!(
"(get_vehicle_availability) Flight plan would end after available timeslot."
);
router_debug!("(get_itinerary) Flight plan would end after available timeslot.");

return Err(ItineraryError::ScheduleConflict);
}

if scheduled_arrival > flight_window.time_end {
// This flight plan would end after the flight window
// Break out and try the next available timeslot
router_debug!("(get_vehicle_availability) Flight plan would end after flight window.");
router_debug!("(get_itinerary) Flight plan would end after flight window.");

return Err(ItineraryError::ScheduleConflict);
}
Expand All @@ -303,7 +295,7 @@ async fn get_itinerary(
// right now it boomerangs back to its original last_vertiport_id
let Some(last) = flight_plans.last() else {
router_error!(
"(get_vehicle_availability) No flight plans found for vehicle {}.",
"(get_itinerary) No flight plans found for vehicle {}.",
vehicle_id
);

Expand All @@ -312,7 +304,7 @@ async fn get_itinerary(

let Some(last_arrival) = &last.scheduled_arrival else {
router_error!(
"(get_vehicle_availability) Last flight plan has no scheduled arrival."
"(get_itinerary) Last flight plan has no scheduled arrival."
);

return Err(ItineraryError::InvalidData);
Expand All @@ -336,7 +328,7 @@ async fn get_itinerary(
// is blocking journeys from this depart timeslot
// Break out and try the next depart timeslot
router_debug!(
"(get_vehicle_availability) No path found from vertiport {}
"(get_itinerary) No path found from vertiport {}
to vertiport {} (from {} to {}).",
arrival_vertiport_id,
availability.vertiport_id,
Expand All @@ -348,7 +340,7 @@ async fn get_itinerary(
}
Err(BestPathError::ClientError) => {
// exit immediately if svc-gis is down, don't allow new flights
router_error!("(get_vehicle_availability) Could not determine path.");
router_error!("(get_itinerary) Could not determine path.");
return Err(ItineraryError::ClientError);
}
};
Expand All @@ -360,9 +352,7 @@ async fn get_itinerary(
if scheduled_arrival > availability.timeslot.time_end {
// This flight plan would end after the available timeslot
// Break out and try the next available timeslot
router_debug!(
"(get_vehicle_availability) Flight plan would end after available timeslot."
);
router_debug!("(get_itinerary) Flight plan would end after available timeslot.");

return Err(ItineraryError::ScheduleConflict);
}
Expand Down
27 changes: 10 additions & 17 deletions server/src/router/schedule.rs
Original file line number Diff line number Diff line change
Expand Up @@ -129,11 +129,7 @@ impl Sub for Timeslot {
}];
}

router_warn!(
"(timeslot_collision) Unhandled case: {:?} {:?}",
self,
other
);
router_warn!("(sub) Unhandled case: {:?} {:?}", self, other);

vec![]
}
Expand Down Expand Up @@ -207,22 +203,22 @@ impl FromStr for Calendar {
/// "DTSTART:20221020T180000Z;DURATION:PT1H" not "DURATION:PT1H;DTSTART:20221020T180000Z"
/// Duration is in ISO8601 format (`iso8601_duration` crate)
fn from_str(calendar_str: &str) -> Result<Self, Self::Err> {
router_debug!("(Calendar from_str) Parsing calendar: {}", calendar_str);
router_debug!("(from_str) Parsing calendar: {}", calendar_str);
let rrule_sets: Vec<&str> = calendar_str
.split("DTSTART:")
.filter(|s| !s.is_empty())
.collect();
router_debug!("(Calendar from_str) rrule_sets: {:?}", rrule_sets);
router_debug!("(from_str) rrule_sets: {:?}", rrule_sets);
let mut recurrent_events: Vec<RecurrentEvent> = Vec::new();
for rrule_set_str in rrule_sets {
router_debug!("(Calendar from_str) rrule_set_str: {}", rrule_set_str);
router_debug!("(from_str) rrule_set_str: {}", rrule_set_str);
let rrules_with_header: Vec<&str> = rrule_set_str
.split('\n')
.filter(|s| !s.is_empty())
.collect();
if rrules_with_header.len() < 2 {
router_error!(
"(Calendar from_str) Invalid rrule {} with header length: {}",
"(from_str) Invalid rrule {} with header length: {}",
calendar_str,
rrules_with_header.len()
);
Expand All @@ -236,7 +232,7 @@ impl FromStr for Calendar {
.collect();
if header_parts.len() != 2 {
router_error!(
"(Calendar from_str) Invalid header parts length: {}",
"(from_str) Invalid header parts length: {}",
header_parts.len()
);
return Err(CalendarError::HeaderPartsLength);
Expand All @@ -245,20 +241,20 @@ impl FromStr for Calendar {
let dtstart = header_parts[0];
let duration: &str = header_parts[1];
let Ok(duration) = duration.parse::<Iso8601Duration>() else {
router_error!("(Calendar from_str) Invalid duration: {:?}", duration);
router_error!("(from_str) Invalid duration: {:?}", duration);
return Err(CalendarError::Duration);
};

let Some(duration) = duration.to_chrono() else {
router_error!("(Calendar from_str) Could not convert duration to chrono::DateTime: {:?}", duration);
router_error!("(from_str) Could not convert duration to chrono::DateTime: {:?}", duration);
return Err(CalendarError::Duration);
};

let str = "DTSTART:".to_owned() + dtstart + "\n" + rrules.join("\n").as_str();
let rrset_res = RRuleSet::from_str(&str);

let Ok(rrule_set) = rrset_res else {
router_error!("(Calendar from_str) Invalid rrule set: {:?}", rrset_res.unwrap_err());
router_error!("(from_str) Invalid rrule set: {:?}", rrset_res.unwrap_err());
return Err(CalendarError::RruleSet);
};

Expand All @@ -267,10 +263,7 @@ impl FromStr for Calendar {
duration,
});
}
router_debug!(
"(Calendar from_str) Parsed calendar: {:?}",
recurrent_events
);
router_debug!("(from_str) Parsed calendar: {:?}", recurrent_events);
Ok(Calendar {
events: recurrent_events,
})
Expand Down
Loading

0 comments on commit 40506a2

Please sign in to comment.