Ensure history database is closed in test

Hopefully fixes the last Windows test on Python 3.3
Close database when HistorySavingThread stops
Merge pull request #5874 from takluyver/test-hist-close-db

Ensure history database is closed in test