-
Notifications
You must be signed in to change notification settings - Fork 359
Make child-process tests portable across operating systems #12355
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
AlexeyKuznetsov-DD
wants to merge
4
commits into
master
Choose a base branch
from
alexeyk/pure-java-test-commands
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+467
−5
Open
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
2562626
Make child-process test commands portable
AlexeyKuznetsov-DD 35f4ffd
Merge branch 'master' into alexeyk/pure-java-test-commands
AlexeyKuznetsov-DD 58b0060
Merge branch 'master' into alexeyk/pure-java-test-commands
AlexeyKuznetsov-DD 0d67340
Match emulated echo output charset
AlexeyKuznetsov-DD File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
5 changes: 3 additions & 2 deletions
5
internal-api/src/test/groovy/datadog/trace/util/ProcessSupervisorTest.groovy
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
114 changes: 114 additions & 0 deletions
114
utils/test-utils/src/main/java/datadog/trace/test/util/PortableCommand.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,114 @@ | ||
| package datadog.trace.test.util; | ||
|
|
||
| import datadog.environment.OperatingSystem; | ||
| import datadog.trace.api.internal.VisibleForTesting; | ||
| import java.nio.file.Files; | ||
| import java.nio.file.Path; | ||
| import java.nio.file.Paths; | ||
| import java.security.CodeSource; | ||
| import java.util.ArrayList; | ||
| import java.util.Arrays; | ||
| import java.util.List; | ||
|
|
||
| /** | ||
| * Builds portable command lines for simple test utilities. | ||
| * | ||
| * <ul> | ||
| * <li>POSIX uses native system commands. | ||
| * <li>Windows uses {@link PortableCommandRunner} in a child JVM to emulate their behavior. | ||
| * </ul> | ||
| */ | ||
| public final class PortableCommand { | ||
| private static final String MIN_HEAP = "-Xms8m"; | ||
| private static final String MAX_HEAP = "-Xmx16m"; | ||
|
|
||
| /** Windows has no usable native equivalent for any of these commands. */ | ||
| private static final boolean EMULATED = OperatingSystem.isWindows(); | ||
|
|
||
| private PortableCommand() {} | ||
|
|
||
| public static String[] echo(String value) { | ||
| return echo(value, EMULATED); | ||
| } | ||
|
|
||
| public static String[] cat() { | ||
| return cat(EMULATED); | ||
| } | ||
|
|
||
| public static String[] sleep(long durationSec) { | ||
| return sleep(durationSec, EMULATED); | ||
| } | ||
|
|
||
| public static String[] runForever() { | ||
| return runForever(EMULATED); | ||
| } | ||
|
|
||
| @VisibleForTesting | ||
| static String[] echo(String value, boolean emulated) { | ||
| return emulated ? emulate("echo", value) : new String[] {"echo", value}; | ||
|
AlexeyKuznetsov-DD marked this conversation as resolved.
|
||
| } | ||
|
|
||
| @VisibleForTesting | ||
| static String[] cat(boolean emulated) { | ||
| return emulated ? emulate("cat") : new String[] {"cat"}; | ||
| } | ||
|
|
||
| @VisibleForTesting | ||
| static String[] sleep(long durationSec, boolean emulated) { | ||
| if (durationSec < 0) { | ||
| throw new IllegalArgumentException("Sleep duration must not be negative: " + durationSec); | ||
| } | ||
| // The native sleep takes seconds; the emulated runner takes milliseconds. | ||
| return emulated | ||
| ? emulate("sleep", Long.toString(durationSec * 1000)) | ||
| : new String[] {"sleep", Long.toString(durationSec)}; | ||
| } | ||
|
|
||
| @VisibleForTesting | ||
| static String[] runForever(boolean emulated) { | ||
| return emulated | ||
| ? emulate("sleep", Long.toString(Long.MAX_VALUE)) | ||
| : new String[] {"tail", "-f", "/dev/null"}; | ||
| } | ||
|
|
||
| private static String[] emulate(String... arguments) { | ||
| Path executable = javaExecutable(); | ||
| Path classpath = classpathEntry(); | ||
|
|
||
| List<String> command = new ArrayList<>(); | ||
| command.add(executable.toString()); | ||
| command.add(MIN_HEAP); | ||
| command.add(MAX_HEAP); | ||
| command.add("-cp"); | ||
| command.add(classpath.toString()); | ||
| command.add(PortableCommandRunner.class.getName()); | ||
| command.addAll(Arrays.asList(arguments)); | ||
| return command.toArray(new String[0]); | ||
| } | ||
|
|
||
| private static Path javaExecutable() { | ||
| return javaExecutable(Paths.get(System.getProperty("java.home"))); | ||
| } | ||
|
|
||
| @VisibleForTesting | ||
| static Path javaExecutable(Path javaHome) { | ||
| Path bin = javaHome.resolve("bin"); | ||
| for (String name : new String[] {"java", "java.exe"}) { | ||
| Path candidate = bin.resolve(name); | ||
| if (Files.isRegularFile(candidate)) { | ||
| return candidate; | ||
| } | ||
| } | ||
| throw new IllegalStateException("Could not find a Java executable under " + bin); | ||
| } | ||
|
|
||
| private static Path classpathEntry() { | ||
| CodeSource source = PortableCommand.class.getProtectionDomain().getCodeSource(); | ||
| try { | ||
| return Paths.get(source.getLocation().toURI()); | ||
| } catch (Exception e) { | ||
| throw new IllegalStateException( | ||
| "Cannot determine the classpath of " + PortableCommand.class.getName(), e); | ||
| } | ||
| } | ||
| } | ||
55 changes: 55 additions & 0 deletions
55
utils/test-utils/src/main/java/datadog/trace/test/util/PortableCommandRunner.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,55 @@ | ||
| package datadog.trace.test.util; | ||
|
|
||
| import static java.nio.charset.Charset.defaultCharset; | ||
|
|
||
| import datadog.trace.api.internal.VisibleForTesting; | ||
| import de.thetaphi.forbiddenapis.SuppressForbidden; | ||
| import java.io.IOException; | ||
| import java.io.InputStream; | ||
| import java.io.OutputStream; | ||
|
|
||
| /** | ||
| * Emulates the utilities described by {@link PortableCommand} inside a JVM, for platforms that have | ||
| * no usable native equivalent. Spawned as a child process; not meant to be called directly. | ||
| */ | ||
| public final class PortableCommandRunner { | ||
| private PortableCommandRunner() {} | ||
|
|
||
| @SuppressForbidden | ||
| public static void main(String[] arguments) throws IOException, InterruptedException { | ||
| execute(arguments, System.in, System.out); | ||
| System.out.flush(); | ||
| } | ||
|
|
||
| @VisibleForTesting | ||
| static void execute(String[] arguments, InputStream input, OutputStream output) | ||
| throws IOException, InterruptedException { | ||
| if (arguments.length == 0) { | ||
| throw new IllegalArgumentException("Missing command"); | ||
| } | ||
| switch (arguments[0]) { | ||
| case "echo": | ||
| output.write((argument(arguments) + System.lineSeparator()).getBytes(defaultCharset())); | ||
| break; | ||
| case "cat": | ||
| byte[] buffer = new byte[8192]; | ||
| int read; | ||
| while ((read = input.read(buffer)) != -1) { | ||
| output.write(buffer, 0, read); | ||
| } | ||
| break; | ||
| case "sleep": | ||
| Thread.sleep(Long.parseLong(argument(arguments))); | ||
| break; | ||
| default: | ||
| throw new IllegalArgumentException("Unknown command: " + arguments[0]); | ||
| } | ||
| } | ||
|
|
||
| private static String argument(String[] arguments) { | ||
| if (arguments.length < 2) { | ||
| throw new IllegalArgumentException("Command '" + arguments[0] + "' requires an argument"); | ||
| } | ||
| return arguments[1]; | ||
| } | ||
| } |
79 changes: 79 additions & 0 deletions
79
utils/test-utils/src/test/java/datadog/trace/test/util/PortableCommandRunnerTest.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,79 @@ | ||
| package datadog.trace.test.util; | ||
|
|
||
| import static java.nio.charset.StandardCharsets.UTF_8; | ||
| import static org.junit.jupiter.api.Assertions.assertEquals; | ||
| import static org.junit.jupiter.api.Assertions.assertThrows; | ||
| import static org.junit.jupiter.api.Assertions.assertTrue; | ||
|
|
||
| import java.io.ByteArrayInputStream; | ||
| import java.io.ByteArrayOutputStream; | ||
| import java.io.InputStream; | ||
| import org.junit.jupiter.api.Test; | ||
|
|
||
| /** | ||
| * Unit tests for the command dispatch. In production the runner only ever executes as a child | ||
| * process, where its behavior is covered end to end by the emulated cases of {@link | ||
| * PortableCommandTest} — but a child JVM is opaque to both the coverage report and to assertions | ||
| * about why a command misbehaved, so the dispatch is driven directly here. | ||
| */ | ||
| class PortableCommandRunnerTest { | ||
| @Test | ||
| void echoes() throws Exception { | ||
| ByteArrayOutputStream output = new ByteArrayOutputStream(); | ||
|
|
||
| PortableCommandRunner.execute(new String[] {"echo", "value"}, emptyInput(), output); | ||
|
|
||
| assertEquals("value" + System.lineSeparator(), new String(output.toByteArray(), UTF_8)); | ||
| } | ||
|
|
||
| @Test | ||
| void copiesInput() throws Exception { | ||
| ByteArrayOutputStream output = new ByteArrayOutputStream(); | ||
| InputStream input = new ByteArrayInputStream("payload".getBytes(UTF_8)); | ||
|
|
||
| PortableCommandRunner.execute(new String[] {"cat"}, input, output); | ||
|
|
||
| assertEquals("payload", new String(output.toByteArray(), UTF_8)); | ||
| } | ||
|
|
||
| @Test | ||
| void sleeps() throws Exception { | ||
| long start = System.nanoTime(); | ||
|
|
||
| PortableCommandRunner.execute( | ||
| new String[] {"sleep", "50"}, emptyInput(), new ByteArrayOutputStream()); | ||
|
|
||
| assertTrue((System.nanoTime() - start) / 1_000_000 >= 40, "sleep returned immediately"); | ||
| } | ||
|
|
||
| @Test | ||
| void rejectsMissingCommand() { | ||
| assertThrows( | ||
| IllegalArgumentException.class, | ||
| () -> | ||
| PortableCommandRunner.execute( | ||
| new String[0], emptyInput(), new ByteArrayOutputStream())); | ||
| } | ||
|
|
||
| @Test | ||
| void rejectsUnknownCommand() { | ||
| assertThrows( | ||
| IllegalArgumentException.class, | ||
| () -> | ||
| PortableCommandRunner.execute( | ||
| new String[] {"rm"}, emptyInput(), new ByteArrayOutputStream())); | ||
| } | ||
|
|
||
| @Test | ||
| void rejectsMissingArgument() { | ||
| assertThrows( | ||
| IllegalArgumentException.class, | ||
| () -> | ||
| PortableCommandRunner.execute( | ||
| new String[] {"echo"}, emptyInput(), new ByteArrayOutputStream())); | ||
| } | ||
|
|
||
| private static InputStream emptyInput() { | ||
| return new ByteArrayInputStream(new byte[0]); | ||
| } | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.