package hudson.model.queue;

import hudson.model.FreeStyleProject;
import hudson.model.Node;
import hudson.model.Queue;
import hudson.model.Queue.Item;
import hudson.model.TaskListener;
import hudson.util.StreamTaskListener;
import java.io.StringWriter;
import java.util.logging.Level;
import static org.hamcrest.Matchers.containsString;
import static org.junit.Assert.*;
import org.junit.Rule;
import org.junit.Test;
import org.jvnet.hudson.test.Issue;
import org.jvnet.hudson.test.JenkinsRule;
import org.jvnet.hudson.test.LoggerRule;
import org.jvnet.hudson.test.TestExtension;

public class QueueTaskDispatcherTest {

    @Rule
    public JenkinsRule r = new JenkinsRule();

    @Rule
    public LoggerRule logging = new LoggerRule().record(Queue.class, Level.ALL);

    @Test
    public void canRunBlockageIsDisplayed() throws Exception {
        FreeStyleProject project = r.createFreeStyleProject();
        r.jenkins.getQueue().schedule(project);

        Item item = r.jenkins.getQueue().getItem(project);
        for (int i = 0; i < 4 * 60 && !item.isBlocked(); i++) {
            Thread.sleep(250);
            item = r.jenkins.getQueue().getItem(project);
        }
        assertTrue("Not blocked after 60 seconds", item.isBlocked());
        assertEquals("Expected CauseOfBlockage to be returned", "blocked by canRun", item.getWhy());
    }

    @TestExtension("canRunBlockageIsDisplayed")
    public static class MyQueueTaskDispatcher extends QueueTaskDispatcher {
        @Override
        public CauseOfBlockage canRun(Item item) {
            return new CauseOfBlockage() {
                @Override
                public String getShortDescription() {
                    return "blocked by canRun";
                }
            };
        }
    }

    @Issue("JENKINS-38514")
    @Test
    public void canTakeBlockageIsDisplayed() throws Exception {
        FreeStyleProject project = r.createFreeStyleProject();
        r.jenkins.getQueue().schedule(project);
        Queue.Item item;
        while (true) {
            item = r.jenkins.getQueue().getItem(project);
            if (item.isBuildable()) {
                break;
            }
            Thread.sleep(100);
        }
        CauseOfBlockage cob = item.getCauseOfBlockage();
        assertNotNull(cob);
        assertThat(cob.getShortDescription(), containsString("blocked by canTake"));
        StringWriter w = new StringWriter();
        TaskListener l = new StreamTaskListener(w);
        cob.print(l);
        l.getLogger().flush();
        assertThat(w.toString(), containsString("blocked by canTake"));
    }

    @TestExtension("canTakeBlockageIsDisplayed")
    public static class AnotherQueueTaskDispatcher extends QueueTaskDispatcher {
        @Override
        public CauseOfBlockage canTake(Node node, Queue.BuildableItem item) {
            return new CauseOfBlockage() {
                @Override
                public String getShortDescription() {
                    return "blocked by canTake";
                }
            };
        }
    }

}

@@ -945,16 +945,67 @@
 (project
+, 0);%0A%0A        r.getInstance().getQueue().maintain(
 );%0A%0A    
@@ -1063,170 +1063,8 @@
 t);%0A
-        for (int i = 0; i %3C 4 * 60 && !item.isBlocked(); i++) %7B%0A            Thread.sleep(250);%0A            item = r.jenkins.getQueue().getItem(project);%0A        %7D
 %0A   
@@ -1095,25 +1095,8 @@
 cked
- after 60 seconds
 %22, i
@@ -1792,32 +1792,33 @@
 StyleProject();%0A
+%0A
         r.jenkin
@@ -1838,32 +1838,35 @@
 schedule(project
+, 0
 );%0A        Queue
@@ -1864,221 +1864,142 @@
     
-Queue.Item item;%0A        while (true) %7B%0A            item = r.jenkins.getQueue().getItem(project);%0A            if (item.isBuildable()) %7B%0A                break;%0A            %7D%0A            Thread.sleep(100);%0A        %7D
+r.getInstance().getQueue().maintain();%0A%0A        Queue.Item item = r.jenkins.getQueue().getItem(project);%0A        assertNotNull(item);%0A
 %0A   
@@ -2161,24 +2161,25 @@
 canTake%22));%0A
+%0A
         Stri

Fix the race condtion in QueueTaskDispatcherTest