hargettp/courier — pull request #6 by carletes (2014-07-05 16:25:36+00:00)
Title: Allow test suite to run with multiple threads

The current tests cannot be run in parallel, because several tests use the same ports (20000 and 20001) for their endpoints:

```
$ cabal test --test-options=-j10
Building courier-0.1.0.15...
Preprocessing library courier-0.1.0.15...
In-place registering courier-0.1.0.15...
Preprocessing test suite 'test-courier' for courier-0.1.0.15...
Running 1 test suites...
Test suite test-courier: RUNNING...
[..]
2014-07-05 17:15:29 BST [WARNING] - Listen error on port localhost:20001: bind: resource busy (Address already in use)
2014-07-05 17:15:29 BST [WARNING] - Listen error on port localhost:20001: bind: resource busy (Address already in use)
2014-07-05 17:15:29 BST [WARNING] - Listen error on port localhost:20001: bind: resource busy (Address already in use)
2014-07-05 17:15:29 BST [WARNING] - Listen error on port localhost:20001: bind: resource busy (Address already in use)
2014-07-05 17:15:29 BST [WARNING] - Listen error on port localhost:20001: bind: resource busy (Address already in use)
2014-07-05 17:15:29 BST [WARNING] - IPv6 not available
2014-07-05 17:15:29 BST [WARNING] - Listen error on port localhost:20002: bind: resource busy (Address already in use)
2014-07-05 17:15:29 BST [WARNING] - Listen error on port localhost:20002: bind: resource busy (Address already in use)
2014-07-05 17:15:29 BST [WARNING] - Listen error on port localhost:20002: bind: resource busy (Address already in use)
tcp-bind-unbind: [OK]
[..]
2014-07-05 17:15:30 BST [ERROR] - Encountered error running test: HUnitFailure "Received message not same as sent\nexpected: Right \"hi!\"\n but got: Right \"hello\""
2014-07-05 17:15:31 BST [ERROR] - Encountered error running test: HUnitFailure "No message received"
2014-07-05 17:15:31 BST [ERROR] - Encountered error running test: HUnitFailure "No message received"
2014-07-05 17:15:31 BST [ERROR] - Encountered error running test: HUnitFailure "No message received"
2014-07-05 17:15:31 BST [ERROR] - Encountered error running test: HUnitFailure "No message received"
2014-07-05 17:15:31 BST [ERROR] - Encountered error running test: HUnitFailure "No message received"
2014-07-05 17:15:31 BST [ERROR] - Encountered error running test: HUnitFailure "No message received"
[..]
Test Cases Total
Passed 41 41
Failed 7 7
Total 48 48
Test suite test-courier: FAIL
Test suite logged to: dist/test/courier-0.1.0.15-test-courier.log
0 of 1 test suites (0 of 1 test cases) passed.
$
```

The code in this pull request uses arbitrary port numbers, so test may be run now in parallel --- and faster!

With one single thread:

```
$ time cabal test
Building courier-0.1.0.15...
Preprocessing library courier-0.1.0.15...
In-place registering courier-0.1.0.15...
Preprocessing test suite 'test-courier' for courier-0.1.0.15...
Running 1 test suites...
Test suite test-courier: RUNNING...
Test suite test-courier: PASS
Test suite logged to: dist/test/courier-0.1.0.15-test-courier.log
1 of 1 test suites (1 of 1 test cases) passed.

real 0m12.389s
user 0m1.249s
sys 0m0.108s
$
```

With then threads, on my 8-core Linux system:

```
$ time cabal test --test-options=-j10
Building courier-0.1.0.15...
Preprocessing library courier-0.1.0.15...
In-place registering courier-0.1.0.15...
Preprocessing test suite 'test-courier' for courier-0.1.0.15...
Running 1 test suites...
Test suite test-courier: RUNNING...
Test suite test-courier: PASS
Test suite logged to: dist/test/courier-0.1.0.15-test-courier.log
1 of 1 test suites (1 of 1 test cases) passed.

real 0m2.795s
user 0m1.241s
sys 0m0.103s
$
```