use std::sync::Arc;
use super::{BatchAnswer, BatchDoor, BatchError, Batched};
use crate::graphql::{ErrorCode, ToErrorCode};
#[derive(Debug)]
struct Refused;
impl core::fmt::Display for Refused {
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
f.write_str("this query did not run: the pool is closed")
}
}
impl core::error::Error for Refused {}
impl ToErrorCode for Refused {
fn error_code(&self) -> ErrorCode {
ErrorCode::StoreRefused
}
}
#[derive(Debug)]
struct Wordless;
impl core::fmt::Display for Wordless {
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
f.write_str("the remote index timed out")
}
}
impl core::error::Error for Wordless {}
#[derive(Debug, Default)]
struct Table {
refuse: bool,
}
impl Batched<i64, &'static str> for Table {
fn one(&self, key: i64) -> BatchAnswer<'_, &'static str> {
let refuse = self.refuse;
Box::pin(async move {
if refuse {
return Err(BatchError::new(Refused));
}
Ok(match key {
1 => Some("clapper"),
2 => Some("gate"),
_ => None,
})
})
}
}
#[test]
fn a_refusal_that_names_its_word_keeps_it() {
let error = BatchError::new(Refused);
assert_eq!(error.code(), ErrorCode::StoreRefused);
assert_eq!(error.error_code(), ErrorCode::StoreRefused);
}
#[test]
fn a_refusal_without_a_word_is_given_one() {
let error = BatchError::coded(ErrorCode::StoreRefused, Wordless);
assert_eq!(error.code(), ErrorCode::StoreRefused);
assert_eq!(error.to_string(), "the remote index timed out");
}
#[test]
fn the_sentence_is_the_refusals_own() {
let error = BatchError::new(Refused);
assert_eq!(error.to_string(), Refused.to_string());
}
#[test]
fn the_carried_refusal_is_the_source() {
let error = BatchError::new(Refused);
let source = core::error::Error::source(&error).expect("the refusal");
assert_eq!(source.to_string(), Refused.to_string());
assert_eq!(error.refusal().to_string(), Refused.to_string());
}
#[test]
fn a_refusal_clones_to_every_asker() {
let error = BatchError::new(Refused);
let fanned = error.clone();
assert_eq!(fanned.code(), error.code());
assert_eq!(fanned.to_string(), error.to_string());
assert!(
core::ptr::eq(
core::ptr::from_ref(error.refusal()).cast::<()>(),
core::ptr::from_ref(fanned.refusal()).cast::<()>(),
),
"the clone shares the refusal rather than copying it"
);
}
#[tokio::test]
async fn a_door_answers_hits_and_misses() {
let door: BatchDoor<i64, &'static str> = BatchDoor::new(Arc::new(Table::default()));
assert_eq!(door.one(1).await.expect("an answer"), Some("clapper"));
assert_eq!(door.one(2).await.expect("an answer"), Some("gate"));
assert_eq!(
door.one(9).await.expect("an answer"),
None,
"a miss is Ok(None), which is the per-row lane's answer too"
);
}
#[tokio::test]
async fn a_door_carries_its_providers_refusal() {
let door: BatchDoor<i64, &'static str> = BatchDoor::new(Arc::new(Table { refuse: true }));
let refusal = door.one(1).await.expect_err("a refusal");
assert_eq!(refusal.code(), ErrorCode::StoreRefused);
}
#[test]
fn a_door_clones_by_refcount() {
let door: BatchDoor<i64, &'static str> = BatchDoor::new(Arc::new(Table::default()));
let twin = door.clone();
assert!(format!("{twin:?}").contains("BatchDoor"));
}
#[test]
fn a_doors_debug_names_its_two_types() {
let door: BatchDoor<i64, &'static str> = BatchDoor::new(Arc::new(Table::default()));
let rendered = format!("{door:?}");
assert!(rendered.contains("i64"), "{rendered}");
assert!(rendered.contains("str"), "{rendered}");
}