steinman | 6d1bd6c | 2018-12-21 13:06:31 -0800 | [diff] [blame] | 1 | // 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. |
| 14 | package com.google.devtools.build.lib.runtime; |
| 15 | |
| 16 | import static com.google.common.truth.Truth.assertThat; |
michajlo | 660d17f | 2020-03-27 09:01:57 -0700 | [diff] [blame] | 17 | import static org.junit.Assert.assertThrows; |
steinman | 6d1bd6c | 2018-12-21 13:06:31 -0800 | [diff] [blame] | 18 | |
| 19 | import com.google.devtools.build.lib.actions.LocalHostCapacity; |
| 20 | import com.google.devtools.build.lib.actions.ResourceSet; |
| 21 | import com.google.devtools.build.lib.runtime.LoadingPhaseThreadsOption.LoadingPhaseThreadCountConverter; |
| 22 | import com.google.devtools.common.options.OptionsParsingException; |
| 23 | import org.junit.Before; |
| 24 | import org.junit.Test; |
| 25 | import org.junit.runner.RunWith; |
| 26 | import org.junit.runners.JUnit4; |
| 27 | |
| 28 | /** Tests {@link LoadingPhaseThreadCountConverter}. */ |
| 29 | @RunWith(JUnit4.class) |
| 30 | public 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 { |
lberki | f1e5e87 | 2019-05-23 08:11:58 -0700 | [diff] [blame] | 41 | LocalHostCapacity.setLocalHostCapacity(ResourceSet.createWithRamCpu(1, 7)); |
steinman | 6d1bd6c | 2018-12-21 13:06:31 -0800 | [diff] [blame] | 42 | assertThat(loadingPhaseThreadCountConverter.convert("auto")).isEqualTo(7); |
| 43 | } |
| 44 | |
| 45 | @Test |
| 46 | public void testAutoLoadingPhaseThreadsCappedForTests() throws Exception { |
lberki | f1e5e87 | 2019-05-23 08:11:58 -0700 | [diff] [blame] | 47 | LocalHostCapacity.setLocalHostCapacity(ResourceSet.createWithRamCpu(1, 123)); |
steinman | 6d1bd6c | 2018-12-21 13:06:31 -0800 | [diff] [blame] | 48 | 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 | } |