Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 10 additions & 15 deletions org.eclipse.jdt.debug.tests/java9/LogicalStructures.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,8 @@
* Tests built in logical structures.
*/
public class LogicalStructures {

public static void main(String[] args) {
generateGarbage();

Map<String, Integer> map = new HashMap<>();
map.put("one", 1);
map.put("two", 2);
Expand All @@ -33,18 +31,15 @@ public static void main(String[] args) {
Map.Entry<String, Integer> entry = set.iterator().next();
entry.getKey();
}

private static void generateGarbage() {
// generate garbage repeatedly to verify that logical values don't get GCed
Timer timer = new Timer(true);
timer.scheduleAtFixedRate(new TimerTask() {
private List<String> garbage;
@Override
public void run() {
System.gc();
garbage = Arrays.asList(new String("a"), new String("b"), new String("c"));
}
}, 200, 20);

// called from test to verify that logical values don't get GCed
private static void generateGarbageAndGC() throws InterruptedException {
List<Object> garbage;
for (int i = 0; i < 10; ++i) {
System.gc();
garbage = Arrays.asList(new Object(), new Object(), new Object());
Thread.sleep(20);
}
}

}
25 changes: 10 additions & 15 deletions org.eclipse.jdt.debug.tests/testprograms/LogicalStructures.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,8 @@
* Tests built in logical structures.
*/
public class LogicalStructures {

public static void main(String[] args) {
generateGarbage();

Map<String, Integer> map = new HashMap<>();
map.put("one", 1);
map.put("two", 2);
Expand All @@ -33,18 +31,15 @@ public static void main(String[] args) {
Map.Entry<String, Integer> entry = set.iterator().next();
entry.getKey();
}

private static void generateGarbage() {
// generate garbage repeatedly to verify that logical values don't get GCed
Timer timer = new Timer(true);
timer.scheduleAtFixedRate(new TimerTask() {
private List<String> garbage;
@Override
public void run() {
System.gc();
garbage = Arrays.asList(new String("a"), new String("b"), new String("c"));
}
}, 200, 20);

// called from test to verify that logical values don't get GCed
private static void generateGarbageAndGC() throws InterruptedException {
List<Object> garbage;
for (int i = 0; i < 10; ++i) {
System.gc();
garbage = Arrays.asList(new Object(), new Object(), new Object());
Thread.sleep(20);
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -13,17 +13,29 @@
*******************************************************************************/
package org.eclipse.jdt.debug.tests.variables;

import java.io.PrintWriter;
import java.io.StringWriter;
import java.util.concurrent.atomic.AtomicBoolean;

import org.eclipse.core.runtime.CoreException;
import org.eclipse.debug.core.DebugEvent;
import org.eclipse.debug.core.DebugException;
import org.eclipse.debug.core.DebugPlugin;
import org.eclipse.debug.core.ILogicalStructureType;
import org.eclipse.debug.core.model.IBreakpoint;
import org.eclipse.debug.core.model.IValue;
import org.eclipse.debug.core.model.IVariable;
import org.eclipse.jdt.debug.core.IJavaArray;
import org.eclipse.jdt.debug.core.IJavaDebugTarget;
import org.eclipse.jdt.debug.core.IJavaObject;
import org.eclipse.jdt.debug.core.IJavaStackFrame;
import org.eclipse.jdt.debug.core.IJavaThread;
import org.eclipse.jdt.debug.core.IJavaVariable;
import org.eclipse.jdt.debug.eval.IAstEvaluationEngine;
import org.eclipse.jdt.debug.eval.IEvaluationListener;
import org.eclipse.jdt.debug.eval.IEvaluationResult;
import org.eclipse.jdt.debug.tests.AbstractDebugTest;
import org.eclipse.jdt.internal.debug.core.JDIDebugPlugin;

/**
* Tests for logical structures
Expand All @@ -44,7 +56,7 @@ public TestLogicalStructures(String name) {
*/
public void testListLogicalStructure() throws Exception {
String typeName = "LogicalStructures";
createLineBreakpoint(34, typeName);
createLineBreakpoint(32, typeName);
IJavaThread thread= null;
try {
thread= launchToBreakpoint(typeName);
Expand All @@ -64,7 +76,7 @@ public void testListLogicalStructure() throws Exception {
assertEquals("Should be one logical structure type", 1, types.length);

IJavaObject logicalValue = (IJavaObject) types[0].getLogicalStructure(value);
Thread.sleep(500); // run a few GC cycles
gcInSnippet(frame);
assertEquals("Logical value should be an array", "java.lang.Object[]", logicalValue.getJavaType().getName());

IJavaArray array = (IJavaArray) logicalValue;
Expand All @@ -81,7 +93,7 @@ public void testListLogicalStructure() throws Exception {
*/
public void testMapLogicalStructure() throws Exception {
String typeName = "LogicalStructures";
createLineBreakpoint(34, typeName);
createLineBreakpoint(32, typeName);
IJavaThread thread= null;
try {
thread= launchToBreakpoint(typeName);
Expand All @@ -101,7 +113,7 @@ public void testMapLogicalStructure() throws Exception {
assertEquals("Should be one logical structure type", 1, types.length);

IJavaObject logicalValue = (IJavaObject) types[0].getLogicalStructure(value);
Thread.sleep(500); // run a few GC cycles
gcInSnippet(frame);
assertEquals("Logical value should be an array", "java.lang.Object[]", logicalValue.getJavaType().getName());

IJavaArray array = (IJavaArray) logicalValue;
Expand All @@ -118,7 +130,7 @@ public void testMapLogicalStructure() throws Exception {
*/
public void testEntryLogicalStructure() throws Exception {
String typeName = "LogicalStructures";
createLineBreakpoint(34, typeName);
createLineBreakpoint(32, typeName);
IJavaThread thread= null;
try {
thread= launchToBreakpoint(typeName);
Expand All @@ -138,15 +150,63 @@ public void testEntryLogicalStructure() throws Exception {
assertEquals("Should be one logical structure type", 1, types.length);

IJavaObject logicalValue = (IJavaObject) types[0].getLogicalStructure(value);
Thread.sleep(500); // run a few GC cycles
gcInSnippet(frame);
IVariable[] children = logicalValue.getVariables();
assertEquals("Should be two elements in the structure", 2, children.length);
assertEquals("First entry should be key", "key", children[0].getName());
assertEquals("Second entry should be value", "value", children[1].getName());

} finally {
terminateAndRemove(thread);
removeAllBreakpoints();
}
}

private void gcInSnippet(IJavaStackFrame stackFrame) throws CoreException, InterruptedException {
IAstEvaluationEngine engine = JDIDebugPlugin.getDefault().getEvaluationEngine(getProjectContext(), (IJavaDebugTarget) stackFrame.getDebugTarget());
EvaluationListener listener = new EvaluationListener();
engine.evaluate("generateGarbageAndGC()", stackFrame, listener, DebugEvent.EVALUATION_IMPLICIT, false);
listener.await();

}

private static class EvaluationListener implements IEvaluationListener {

private final AtomicBoolean done = new AtomicBoolean(false);
private final StringBuilder errors = new StringBuilder();

@Override
public void evaluationComplete(IEvaluationResult result) {
if (result.hasErrors()) {
errors.append("Evaluation resulted in errors:");
errors.append(System.lineSeparator());
for (String error : result.getErrorMessages()) {
errors.append(error);
errors.append(System.lineSeparator());
}
DebugException exception = result.getException();
if (exception != null) {
StringWriter sw = new StringWriter();
PrintWriter pw = new PrintWriter(sw);
exception.printStackTrace(pw);
errors.append(sw.getBuffer().toString());
errors.append(System.lineSeparator());
}
}
done.set(true);

}

private void await() throws InterruptedException {
long start = System.currentTimeMillis();
while (!done.get() && System.currentTimeMillis() - start <= DEFAULT_TIMEOUT) {
Thread.sleep(10);
}
if (!done.get()) {
fail("Timeout occurred while waiting on evaluation result");
}
if (!errors.isEmpty()) {
fail(errors.toString());
}
}
}
}
Loading