add when_present clauses to reduce flaky test failures

Change-Id: I4a7e2312d46ba1606490c81a4c6a3810cd15e0e9