agent: updated tests to defer agent shutdown instead of directly calling it.