use of java.util.ArrayDeque 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<>();
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.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.");
}
// 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;
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);
}
}
use of java.util.ArrayDeque in project randomizedtesting by randomizedtesting.
the class JUnit4 method execute.
@Override
public void execute() throws BuildException {
validateJUnit4();
validateArguments();
// Initialize random if not already provided.
if (random == null) {
this.random = MoreObjects.firstNonNull(Strings.emptyToNull(getProject().getProperty(SYSPROP_RANDOM_SEED())), SeedUtils.formatSeed(new Random().nextLong()));
}
masterSeed();
// Say hello and continue.
log("<JUnit4> says " + RandomPicks.randomFrom(new Random(masterSeed()), WELCOME_MESSAGES) + " Master seed: " + getSeed(), Project.MSG_INFO);
// Pass the random seed property.
createJvmarg().setValue("-D" + SYSPROP_PREFIX() + "=" + CURRENT_PREFIX());
createJvmarg().setValue("-D" + SYSPROP_RANDOM_SEED() + "=" + random);
// Resolve paths first.
this.classpath = resolveFiles(classpath);
this.bootclasspath = resolveFiles(bootclasspath);
getCommandline().createClasspath(getProject()).add(classpath);
getCommandline().createBootclasspath(getProject()).add(bootclasspath);
// Setup a class loader over test classes. This will be used for loading annotations
// and referenced classes. This is kind of ugly, but mirroring annotation content will
// be even worse and Description carries these.
// TODO: [GH-211] we should NOT be using any actual classes, annotations, etc.
// from client code. Everything should be a mirror.
testsClassLoader = new AntClassLoader(this.getClass().getClassLoader(), getProject(), getCommandline().getClasspath(), true);
// Pass method filter if any.
String testMethodFilter = Strings.emptyToNull(getProject().getProperty(SYSPROP_TESTMETHOD()));
if (testMethodFilter != null) {
Environment.Variable v = new Environment.Variable();
v.setKey(SYSPROP_TESTMETHOD());
v.setValue(testMethodFilter);
getCommandline().addSysproperty(v);
}
// Process test classes and resources.
long start = System.currentTimeMillis();
final TestsCollection testCollection = processTestResources();
final EventBus aggregatedBus = new EventBus("aggregated");
final TestsSummaryEventListener summaryListener = new TestsSummaryEventListener();
aggregatedBus.register(summaryListener);
for (Object o : listeners) {
if (o instanceof ProjectComponent) {
((ProjectComponent) o).setProject(getProject());
}
if (o instanceof AggregatedEventListener) {
((AggregatedEventListener) o).setOuter(this);
}
aggregatedBus.register(o);
}
if (testCollection.testClasses.isEmpty()) {
aggregatedBus.post(new AggregatedQuitEvent());
} else {
start = System.currentTimeMillis();
// reports) will have a problem with duplicate suite names, for example.
if (uniqueSuiteNames) {
testCollection.onlyUniqueSuiteNames();
}
final int jvmCount = determineForkedJvmCount(testCollection);
final List<ForkedJvmInfo> slaveInfos = new ArrayList<>();
for (int jvmid = 0; jvmid < jvmCount; jvmid++) {
final ForkedJvmInfo slaveInfo = new ForkedJvmInfo(jvmid, jvmCount);
slaveInfos.add(slaveInfo);
}
if (jvmCount > 1 && uniqueSuiteNames && testCollection.hasReplicatedSuites()) {
throw new BuildException(String.format(Locale.ROOT, "There are test suites that request JVM replication and the number of forked JVMs %d is larger than 1. Run on a single JVM.", jvmCount));
}
// Prepare a pool of suites dynamically dispatched to slaves as they become idle.
final Deque<String> stealingQueue = new ArrayDeque<String>(loadBalanceSuites(slaveInfos, testCollection, balancers));
aggregatedBus.register(new Object() {
@Subscribe
public void onSlaveIdle(SlaveIdle slave) {
if (stealingQueue.isEmpty()) {
slave.finished();
} else {
String suiteName = stealingQueue.pop();
slave.newSuite(suiteName);
}
}
});
// Check for filtering expressions.
Vector<Variable> vv = getCommandline().getSystemProperties().getVariablesVector();
for (Variable v : vv) {
if (SysGlobals.SYSPROP_TESTFILTER().equals(v.getKey())) {
try {
Node root = new FilterExpressionParser().parse(v.getValue());
log("Parsed test filtering expression: " + root.toExpression(), Project.MSG_INFO);
} catch (Exception e) {
log("Could not parse filtering expression: " + v.getValue(), e, Project.MSG_WARN);
}
}
}
// Create callables for the executor.
final List<Callable<Void>> slaves = new ArrayList<>();
for (int slave = 0; slave < jvmCount; slave++) {
final ForkedJvmInfo slaveInfo = slaveInfos.get(slave);
slaves.add(new Callable<Void>() {
@Override
public Void call() throws Exception {
executeSlave(slaveInfo, aggregatedBus);
return null;
}
});
}
ExecutorService executor = Executors.newCachedThreadPool();
aggregatedBus.post(new AggregatedStartEvent(slaves.size(), // TODO: this doesn't account for replicated suites.
testCollection.testClasses.size()));
try {
List<Future<Void>> all = executor.invokeAll(slaves);
executor.shutdown();
for (int i = 0; i < slaves.size(); i++) {
Future<Void> f = all.get(i);
try {
f.get();
} catch (ExecutionException e) {
slaveInfos.get(i).executionError = e.getCause();
}
}
} catch (InterruptedException e) {
log("Master interrupted? Weird.", Project.MSG_ERR);
}
aggregatedBus.post(new AggregatedQuitEvent());
for (ForkedJvmInfo si : slaveInfos) {
if (si.start > 0 && si.end > 0) {
log(String.format(Locale.ROOT, "JVM J%d: %8.2f .. %8.2f = %8.2fs", si.id, (si.start - start) / 1000.0f, (si.end - start) / 1000.0f, (si.getExecutionTime() / 1000.0f)), Project.MSG_INFO);
}
}
log("Execution time total: " + Duration.toHumanDuration((System.currentTimeMillis() - start)));
ForkedJvmInfo slaveInError = null;
for (ForkedJvmInfo i : slaveInfos) {
if (i.executionError != null) {
log("ERROR: JVM J" + i.id + " ended with an exception, command line: " + i.getCommandLine());
log("ERROR: JVM J" + i.id + " ended with an exception: " + Throwables.getStackTraceAsString(i.executionError), Project.MSG_ERR);
if (slaveInError == null) {
slaveInError = i;
}
}
}
if (slaveInError != null) {
throw new BuildException("At least one slave process threw an exception, first: " + slaveInError.executionError.getMessage(), slaveInError.executionError);
}
}
final TestsSummary testsSummary = summaryListener.getResult();
if (printSummary) {
log("Tests summary: " + testsSummary, Project.MSG_INFO);
}
if (!testsSummary.isSuccessful()) {
if (!Strings.isNullOrEmpty(failureProperty)) {
getProject().setNewProperty(failureProperty, "true");
}
if (haltOnFailure) {
throw new BuildException(String.format(Locale.ROOT, "There were test failures: %s [seed: %s]", testsSummary, getSeed()));
}
}
if (!leaveTemporary) {
for (Path f : temporaryFiles) {
try {
if (f != null) {
try {
Files.delete(f);
} catch (DirectoryNotEmptyException e) {
throw new DirectoryNotEmptyException("Remaining files: " + listFiles(f));
}
}
} catch (IOException e) {
log("Could not remove temporary path: " + f.toAbsolutePath() + " (" + e + ")", e, Project.MSG_WARN);
}
}
}
if (statsPropertyPrefix != null) {
Project p = getProject();
p.setNewProperty(statsPropertyPrefix + ".tests", Integer.toString(testsSummary.tests));
p.setNewProperty(statsPropertyPrefix + ".errors", Integer.toString(testsSummary.errors));
p.setNewProperty(statsPropertyPrefix + ".failures", Integer.toString(testsSummary.failures));
p.setNewProperty(statsPropertyPrefix + ".ignores", Integer.toString(testsSummary.ignores));
p.setNewProperty(statsPropertyPrefix + ".suites", Integer.toString(testsSummary.suites));
p.setNewProperty(statsPropertyPrefix + ".assumptions", Integer.toString(testsSummary.assumptions));
p.setNewProperty(statsPropertyPrefix + ".suiteErrors", Integer.toString(testsSummary.suiteErrors));
p.setNewProperty(statsPropertyPrefix + ".nonIgnored", Integer.toString(testsSummary.getNonIgnoredTestsCount()));
p.setNewProperty(statsPropertyPrefix + ".successful", Boolean.toString(testsSummary.isSuccessful()));
}
int executedTests = testsSummary.getNonIgnoredTestsCount();
if (executedTests == 0) {
String message = "There were no executed tests: " + testsSummary;
switch(ifNoTests) {
case FAIL:
throw new BuildException(message);
case WARN:
log(message, Project.MSG_WARN);
break;
case IGNORE:
break;
default:
throw new RuntimeException("Unreachable case clause: " + ifNoTests);
}
}
}
use of java.util.ArrayDeque in project jetty.project by eclipse.
the class PrefaceTest method testClientPrefaceReplySentAfterServerPreface.
@Test
public void testClientPrefaceReplySentAfterServerPreface() throws Exception {
start(new ServerSessionListener.Adapter() {
@Override
public Map<Integer, Integer> onPreface(Session session) {
Map<Integer, Integer> settings = new HashMap<>();
settings.put(SettingsFrame.MAX_CONCURRENT_STREAMS, 128);
return settings;
}
@Override
public void onPing(Session session, PingFrame frame) {
session.close(ErrorCode.NO_ERROR.code, null, Callback.NOOP);
}
});
ByteBufferPool byteBufferPool = client.getByteBufferPool();
try (SocketChannel socket = SocketChannel.open()) {
socket.connect(new InetSocketAddress("localhost", connector.getLocalPort()));
Generator generator = new Generator(byteBufferPool);
ByteBufferPool.Lease lease = new ByteBufferPool.Lease(byteBufferPool);
generator.control(lease, new PrefaceFrame());
Map<Integer, Integer> clientSettings = new HashMap<>();
clientSettings.put(SettingsFrame.ENABLE_PUSH, 0);
generator.control(lease, new SettingsFrame(clientSettings, false));
// The PING frame just to make sure the client stops reading.
generator.control(lease, new PingFrame(true));
List<ByteBuffer> buffers = lease.getByteBuffers();
socket.write(buffers.toArray(new ByteBuffer[buffers.size()]));
Queue<SettingsFrame> settings = new ArrayDeque<>();
Parser parser = new Parser(byteBufferPool, new Parser.Listener.Adapter() {
@Override
public void onSettings(SettingsFrame frame) {
settings.offer(frame);
}
}, 4096, 8192);
ByteBuffer buffer = byteBufferPool.acquire(1024, true);
while (true) {
BufferUtil.clearToFill(buffer);
int read = socket.read(buffer);
BufferUtil.flipToFlush(buffer, 0);
if (read < 0)
break;
parser.parse(buffer);
}
Assert.assertEquals(2, settings.size());
SettingsFrame frame1 = settings.poll();
Assert.assertFalse(frame1.isReply());
SettingsFrame frame2 = settings.poll();
Assert.assertTrue(frame2.isReply());
}
}
use of java.util.ArrayDeque in project elasticsearch by elastic.
the class ZenDiscoveryUnitTests method testPendingCSQueueIsClearedWhenClusterStatePublished.
public void testPendingCSQueueIsClearedWhenClusterStatePublished() throws Exception {
ThreadPool threadPool = new TestThreadPool(getClass().getName());
// randomly make minimum_master_nodes a value higher than we have nodes for, so it will force failure
int minMasterNodes = randomBoolean() ? 3 : 1;
Settings settings = Settings.builder().put(DISCOVERY_ZEN_MINIMUM_MASTER_NODES_SETTING.getKey(), Integer.toString(minMasterNodes)).build();
ArrayDeque<Closeable> toClose = new ArrayDeque<>();
try {
final MockTransportService masterTransport = MockTransportService.createNewService(settings, Version.CURRENT, threadPool, null);
masterTransport.start();
DiscoveryNode masterNode = masterTransport.getLocalNode();
toClose.addFirst(masterTransport);
ClusterState state = ClusterStateCreationUtils.state(masterNode, null, masterNode);
// build the zen discovery and cluster service
ClusterService masterClusterService = createClusterService(threadPool, masterNode);
toClose.addFirst(masterClusterService);
state = ClusterState.builder(masterClusterService.getClusterName()).nodes(state.nodes()).build();
setState(masterClusterService, state);
ZenDiscovery masterZen = buildZenDiscovery(settings, masterTransport, masterClusterService, threadPool);
toClose.addFirst(masterZen);
masterTransport.acceptIncomingRequests();
// inject a pending cluster state
masterZen.pendingClusterStatesQueue().addPending(ClusterState.builder(new ClusterName("foreign")).build());
// a new cluster state with a new discovery node (we will test if the cluster state
// was updated by the presence of this node in NodesFaultDetection)
ClusterState newState = ClusterState.builder(masterClusterService.state()).incrementVersion().nodes(DiscoveryNodes.builder(masterClusterService.state().nodes()).masterNodeId(masterNode.getId())).build();
try {
// publishing a new cluster state
ClusterChangedEvent clusterChangedEvent = new ClusterChangedEvent("testing", newState, state);
AssertingAckListener listener = new AssertingAckListener(newState.nodes().getSize() - 1);
masterZen.publish(clusterChangedEvent, listener);
listener.await(1, TimeUnit.HOURS);
// publish was a success, check that queue as cleared
assertThat(masterZen.pendingClusterStates(), emptyArray());
} catch (Discovery.FailedToCommitClusterStateException e) {
// not successful, so the pending queue should stay
assertThat(masterZen.pendingClusterStates(), arrayWithSize(1));
assertThat(masterZen.pendingClusterStates()[0].getClusterName().value(), equalTo("foreign"));
}
} finally {
IOUtils.close(toClose);
terminate(threadPool);
}
}
use of java.util.ArrayDeque in project hive by apache.
the class ParseUtils method containsTokenOfType.
public static boolean containsTokenOfType(ASTNode root, PTFUtils.Predicate<ASTNode> predicate) {
Queue<ASTNode> queue = new ArrayDeque<ASTNode>();
// BFS
queue.add(root);
while (!queue.isEmpty()) {
ASTNode current = queue.remove();
// Otherwise visit the next set of nodes that haven't been seen.
if (predicate.apply(current)) {
return true;
} else {
// Guard because ASTNode.getChildren.iterator returns null if no children available (bug).
if (current.getChildCount() > 0) {
for (Node child : current.getChildren()) {
queue.add((ASTNode) child);
}
}
}
}
return false;
}
Aggregations