RamResourceUsageTracker implements ResourceUsageTracker { @Override public Optional<Resource> getUsedResource(String accountId) throws NotFoundException, ServerException { final Account account = accountManager.getById(accountId); List<WorkspaceImpl> activeWorkspaces = Pages.stream( (maxItems, skipCount) -> workspaceManagerProvider .get() .getByNamespace(account.getName(), true, maxItems, skipCount)) .filter(ws -> STOPPED != ws.getStatus()) .collect(Collectors.toList()); long currentlyUsedRamMB = 0; for (WorkspaceImpl activeWorkspace : activeWorkspaces) { if (WorkspaceStatus.STARTING.equals(activeWorkspace.getStatus())) { WorkspaceConfigImpl config = activeWorkspace.getConfig(); if (config != null) { final EnvironmentImpl startingEnvironment = config.getEnvironments().get(activeWorkspace.getRuntime().getActiveEnv()); if (startingEnvironment != null) { currentlyUsedRamMB += environmentRamCalculator.calculate(startingEnvironment); } } } else { currentlyUsedRamMB += environmentRamCalculator.calculate(activeWorkspace.getRuntime()); } } if (currentlyUsedRamMB > 0) { return Optional.of( new ResourceImpl(RamResourceType.ID, currentlyUsedRamMB, RamResourceType.UNIT)); } else { return Optional.empty(); } } @Inject  RamResourceUsageTracker(
      Provider<WorkspaceManager> workspaceManagerProvider,
      AccountManager accountManager,
      EnvironmentRamCalculator environmentRamCalculator); @Override Optional<Resource> getUsedResource(String accountId);  }
@Test( expectedExceptions = NotFoundException.class, expectedExceptionsMessageRegExp = "Account was not found") public void shouldThrowNotFoundExceptionWhenAccountDoesNotExistOnGettingUsedRam() throws Exception { when(accountManager.getById(any())).thenThrow(new NotFoundException("Account was not found")); ramUsageTracker.getUsedResource(ACCOUNT_ID); }
@Test public void shouldReturnEmptyOptionalWhenAccountHasOnlyStoppedWorkspaces() throws Exception { mockWorkspaces(createWorkspace(WorkspaceStatus.STOPPED, 1000, 500, 500)); final Optional<Resource> usedRamOpt = ramUsageTracker.getUsedResource(ACCOUNT_ID); assertFalse(usedRamOpt.isPresent()); }
@Test public void shouldReturnUsedRamOfRunningWorkspaceForGivenAccount() throws Exception { mockWorkspaces(createWorkspace(WorkspaceStatus.RUNNING, 1000, 500, 500)); when(envRamCalculator.calculate(any(Runtime.class))).thenReturn(2000L); final Optional<Resource> usedRamOpt = ramUsageTracker.getUsedResource(ACCOUNT_ID); assertTrue(usedRamOpt.isPresent()); final Resource usedRam = usedRamOpt.get(); assertEquals(usedRam.getType(), RamResourceType.ID); assertEquals(usedRam.getAmount(), 2000L); assertEquals(usedRam.getUnit(), RamResourceType.UNIT); verify(accountManager).getById(ACCOUNT_ID); verify(workspaceManager).getByNamespace(anyString(), anyBoolean(), anyInt(), anyLong()); }
@Test public void shouldNotSumRamOfStoppedWorkspaceWhenGettingUsedRamForGivenAccount() throws Exception { final WorkspaceImpl stoppedWs = createWorkspace(WorkspaceStatus.STOPPED, 3500); final WorkspaceImpl runningWs = createWorkspace(WorkspaceStatus.RUNNING, 2500); mockWorkspaces(stoppedWs, runningWs); when(envRamCalculator.calculate(runningWs.getRuntime())).thenReturn(2500L); final Optional<Resource> usedRamOpt = ramUsageTracker.getUsedResource(ACCOUNT_ID); assertTrue(usedRamOpt.isPresent()); final Resource usedRam = usedRamOpt.get(); assertEquals(usedRam.getType(), RamResourceType.ID); assertEquals(usedRam.getAmount(), 2500L); assertEquals(usedRam.getUnit(), RamResourceType.UNIT); verify(accountManager).getById(ACCOUNT_ID); verify(workspaceManager).getByNamespace(anyString(), anyBoolean(), anyInt(), anyLong()); }
@Test public void returnUsedRamOfStartingWorkspaceForGivenAccount() throws Exception { mockWorkspaces(createWorkspace(WorkspaceStatus.STARTING, 1000, 500, 500)); when(envRamCalculator.calculate(any(Environment.class))).thenReturn(2000L); final Optional<Resource> usedRamOpt = ramUsageTracker.getUsedResource(ACCOUNT_ID); assertTrue(usedRamOpt.isPresent()); final Resource usedRam = usedRamOpt.get(); assertEquals(usedRam.getType(), RamResourceType.ID); assertEquals(usedRam.getAmount(), 2000L); assertEquals(usedRam.getUnit(), RamResourceType.UNIT); verify(accountManager).getById(ACCOUNT_ID); verify(workspaceManager).getByNamespace(anyString(), anyBoolean(), anyInt(), anyLong()); }