DefaultAgentCache implements AgentCache { @Override public List<AgentInstanceGroup> getInstanceGroups() { return new ArrayList<>(dataSnapshot.getInstanceGroups()); } @Inject  DefaultAgentCache(AgentManagementConfiguration configuration,
                             AgentStore agentStore,
                             InstanceCloudConnector connector,
                             Registry registry);   DefaultAgentCache(AgentManagementConfiguration configuration,
                      AgentStore agentStore,
                      InstanceCloudConnector connector,
                      Registry registry,
                      Scheduler scheduler); @Activator void enterActiveMode(); void shutdown(); @Override List<AgentInstanceGroup> getInstanceGroups(); @Override AgentInstanceGroup getInstanceGroup(String instanceGroupId); @Override Optional<AgentInstanceGroup> findInstanceGroup(String instanceGroupId); @Override Set<AgentInstance> getAgentInstances(String instanceGroupId); @Override AgentInstance getAgentInstance(String instanceId); @Override Optional<AgentInstance> findAgentInstance(String instanceId); @Override Single<AgentInstanceGroup> updateInstanceGroupStore(String instanceGroupId, Function<AgentInstanceGroup, AgentInstanceGroup> function); @Override Single<AgentInstanceGroup> updateInstanceGroupStoreAndSyncCloud(String instanceGroupId, Function<AgentInstanceGroup, AgentInstanceGroup> function); @Override Single<AgentInstance> updateAgentInstanceStore(String instanceId, Function<AgentInstance, AgentInstance> function); @Override Completable removeInstances(String instanceGroupId, Set<String> agentInstanceIds); @Override Observable<CacheUpdateEvent> events();  }
@Test public void testDiscoverNewInstanceGroup() { int initialCount = cache.getInstanceGroups().size(); InstanceGroup instanceGroupToAdd = instanceGroupsGenerator.getValue(); testConnector.addInstanceGroup(instanceGroupToAdd); testScheduler.advanceTimeBy(FULL_CACHE_REFRESH_INTERVAL_MS, TimeUnit.MILLISECONDS); assertThat(cache.getInstanceGroups()).hasSize(initialCount + 1); verify(agentStore, times(initialCount + 1)).storeAgentInstanceGroup(any()); expectInstanceGroupUpdateEvent(eventSubscriber, instanceGroupToAdd.getId()); }
@Test public void testEventsAreNotDuplicated() { List<InstanceGroup> instanceGroups = testConnector.getInstanceGroups().toBlocking().single(); InstanceGroup instanceGroupToRemove = instanceGroups.get(0); InstanceGroup instanceGroupToUpdate = instanceGroups.get(1); InstanceGroup instanceGroupToAdd = instanceGroupsGenerator.getValue(); testConnector.addInstanceGroup(instanceGroupToAdd); testScheduler.advanceTimeBy(SYNCNHRONIZE_WITH_INSTANCE_CACHE_INTERVAL * 3, TimeUnit.MILLISECONDS); expectInstanceGroupUpdateEvent(eventSubscriber, instanceGroupToAdd.getId()); expectNoInstanceUpdateEvent(eventSubscriber); testConnector.removeInstanceGroup(instanceGroupToRemove.getId()); testScheduler.advanceTimeBy(SYNCNHRONIZE_WITH_INSTANCE_CACHE_INTERVAL * 3, TimeUnit.MILLISECONDS); expectInstanceGroupUpdateEvent(eventSubscriber, instanceGroupToRemove.getId()); expectNoInstanceUpdateEvent(eventSubscriber); InstanceGroup updatedSecond = instanceGroupToUpdate.toBuilder().withMax(100).build(); testConnector.updateInstanceGroup(instanceGroupToUpdate.getId(), updatedSecond); testScheduler.advanceTimeBy(SYNCNHRONIZE_WITH_INSTANCE_CACHE_INTERVAL * 3, TimeUnit.MILLISECONDS); expectInstanceGroupUpdateEvent(eventSubscriber, instanceGroupToUpdate.getId()); expectNoInstanceUpdateEvent(eventSubscriber); }