@SuppressFBWarnings(value="DM_DEFAULT_ENCODING", justification="Only used by tests anyway.") public void print(PrintStream ps) { print(new PrintWriter(ps,true)); }
public String getThreadDump() { return execution.getThreadDump().toString(); }
public static CpsThreadDump from(CpsThreadGroup g) { // all the threads that share the same head form a logically single thread Map<FlowHead, List<CpsThread>> m = new LinkedHashMap<FlowHead,List<CpsThread>>(); for (CpsThread t : g.threads.values()) { List<CpsThread> l = m.get(t.head); if (l==null) m.put(t.head, l = new ArrayList<CpsThread>()); l.add(t); } CpsThreadDump td = new CpsThreadDump(); for (List<CpsThread> e : m.values()) td.threads.add(new ThreadInfo(e)); return td; }
td.print(System.out); assertStackTrace( td.getThreads().get(0), "DSL.semaphore(waiting on x/1)", "WorkflowScript.bar(WorkflowScript:3)", "WorkflowScript.run(WorkflowScript:10)"); assertStackTrace( td.getThreads().get(1), "DSL.semaphore(waiting on y/1)", "WorkflowScript.bar(WorkflowScript:3)",
return CpsThreadDump.fromText(b.toString()); } else { return CpsThreadDump.fromText("Program state is unknown"); throw new AssertionError(); // since we are checking programPromise.isDone() upfront } catch (ExecutionException e) { return CpsThreadDump.from(new Exception("Failed to resurrect program state",e));
public CpsThreadDump getThreadDump() { return CpsThreadDump.from(this); }
@Test public void nativeMethods() throws Exception { p.setDefinition(new CpsFlowDefinition( "@NonCPS def untransformed() {Thread.sleep(Long.MAX_VALUE)}\n" + "def helper() {echo 'sleeping'; /* flush output */ sleep 1; untransformed()}\n" + "helper()", false)); WorkflowRun b = p.scheduleBuild2(0).waitForStart(); CpsFlowExecution e = (CpsFlowExecution) b.getExecutionPromise().get(); j.waitForMessage("sleeping", b); do { // wait for the CPS VM to be busy (opposite of waitForSuspension) Thread.sleep(100); } while (!e.blocksRestart()); CpsThreadDump td = e.getThreadDump(); td.print(System.out); assertStackTrace(td.getThreads().get(0), // TODO would like to see untransformed and Thread.sleep here "WorkflowScript.helper(WorkflowScript:2)", "WorkflowScript.run(WorkflowScript:3)"); b.doKill(); }
/** * A mock thread dump that merely displays some fixed text. * @param text possibly multiline string */ @SuppressWarnings("serial") public static @Nonnull CpsThreadDump fromText(@Nonnull final String text) { return CpsThreadDump.from(new Throwable() { @Override public String toString() { return text; } @Override public Throwable fillInStackTrace() { return this; // irrelevant } }); }
@Test public void load() throws Exception { j.jenkins.getWorkspaceFor(p).child("lib.groovy").write("def m() {semaphore 'here'}; this", null); p.setDefinition(new CpsFlowDefinition("def lib; node {lib = load 'lib.groovy'}; lib.m()", true)); WorkflowRun b = p.scheduleBuild2(0).waitForStart(); SemaphoreStep.waitForStart("here/1", b); CpsThreadDump td = b.getAction(CpsThreadDumpAction.class).threadDumpSynchronous(); td.print(System.out); assertStackTrace(td.getThreads().get(0), "DSL.semaphore(waiting on here/1)", "Script1.m(Script1.groovy:1)", "WorkflowScript.run(WorkflowScript:1)"); }
@Override public String toString() { StringWriter sw = new StringWriter(); print(new PrintWriter(sw)); return sw.toString(); }
@Test public void getStatus() throws Exception { WorkflowJob p = r.createProject(WorkflowJob.class, "p"); p.setDefinition(new CpsFlowDefinition("slowBlock {semaphore 'wait'}", true)); WorkflowRun b = p.scheduleBuild2(0).waitForStart(); startEnter.acquire(); assertThat(((CpsFlowExecution) b.getExecution()).getThreadDump().toString(), containsString("at DSL.slowBlock(running in thread: org.jenkinsci.plugins.workflow.steps.SynchronousNonBlockingStepExecution [#")); Thread.sleep(250); // give CPS thread some time to go back to sleep assertTrue(SynchronousNonBlockingStepExecutionTest.blocksRestart(b)); startExit.release(); SemaphoreStep.waitForStart("wait/1", b); assertThat(((CpsFlowExecution) b.getExecution()).getThreadDump().toString(), containsString("at DSL.slowBlock(not currently scheduled, or running blocks)")); while (SynchronousNonBlockingStepExecutionTest.blocksRestart(b)) { Thread.sleep(100); // as above } SemaphoreStep.success("wait/1", null); endEnter.acquire(); assertThat(((CpsFlowExecution) b.getExecution()).getThreadDump().toString(), containsString("at DSL.slowBlock(running in thread: org.jenkinsci.plugins.workflow.steps.SynchronousNonBlockingStepExecution [#")); Thread.sleep(250); // as above assertTrue(SynchronousNonBlockingStepExecutionTest.blocksRestart(b)); endExit.release(); r.assertBuildStatusSuccess(r.waitForCompletion(b)); }
public static CpsThreadDump from(Throwable t) { CpsThreadDump td = new CpsThreadDump(); td.threads.add(new ThreadInfo(t)); return td;}
td.print(System.out); List<ThreadInfo> threads = td.getThreads(); assertEquals(1, threads.size()); ThreadInfo t = threads.get(0);
@Override public void writeTo(OutputStream outputStream) throws IOException { PrintWriter pw = new PrintWriter(new OutputStreamWriter(outputStream, Charsets.UTF_8)); for (FlowExecution flow : FlowExecutionList.get()) { if (flow instanceof CpsFlowExecution) { pw.println("Build: " + flow.getOwner().getExecutable()); ((CpsFlowExecution) flow).getThreadDump().print(pw); pw.println("Approximate graph size: " + ((CpsFlowExecution) flow).approximateNodeCount()); pw.println(); } } pw.flush(); } });