/
ProcessUtil.java
580 lines (537 loc) · 14.9 KB
/
ProcessUtil.java
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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
/*******************************************************************************
* Copyright (c) 2019 Bosch Software Innovations GmbH and others.
*
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v2.0
* and Eclipse Distribution License v1.0 which accompany this distribution.
*
* The Eclipse Public License is available at
* http://www.eclipse.org/legal/epl-v20.html
* and the Eclipse Distribution License is available at
* http://www.eclipse.org/org/documents/edl-v10.html.
*
* Contributors:
* Achim Kraus (Bosch Software Innovations GmbH) - initial implementation.
******************************************************************************/
package org.eclipse.californium.interoperability.test;
import static org.hamcrest.CoreMatchers.notNullValue;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertTrue;
import static org.junit.Assume.assumeNotNull;
import static org.junit.Assume.assumeThat;
import static org.junit.Assume.assumeTrue;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.Reader;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.concurrent.TimeUnit;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import org.eclipse.californium.elements.util.ClockUtil;
import org.eclipse.californium.elements.util.StringUtil;
import org.junit.AssumptionViolatedException;
/**
* Utility to start external tools.
*/
public class ProcessUtil {
public static final long TIMEOUT_MILLIS = 2000;
public static final long FOLLOW_UP_TIMEOUT_MILLIS = 100;
/**
* Process of external tool.
*/
private Process process;
/**
* Tag for logging.
*/
private String tag;
/**
* Realtime nanoseconds of last console output of external tool.
*
* @since 3.0
*/
private long lastConsoleUpdate = ClockUtil.nanoRealtime();
/**
* Console output of external tool. Considered to be rather small.
*/
private String console = "";
/**
* Result of external tool. RC and console output.
*/
private ProcessResult result;
/**
* Result of version request of external tool. RC and console output.
*
* @since 3.8
*/
protected ProcessResult versionResult;
/**
* Version of external tool.
*
* @since 3.8
*/
protected String version;
/**
* List with extra arguments.
*
* @since 3.8
*/
protected List<String> extraArgs = new ArrayList<>();
private boolean stopped;
private volatile boolean verbose;
/**
* Create instance.
*/
public ProcessUtil() {
setTag("");
}
/**
* Enable/disable verbose mode.
*
* @param verbose {@code true}, enable verbose mode, {@code false}, disable
* it.
* @since 3.0
*/
public void setDebug(boolean verbose) {
this.verbose = verbose;
}
/**
* Shutdown external tool.
*/
public void shutdown() throws InterruptedException {
stop();
waitResult(TIMEOUT_MILLIS);
setProcess(null);
tag = "";
setConsole("");
}
public void print(List<String> args) {
for (String arg : args) {
System.out.print(arg);
System.out.print(" ");
}
System.out.println();
}
/**
* Set logging tag for exit message.
*
* @param tag logging tag for exit message
* @since 3.0
*/
public void setTag(String tag) {
if (tag == null) {
tag = "";
} else if (!tag.isEmpty()) {
tag = ", " + tag;
}
this.tag = tag;
}
/**
* Clear current extra arguments.
*
* @since 3.8
*/
public void clearExtraArgs() {
extraArgs.clear();
}
/**
* Add extra argument.
*
* @param args extra arguments
* @since 3.8
*/
public void addExtraArgs(String... args) {
for (String arg : args) {
extraArgs.add(arg);
}
}
/**
* Start external tool.
*
* @param args list of arguments to start the external tool
* @throws IOException if start fails.
*/
public void execute(String... args) throws IOException {
execute(Arrays.asList(args));
}
/**
* Start external tool.
*
* @param args list of arguments to start the external tool
* @throws IOException if start fails.
*/
public void execute(List<String> args) throws IOException {
setConsole("");
setStopped(false);
ProcessBuilder builder = new ProcessBuilder(args);
builder.redirectErrorStream(true);
Process process = builder.start();
setProcess(process);
startReadingOutput(process, tag);
}
/**
* Send provided message to the external tools input.
*
* @param message message to be written to the external tools input.
* @throws IOException if an error occurred when writing
*/
public void send(String message) throws IOException {
Process process = getProcess();
if (process != null) {
System.out.println("< " + message);
process.getOutputStream().write(message.getBytes());
process.getOutputStream().flush();
} else {
throw new IllegalStateException("process not running");
}
}
/**
* Send provided message to the external tools input with line ending added.
*
* @param message message to be written with added line ending to the
* external tools input.
* @throws IOException if an error occurred when writing
*/
public void sendln(String message) throws IOException {
send(message + StringUtil.lineSeparator());
}
/**
* Wait for regular expression on console output of external tool.
*
* @param regex regular expression
* @param timeoutMillis timeout to wait in milliseconds
* @return {@code true}, if regular expression could be found in time,
* {@code false}, otherwise.
* @throws InterruptedException if waiting was interrupted
*/
public synchronized boolean waitConsole(String regex, long timeoutMillis) throws InterruptedException {
boolean found;
long lastConsoleUpdate = this.lastConsoleUpdate;
long end = System.nanoTime() + TimeUnit.MILLISECONDS.toNanos(timeoutMillis);
Pattern pattern = Pattern.compile(regex);
while (!(found = pattern.matcher(console).find())) {
if (lastConsoleUpdate != this.lastConsoleUpdate) {
lastConsoleUpdate = this.lastConsoleUpdate;
end = System.nanoTime() + TimeUnit.MILLISECONDS.toNanos(timeoutMillis);
}
long left = TimeUnit.NANOSECONDS.toMillis(end - System.nanoTime());
if (left > 0) {
wait(left);
} else {
break;
}
}
return found;
}
/**
* Assert regular expression on console output of external tool.
*
* Waits at most 2000 milliseconds until the external tool has finished.
*
* @param regex regular expression
* @throws InterruptedException if waiting for result was interrupted
*/
public void assertConsole(String regex) throws InterruptedException {
ProcessResult result = waitResult(TIMEOUT_MILLIS);
assertNotNull("process not finished!", result);
assertTrue("\"" + regex + "\" missing in console output!", result.contains(regex));
}
/**
* Get external tool's version.
*
* @param timeMillis timeout in milliseconds
* @return result of version command. {@code null}, if not available.
* @since 3.8
*/
public ProcessResult getToolVersion(long timeMillis) {
return null;
}
/**
* Get version of external tool.
*
* @return version of external tool, or {@code null}, if not available.
* @see #getToolVersion(long)
* @since 3.8 (moved from LibCoapProcessUtil)
*/
public String getVersion() {
if (versionResult == null) {
getToolVersion(TIMEOUT_MILLIS);
}
return version;
}
/**
* Compare version with version of external tool.
*
* @param version version to compare
* @return {@code <0}, if the provided version is newer, {@code 0}, if the
* versions are matching, {@code >0}, if provided version is older.
* @see #getToolVersion(long)
* @since 3.8 (moved from LibCoapProcessUtil)
*/
public int compareVersion(String version) {
if (versionResult == null) {
getToolVersion(TIMEOUT_MILLIS);
}
assumeThat("version not available!", version, notNullValue());
return compareVersion(this.version, version);
}
/**
* Assume a minimum version of the external tool.
*
* @param version assumed minimum version
* @throws AssumptionViolatedException if the external tool's version is
* older than the provided one.
* @see #getToolVersion(long)
* @since 3.8 (moved from LibCoapProcessUtil)
*/
public void assumeMinVersion(String version) {
if (versionResult == null) {
getToolVersion(TIMEOUT_MILLIS);
}
assumeNotNull(this.version);
assumeTrue(this.version + " > " + version, compareVersion(this.version, version) >= 0);
}
/**
* Compare versions.
*
* Split the provided arguments at the {@code '.'}s.
*
* @param version1 version 1
* @param version2 version 2
* @return {@code <0}, if version 1 is older than version 2, {@code 0}, if the
* versions are matching, {@code >0}, if version 1 is newer than
* version2.
* @since 3.8 (moved from LibCoapProcessUtil)
*/
public static int compareVersion(String version1, String version2) {
String[] versionPath1 = version1.split("\\.");
String[] versionPath2 = version2.split("\\.");
int length = versionPath1.length;
if (versionPath2.length < length) {
length = versionPath2.length;
}
for (int index = 0; index < length; ++index) {
int cmp = versionPath1[index].compareTo(versionPath2[index]);
if (cmp != 0) {
return cmp;
}
}
return versionPath1.length - versionPath2.length;
}
/**
* Get console quiet period in milliseconds.
*
* @return milliseconds since the last console update.
* @since 3.0
*/
public long getConsoleQuietMillis() {
long last;
synchronized (this) {
last = lastConsoleUpdate;
}
return TimeUnit.NANOSECONDS.toMillis(ClockUtil.nanoRealtime() - last);
}
/**
* Wait for external tool to finish.
*
* Clears {@link #console} as well.
*
* @param timeoutMillis timeout to wait in milliseconds
* @return result of external tool, or {@code null}, if external tool hasn't
* finished.
* @throws InterruptedException if waiting for finish was interrupted
*/
public synchronized ProcessResult waitResult(long timeoutMillis) throws InterruptedException {
Process process = getProcess();
if (process != null) {
long lastConsoleUpdate = this.lastConsoleUpdate;
long end = System.nanoTime() + TimeUnit.MILLISECONDS.toNanos(timeoutMillis);
while (result == null) {
if (lastConsoleUpdate != this.lastConsoleUpdate) {
lastConsoleUpdate = this.lastConsoleUpdate;
end = System.nanoTime() + TimeUnit.MILLISECONDS.toNanos(timeoutMillis);
}
long left = TimeUnit.NANOSECONDS.toMillis(end - System.nanoTime());
if (left > 0) {
wait(left);
} else {
break;
}
}
if (result != null) {
setProcess(null);
}
}
return result;
}
/**
* Stop the process.
*
* @return {@code true}, if stop is forced, {@code false}, otherwise.
* @since 3.0 (add return value)
*/
public boolean stop() {
clearExtraArgs();
Process process = getProcess();
if (process != null) {
try {
process.exitValue();
System.out.println("process stopped" + tag);
} catch (IllegalThreadStateException ex) {
setStopped(true);
process.destroy();
System.out.println("process forced stopped" + tag);
return true;
}
}
return false;
}
private synchronized void setStopped(boolean stopped) {
this.stopped = stopped;
}
private synchronized boolean isStopped() {
return stopped;
}
private synchronized void setProcess(Process process) {
this.process = process;
}
private synchronized Process getProcess() {
return process;
}
/**
* Start reading output.
*
* @param process process to read output
* @param tag logging tag for exit message
*/
private void startReadingOutput(final Process process, final String tag) {
setResult(null);
Thread thread = new Thread(new Runnable() {
@Override
public void run() {
try {
boolean stopped = false;
long time = System.nanoTime();
Reader reader = null;
StringBuilder console = new StringBuilder();
try {
reader = new InputStreamReader(process.getInputStream());
char[] buffer = new char[2048];
int read;
while ((read = reader.read(buffer)) >= 0) {
stopped = isStopped();
if (stopped) {
break;
}
String out = new String(buffer, 0, read);
if (verbose) {
System.out.println("> (" + out.length() + " bytes)");
}
System.out.print(out);
System.out.flush();
console.append(out);
setConsole(console.toString());
}
} catch (IOException e) {
stopped = isStopped();
if (!stopped) {
e.printStackTrace();
}
} finally {
if (reader != null) {
try {
reader.close();
} catch (IOException e) {
}
}
}
int rc = process.waitFor();
time = TimeUnit.NANOSECONDS.toMillis(System.nanoTime() - time);
StringBuilder message = new StringBuilder("> exit: ");
message.append(process.exitValue());
message.append(" (").append(time).append("ms");
if (stopped) {
message.append(", stopped");
}
message.append(tag).append(").");
System.out.println(message);
setResult(new ProcessResult(rc, console.toString()));
} catch (InterruptedException e) {
e.printStackTrace();
}
}
});
thread.start();
}
/**
* Set last accumulated console output of external tool.
*
* @param console accumulated console output of external tool
*/
private synchronized void setConsole(String console) {
this.console = console;
this.lastConsoleUpdate = ClockUtil.nanoRealtime();
notifyAll();
}
/**
* Set result of external tools
*
* @param result result of external tools
*/
private synchronized void setResult(ProcessResult result) {
this.result = result;
notifyAll();
}
/**
* Process result.
*
* Result of external tool.
*/
public static class ProcessResult {
/**
* Return code on finish of external tool.
*/
public final int rc;
/**
* Console output of external tool.
*/
public final String console;
/**
* Create new result
*
* @param rc return code of external tool
* @param console accumulated console output of external tool
*/
private ProcessResult(int rc, String console) {
this.rc = rc;
this.console = console;
}
/**
* Check, if console output contains a match of the regular expression.
*
* @param regex regular expression
* @return {@code true}, if match is contained, {@code false},
* otherwise.
*/
public boolean contains(String regex) {
return match(regex) != null;
}
/**
* Check, if console output contains a match of the regular expression.
*
* @param regex regular expression
* @return matcher to read groups, {@code null}, if the expression is
* not contained.
*/
public Matcher match(String regex) {
Pattern pattern = Pattern.compile(regex);
Matcher matcher = pattern.matcher(console);
if (matcher.find()) {
return matcher;
} else {
return null;
}
}
}
}