### Question:
Supervisor { @SuppressWarnings("MissingSwitchDefault") public void setGoal(final Goal goal) { if (this.goal == goal) { return; } log.debug("Supervisor {}: setting goal: {}", job.getId(), goal); this.goal = goal; statusUpdater.setGoal(goal); switch (goal) { case START: currentCommand = new Start(); reactor.signal(); metrics.supervisorStarted(); break; case STOP: case UNDEPLOY: currentCommand = new Stop(); reactor.signal(); metrics.supervisorStopped(); break; } } Supervisor(final Builder builder); @SuppressWarnings("MissingSwitchDefault") void setGoal(final Goal goal); void close(); void join(); boolean isStarting(); boolean isStopping(); boolean isDone(); String containerId(); static Builder newBuilder(); @Override String toString();  }### Answer:
@Test public void verifySupervisorRestartsExitedContainer() throws Exception { final String containerId1 = "deadbeef1"; final String containerId2 = "deadbeef2"; final ContainerCreation createResponse1 = ContainerCreation.builder().id(containerId1).build(); final ContainerCreation createResponse2 = ContainerCreation.builder().id(containerId2).build(); when(docker.createContainer(any(ContainerConfig.class), any(String.class))) .thenReturn(createResponse1); final ImageInfo imageInfo = mock(ImageInfo.class); when(docker.inspectImage(IMAGE)).thenReturn(imageInfo); when(docker.inspectContainer(eq(containerId1))).thenReturn(runningResponse); final SettableFuture<ContainerExit> waitFuture1 = SettableFuture.create(); final SettableFuture<ContainerExit> waitFuture2 = SettableFuture.create(); when(docker.waitContainer(containerId1)).thenAnswer(futureAnswer(waitFuture1)); when(docker.waitContainer(containerId2)).thenAnswer(futureAnswer(waitFuture2)); sut.setGoal(START); verify(docker, timeout(30000)).createContainer(any(ContainerConfig.class), any(String.class)); verify(docker, timeout(30000)).startContainer(eq(containerId1)); verify(docker, timeout(30000)).waitContainer(containerId1); when(docker.inspectContainer(eq(containerId1))).thenReturn(stoppedResponse); when(docker.createContainer(any(ContainerConfig.class), any(String.class))) .thenReturn(createResponse2); when(docker.inspectContainer(eq(containerId2))).thenReturn(runningResponse); waitFuture1.set(ContainerExit.create(1L)); verify(docker, timeout(30000)).createContainer(any(ContainerConfig.class), any(String.class)); verify(docker, timeout(30000)).startContainer(eq(containerId2)); verify(docker, timeout(30000)).waitContainer(containerId2); }
@Test public void verifySupervisorHandlesUndeployingOfNotRunningContainerWithGracePeriod() throws Exception { final int gracePeriod = 5; final Job job = JOB.toBuilder() .setGracePeriod(gracePeriod) .build(); final Supervisor sut = createSupervisor(job); sut.setGoal(Goal.UNDEPLOY); verify(model, timeout(30000)).setTaskStatus(eq(job.getId()), argThat(is(taskStatusWithState(TaskStatus.State.STOPPING)))); verify(model, timeout(30000)).setTaskStatus(eq(job.getId()), argThat(is(taskStatusWithState(TaskStatus.State.STOPPED)))); verify(sleeper, never()).sleep(gracePeriod * 1000); }