Search in sources :

Example 6 with Resource

use of org.eclipse.che.multiuser.resource.model.Resource in project che-server by eclipse-che.

the class RamResourceUsageTrackerTest method shouldNotSumRamOfStoppedWorkspaceWhenGettingUsedRamForGivenAccount.

@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());
}
Also used : WorkspaceImpl(org.eclipse.che.api.workspace.server.model.impl.WorkspaceImpl) Resource(org.eclipse.che.multiuser.resource.model.Resource) Test(org.testng.annotations.Test)

Example 7 with Resource

use of org.eclipse.che.multiuser.resource.model.Resource in project che-server by eclipse-che.

the class WorkspaceResourceUsageTrackerTest method shouldReturnUsedWorkspacesForGivenAccount.

@Test
public void shouldReturnUsedWorkspacesForGivenAccount() throws Exception {
    when(accountManager.getById(any())).thenReturn(account);
    when(account.getName()).thenReturn("testAccount");
    when(workspaceManager.getByNamespace(anyString(), anyBoolean(), anyInt(), anyLong())).thenReturn(new Page<>(Arrays.asList(new WorkspaceImpl(), new WorkspaceImpl(), new WorkspaceImpl()), 0, 3, 3));
    Optional<Resource> usedWorkspacesOpt = workspaceResourceUsageTracker.getUsedResource("account123");
    assertTrue(usedWorkspacesOpt.isPresent());
    Resource usedWorkspaces = usedWorkspacesOpt.get();
    assertEquals(usedWorkspaces.getType(), WorkspaceResourceType.ID);
    assertEquals(usedWorkspaces.getAmount(), 3);
    assertEquals(usedWorkspaces.getUnit(), WorkspaceResourceType.UNIT);
    verify(accountManager).getById(eq("account123"));
    verify(workspaceManager).getByNamespace(eq("testAccount"), eq(false), anyInt(), anyLong());
}
Also used : WorkspaceImpl(org.eclipse.che.api.workspace.server.model.impl.WorkspaceImpl) Resource(org.eclipse.che.multiuser.resource.model.Resource) Test(org.testng.annotations.Test)

Example 8 with Resource

use of org.eclipse.che.multiuser.resource.model.Resource in project che-server by eclipse-che.

the class ResourceManagerTest method testGetsAccountTotalResources.

@Test
public void testGetsAccountTotalResources() throws Exception {
    final List<Resource> res = ImmutableList.of(new ResourceImpl("RAM", 2048, "mb"), new ResourceImpl("timeout", 15, "minutes"));
    doReturn(res).when(providedResources).getResources();
    List<? extends Resource> actual = resourceManager.getTotalResources(ACCOUNT_ID);
    assertEquals(actual.size(), res.size());
    assertTrue(actual.containsAll(res));
}
Also used : ResourceImpl(org.eclipse.che.multiuser.resource.spi.impl.ResourceImpl) Resource(org.eclipse.che.multiuser.resource.model.Resource) Test(org.testng.annotations.Test)

Example 9 with Resource

use of org.eclipse.che.multiuser.resource.model.Resource in project che-server by eclipse-che.

the class ResourceAggregatorTest method shouldTestResourcesAggregationByTypes.

@Test
public void shouldTestResourcesAggregationByTypes() throws Exception {
    // given
    final ResourceImpl aResource = new ResourceImpl(A_RESOURCE_TYPE, 123, "unit");
    final ResourceImpl bResource = new ResourceImpl(B_RESOURCE_TYPE, 123, "unit");
    final ResourceImpl anotherBResource = new ResourceImpl(B_RESOURCE_TYPE, 321, "unit");
    final ResourceImpl aggregatedBResources = new ResourceImpl(B_RESOURCE_TYPE, 444, "unit");
    when(bResourceType.aggregate(any(), any())).thenReturn(aggregatedBResources);
    // when
    final Map<String, Resource> aggregatedResources = resourceAggregator.aggregateByType(asList(aResource, bResource, anotherBResource));
    // then
    verify(bResourceType).aggregate(eq(bResource), eq(anotherBResource));
    verify(aResourceType, never()).aggregate(any(), any());
    assertEquals(aggregatedResources.size(), 2);
    assertTrue(aggregatedResources.containsKey(A_RESOURCE_TYPE));
    assertTrue(aggregatedResources.containsValue(aResource));
    assertTrue(aggregatedResources.containsKey(B_RESOURCE_TYPE));
    assertTrue(aggregatedResources.containsValue(aggregatedBResources));
}
Also used : ResourceImpl(org.eclipse.che.multiuser.resource.spi.impl.ResourceImpl) Resource(org.eclipse.che.multiuser.resource.model.Resource) Test(org.testng.annotations.Test)

