### Question:
JobManagerRunner implements LeaderContender, OnCompletionActions, AutoCloseableAsync { public void start() throws Exception { try { leaderElectionService.start(this); } catch (Exception e) { log.error("Could not start the JobManager because the leader election service did not start.", e); throw new Exception("Could not start the leader election service.", e); } } JobManagerRunner(
			final JobGraph jobGraph,
			final JobMasterServiceFactory jobMasterFactory,
			final HighAvailabilityServices haServices,
			final LibraryCacheManager libraryCacheManager,
			final Executor executor,
			final FatalErrorHandler fatalErrorHandler); CompletableFuture<JobMasterGateway> getLeaderGatewayFuture(); JobGraph getJobGraph(); CompletableFuture<ArchivedExecutionGraph> getResultFuture(); void start(); @Override CompletableFuture<Void> closeAsync(); @Override void jobReachedGloballyTerminalState(ArchivedExecutionGraph executionGraph); @Override void jobFinishedByOther(); @Override void jobMasterFailed(Throwable cause); @Override void grantLeadership(final UUID leaderSessionID); @Override void revokeLeadership(); @Override String getAddress(); @Override void handleError(Exception exception);  }### Answer:
@Test public void testLibraryCacheManagerRegistration() throws Exception { final BlobLibraryCacheManager libraryCacheManager = new BlobLibraryCacheManager( VoidPermanentBlobService.INSTANCE, FlinkUserCodeClassLoaders.ResolveOrder.CHILD_FIRST, new String[]{}); final JobManagerRunner jobManagerRunner = createJobManagerRunner(libraryCacheManager); try { jobManagerRunner.start(); final JobID jobID = jobGraph.getJobID(); assertThat(libraryCacheManager.hasClassLoader(jobID), is(true)); jobManagerRunner.close(); assertThat(libraryCacheManager.hasClassLoader(jobID), is(false)); } finally { jobManagerRunner.close(); } }
@Test public void testConcurrentLeadershipOperationsBlockingSuspend() throws Exception { final CompletableFuture<Acknowledge> suspendedFuture = new CompletableFuture<>(); TestingJobMasterServiceFactory jobMasterServiceFactory = new TestingJobMasterServiceFactory( () -> new TestingJobMasterService( "localhost", e -> suspendedFuture)); JobManagerRunner jobManagerRunner = createJobManagerRunner(jobMasterServiceFactory); jobManagerRunner.start(); leaderElectionService.isLeader(UUID.randomUUID()).get(); leaderElectionService.notLeader(); final CompletableFuture<UUID> leaderFuture = leaderElectionService.isLeader(UUID.randomUUID()); assertThat(leaderFuture.isDone(), is(false)); try { leaderFuture.get(1L, TimeUnit.MILLISECONDS); fail("Granted leadership even though the JobMaster has not been suspended."); } catch (TimeoutException expected) { } suspendedFuture.complete(Acknowledge.get()); leaderFuture.get(); }
@Test public void testConcurrentLeadershipOperationsBlockingGainLeadership() throws Exception { final CompletableFuture<Exception> suspendFuture = new CompletableFuture<>(); final CompletableFuture<Acknowledge> startFuture = new CompletableFuture<>(); TestingJobMasterServiceFactory jobMasterServiceFactory = new TestingJobMasterServiceFactory( () -> new TestingJobMasterService( "localhost", e -> { suspendFuture.complete(e); return CompletableFuture.completedFuture(Acknowledge.get()); }, ignored -> startFuture)); JobManagerRunner jobManagerRunner = createJobManagerRunner(jobMasterServiceFactory); jobManagerRunner.start(); leaderElectionService.isLeader(UUID.randomUUID()); leaderElectionService.notLeader(); assertThat(suspendFuture.isDone(), is(false)); try { suspendFuture.get(1L, TimeUnit.MILLISECONDS); fail("Suspended leadership even though the JobMaster has not been started."); } catch (TimeoutException expected) { } startFuture.complete(Acknowledge.get()); suspendFuture.get(); }