SystemRamCheckingWorkspaceManager extends LimitsCheckingWorkspaceManager { @VisibleForTesting <T extends WorkspaceImpl> T checkSystemRamLimitAndPropagateLimitedThroughputStart( WorkspaceCallback<T> callback) throws ServerException, NotFoundException, ConflictException { if (startSemaphore == null) { return checkSystemRamLimitAndPropagateStart(callback); } else { try { startSemaphore.acquire(); return checkSystemRamLimitAndPropagateStart(callback); } catch (InterruptedException e) { currentThread().interrupt(); throw new ServerException(e.getMessage(), e); } finally { startSemaphore.release(); } } } @Inject  SystemRamCheckingWorkspaceManager(
      WorkspaceDao workspaceDao,
      WorkspaceRuntimes runtimes,
      EventService eventService,
      AccountManager accountManager,
      @Named("che.workspace.auto_snapshot") boolean defaultAutoSnapshot,
      @Named("che.workspace.auto_restore") boolean defaultAutoRestore,
      SnapshotDao snapshotDao,
      WorkspaceSharedPool sharedPool,
      // own injects
      @Named("che.limits.workspace.env.ram") String maxRamPerEnv,
      @Named("limits.workspace.start.throughput") int maxSameTimeStartWSRequests,
      SystemRamInfoProvider systemRamInfoProvider,
      EnvironmentRamCalculator environmentRamCalculator,
      ResourceUsageManager resourceUsageManager,
      ResourcesLocks resourcesLocks); @Override WorkspaceImpl startWorkspace(
      String workspaceId, @Nullable String envName, @Nullable Boolean restore); @Override WorkspaceImpl startWorkspace(WorkspaceConfig config, String namespace, boolean isTemporary);  }
@Test public void shouldAcquireAndReleaseSemaphoreIfThroughputPropertyIsMoreThanZero() throws Exception { final SystemRamCheckingWorkspaceManager manager = managerBuilder().build(); Semaphore semaphore = mock(Semaphore.class); WorkspaceCallback callback = mock(WorkspaceCallback.class); manager.startSemaphore = semaphore; manager.checkSystemRamLimitAndPropagateLimitedThroughputStart(callback); verify(semaphore).acquire(); verify(semaphore).release(); }
@Test public void shouldSetSemaphoreToNullIfThroughputPropertyIsZero() throws Exception { final SystemRamCheckingWorkspaceManager manager = managerBuilder().setMaxSameTimeStartWSRequests(0).build(); WorkspaceCallback callback = mock(WorkspaceCallback.class); manager.checkSystemRamLimitAndPropagateLimitedThroughputStart(callback); assertNull(manager.startSemaphore); }
@Test public void shouldSetSemaphoreToNullIfThroughputPropertyIsLessThenZero() throws Exception { final SystemRamCheckingWorkspaceManager manager = managerBuilder().setMaxSameTimeStartWSRequests(-1).build(); WorkspaceCallback callback = mock(WorkspaceCallback.class); manager.checkSystemRamLimitAndPropagateLimitedThroughputStart(callback); assertNull(manager.startSemaphore); }