Clean up suite shutdown and fix tests accordingly.

Database test: comparison of events with the reference version was
failing due to a difference in event ordering because "order by name,
message" is ambiguous when only a few events recorded messages.
State-dump tests and one restart test: on this branch we exit from main
loop immediately on shutdown instead of going further through the main
loop which leads to minor inconsequential state differences at shutdown.