AgentService extends AbstractAgentService implements TopicListener<AgentRequest>, AgentStatusUpdateListener, ConnectionAgentListener { Set<AgentInfo> selectAgent(User user, Set<AgentInfo> allFreeAgents, int agentCount) { Set<AgentInfo> recentlyUsedAgents = hazelcastService.getOrDefault(CACHE_RECENTLY_USED_AGENTS, user.getUserId(), emptySet()); Comparator<AgentInfo> recentlyUsedPriorityComparator = (agent1, agent2) -> { if (recentlyUsedAgents.contains(agent1)) { return -1; } if (recentlyUsedAgents.contains(agent2)) { return 1; } return 0; }; Stream<AgentInfo> ownedFreeAgentStream = allFreeAgents .stream() .filter(agentInfo -> isOwnedAgent(agentInfo, user.getUserId())) .sorted(recentlyUsedPriorityComparator); Stream<AgentInfo> freeAgentStream = allFreeAgents .stream() .filter(this::isCommonAgent) .sorted(recentlyUsedPriorityComparator); return concat(ownedFreeAgentStream, freeAgentStream) .limit(agentCount) .collect(toSet()); }  @PostConstruct void init(); List<AgentInfo> getAllActive(); @Override Map<String, MutableInt> getAvailableAgentCountMap(String userId); @Override AgentControllerIdentityImplementation getAgentIdentityByIpAndName(String ip, String name); AgentInfo getAgent(String ip, String name); @Transactional void approve(String ip, String name, boolean approve); Set<AgentInfo> getAllAttachedFreeApprovedAgents(); Set<AgentInfo> getAllAttachedFreeApprovedAgentsForUser(String userId); synchronized void runAgent(User user, final SingleConsole singleConsole,
									  final GrinderProperties grinderProperties, final Integer agentCount); @Override void stop(String ip, String name); void stop(AgentControllerIdentityImplementation agentIdentity); @Override void update(String ip, String name); @Override SystemDataModel getSystemDataModel(String ip, String name, String region); void updateAgent(AgentIdentity agentIdentity); void addConnectionAgent(String ip, int port, String region); @Override int getReadyAgentCount(String userId, String targetRegion); @Override List<AgentInfo> getAllAttached(); @Override String createKey(AgentControllerIdentityImplementation agentIdentity); @Override void execute(TopicEvent<AgentRequest> event); @Override void update(Map<AgentIdentity, AgentProcessControlImplementation.AgentStatus> agentMap); @Override void onConnectionAgentMessage(String ip, String name, int port);  }
@Test public void selectAgentsTest() { Set<AgentInfo> recentlyUsedAgents = getRecentlyUsedAgents(); hazelcastService.put(CACHE_RECENTLY_USED_AGENTS, TEST_USER_ID, recentlyUsedAgents); User testUser = new User(); testUser.setUserId(TEST_USER_ID); Set<AgentInfo> allFreeAgents = getFreeAgents(); Set<AgentInfo> selectedAgents = agentService.selectAgent(testUser, allFreeAgents, 5); assertThat(selectedAgents.size(), is(5)); List<AgentInfo> selectedOwnedAgents = selectedAgents .stream() .filter(agentInfo -> agentInfo.getRegion().contains("owned")) .collect(toList()); assertThat(selectedOwnedAgents.size(), is(2)); assertTrue(selectedAgents.contains(createAgentInfo("test-agent-1", "test-region"))); assertTrue(selectedAgents.contains(createAgentInfo("test-agent-3", "test-region"))); assertTrue(selectedAgents.contains(createAgentInfo("test-agent-5", "test-region"))); recentlyUsedAgents.add(createAgentInfo("test-agent-11", "test-region_owned_test-user")); recentlyUsedAgents.add(createAgentInfo("test-agent-14", "test-region_owned_test-user")); hazelcastService.put(CACHE_RECENTLY_USED_AGENTS, TEST_USER_ID, recentlyUsedAgents); allFreeAgents.add(createAgentInfo("test-agent-8", "test-region_owned_test-user")); allFreeAgents.add(createAgentInfo("test-agent-9", "test-region_owned_test-user")); allFreeAgents.add(createAgentInfo("test-agent-10", "test-region_owned_test-user")); allFreeAgents.add(createAgentInfo("test-agent-11", "test-region_owned_test-user")); allFreeAgents.add(createAgentInfo("test-agent-12", "test-region_owned_test-user")); allFreeAgents.add(createAgentInfo("test-agent-13", "test-region_owned_test-user")); allFreeAgents.add(createAgentInfo("test-agent-14", "test-region_owned_test-user")); allFreeAgents.add(createAgentInfo("test-agent-15", "test-region_owned_test-user")); selectedAgents = agentService.selectAgent(testUser, allFreeAgents, 3); assertThat(selectedAgents.size(), is(3)); assertTrue(selectedAgents.contains(createAgentInfo("test-agent-6", "test-region_owned_test-user"))); assertTrue(selectedAgents.contains(createAgentInfo("test-agent-11", "test-region_owned_test-user"))); assertTrue(selectedAgents.contains(createAgentInfo("test-agent-14", "test-region_owned_test-user"))); hazelcastService.delete(CACHE_RECENTLY_USED_AGENTS, TEST_USER_ID); }