@Test
  public void testClusterHostInfoCache() throws Exception {

    Type type = new TypeToken<Map<String, Set<String>>>() {}.getType();

    //Data for stages
    Map<String, Set<String>> clusterHostInfo1 = StageUtils.getGson().fromJson(CLUSTER_HOST_INFO, type);
    int stageId = 1;
    int requestId1 = 1;
    int requestId2 = 2;

    Properties properties = new Properties();
    Configuration conf = new Configuration(properties);
    Clusters fsm = mock(Clusters.class);
    Cluster oneClusterMock = mock(Cluster.class);
    Service serviceObj = mock(Service.class);
    ServiceComponent scomp = mock(ServiceComponent.class);
    ServiceComponentHost sch = mock(ServiceComponentHost.class);
    UnitOfWork unitOfWork = mock(UnitOfWork.class);
    AgentCommandsPublisher agentCommandsPublisher = mock(AgentCommandsPublisher.class);
    when(fsm.getCluster(anyString())).thenReturn(oneClusterMock);
    when(oneClusterMock.getService(anyString())).thenReturn(serviceObj);
    when(serviceObj.getServiceComponent(anyString())).thenReturn(scomp);
    when(scomp.getServiceComponentHost(anyString())).thenReturn(sch);
    when(serviceObj.getCluster()).thenReturn(oneClusterMock);
    Host host = mock(Host.class);
    HashMap<String, ServiceComponentHost> hosts =
      new HashMap<>();
    hosts.put(hostname, sch);
    when(scomp.getServiceComponentHosts()).thenReturn(hosts);

    when(fsm.getHost(anyString())).thenReturn(host);
    when(host.getState()).thenReturn(HostState.HEALTHY);
    when(host.getHostName()).thenReturn(hostname);
    when(host.getHostId()).thenReturn(hostId);

    ActionDBAccessor db = mock(ActionDBAccessorImpl.class);
    HostRoleCommandDAO hostRoleCommandDAOMock = mock(HostRoleCommandDAO.class);
    Mockito.doNothing().when(hostRoleCommandDAOMock).publishTaskCreateEvent(anyListOf(HostRoleCommand.class));

    RequestEntity request = mock(RequestEntity.class);
    when(request.isExclusive()).thenReturn(false);
    when(request.getClusterHostInfo()).thenReturn(CLUSTER_HOST_INFO);
    when(db.getRequestEntity(anyLong())).thenReturn(request);

    Stage s1 = StageUtils.getATestStage(requestId1, stageId, hostname,
      "{\"host_param\":\"param_value\"}", "{\"stage_param\":\"param_value\"}");
    Stage s2 = StageUtils.getATestStage(requestId2, stageId, hostname,
      "{\"host_param\":\"param_value\"}", "{\"stage_param\":\"param_value\"}");

    when(db.getCommandsInProgressCount()).thenReturn(1);
    when(db.getFirstStageInProgressPerRequest()).thenReturn(Collections.singletonList(s1));

    //Keep large number of attempts so that the task is not expired finally
    //Small action timeout to test rescheduling
    ActionScheduler scheduler = new ActionScheduler(100, 100, db, fsm,
        10000, new HostsMap((String) null), unitOfWork, null, conf, entityManagerProviderMock,
        hostRoleCommandDAOMock, (HostRoleCommandFactory)null, agentCommandsPublisher);
    scheduler.setTaskTimeoutAdjustment(false);

    List<AgentCommand> commands = waitForQueueSize(hostId, agentCommandsPublisher, 1, scheduler);

    assertTrue(commands != null && commands.size() == 1);
    AgentCommand scheduledCommand = commands.get(0);
    assertTrue(scheduledCommand instanceof ExecutionCommand);
    assertEquals(String.valueOf(requestId1) + "-" + stageId, ((ExecutionCommand) scheduledCommand).getCommandId());

    assertEquals(clusterHostInfo1, ((ExecutionCommand) scheduledCommand).getClusterHostInfo());

    when(db.getCommandsInProgressCount()).thenReturn(1);
    when(db.getFirstStageInProgressPerRequest()).thenReturn(Collections.singletonList(s2));

    //Verify that ActionSheduler does not return cached value of cluster host info for new requestId
    commands = waitForQueueSize(hostId, agentCommandsPublisher, 1, scheduler);

    assertTrue(commands != null && commands.size() == 1);
    scheduledCommand = commands.get(0);
    assertTrue(scheduledCommand instanceof ExecutionCommand);
    assertEquals(String.valueOf(requestId2) + "-" + stageId, ((ExecutionCommand) scheduledCommand).getCommandId());
    assertEquals(clusterHostInfo1, ((ExecutionCommand) scheduledCommand).getClusterHostInfo());
  }
This test sends verifies that ActionScheduler returns up-to-date cluster host info and caching works correctly.
ambari-server/src/test/java/org/apache/ambari/server/actionmanager/TestActionScheduler.java