Search in sources :

Example 1 with Serializer

use of com.carrotsearch.ant.tasks.junit4.events.Serializer in project randomizedtesting by randomizedtesting.

the class SlaveMain method main.

/**
 * Console entry point.
 */
@SuppressWarnings("resource")
public static void main(String[] allArgs) {
    int exitStatus = 0;
    Serializer serializer = null;
    try {
        final ArrayDeque<String> args = new ArrayDeque<String>(Arrays.asList(allArgs));
        // Options.
        boolean debugStream = false;
        boolean flushFrequently = false;
        File eventsFile = null;
        boolean suitesOnStdin = false;
        List<String> testClasses = new ArrayList<>();
        String runListeners = null;
        while (!args.isEmpty()) {
            String option = args.pop();
            if (option.equals(OPTION_FREQUENT_FLUSH)) {
                flushFrequently = true;
            } else if (option.equals(OPTION_STDIN)) {
                suitesOnStdin = true;
            } else if (option.equals(OPTION_SYSOUTS)) {
                multiplexStdStreams = true;
            } else if (option.equals(OPTION_EVENTSFILE)) {
                eventsFile = new File(args.pop());
                if (eventsFile.isFile() && eventsFile.length() > 0) {
                    RandomAccessFile raf = new RandomAccessFile(eventsFile, "rw");
                    raf.setLength(0);
                    raf.close();
                }
            } else if (option.equals(OPTION_RUN_LISTENERS)) {
                runListeners = args.pop();
            } else if (option.startsWith(OPTION_DEBUGSTREAM)) {
                debugStream = true;
            } else if (option.startsWith("@")) {
                // Append arguments file, one line per option.
                args.addAll(Arrays.asList(readArgsFile(option.substring(1))));
            } else {
                // The default expectation is a test class.
                testClasses.add(option);
            }
        }
        // Set up events channel and events serializer.
        if (eventsFile == null) {
            throw new IOException("You must specify communication channel for events.");
        }
        // Delay the forked JVM a bit (for tests).
        if (System.getProperty(SYSPROP_FORKEDJVM_DELAY_MS) != null) {
            Thread.sleep(Integer.parseInt(System.getProperty(SYSPROP_FORKEDJVM_DELAY_MS)));
        }
        // Send bootstrap package.
        serializer = new Serializer(new EventsOutputStream(eventsFile)).serialize(new BootstrapEvent()).flush();
        // Redirect original streams and start running tests.
        redirectStreams(serializer, flushFrequently);
        final SlaveMain main = new SlaveMain(serializer);
        main.flushFrequently = flushFrequently;
        main.debugMessagesFile = debugStream ? new File(eventsFile.getAbsolutePath() + ".debug") : null;
        main.runListeners = runListeners;
        final Iterator<String> stdInput;
        if (suitesOnStdin) {
            stdInput = new StdInLineIterator(main.serializer);
        } else {
            stdInput = Collections.<String>emptyList().iterator();
        }
        main.execute(Iterators.concat(testClasses.iterator(), stdInput));
        // For unhandled exceptions tests.
        if (System.getProperty(SYSPROP_FIRERUNNERFAILURE) != null) {
            throw new Exception(System.getProperty(SYSPROP_FIRERUNNERFAILURE));
        }
    } catch (Throwable t) {
        lastResortMemory = null;
        tryWaitingForGC();
        if (t.getClass() == oomClass) {
            exitStatus = ERR_OOM;
            warn("JVM out of memory.", t);
        } else {
            exitStatus = ERR_EXCEPTION;
            warn("Exception at main loop level.", t);
        }
    }
    try {
        if (serializer != null) {
            try {
                serializer.close();
            } catch (Throwable t) {
                warn("Exception closing serializer.", t);
            }
        }
    } finally {
        JvmExit.halt(exitStatus);
    }
}
Also used : BootstrapEvent(com.carrotsearch.ant.tasks.junit4.events.BootstrapEvent) ArrayList(java.util.ArrayList) IOException(java.io.IOException) ArrayDeque(java.util.ArrayDeque) IOException(java.io.IOException) NoTestsRemainException(org.junit.runner.manipulation.NoTestsRemainException) RandomAccessFile(java.io.RandomAccessFile) RandomAccessFile(java.io.RandomAccessFile) File(java.io.File) Serializer(com.carrotsearch.ant.tasks.junit4.events.Serializer)

Example 2 with Serializer

use of com.carrotsearch.ant.tasks.junit4.events.Serializer in project randomizedtesting by randomizedtesting.

the class TestJsonByteArrayRoundtrip method check.

private void check(byte[] bytes) throws Exception {
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    Serializer s = new Serializer(baos);
    s.serialize(new AppendStdErrEvent(bytes, 0, bytes.length));
    s.flush();
    s.close();
    Deserializer deserializer = new Deserializer(new ByteArrayInputStream(baos.toByteArray()), Thread.currentThread().getContextClassLoader());
    IEvent deserialize = deserializer.deserialize();
    Assert.assertTrue(deserialize instanceof AppendStdErrEvent);
    AppendStdErrEvent e = ((AppendStdErrEvent) deserialize);
    baos.reset();
    e.copyTo(baos);
    Assert.assertTrue("Exp: " + Arrays.toString(bytes) + "\n" + "was: " + Arrays.toString(baos.toByteArray()), Arrays.equals(bytes, baos.toByteArray()));
}
Also used : AppendStdErrEvent(com.carrotsearch.ant.tasks.junit4.events.AppendStdErrEvent) IEvent(com.carrotsearch.ant.tasks.junit4.events.IEvent) ByteArrayInputStream(java.io.ByteArrayInputStream) Deserializer(com.carrotsearch.ant.tasks.junit4.events.Deserializer) ByteArrayOutputStream(java.io.ByteArrayOutputStream) Serializer(com.carrotsearch.ant.tasks.junit4.events.Serializer)

Example 3 with Serializer

use of com.carrotsearch.ant.tasks.junit4.events.Serializer 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

AppendStdErrEvent (com.carrotsearch.ant.tasks.junit4.events.AppendStdErrEvent)2 Serializer (com.carrotsearch.ant.tasks.junit4.events.Serializer)2 AppendStdOutEvent (com.carrotsearch.ant.tasks.junit4.events.AppendStdOutEvent)1 BootstrapEvent (com.carrotsearch.ant.tasks.junit4.events.BootstrapEvent)1 Deserializer (com.carrotsearch.ant.tasks.junit4.events.Deserializer)1 IEvent (com.carrotsearch.ant.tasks.junit4.events.IEvent)1 SuppressForbidden (com.carrotsearch.randomizedtesting.annotations.SuppressForbidden)1 BufferedOutputStream (java.io.BufferedOutputStream)1 ByteArrayInputStream (java.io.ByteArrayInputStream)1 ByteArrayOutputStream (java.io.ByteArrayOutputStream)1 File (java.io.File)1 IOException (java.io.IOException)1 PrintStream (java.io.PrintStream)1 RandomAccessFile (java.io.RandomAccessFile)1 ArrayDeque (java.util.ArrayDeque)1 ArrayList (java.util.ArrayList)1 NoTestsRemainException (org.junit.runner.manipulation.NoTestsRemainException)1