chore(all): Add back test for smtp pool

This commit is contained in:
Alexis Mousset
2020-05-03 11:26:56 +02:00
parent 7dd392401c
commit 88df2a502d

View File

@@ -0,0 +1,61 @@
#[cfg(all(test, feature = "smtp-transport", feature = "r2d2"))]
mod test {
use lettre::{Envelope, SmtpTransport, Tls, Transport};
use r2d2::Pool;
use std::{sync::mpsc, thread};
fn envelope() -> Envelope {
Envelope::new(
Some("user@localhost".parse().unwrap()),
vec!["root@localhost".parse().unwrap()],
)
.unwrap()
}
#[test]
fn send_one() {
let client = SmtpTransport::new("127.0.0.1").port(2525);
let c = client.clone();
let pool = Pool::builder().max_size(1).build(c).unwrap();
let mailer = client.pool(pool);
let result = mailer.send_raw(&envelope(), b"test");
assert!(result.is_ok());
}
#[test]
fn send_from_thread() {
let client = SmtpTransport::new("127.0.0.1").port(2525);
let c = client.clone();
let pool = Pool::builder().max_size(1).build(c).unwrap();
let mailer = client.pool(pool);
let (s1, r1) = mpsc::channel();
let (s2, r2) = mpsc::channel();
let mailer1 = mailer.clone();
let t1 = thread::spawn(move || {
s1.send(()).unwrap();
r2.recv().unwrap();
mailer1
.send_raw(&envelope(), b"test1")
.expect("Send failed from thread 1");
});
let mailer2 = mailer.clone();
let t2 = thread::spawn(move || {
s2.send(()).unwrap();
r1.recv().unwrap();
mailer2
.send_raw(&envelope(), b"test2")
.expect("Send failed from thread 2");
});
t1.join().unwrap();
t2.join().unwrap();
mailer
.send_raw(&envelope(), b"test")
.expect("Send failed from main thread");
}
}