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.

ProgressReportTest.java 2.9KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  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.scanner.util;
  21. import java.util.Set;
  22. import java.util.regex.Pattern;
  23. import org.junit.Rule;
  24. import org.junit.Test;
  25. import org.junit.rules.DisableOnDebug;
  26. import org.junit.rules.TestRule;
  27. import org.junit.rules.Timeout;
  28. import org.sonar.api.utils.log.LogTester;
  29. import static org.assertj.core.api.Assertions.assertThat;
  30. public class ProgressReportTest {
  31. private static final String THREAD_NAME = "progress";
  32. @Rule
  33. public TestRule safeguardTimeout = new DisableOnDebug(Timeout.seconds(60));
  34. @Rule
  35. public LogTester logTester = new LogTester();
  36. private ProgressReport underTest = new ProgressReport(THREAD_NAME, 1);
  37. @Test
  38. public void stop_thread_on_stop() {
  39. underTest.start("start");
  40. assertThat(isThreadAlive(THREAD_NAME)).isTrue();
  41. underTest.stop("stop");
  42. assertThat(isThreadAlive(THREAD_NAME)).isFalse();
  43. }
  44. @Test
  45. public void do_not_block_app() {
  46. underTest.start("start");
  47. assertThat(isDaemon(THREAD_NAME)).isTrue();
  48. underTest.stop("stop");
  49. }
  50. @Test
  51. public void do_log() {
  52. underTest.start("start");
  53. underTest.message("Some message");
  54. boolean logged = false;
  55. while (!logged) {
  56. logged = logTester.logs().contains("Some message");
  57. }
  58. underTest.stop("stop");
  59. assertThat(logTester.logs().stream().anyMatch(s -> Pattern.matches("stop", s))).isTrue();
  60. }
  61. @Test
  62. public void do_log_with_time() {
  63. underTest.start("start");
  64. underTest.stopAndLogTotalTime("stop");
  65. assertThat(logTester.logs().stream().anyMatch(s -> Pattern.matches("stop \\(done\\) \\| time=[0-9]+ms", s))).isTrue();
  66. }
  67. private static boolean isDaemon(String name) {
  68. Thread t = getThread(name);
  69. return (t != null) && t.isDaemon();
  70. }
  71. private static boolean isThreadAlive(String name) {
  72. Thread t = getThread(name);
  73. return (t != null) && t.isAlive();
  74. }
  75. private static Thread getThread(String name) {
  76. Set<Thread> threads = Thread.getAllStackTraces().keySet();
  77. for (Thread t : threads) {
  78. if (t.getName().equals(name)) {
  79. return t;
  80. }
  81. }
  82. return null;
  83. }
  84. }