use std::sync::{Arc, Weak};
use crate::registry::Shared;
use crate::{Completion, Engine, EngineConfig, Error, RequestId};
#[derive(Clone, Debug)]
pub struct TestController {
shared: Weak<Shared>,
}
impl TestController {
pub fn complete(&self, id: RequestId, completion: Completion) -> bool {
let Some(shared) = self.shared.upgrade() else {
return false;
};
let won = shared.complete_id(id, completion);
shared.queue.wake();
won
}
#[must_use]
pub fn active_requests(&self) -> usize {
self.shared
.upgrade()
.map_or(0, |shared| shared.active_count())
}
}
pub fn engine(config: EngineConfig) -> Result<(Engine, TestController), Error> {
let engine = Engine::with_backend(config, crate::backend::held())?;
let shared: Arc<Shared> = engine.shared_for_testing();
let controller = TestController {
shared: Arc::downgrade(&shared),
};
Ok((engine, controller))
}
#[cfg(feature = "native")]
pub fn native_http_engine(config: EngineConfig) -> Result<Engine, Error> {
let factory = crate::backend::native_http_factory(&config);
Engine::with_spawned_factory(config, factory)
}
#[cfg(feature = "native")]
pub fn native_http_engine_with_nameserver(
config: EngineConfig,
nameserver: std::net::SocketAddr,
) -> Result<Engine, Error> {
let factory = crate::backend::native_http_factory_with_nameserver(&config, nameserver);
Engine::with_spawned_factory(config, factory)
}
#[cfg(feature = "native")]
pub fn native_http_engine_with_nameserver_and_search_suffixes(
config: EngineConfig,
nameserver: std::net::SocketAddr,
suffixes: impl IntoIterator<Item = impl AsRef<str>>,
) -> Result<Engine, Error> {
let factory = crate::backend::native_http_factory_with_nameserver_and_search_suffixes(
&config, nameserver, suffixes,
);
Engine::with_spawned_factory(config, factory)
}
#[cfg(feature = "native")]
pub fn native_http_engine_manual_with_nameserver(
config: EngineConfig,
nameserver: std::net::SocketAddr,
) -> Result<Engine, Error> {
let backend = crate::backend::native_http_backend_with_nameserver(&config, nameserver)?;
Engine::with_backend(config, backend)
}
#[cfg(feature = "native")]
pub fn native_http_engine_with_system_dns(config: EngineConfig) -> Result<Engine, Error> {
let factory = crate::backend::native_http_factory_with_system_dns(&config)?;
Engine::with_spawned_factory(config, factory)
}
#[cfg(feature = "native")]
pub fn native_https_engine_with_nameserver(
config: EngineConfig,
nameserver: std::net::SocketAddr,
) -> Result<Engine, Error> {
let factory = crate::backend::native_https_factory_with_nameserver(&config, nameserver)?;
Engine::with_spawned_factory(config, factory)
}
#[cfg(feature = "native")]
pub fn native_https_engine_with_system_dns(config: EngineConfig) -> Result<Engine, Error> {
let factory = crate::backend::native_https_factory_with_system_dns(&config)?;
Engine::with_spawned_factory(config, factory)
}
#[cfg(feature = "native")]
pub fn native_https_engine_with_nameserver_and_test_root(
config: EngineConfig,
nameserver: std::net::SocketAddr,
root_der: Vec<u8>,
) -> Result<Engine, Error> {
let factory = crate::backend::native_https_factory_with_nameserver_and_test_root(
&config, nameserver, root_der,
)?;
Engine::with_spawned_factory(config, factory)
}
#[cfg(feature = "native")]
pub fn native_https_manual_engine_with_nameserver_and_test_root(
config: EngineConfig,
nameserver: std::net::SocketAddr,
root_der: Vec<u8>,
) -> Result<Engine, Error> {
if config.run_mode() != crate::RunMode::Manual {
return Err(Error::new(
crate::ErrorKind::WrongMode,
"the manual native HTTPS proving constructor requires manual Engine mode",
));
}
let backend = crate::backend::native_https_backend_with_nameserver_and_test_root(
&config, nameserver, root_der,
)?;
Engine::with_backend(config, backend)
}