Struct httptest::ServerPool

source ·
pub struct ServerPool(/* private fields */);
Expand description

A pool of shared servers.

The typical way to use this library is to create a new Server for each test using Server::run. This way each test contains it’s own independent state. However for very large test suites it may be beneficial to share servers between test runs. This is typically only desirable when running into limits because the test framework spins up an independent thread for each test and system wide resources (TCP ports) may become scarce. In those cases you can opt into using a shared ServerPool that will create a maximum of N servers that tests can share. Invoking get_server on the pool will return a ServerHandle that deref’s to a Server. For the life of the ServerHandle it has unique access to this server instance. When the handle is dropped the server expectations are asserted and cleared and the server is returned back into the ServerPool for use by another test.

Example:

// Create a server pool that will create at most 99 servers.
static SERVER_POOL: ServerPool = ServerPool::new(99);

#[test]
fn test_one() {
    let server = SERVER_POOL.get_server();
    server.expect(Expectation::matching(any()).respond_with(status_code(200)));
    // invoke http requests to server.
     
    // server will assert expectations are met on drop.
}

#[test]
fn test_two() {
    let server = SERVER_POOL.get_server();
    server.expect(Expectation::matching(any()).respond_with(status_code(200)));
    // invoke http requests to server.
     
    // server will assert expectations are met on drop.
}

A pool of running servers.

Implementations§

source§

impl ServerPool

source

pub const fn new(max_servers: usize) -> Self

Create a new pool of servers.

max_servers is the maximum number of servers that will be created. servers are created on-demand when get_server is invoked.

source

pub fn get_server(&self) -> ServerHandle<'_>

Get the next available server from the pool.

Trait Implementations§

source§

impl Debug for ServerPool

source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more

Auto Trait Implementations§

Blanket Implementations§

source§

impl<T> Any for T
where T: 'static + ?Sized,

source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
source§

impl<T> Borrow<T> for T
where T: ?Sized,

source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
source§

impl<T> From<T> for T

source§

fn from(t: T) -> T

Returns the argument unchanged.

§

impl<T> Instrument for T

§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided [Span], returning an Instrumented wrapper. Read more
§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
source§

impl<T, U> Into<U> for T
where U: From<T>,

source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

§

type Error = Infallible

The type returned in the event of a conversion error.
source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
§

impl<T> WithSubscriber for T

§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a [WithDispatch] wrapper. Read more
§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a [WithDispatch] wrapper. Read more