WorkspaceServiceTermination implements ServiceTermination { @Override public void suspend() throws InterruptedException, UnsupportedOperationException { try { runtimeInfrastructure.getIdentities(); } catch (UnsupportedOperationException | InfrastructureException e) { throw new UnsupportedOperationException("Current infrastructure does not support suspend."); } Preconditions.checkState(runtimes.refuseStart()); WorkspaceSuspendedEventsPropagator propagator = new WorkspaceSuspendedEventsPropagator(); eventService.subscribe(propagator); try { waitAllWorkspacesRunningOrStopped(); sharedPool.shutdown(); } finally { eventService.unsubscribe(propagator); } try { workspaceRemover.shutdown(); } catch (Exception ignored) { } } @Inject  WorkspaceServiceTermination(
      WorkspaceManager manager,
      WorkspaceSharedPool sharedPool,
      WorkspaceRuntimes runtimes,
      RuntimeInfrastructure runtimeInfrastructure,
      EventService eventService,
      TemporaryWorkspaceRemover workspaceRemover); @Override String getServiceName(); @Override Set<String> getDependencies(); @Override void terminate(); @Override void suspend(); static final String SERVICE_NAME; }
@Test public void suspendsWorkspaceService() throws Exception { AtomicBoolean isAnyRunning = new AtomicBoolean(true); String workspaceId = "workspace123"; when(workspaceRuntimes.getActive()).thenReturn(Collections.singleton(workspaceId)); when(workspaceRuntimes.getStatus(workspaceId)).thenReturn(WorkspaceStatus.RUNNING); when(workspaceRuntimes.isAnyInProgress()) .thenAnswer( inv -> { boolean prev = isAnyRunning.get(); isAnyRunning.set(false); return prev; }); termination.suspend(); verify(workspaceRuntimes, atLeastOnce()).isAnyInProgress(); verify(workspaceManager, never()).stopWorkspace(anyString(), anyMap()); }