Skip to content

Commit 9d48dad

Browse files
committed
test constructor with timeout
1 parent f719b58 commit 9d48dad

File tree

2 files changed

+15
-4
lines changed

2 files changed

+15
-4
lines changed

opengrok-indexer/src/main/java/org/opengrok/indexer/util/Executor.java

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,7 @@ public Executor(List<String> cmdList) {
9292
* @param cmdList A list containing the command to execute
9393
* @param workingDirectory The directory the process should have as the working directory
9494
*/
95-
public Executor(List<String> cmdList, File workingDirectory) {
95+
public Executor(List<String> cmdList, @Nullable File workingDirectory) {
9696
this(cmdList, workingDirectory, new HashMap<>());
9797
}
9898

@@ -104,7 +104,7 @@ public Executor(List<String> cmdList, File workingDirectory) {
104104
* it will be terminated. If the value is 0, no timer
105105
* will be set up.
106106
*/
107-
public Executor(List<String> cmdList, File workingDirectory, int timeout) {
107+
public Executor(List<String> cmdList, @Nullable File workingDirectory, int timeout) {
108108
this(cmdList, workingDirectory, new HashMap<>());
109109

110110
this.timeout = timeout * 1000;
@@ -116,7 +116,7 @@ public Executor(List<String> cmdList, File workingDirectory, int timeout) {
116116
* @param cmdList A list containing the command to execute
117117
* @param workingDirectory The directory the process should have as the working directory
118118
*/
119-
public Executor(List<String> cmdList, File workingDirectory, Map<String, String> environ) {
119+
public Executor(List<String> cmdList, @Nullable File workingDirectory, Map<String, String> environ) {
120120
this.cmdList = cmdList;
121121
this.workingDirectory = workingDirectory;
122122
this.environ = environ;
@@ -144,6 +144,10 @@ private void setDefaultTimeout() {
144144
this.timeout = timeoutSec * 1000;
145145
}
146146

147+
int getTimeout() {
148+
return timeout / 1000;
149+
}
150+
147151
/**
148152
* Execute the command and collect the output. All exceptions will be
149153
* logged.
@@ -239,7 +243,7 @@ public int exec(final boolean reportExceptions, StreamHandler handler) {
239243
@Override public void run() {
240244
LOGGER.log(Level.WARNING,
241245
String.format("Terminating process of command [%s] in directory '%s' " +
242-
"due to timeout %d seconds", cmd_str, dir_str, timeout / 1000));
246+
"due to timeout %d seconds", cmd_str, dir_str, getTimeout()));
243247
proc.destroy();
244248
}
245249
}, timeout);

opengrok-indexer/src/test/java/org/opengrok/indexer/util/ExecutorTest.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,13 @@
4848
*/
4949
class ExecutorTest {
5050

51+
@Test
52+
void testConstructorWithTimeout() {
53+
int timeout = 42;
54+
Executor executor = new Executor(List.of("foo"), null, timeout);
55+
assertEquals(timeout, executor.getTimeout());
56+
}
57+
5158
@Test
5259
void testString() {
5360
List<String> cmdList = new ArrayList<>();

0 commit comments

Comments
 (0)