Add tests.

Also allow the close method to be called multiple times.