blob: 2159ffe41c191f16c2f6e21497bb49fcb3f550a8 [file] [log] [blame]
steinman6d1bd6c2018-12-21 13:06:31 -08001// Copyright 2018 The Bazel Authors. All rights reserved.
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7// http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14package com.google.devtools.build.lib.runtime;
15
16import static com.google.common.truth.Truth.assertThat;
michajlo660d17f2020-03-27 09:01:57 -070017import static org.junit.Assert.assertThrows;
steinman6d1bd6c2018-12-21 13:06:31 -080018
19import com.google.devtools.build.lib.actions.LocalHostCapacity;
20import com.google.devtools.build.lib.actions.ResourceSet;
21import com.google.devtools.build.lib.runtime.LoadingPhaseThreadsOption.LoadingPhaseThreadCountConverter;
22import com.google.devtools.common.options.OptionsParsingException;
23import org.junit.Before;
24import org.junit.Test;
25import org.junit.runner.RunWith;
26import org.junit.runners.JUnit4;
27
28/** Tests {@link LoadingPhaseThreadCountConverter}. */
29@RunWith(JUnit4.class)
30public class LoadingPhaseThreadsConverterTest {
31
32 private LoadingPhaseThreadCountConverter loadingPhaseThreadCountConverter;
33
34 @Before
35 public void setUp() throws OptionsParsingException {
36 loadingPhaseThreadCountConverter = new LoadingPhaseThreadCountConverter();
37 }
38
39 @Test
40 public void testAutoLoadingPhaseThreadsUsesHardwareSettings() throws Exception {
lberkif1e5e872019-05-23 08:11:58 -070041 LocalHostCapacity.setLocalHostCapacity(ResourceSet.createWithRamCpu(1, 7));
steinman6d1bd6c2018-12-21 13:06:31 -080042 assertThat(loadingPhaseThreadCountConverter.convert("auto")).isEqualTo(7);
43 }
44
45 @Test
46 public void testAutoLoadingPhaseThreadsCappedForTests() throws Exception {
lberkif1e5e872019-05-23 08:11:58 -070047 LocalHostCapacity.setLocalHostCapacity(ResourceSet.createWithRamCpu(1, 123));
steinman6d1bd6c2018-12-21 13:06:31 -080048 assertThat(loadingPhaseThreadCountConverter.convert("auto")).isEqualTo(20);
49 }
50
51 @Test
52 public void testExplicitLoadingPhaseThreadsCappedForTests() throws Exception {
53 assertThat(loadingPhaseThreadCountConverter.convert("200")).isEqualTo(20);
54 }
55
56 @Test
57 public void testExplicitLoadingPhaseThreadsMustBeAtLeast1() throws Exception {
58 OptionsParsingException thrown =
59 assertThrows(
60 OptionsParsingException.class, () -> loadingPhaseThreadCountConverter.convert("0"));
61 assertThat(thrown).hasMessageThat().contains("must be at least 1");
62 }
63}