SystemRamCheckingWorkspaceManager extends LimitsCheckingWorkspaceManager { @VisibleForTesting <T extends WorkspaceImpl> T checkSystemRamLimitAndPropagateStart(WorkspaceCallback<T> callback) throws ServerException, NotFoundException, ConflictException { if (systemRamInfoProvider.getSystemRamInfo().isSystemRamLimitExceeded()) { throw new LimitExceededException( "Low RAM. Your workspace cannot be started until the system has more RAM available."); } return callback.call(); } @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( expectedExceptions = LimitExceededException.class, expectedExceptionsMessageRegExp = "Low RAM. Your workspace cannot be started until the system has more RAM available." ) public void shouldNotBeAbleToStartNewWorkspaceIfSystemRamLimitIsExceeded() throws Exception { when(systemRamInfoProvider.getSystemRamInfo()) .thenReturn(new SystemRamInfo(parseSize("2.95 GiB"), parseSize("3 GiB"))); final SystemRamCheckingWorkspaceManager manager = managerBuilder().setSystemRamInfoProvider(systemRamInfoProvider).build(); manager.checkSystemRamLimitAndPropagateStart(null); }
@Test public void shouldCallStartCallbackIfEverythingIsOkayWithSystemRamLimits() throws Exception { final WorkspaceCallback callback = mock(WorkspaceCallback.class); final SystemRamCheckingWorkspaceManager manager = managerBuilder().build(); manager.checkSystemRamLimitAndPropagateStart(callback); verify(callback).call(); }