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.

CommonCEQueueStatusImplTest.java 5.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  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.monitoring;
  21. import java.util.Optional;
  22. import java.util.Random;
  23. import org.junit.Test;
  24. import org.sonar.db.DbClient;
  25. import org.sonar.server.property.InternalProperties;
  26. import static org.assertj.core.api.Assertions.assertThat;
  27. import static org.assertj.core.api.Assertions.assertThatThrownBy;
  28. import static org.mockito.ArgumentMatchers.any;
  29. import static org.mockito.ArgumentMatchers.eq;
  30. import static org.mockito.Mockito.when;
  31. public abstract class CommonCEQueueStatusImplTest {
  32. private static final int SOME_RANDOM_MAX = 96535;
  33. private static final int SOME_PROCESSING_TIME = 8723;
  34. private DbClient dbClient;
  35. protected CommonCEQueueStatusImplTest(DbClient dbClient) {
  36. this.dbClient = dbClient;
  37. }
  38. public DbClient getDbClient() {
  39. return dbClient;
  40. }
  41. protected abstract CEQueueStatusImpl getUnderTest();
  42. @Test
  43. public void verify_just_created_instance_metrics() {
  44. assertThat(getUnderTest().getInProgressCount()).isZero();
  45. assertThat(getUnderTest().getErrorCount()).isZero();
  46. assertThat(getUnderTest().getSuccessCount()).isZero();
  47. assertThat(getUnderTest().getProcessingTime()).isZero();
  48. }
  49. @Test
  50. public void addInProgress_increases_InProgress() {
  51. getUnderTest().addInProgress();
  52. assertThat(getUnderTest().getInProgressCount()).isOne();
  53. assertThat(getUnderTest().getErrorCount()).isZero();
  54. assertThat(getUnderTest().getSuccessCount()).isZero();
  55. assertThat(getUnderTest().getProcessingTime()).isZero();
  56. }
  57. @Test
  58. public void addInProgress_any_number_of_call_change_by_1_per_call() {
  59. int calls = new Random().nextInt(SOME_RANDOM_MAX);
  60. for (int i = 0; i < calls; i++) {
  61. getUnderTest().addInProgress();
  62. }
  63. assertThat(getUnderTest().getInProgressCount()).isEqualTo(calls);
  64. assertThat(getUnderTest().getProcessingTime()).isZero();
  65. }
  66. @Test
  67. public void addError_throws_IAE_if_time_is_less_than_0() {
  68. assertThatThrownBy(() -> getUnderTest().addError(-1))
  69. .isInstanceOf(IllegalArgumentException.class)
  70. .hasMessage("Processing time can not be < 0");
  71. }
  72. @Test
  73. public void addError_increases_Error_and_decreases_InProgress_by_1_without_check_on_InProgress() {
  74. getUnderTest().addError(SOME_PROCESSING_TIME);
  75. assertThat(getUnderTest().getInProgressCount()).isEqualTo(-1);
  76. assertThat(getUnderTest().getErrorCount()).isOne();
  77. assertThat(getUnderTest().getSuccessCount()).isZero();
  78. assertThat(getUnderTest().getProcessingTime()).isEqualTo(SOME_PROCESSING_TIME);
  79. }
  80. @Test
  81. public void addError_any_number_of_call_change_by_1_per_call() {
  82. int calls = new Random().nextInt(SOME_RANDOM_MAX);
  83. for (int i = 0; i < calls; i++) {
  84. getUnderTest().addError(1);
  85. }
  86. assertThat(getUnderTest().getErrorCount()).isEqualTo(calls);
  87. assertThat(getUnderTest().getInProgressCount()).isEqualTo(-calls);
  88. assertThat(getUnderTest().getProcessingTime()).isEqualTo(calls);
  89. }
  90. @Test
  91. public void addSuccess_throws_IAE_if_time_is_less_than_0() {
  92. assertThatThrownBy(() -> getUnderTest().addSuccess(-1))
  93. .isInstanceOf(IllegalArgumentException.class)
  94. .hasMessage("Processing time can not be < 0");
  95. }
  96. @Test
  97. public void addSuccess_increases_Error_and_decreases_InProgress_by_1_without_check_on_InProgress() {
  98. getUnderTest().addSuccess(SOME_PROCESSING_TIME);
  99. assertThat(getUnderTest().getInProgressCount()).isEqualTo(-1);
  100. assertThat(getUnderTest().getErrorCount()).isZero();
  101. assertThat(getUnderTest().getSuccessCount()).isOne();
  102. assertThat(getUnderTest().getProcessingTime()).isEqualTo(SOME_PROCESSING_TIME);
  103. }
  104. @Test
  105. public void addSuccess_any_number_of_call_change_by_1_per_call() {
  106. int calls = new Random().nextInt(SOME_RANDOM_MAX);
  107. for (int i = 0; i < calls; i++) {
  108. getUnderTest().addSuccess(1);
  109. }
  110. assertThat(getUnderTest().getSuccessCount()).isEqualTo(calls);
  111. assertThat(getUnderTest().getInProgressCount()).isEqualTo(-calls);
  112. assertThat(getUnderTest().getProcessingTime()).isEqualTo(calls);
  113. }
  114. @Test
  115. public void workers_pause_is_loaded_from_db() {
  116. when(dbClient.internalPropertiesDao().selectByKey(any(), eq(InternalProperties.COMPUTE_ENGINE_PAUSE))).thenReturn(Optional.of("true"));
  117. assertThat(getUnderTest().areWorkersPaused()).isTrue();
  118. }
  119. @Test
  120. public void workers_pause_is_false_by_default() {
  121. assertThat(getUnderTest().areWorkersPaused()).isFalse();
  122. }
  123. }