Example 10 with Resource

use of org.eclipse.che.multiuser.resource.model.Resource in project che-server by eclipse-che.

the class ResourceAggregator method excess.

/**
 * Returns list which contains resources from specified {@code sourceResources} which have
 * excessive amount in compare to specified {@code resourcesToCompare}.
 *
 * <p>Example :
 *
 * <pre>
 * |      \      | Source    | To compare| Result   |
 * |:------------|:----------|:----------|:---------|
 * | Resource1   | 5         | 3         | 2        |
 * | ----------- | --------- | --------- | -------- |
 * | Resource2   | -         | 9         | -        |
 * | ----------- | --------- | --------- | -------- |
 * | Resource3   | 1         | -         | 1        |
 * | ----------- | --------- | --------- | -------- |
 * </pre>
 *
 * @param sourceResources the source resources
 * @param resourcesToCompare the resources which should be compared to {@code sourceResources}
 * @throws IllegalArgumentException when {@code sourceResources} or {@code resourcesToCompare}
 *     contain resource with not supported type
 */
public List<? extends Resource> excess(List<? extends Resource> sourceResources, List<? extends Resource> resourcesToCompare) {
    checkSupporting(sourceResources);
    checkSupporting(resourcesToCompare);
    final Map<String, Resource> result = sourceResources.stream().collect(Collectors.toMap(Resource::getType, Function.identity()));
    for (Resource toCompare : resourcesToCompare) {
        String resourceType = toCompare.getType();
        final Resource sourceResource = result.get(resourceType);
        if (sourceResource != null) {
            if (sourceResource.getAmount() == toCompare.getAmount()) {
                // source resource doesn't have excessive amount
                result.remove(resourceType);
                continue;
            }
            try {
                Resource excess = deduct(sourceResource, toCompare);
                if (excess.getAmount() == 0) {
                    // source resource doesn't have excessive amount
                    result.remove(resourceType);
                } else {
                    result.put(resourceType, excess);
                }
            } catch (NoEnoughResourcesException e) {
                // source resource doesn't have excessive amount
                result.remove(resourceType);
            }
        }
    }
    return new ArrayList<>(result.values());
}
Also used : NoEnoughResourcesException(org.eclipse.che.multiuser.resource.api.exception.NoEnoughResourcesException) Resource(org.eclipse.che.multiuser.resource.model.Resource) ArrayList(java.util.ArrayList)

Aggregations

Resource (org.eclipse.che.multiuser.resource.model.Resource)52 ResourceImpl (org.eclipse.che.multiuser.resource.spi.impl.ResourceImpl)24 Test (org.testng.annotations.Test)24 ArrayList (java.util.ArrayList)14 NotFoundException (org.eclipse.che.api.core.NotFoundException)12 WorkspaceImpl (org.eclipse.che.api.workspace.server.model.impl.WorkspaceImpl)12 Optional (java.util.Optional)10 Inject (javax.inject.Inject)10 Singleton (javax.inject.Singleton)10 AccountManager (org.eclipse.che.account.api.AccountManager)10 Account (org.eclipse.che.account.shared.model.Account)10 ServerException (org.eclipse.che.api.core.ServerException)10 ResourceUsageTracker (org.eclipse.che.multiuser.resource.api.ResourceUsageTracker)10 NoEnoughResourcesException (org.eclipse.che.multiuser.resource.api.exception.NoEnoughResourcesException)10 List (java.util.List)8 WorkspaceManager (org.eclipse.che.api.workspace.server.WorkspaceManager)8 VisibleForTesting (com.google.common.annotations.VisibleForTesting)6 HashMap (java.util.HashMap)6 Collectors (java.util.stream.Collectors)6 Provider (javax.inject.Provider)6