Add test that ensures that iteration stops listening to template instances that are no longer in use.

BUG=None

Review URL: https://codereview.appspot.com/7860046