Search in sources :

Example 6 with SuppressForbidden

use of com.carrotsearch.randomizedtesting.annotations.SuppressForbidden in project randomizedtesting by randomizedtesting.

the class JUnit4 method forkProcess.

/**
   * Execute a slave process. Pump events to the given event bus.
   */
@SuppressForbidden("legitimate sysstreams.")
private Execute forkProcess(ForkedJvmInfo slaveInfo, EventBus eventBus, CommandlineJava commandline, InputStream eventStream, OutputStream sysout, OutputStream syserr, RandomAccessFile streamsBuffer) {
    try {
        final LocalSlaveStreamHandler streamHandler = new LocalSlaveStreamHandler(eventBus, testsClassLoader, System.err, eventStream, sysout, syserr, heartbeat, streamsBuffer);
        // Add certain properties to allow identification of the forked JVM from within
        // the subprocess. This can be used for policy files etc.
        final Path cwd = getWorkingDirectory(slaveInfo);
        Variable v = new Variable();
        v.setKey(CHILDVM_SYSPROP_CWD);
        v.setFile(cwd.toAbsolutePath().normalize().toFile());
        commandline.addSysproperty(v);
        v = new Variable();
        v.setKey(SysGlobals.CHILDVM_SYSPROP_JVM_ID);
        v.setValue(Integer.toString(slaveInfo.id));
        commandline.addSysproperty(v);
        v = new Variable();
        v.setKey(SysGlobals.CHILDVM_SYSPROP_JVM_COUNT);
        v.setValue(Integer.toString(slaveInfo.slaves));
        commandline.addSysproperty(v);
        // Emit command line before -stdin to avoid confusion.
        slaveInfo.slaveCommandLine = escapeAndJoin(commandline.getCommandline());
        log("Forked child JVM at '" + cwd.toAbsolutePath().normalize() + "', command (may need escape sequences for your shell):\n" + slaveInfo.slaveCommandLine, Project.MSG_VERBOSE);
        final Execute execute = new Execute();
        execute.setCommandline(commandline.getCommandline());
        execute.setVMLauncher(true);
        execute.setWorkingDirectory(cwd.toFile());
        execute.setStreamHandler(streamHandler);
        execute.setNewenvironment(newEnvironment);
        if (env.getVariables() != null)
            execute.setEnvironment(env.getVariables());
        log("Starting JVM J" + slaveInfo.id, Project.MSG_DEBUG);
        execute.execute();
        return execute;
    } catch (IOException e) {
        throw new BuildException("Could not start the child process. Run ant with -verbose to get" + " the execution details.", e);
    }
}
Also used : Path(java.nio.file.Path) Variable(org.apache.tools.ant.types.Environment.Variable) Execute(org.apache.tools.ant.taskdefs.Execute) IOException(java.io.IOException) BuildException(org.apache.tools.ant.BuildException) SuppressForbidden(com.carrotsearch.randomizedtesting.annotations.SuppressForbidden)

Example 7 with SuppressForbidden

use of com.carrotsearch.randomizedtesting.annotations.SuppressForbidden in project randomizedtesting by randomizedtesting.

the class SlaveMain method redirectStreams.

/**
   * Redirect standard streams so that the output can be passed to listeners.
   */
@SuppressForbidden("legitimate sysstreams.")
private static void redirectStreams(final Serializer serializer, final boolean flushFrequently) {
    final PrintStream origSysOut = System.out;
    final PrintStream origSysErr = System.err;
    // Set warnings stream to System.err.
    warnings = System.err;
    AccessController.doPrivileged(new PrivilegedAction<Void>() {

        @SuppressForbidden("legitimate PrintStream with default charset.")
        @Override
        public Void run() {
            System.setOut(new PrintStream(new BufferedOutputStream(new ChunkedStream() {

                @Override
                public void write(byte[] b, int off, int len) throws IOException {
                    if (multiplexStdStreams) {
                        origSysOut.write(b, off, len);
                    }
                    serializer.serialize(new AppendStdOutEvent(b, off, len));
                    if (flushFrequently)
                        serializer.flush();
                }
            })));
            System.setErr(new PrintStream(new BufferedOutputStream(new ChunkedStream() {

                @Override
                public void write(byte[] b, int off, int len) throws IOException {
                    if (multiplexStdStreams) {
                        origSysErr.write(b, off, len);
                    }
                    serializer.serialize(new AppendStdErrEvent(b, off, len));
                    if (flushFrequently)
                        serializer.flush();
                }
            })));
            return null;
        }
    });
}
Also used : AppendStdErrEvent(com.carrotsearch.ant.tasks.junit4.events.AppendStdErrEvent) PrintStream(java.io.PrintStream) AppendStdOutEvent(com.carrotsearch.ant.tasks.junit4.events.AppendStdOutEvent) SuppressForbidden(com.carrotsearch.randomizedtesting.annotations.SuppressForbidden) BufferedOutputStream(java.io.BufferedOutputStream) SuppressForbidden(com.carrotsearch.randomizedtesting.annotations.SuppressForbidden)

Aggregations

SuppressForbidden (com.carrotsearch.randomizedtesting.annotations.SuppressForbidden)7 IOException (java.io.IOException)4 BuildException (org.apache.tools.ant.BuildException)3 PrintStream (java.io.PrintStream)2 Path (java.nio.file.Path)2 AppendStdErrEvent (com.carrotsearch.ant.tasks.junit4.events.AppendStdErrEvent)1 AppendStdOutEvent (com.carrotsearch.ant.tasks.junit4.events.AppendStdOutEvent)1 JvmOutputEvent (com.carrotsearch.ant.tasks.junit4.events.aggregated.JvmOutputEvent)1 CharSink (com.google.common.io.CharSink)1 BufferedOutputStream (java.io.BufferedOutputStream)1 OutputStreamWriter (java.io.OutputStreamWriter)1 SimpleDateFormat (java.text.SimpleDateFormat)1 Date (java.util.Date)1 Execute (org.apache.tools.ant.taskdefs.Execute)1 Variable (org.apache.tools.ant.types.Environment.Variable)1 AssumptionViolatedException (org.junit.internal.AssumptionViolatedException)1 Description (org.junit.runner.Description)1