state/{api,apiserver}: don't leak heartbeat goroutine

- In api.State.Close, signal and wait for the heartbeat
  goroutine to stop before returning.
- Add various missing api.State.Close calls to apiserver
  tests.

Fixes https://bugs.launchpad.net/juju-core/+bug/1334531