diff options
author | Quentin Dufour <quentin@deuxfleurs.fr> | 2024-01-03 09:47:52 +0100 |
---|---|---|
committer | Quentin Dufour <quentin@deuxfleurs.fr> | 2024-01-03 09:47:52 +0100 |
commit | 9ce8e18fb81c4ef0bf146c5d28981d8f2a6fddd9 (patch) | |
tree | 96a2e3b62069485e0c45ce022ec472dd85bd3935 /tests/common.rs | |
parent | 7ebc708acab9c91db41652cfbfe2814a3a27569d (diff) | |
download | aerogramme-9ce8e18fb81c4ef0bf146c5d28981d8f2a6fddd9.tar.gz aerogramme-9ce8e18fb81c4ef0bf146c5d28981d8f2a6fddd9.zip |
Common module in test created
Diffstat (limited to 'tests/common.rs')
-rw-r--r-- | tests/common.rs | 43 |
1 files changed, 43 insertions, 0 deletions
diff --git a/tests/common.rs b/tests/common.rs new file mode 100644 index 0000000..0d99ce8 --- /dev/null +++ b/tests/common.rs @@ -0,0 +1,43 @@ +use anyhow::{bail, Context, Result}; +use std::net::{Shutdown, TcpStream}; +use std::process::Command; +use std::{thread, time}; + +static SMALL_DELAY: time::Duration = time::Duration::from_millis(200); + +pub fn aerogramme_provider_daemon_dev(mut fx: impl FnMut(&mut TcpStream, &mut TcpStream) -> Result<()>) -> Result<()> { + let mut daemon = Command::new(env!("CARGO_BIN_EXE_aerogramme")) + .arg("--dev") + .arg("provider") + .arg("daemon") + .spawn()?; + + let mut max_retry = 20; + let mut imap_socket = loop { + max_retry -= 1; + match (TcpStream::connect("[::1]:1143"), max_retry) { + (Err(e), 0) => bail!("no more retry, last error is: {}", e), + (Err(e), _) => { + println!("unable to connect: {} ; will retry in 1 sec", e); + } + (Ok(v), _) => break v, + } + thread::sleep(SMALL_DELAY); + }; + + let mut lmtp_socket = TcpStream::connect("[::1]:1025").context("lmtp socket must be connected")?; + + println!("-- ready to test imap features --"); + let result = fx(&mut imap_socket, &mut lmtp_socket); + println!("-- test teardown --"); + + imap_socket + .shutdown(Shutdown::Both) + .context("closing imap socket at the end of the test")?; + lmtp_socket + .shutdown(Shutdown::Both) + .context("closing lmtp socket at the end of the test")?; + daemon.kill().context("daemon should be killed")?; + + result.context("all tests passed") +} |