test: Don’t try to close nil stream

If errors occur while creating the stream, it will be nil, so close after checking the error.