1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
|
/*
* SonarQube
* Copyright (C) 2009-2025 SonarSource SA
* mailto:info AT sonarsource DOT com
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 3 of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this program; if not, write to the Free Software Foundation,
* Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/
package org.sonar.scanner.util;
import java.util.Set;
import java.util.concurrent.TimeUnit;
import java.util.regex.Pattern;
import org.awaitility.Awaitility;
import org.awaitility.core.ConditionTimeoutException;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.DisableOnDebug;
import org.junit.rules.TestRule;
import org.junit.rules.Timeout;
import org.slf4j.event.Level;
import org.sonar.api.testfixtures.log.LogTester;
import static org.assertj.core.api.Assertions.assertThat;
public class ProgressReportTest {
private static final String THREAD_NAME = "progress";
@Rule
public TestRule safeguardTimeout = new DisableOnDebug(Timeout.seconds(60));
@Rule
public LogTester logTester = new LogTester();
private final ProgressReport underTest = new ProgressReport(THREAD_NAME, 1);
@Test
public void stop_thread_on_stop() {
underTest.start("start");
assertThat(isThreadAlive(THREAD_NAME)).isTrue();
underTest.stop("stop");
assertThat(isThreadAlive(THREAD_NAME)).isFalse();
}
@Test
public void do_not_block_app() {
underTest.start("start");
assertThat(isDaemon(THREAD_NAME)).isTrue();
underTest.stop("stop");
}
@Test
public void do_log() {
logTester.setLevel(Level.DEBUG);
underTest.start("start");
underTest.message("Some message");
boolean logged = false;
while (!logged) {
logged = waitForMessageStartingWith("Some message");
}
underTest.stop("stop");
assertThat(waitForMessageStartingWith("stop")).isTrue();
}
@Test
public void do_log_message_supplier() {
logTester.setLevel(Level.DEBUG);
underTest.start("start");
underTest.message(() -> "Some message " + System.currentTimeMillis());
boolean logged = false;
while (!logged) {
logged = waitForMessageStartingWith("Some message ");
}
underTest.stop("stop");
assertThat(waitForMessageStartingWith("stop")).isTrue();
}
private boolean waitForMessageStartingWith(String message) {
try {
Awaitility.await().atMost(5, TimeUnit.SECONDS).until(() -> logTester.logs().stream().anyMatch(s -> s.startsWith(message)));
} catch (ConditionTimeoutException e) {
return false;
}
return true;
}
@Test
public void do_log_with_time() {
underTest.start("start");
underTest.stopAndLogTotalTime("stop");
assertThat(logTester.logs().stream().anyMatch(s -> Pattern.matches("stop \\(done\\) \\| time=[0-9]+ms", s))).isTrue();
}
private static boolean isDaemon(String name) {
Thread t = getThread(name);
return (t != null) && t.isDaemon();
}
private static boolean isThreadAlive(String name) {
Thread t = getThread(name);
return (t != null) && t.isAlive();
}
private static Thread getThread(String name) {
Set<Thread> threads = Thread.getAllStackTraces().keySet();
for (Thread t : threads) {
if (t.getName().equals(name)) {
return t;
}
}
return null;
}
}
|