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.

CEQueueStatusImplConcurrentTest.java 3.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. /*
  2. * SonarQube
  3. * Copyright (C) 2009-2019 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.ArrayList;
  22. import java.util.Collections;
  23. import java.util.List;
  24. import java.util.concurrent.ExecutorService;
  25. import java.util.concurrent.Executors;
  26. import java.util.concurrent.ThreadFactory;
  27. import java.util.concurrent.TimeUnit;
  28. import org.junit.After;
  29. import org.junit.Test;
  30. import org.sonar.api.utils.System2;
  31. import org.sonar.db.DbClient;
  32. import static org.assertj.core.api.Assertions.assertThat;
  33. import static org.mockito.Mockito.mock;
  34. public class CEQueueStatusImplConcurrentTest {
  35. private ExecutorService executorService = Executors.newFixedThreadPool(10, new ThreadFactory() {
  36. private int cnt = 0;
  37. @Override
  38. public Thread newThread(Runnable r) {
  39. return new Thread(r, CEQueueStatusImplConcurrentTest.class.getSimpleName() + cnt++);
  40. }
  41. });
  42. private CEQueueStatusImpl underTest = new CEQueueStatusImpl(mock(DbClient.class), mock(System2.class));
  43. @After
  44. public void tearDown() {
  45. executorService.shutdownNow();
  46. }
  47. @Test
  48. public void test_concurrent_modifications_in_any_order() throws InterruptedException {
  49. for (Runnable runnable : buildShuffleCallsToUnderTest()) {
  50. executorService.submit(runnable);
  51. }
  52. executorService.awaitTermination(1, TimeUnit.SECONDS);
  53. assertThat(underTest.getInProgressCount()).isEqualTo(1);
  54. assertThat(underTest.getErrorCount()).isEqualTo(17);
  55. assertThat(underTest.getSuccessCount()).isEqualTo(80);
  56. assertThat(underTest.getProcessingTime()).isEqualTo(177);
  57. }
  58. private List<Runnable> buildShuffleCallsToUnderTest() {
  59. List<Runnable> res = new ArrayList<>();
  60. for (int i = 0; i < 98; i++) {
  61. res.add(new AddInProgressRunnable());
  62. }
  63. for (int i = 0; i < 80; i++) {
  64. res.add(new AddSuccessRunnable());
  65. }
  66. for (int i = 0; i < 17; i++) {
  67. res.add(new AddErrorRunnable());
  68. }
  69. Collections.shuffle(res);
  70. return res;
  71. }
  72. private class AddInProgressRunnable implements Runnable {
  73. @Override
  74. public void run() {
  75. underTest.addInProgress();
  76. }
  77. }
  78. private class AddErrorRunnable implements Runnable {
  79. @Override
  80. public void run() {
  81. underTest.addError(1);
  82. }
  83. }
  84. private class AddSuccessRunnable implements Runnable {
  85. @Override
  86. public void run() {
  87. underTest.addSuccess(2);
  88. }
  89. }
  90. }