You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

CeConfigurationImplTest.java 4.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. /*
  2. * SonarQube
  3. * Copyright (C) 2009-2021 SonarSource SA
  4. * mailto:info AT sonarsource DOT com
  5. *
  6. * This program is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU Lesser General Public
  8. * License as published by the Free Software Foundation; either
  9. * version 3 of the License, or (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  14. * Lesser General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU Lesser General Public License
  17. * along with this program; if not, write to the Free Software Foundation,
  18. * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  19. */
  20. package org.sonar.ce.configuration;
  21. import java.util.Random;
  22. import org.junit.Test;
  23. import org.sonar.api.config.internal.ConfigurationBridge;
  24. import org.sonar.api.config.internal.MapSettings;
  25. import org.sonar.api.utils.MessageException;
  26. import static java.lang.Math.abs;
  27. import static org.assertj.core.api.Assertions.assertThat;
  28. import static org.assertj.core.api.Assertions.assertThatThrownBy;
  29. public class CeConfigurationImplTest {
  30. public static final ConfigurationBridge EMPTY_CONFIGURATION = new ConfigurationBridge(new MapSettings());
  31. private SimpleWorkerCountProvider workerCountProvider = new SimpleWorkerCountProvider();
  32. @Test
  33. public void getWorkerCount_returns_1_when_there_is_no_WorkerCountProvider() {
  34. assertThat(new CeConfigurationImpl(EMPTY_CONFIGURATION).getWorkerCount()).isOne();
  35. }
  36. @Test
  37. public void getWorkerMaxCount_returns_1_when_there_is_no_WorkerCountProvider() {
  38. assertThat(new CeConfigurationImpl(EMPTY_CONFIGURATION).getWorkerMaxCount()).isOne();
  39. }
  40. @Test
  41. public void getWorkerCount_returns_value_returned_by_WorkerCountProvider_when_available() {
  42. int value = randomValidWorkerCount();
  43. workerCountProvider.set(value);
  44. assertThat(new CeConfigurationImpl(EMPTY_CONFIGURATION, workerCountProvider).getWorkerCount()).isEqualTo(value);
  45. }
  46. @Test
  47. public void getWorkerMaxCount_returns_10_whichever_the_value_returned_by_WorkerCountProvider() {
  48. int value = randomValidWorkerCount();
  49. workerCountProvider.set(value);
  50. assertThat(new CeConfigurationImpl(EMPTY_CONFIGURATION, workerCountProvider).getWorkerMaxCount()).isEqualTo(10);
  51. }
  52. @Test
  53. public void constructor_throws_MessageException_when_WorkerCountProvider_returns_0() {
  54. workerCountProvider.set(0);
  55. assertThatThrownBy(() -> new CeConfigurationImpl(EMPTY_CONFIGURATION, workerCountProvider))
  56. .isInstanceOf(MessageException.class)
  57. .hasMessage("Worker count '0' is invalid. " +
  58. "It must be an integer strictly greater than 0 and less or equal to 10");
  59. }
  60. @Test
  61. public void constructor_throws_MessageException_when_WorkerCountProvider_returns_less_than_0() {
  62. int value = -1 - abs(new Random().nextInt());
  63. workerCountProvider.set(value);
  64. assertThatThrownBy(() -> new CeConfigurationImpl(EMPTY_CONFIGURATION, workerCountProvider))
  65. .isInstanceOf(MessageException.class)
  66. .hasMessage("Worker count '" + value + "' is invalid. " +
  67. "It must be an integer strictly greater than 0 and less or equal to 10");
  68. }
  69. @Test
  70. public void constructor_throws_MessageException_when_WorkerCountProvider_returns_more_then_10() {
  71. int value = 10 + abs(new Random().nextInt());
  72. workerCountProvider.set(value);
  73. assertThatThrownBy(() -> new CeConfigurationImpl(EMPTY_CONFIGURATION, workerCountProvider))
  74. .isInstanceOf(MessageException.class)
  75. .hasMessage("Worker count '" + value + "' is invalid. " +
  76. "It must be an integer strictly greater than 0 and less or equal to 10");
  77. }
  78. @Test
  79. public void getCleanCeTasksInitialDelay_returns_0() {
  80. assertThat(new CeConfigurationImpl(EMPTY_CONFIGURATION).getCleanTasksInitialDelay())
  81. .isZero();
  82. workerCountProvider.set(1);
  83. assertThat(new CeConfigurationImpl(EMPTY_CONFIGURATION, workerCountProvider).getCleanTasksInitialDelay())
  84. .isZero();
  85. }
  86. @Test
  87. public void getCleanCeTasksDelay_returns_2() {
  88. assertThat(new CeConfigurationImpl(EMPTY_CONFIGURATION).getCleanTasksDelay())
  89. .isEqualTo(2L);
  90. workerCountProvider.set(1);
  91. assertThat(new CeConfigurationImpl(EMPTY_CONFIGURATION, workerCountProvider).getCleanTasksDelay())
  92. .isEqualTo(2L);
  93. }
  94. private static final class SimpleWorkerCountProvider implements WorkerCountProvider {
  95. private int value = 0;
  96. void set(int value) {
  97. this.value = value;
  98. }
  99. @Override
  100. public int get() {
  101. return value;
  102. }
  103. }
  104. private static int randomValidWorkerCount() {
  105. return 1 + Math.abs(new Random().nextInt(10));
  106. }
  107. }