test_threaded.py: Refactor; check results & exceptions

Make code more readable/maintainable by factoring out common code, using
new helpers.StoredProc class, using `with` statement, etc.

Also check that results and exceptions are as expected.