2

I have some tests written, each function starts iron's HTTP server and closes it after test is done:

extern crate iron;

use iron::prelude::*;

fn hello_world(_: &mut Request) -> IronResult<Response> {
    Ok(Response::with((iron::status::Ok, "Hello World")))
}

#[test]
fn test1() {
    let mut server = Iron::new(hello_world).http("localhost:3000").unwrap();
    server.close().unwrap();
}

#[test]
fn test2() {
    let mut server = Iron::new(hello_world).http("localhost:3000").unwrap();
    server.close().unwrap();
}

Doing cargo test I'm having:

Running target/debug/lib-f236975fe924352b

running 2 tests
test test1 ... ok
test test2 ... FAILED

failures:

---- test2 stdout ----
thread 'test2' panicked at 'called `Result::unwrap()` on an `Err` value: Io(Error { repr: Os { code: 98, message: "Address already in use" } })', ../src/libcore/result.rs:736



failures:
test2

test result: FAILED. 1 passed; 1 failed; 0 ignored; 0 measured

It seems like the port is still in use by the start of the second test.

Shepmaster
  • 388,571
  • 95
  • 1,107
  • 1,366
franza
  • 2,297
  • 25
  • 39
  • To be honest, if possible, I would avoid using any network traffic in a unit test. You should not be unit-testing Iron, after all, only your own code. – Matthieu M. Dec 09 '15 at 08:28
  • Totally agree with you, but I never said that I was writing unit tests. I was trying to implement some test cases which proves that my code interacts correctly with `iron`. More like functional testing, or whatever. – franza Dec 10 '15 at 16:18

1 Answers1

3

Rust's test runner is parallel by default, so yes, I would expect that this would happen. I would pick a different port number for each test, at least to start, since it's so easy.

Steve Klabnik
  • 14,521
  • 4
  • 58
  • 99