karma-runner/karma — pull request #928 by jpommerening (2014-02-18 15:55:43+00:00)
Title: Stop the WebServer after running tests

When we're done running the tests and all clients are disconnected, we should stop the HTTP server. This way another karma instance (or anything else for that matter) may reuse the port later.

For example, if we're using `grunt-karma` and have multiple successive Karma tasks we want to run, the output will look something like this (notice how karma consumes more and more ports):
```console
$ grunt karma
Running "karma:part_one" (karma) task
INFO [karma]: Karma v0.10.9 server started at http://localhost:9876/
INFO [launcher]: Starting browser PhantomJS
INFO [PhantomJS 1.9.7 (Linux)]: Connected on socket -7kxMeoUxVJpOdRChLvW
PhantomJS 1.9.7 (Linux): Executed 55 of 55 SUCCESS (0.246 secs / 0.017 secs)
TOTAL: 55 SUCCESS

Running "karma:part_two" (karma) task
WARN [karma]: Port 9876 in use
INFO [karma]: Karma v0.10.9 server started at http://localhost:9877/
INFO [launcher]: Starting browser PhantomJS
INFO [PhantomJS 1.9.7 (Linux)]: Connected on socket hSO6q1EwCkDNwF5ohNQT
PhantomJS 1.9.7 (Linux): Executed 6 of 6 SUCCESS (0.193 secs / 0.006 secs)
TOTAL: 6 SUCCESS

Running "karma:part_three" (karma) task
WARN [karma]: Port 9876 in use
WARN [karma]: Port 9877 in use
INFO [karma]: Karma v0.10.9 server started at http://localhost:9878/
INFO [launcher]: Starting browser PhantomJS
INFO [PhantomJS 1.9.7 (Linux)]: Connected on socket sREEumil-I0NjY9ZhOiv
PhantomJS 1.9.7 (Linux): Executed 26 of 26 SUCCESS (0.249 secs / 0.011 secs)
TOTAL: 26 SUCCESS
```

This is most likely what is going on in karma-runner/grunt-karma#72.

To resolve this, I added a few lines in `disconnectBrowsers()` to call the `http.Server.close()` method. The final `done` callback is deferred until the server closed it's connections and released the port.