WorkspaceServiceTermination implements ServiceTermination { @Override public void terminate() throws InterruptedException { Preconditions.checkState(runtimes.refuseStart()); WorkspaceStoppedEventsPropagator propagator = new WorkspaceStoppedEventsPropagator(); eventService.subscribe(propagator); try { stopRunningAndStartingWorkspacesAsync(); waitAllWorkspacesStopped(); 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(dataProvider = "workspaceStoppedOnTerminationStatuses", timeOut = 1000L) public void shutsDownWorkspaceServiceIfFullShutdownRequested(WorkspaceStatus status) throws Exception { String workspaceId = "workspace123"; AtomicBoolean isAnyRunning = new AtomicBoolean(true); when(workspaceRuntimes.isAnyActive()).thenAnswer(inv -> isAnyRunning.get()); when(workspaceRuntimes.getActive()).thenReturn(Collections.singleton(workspaceId)); when(workspaceRuntimes.getStatus(workspaceId)).thenReturn(status); when(runtimeInfrastructure.getIdentities()).thenReturn(Collections.emptySet()); doAnswer( inv -> { isAnyRunning.set(false); return null; }) .when(workspaceManager) .stopWorkspace("workspace123", Collections.emptyMap()); termination.terminate(); }
@Test public void publishesStoppedWorkspaceStoppedEventsAsServiceItemStoppedEvents() throws Exception { when(workspaceRuntimes.getActive()).thenReturn(ImmutableSet.of("id1", "id2", "id3")); doAnswer( inv -> { @SuppressWarnings("unchecked") EventSubscriber<WorkspaceStatusEvent> subscriber = (EventSubscriber<WorkspaceStatusEvent>) inv.getArguments()[0]; subscriber.onEvent(newWorkspaceStatusEvent(WorkspaceStatus.STARTING, "id1")); subscriber.onEvent(newWorkspaceStatusEvent(WorkspaceStatus.RUNNING, "id1")); subscriber.onEvent(newWorkspaceStatusEvent(WorkspaceStatus.STOPPING, "id1")); subscriber.onEvent(newWorkspaceStatusEvent(WorkspaceStatus.STOPPED, "id1")); subscriber.onEvent(newWorkspaceStatusEvent(WorkspaceStatus.RUNNING, "id2")); subscriber.onEvent(newWorkspaceStatusEvent(WorkspaceStatus.STOPPING, "id2")); subscriber.onEvent(newWorkspaceStatusEvent(WorkspaceStatus.STOPPED, "id2")); subscriber.onEvent(newWorkspaceStatusEvent(WorkspaceStatus.STOPPED, "id3")); return null; }) .when(eventService) .subscribe(any()); termination.terminate(); verify(eventService) .publish( newDto(SystemServiceItemStoppedEventDto.class) .withService("workspace") .withItem("id1") .withCurrent(1) .withTotal(3)); verify(eventService) .publish( newDto(SystemServiceItemStoppedEventDto.class) .withService("workspace") .withItem("id2") .withCurrent(2) .withTotal(3)); verify(eventService) .publish( newDto(SystemServiceItemStoppedEventDto.class) .withService("workspace") .withItem("id3") .withCurrent(3) .withTotal(3)); }