@Test
public void testScaleDownForIdleAgentsPrefersPhasedOutInstanceGroups() {

    when(configuration.getFlexMinIdle()).thenReturn(0);
    when(configuration.getFlexMaxIdle()).thenReturn(5);
    AgentInstanceGroup activeInstanceGroup = createPartition("activeInstanceGroup", InstanceGroupLifecycleState.Active, "r4.16xlarge", 0, 5, 10);
    AgentInstanceGroup phasedOutInstanceGroup = createPartition("phasedOutInstanceGroup", InstanceGroupLifecycleState.PhasedOut, "r4.16xlarge", 0, 5, 10);
    List<AgentInstanceGroup> instanceGroups = Lists.newArrayList(phasedOutInstanceGroup, activeInstanceGroup);
    when(agentManagementService.getInstanceGroups()).thenReturn(instanceGroups);
    List<AgentInstance> activeInstances = createAgents(5, "activeInstanceGroup", false);
    when(agentManagementService.getAgentInstances("activeInstanceGroup")).thenReturn(activeInstances);
    List<AgentInstance> phasedOutInstances = createAgents(5, "phasedOutInstanceGroup", false);
    when(agentManagementService.getAgentInstances("phasedOutInstanceGroup")).thenReturn(phasedOutInstances);
    when(v3JobOperations.getTasks()).thenReturn(Collections.emptyList());
    testScheduler.advanceTimeBy(11, TimeUnit.MINUTES);
    ClusterAgentAutoScaler clusterAgentAutoScaler = new ClusterAgentAutoScaler(titusRuntime, configuration,
            agentManagementService, v3JobOperations, schedulingService, testScheduler);
    clusterAgentAutoScaler.doAgentScaling().await();
    verify(agentManagementService, times(5)).updateAgentInstanceAttributes(matches("phasedOutInstanceGroup.*"), any());
    verify(agentManagementService, times(0)).updateAgentInstanceAttributes(matches("activeInstanceGroup.*"), any());
}