### Question:
JobPollingService extends AbstractScheduledService { @Override protected void runOneIteration() { monitor.debug(() -> "JobMetadata.isInitialized(): " + JobMetadata.isInitialized()); if (JobMetadata.isInitialized()) { if (stopwatch.elapsed(TimeUnit.SECONDS) > credsTimeoutSeconds) { UUID jobId = JobMetadata.getJobId(); markJobTimedOut(jobId); String message = format( "Waited over %d seconds for the creds to be provided on the claimed job: %s", credsTimeoutSeconds, jobId); monitor.severe(() -> message, EventCode.WORKER_CREDS_TIMEOUT); throw new CredsTimeoutException(message, jobId); } pollUntilJobIsReady(); } else { pollForUnassignedJob(); } } @Inject  JobPollingService(
      JobStore store,
      TransferKeyGenerator transferKeyGenerator,
      PublicKeySerializer publicKeySerializer,
      Scheduler scheduler,
      Monitor monitor,
      ExtensionContext context);   }### Answer:
@Test public void pollingLifeCycle() throws Exception { when(asymmetricKeyGenerator.generate()).thenReturn(TEST_KEY_PAIR); assertThat(JobMetadata.isInitialized()).isFalse(); jobPollingService.runOneIteration(); assertThat(JobMetadata.isInitialized()).isFalse(); PortabilityJob job = store.findJob(TEST_ID); assertThat(job).isNull(); job = PortabilityJob.builder() .setTransferDataType("photo") .setExportService("DummyExportService") .setImportService("DummyImportService") .setAndValidateJobAuthorization( JobAuthorization.builder() .setEncryptionScheme("cleartext") .setState(State.INITIAL) .setSessionSecretKey("fooBar") .build()) .build(); store.createJob(TEST_ID, job); job = store.findJob(TEST_ID); assertThat(job.jobAuthorization().state()).isEqualTo(State.INITIAL); assertThat(job.jobAuthorization().encryptedAuthData()).isNull(); job = job.toBuilder() .setAndValidateJobAuthorization( job.jobAuthorization().toBuilder().setState(State.CREDS_AVAILABLE).build()) .build(); store.updateJobAuthStateToCredsAvailable(TEST_ID); job = store.findJob(TEST_ID); assertThat(job.jobAuthorization().state()).isEqualTo(State.CREDS_AVAILABLE); assertThat(job.jobAuthorization().encryptedAuthData()).isNull(); jobPollingService.runOneIteration(); assertThat(JobMetadata.isInitialized()).isTrue(); assertThat(JobMetadata.getJobId()).isEqualTo(TEST_ID); job = store.findJob(TEST_ID); assertThat(job.jobAuthorization().state()) .isEqualTo(JobAuthorization.State.CREDS_ENCRYPTION_KEY_GENERATED); assertThat(job.jobAuthorization().authPublicKey()).isNotEmpty(); job = job.toBuilder() .setAndValidateJobAuthorization( job.jobAuthorization() .toBuilder() .setEncryptedAuthData("dummy export data") .setState(State.CREDS_STORED) .build()) .build(); store.updateJobWithCredentials(TEST_ID, job); jobPollingService.runOneIteration(); job = store.findJob(TEST_ID); JobAuthorization jobAuthorization = job.jobAuthorization(); assertThat(jobAuthorization.state()).isEqualTo(JobAuthorization.State.CREDS_STORED); assertThat(jobAuthorization.encryptedAuthData()).isNotEmpty(); store.remove(TEST_ID); }