// License: see LICENSE file at root directory of `master` branch

#[test]
fn tests() -> std::io::Result<()> {
    use namaste::{
        Uid, DEFAULT_RW_TIMEOUT,
        client::Client,
    };

    use crate::{
        server::Server,
    };

    const UID: Uid = [
        0xfa, 0xea, 0xa0, 0x87, 0xc1, 0x80, 0xb8, 0xbb, 0xf1, 0xd2, 0x58, 0x92, 0x2c, 0x21, 0xff, 0xca, 0x42, 0xb5, 0xf5, 0x62, 0xb7, 0x1a, 0x8f,
        0xe5, 0xc8, 0x31, 0x59, 0x16, 0x41, 0x42, 0x4c, 0xa4, 0x31, 0x28, 0xaf, 0x1f, 0xad, 0x63, 0xe8, 0x76, 0x85, 0x10, 0x2d, 0xec, 0x2b, 0xa7,
        0xd9, 0xc8, 0x4b, 0xf3, 0xf0, 0xe9, 0x37, 0xba, 0x5b, 0xe8, 0x05, 0x86, 0x9b, 0x7a, 0x29, 0x8a, 0xce, 0x38,
    ];
    const HANDSHAKE_UID: Uid = [
        0x6c, 0x34, 0xd6, 0x08, 0x0d, 0x6c, 0x49, 0x26, 0x99, 0x0e, 0x82, 0xed, 0xb5, 0xb9, 0x59, 0x75, 0x70, 0x9d, 0x73, 0xac, 0x8b, 0x4b, 0xeb,
        0x4e, 0xd7, 0x78, 0xfe, 0xb7, 0xea, 0xee, 0x23, 0xbb, 0x0a, 0x59, 0xa7, 0x1f, 0x0a, 0x76, 0xd4, 0x4d, 0x1d, 0x84, 0x00, 0x64, 0xa8, 0xfd,
        0xc2, 0x36, 0x16, 0x6f, 0xf0, 0xa7, 0xba, 0x3f, 0x71, 0xb5, 0x78, 0x8f, 0xbf, 0xbd, 0x90, 0xce, 0x44, 0x56,
    ];
    const NAMASTE_SERVER_PORT: Option<u16> = Some(64006);

    let server = Server::start(NAMASTE_SERVER_PORT, DEFAULT_RW_TIMEOUT, unsafe { blackhole::get_online_processors!(None) as usize})?;

    let client = Client::book(UID, HANDSHAKE_UID, NAMASTE_SERVER_PORT, DEFAULT_RW_TIMEOUT)?.unwrap();
    assert!(Client::book(UID, HANDSHAKE_UID, NAMASTE_SERVER_PORT, DEFAULT_RW_TIMEOUT)?.is_none());
    assert!(client.check_out()?);
    assert!(Client::book(UID, HANDSHAKE_UID, NAMASTE_SERVER_PORT, DEFAULT_RW_TIMEOUT)?.unwrap().check_out()?);
    assert!(client.check_out()? == false);

    let client = Client::book(HANDSHAKE_UID, UID, NAMASTE_SERVER_PORT, DEFAULT_RW_TIMEOUT)?.unwrap();
    assert!(Client::book(HANDSHAKE_UID, UID, NAMASTE_SERVER_PORT, DEFAULT_RW_TIMEOUT)?.is_none());
    assert!(client.check_out()?);
    assert!(Client::book(HANDSHAKE_UID, UID, NAMASTE_SERVER_PORT, DEFAULT_RW_TIMEOUT)?.unwrap().check_out()?);
    assert!(client.check_out()? == false);

    drop(server);
    Ok(())
}
