<commit_before>package org.jenkins.plugins.lockableresources;

import static org.junit.Assume.assumeFalse;

import hudson.model.FreeStyleBuild;
import hudson.model.FreeStyleProject;
import java.util.Collections;
import org.jenkinsci.plugins.workflow.cps.CpsFlowDefinition;
import org.jenkinsci.plugins.workflow.job.WorkflowJob;
import org.jenkinsci.plugins.workflow.job.WorkflowRun;
import org.jenkinsci.plugins.workflow.test.steps.SemaphoreStep;
import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
import org.jvnet.hudson.test.JenkinsRule;
import org.jvnet.hudson.test.RestartableJenkinsRule;

public class LockStepWithRestartTest extends LockStepTestBase {

  @Rule public RestartableJenkinsRule story = new RestartableJenkinsRule();

  @Before
  public void disableOnNewerJava() {
    // FIXME: These tests break on Java 11
    assumeFalse(Double.parseDouble(System.getProperty("java.specification.version", "1.8")) > 10.0);
  }

  @Test
  public void lockOrderRestart() {
    story.then(
        (JenkinsRule j) -> {
          LockableResourcesManager.get().createResource("resource1");
          WorkflowJob p = j.jenkins.createProject(WorkflowJob.class, "p");
          p.setDefinition(
              new CpsFlowDefinition(
                  "lock('resource1') {\n" + "  semaphore 'wait-inside'\n" + "}\n" + "echo 'Finish'",
                  true));
          WorkflowRun b1 = p.scheduleBuild2(0).waitForStart();
          SemaphoreStep.waitForStart("wait-inside/1", b1);
          WorkflowRun b2 = p.scheduleBuild2(0).waitForStart();
          // Ensure that b2 reaches the lock before b3
          j.waitForMessage(
              "[resource1] is locked by " + b1.getFullDisplayName() + ", waiting...", b2);
          isPaused(b2, 1, 1);
          WorkflowRun b3 = p.scheduleBuild2(0).waitForStart();
          // Both 2 and 3 are waiting for locking resource1

          j.waitForMessage(
              "[resource1] is locked by " + b1.getFullDisplayName() + ", waiting...", b3);
          isPaused(b3, 1, 1);
        });

    story.then(
        (JenkinsRule j) -> {
          WorkflowJob p = j.jenkins.getItemByFullName("p", WorkflowJob.class);
          WorkflowRun b1 = p.getBuildByNumber(1);
          WorkflowRun b2 = p.getBuildByNumber(2);
          WorkflowRun b3 = p.getBuildByNumber(3);

          // Unlock resource1
          SemaphoreStep.success("wait-inside/1", null);
          j.waitForMessage("Lock released on resource [resource1]", b1);
          isPaused(b1, 1, 0);

          j.waitForMessage("Lock acquired on [resource1]", b2);
          isPaused(b2, 1, 0);
          j.assertLogContains(
              "[resource1] is locked by " + b1.getFullDisplayName() + ", waiting...", b3);
          isPaused(b3, 1, 1);
          SemaphoreStep.success("wait-inside/2", null);
          SemaphoreStep.waitForStart("wait-inside/3", b3);
          j.assertLogContains("Lock acquired on [resource1]", b3);
          SemaphoreStep.success("wait-inside/3", null);
          j.waitForMessage("Finish", b3);
          isPaused(b3, 1, 0);
        });
  }

  @Test
  public void interoperabilityOnRestart() {
    story.then(
        (JenkinsRule j) -> {
          LockableResourcesManager.get().createResource("resource1");
          WorkflowJob p = j.jenkins.createProject(WorkflowJob.class, "p");
          p.setDefinition(
              new CpsFlowDefinition(
                  "lock('resource1') {\n" + "  semaphore 'wait-inside'\n" + "}\n" + "echo 'Finish'",
                  true));
          WorkflowRun b1 = p.scheduleBuild2(0).waitForStart();
          SemaphoreStep.waitForStart("wait-inside/1", b1);
          isPaused(b1, 1, 0);

          FreeStyleProject f = j.createFreeStyleProject("f");
          f.addProperty(new RequiredResourcesProperty("resource1", null, null, null, null));

          f.scheduleBuild2(0);
          TestHelpers.waitForQueue(j.jenkins, f);
        });

    story.then(
        (JenkinsRule j) -> {
          WorkflowJob p = j.jenkins.getItemByFullName("p", WorkflowJob.class);
          FreeStyleProject f = j.jenkins.getItemByFullName("f", FreeStyleProject.class);
          WorkflowRun b1 = p.getBuildByNumber(1);

          // Unlock resource1
          SemaphoreStep.success("wait-inside/1", null);
          j.waitForMessage("Lock released on resource [resource1]", b1);
          isPaused(b1, 1, 0);

          FreeStyleBuild fb1 = null;
          System.out.print("Waiting for freestyle #1 to start building");
          while ((fb1 = f.getBuildByNumber(1)) == null) {
            Thread.sleep(250);
            System.out.print('.');
          }
          System.out.println();

          j.waitForMessage("acquired lock on [resource1]", fb1);
        });
  }

  @Test
  public void testReserveOverRestart() {
    story.then(
        (JenkinsRule j) -> {
          LockableResourcesManager manager = LockableResourcesManager.get();
          manager.createResource("resource1");
          manager.reserve(Collections.singletonList(manager.fromName("resource1")), "user");

          WorkflowJob p = j.jenkins.createProject(WorkflowJob.class, "p");
          p.setDefinition(
              new CpsFlowDefinition(
                  "lock('resource1') {\n" + "  echo 'inside'\n" + "}\n" + "echo 'Finish'", true));
          WorkflowRun b1 = p.scheduleBuild2(0).waitForStart();
          j.waitForMessage("[resource1] is locked, waiting...", b1);
          isPaused(b1, 1, 1);

          FreeStyleProject f = j.createFreeStyleProject("f");
          f.addProperty(new RequiredResourcesProperty("resource1", null, null, null, null));

          f.scheduleBuild2(0);
          TestHelpers.waitForQueue(j.jenkins, f);
        });

    story.then(
        (JenkinsRule j) -> {
          WorkflowJob p = j.jenkins.getItemByFullName("p", WorkflowJob.class);
          FreeStyleProject f = j.jenkins.getItemByFullName("f", FreeStyleProject.class);
          WorkflowRun b1 = p.getBuildByNumber(1);

          LockableResourcesManager manager = LockableResourcesManager.get();
          manager.createResource("resource1");
          manager.unreserve(Collections.singletonList(manager.fromName("resource1")));

          j.waitForMessage("Lock released on resource [resource1]", b1);
          isPaused(b1, 1, 0);
          j.waitForMessage("Finish", b1);
          isPaused(b1, 1, 0);

          j.waitUntilNoActivity();
        });
  }
}
<commit_msg>Run all tests on Java 11+<commit_after>package org.jenkins.plugins.lockableresources;

import hudson.model.FreeStyleBuild;
import hudson.model.FreeStyleProject;
import java.util.Collections;
import org.jenkinsci.plugins.workflow.cps.CpsFlowDefinition;
import org.jenkinsci.plugins.workflow.job.WorkflowJob;
import org.jenkinsci.plugins.workflow.job.WorkflowRun;
import org.jenkinsci.plugins.workflow.test.steps.SemaphoreStep;
import org.junit.Rule;
import org.junit.Test;
import org.jvnet.hudson.test.JenkinsRule;
import org.jvnet.hudson.test.RestartableJenkinsRule;

public class LockStepWithRestartTest extends LockStepTestBase {

  @Rule public RestartableJenkinsRule story = new RestartableJenkinsRule();

  @Test
  public void lockOrderRestart() {
    story.then(
        (JenkinsRule j) -> {
          LockableResourcesManager.get().createResource("resource1");
          WorkflowJob p = j.jenkins.createProject(WorkflowJob.class, "p");
          p.setDefinition(
              new CpsFlowDefinition(
                  "lock('resource1') {\n" + "  semaphore 'wait-inside'\n" + "}\n" + "echo 'Finish'",
                  true));
          WorkflowRun b1 = p.scheduleBuild2(0).waitForStart();
          SemaphoreStep.waitForStart("wait-inside/1", b1);
          WorkflowRun b2 = p.scheduleBuild2(0).waitForStart();
          // Ensure that b2 reaches the lock before b3
          j.waitForMessage(
              "[resource1] is locked by " + b1.getFullDisplayName() + ", waiting...", b2);
          isPaused(b2, 1, 1);
          WorkflowRun b3 = p.scheduleBuild2(0).waitForStart();
          // Both 2 and 3 are waiting for locking resource1

          j.waitForMessage(
              "[resource1] is locked by " + b1.getFullDisplayName() + ", waiting...", b3);
          isPaused(b3, 1, 1);
        });

    story.then(
        (JenkinsRule j) -> {
          WorkflowJob p = j.jenkins.getItemByFullName("p", WorkflowJob.class);
          WorkflowRun b1 = p.getBuildByNumber(1);
          WorkflowRun b2 = p.getBuildByNumber(2);
          WorkflowRun b3 = p.getBuildByNumber(3);

          // Unlock resource1
          SemaphoreStep.success("wait-inside/1", null);
          j.waitForMessage("Lock released on resource [resource1]", b1);
          isPaused(b1, 1, 0);

          j.waitForMessage("Lock acquired on [resource1]", b2);
          isPaused(b2, 1, 0);
          j.assertLogContains(
              "[resource1] is locked by " + b1.getFullDisplayName() + ", waiting...", b3);
          isPaused(b3, 1, 1);
          SemaphoreStep.success("wait-inside/2", null);
          SemaphoreStep.waitForStart("wait-inside/3", b3);
          j.assertLogContains("Lock acquired on [resource1]", b3);
          SemaphoreStep.success("wait-inside/3", null);
          j.waitForMessage("Finish", b3);
          isPaused(b3, 1, 0);
        });
  }

  @Test
  public void interoperabilityOnRestart() {
    story.then(
        (JenkinsRule j) -> {
          LockableResourcesManager.get().createResource("resource1");
          WorkflowJob p = j.jenkins.createProject(WorkflowJob.class, "p");
          p.setDefinition(
              new CpsFlowDefinition(
                  "lock('resource1') {\n" + "  semaphore 'wait-inside'\n" + "}\n" + "echo 'Finish'",
                  true));
          WorkflowRun b1 = p.scheduleBuild2(0).waitForStart();
          SemaphoreStep.waitForStart("wait-inside/1", b1);
          isPaused(b1, 1, 0);

          FreeStyleProject f = j.createFreeStyleProject("f");
          f.addProperty(new RequiredResourcesProperty("resource1", null, null, null, null));

          f.scheduleBuild2(0);
          TestHelpers.waitForQueue(j.jenkins, f);
        });

    story.then(
        (JenkinsRule j) -> {
          WorkflowJob p = j.jenkins.getItemByFullName("p", WorkflowJob.class);
          FreeStyleProject f = j.jenkins.getItemByFullName("f", FreeStyleProject.class);
          WorkflowRun b1 = p.getBuildByNumber(1);

          // Unlock resource1
          SemaphoreStep.success("wait-inside/1", null);
          j.waitForMessage("Lock released on resource [resource1]", b1);
          isPaused(b1, 1, 0);

          FreeStyleBuild fb1 = null;
          System.out.print("Waiting for freestyle #1 to start building");
          while ((fb1 = f.getBuildByNumber(1)) == null) {
            Thread.sleep(250);
            System.out.print('.');
          }
          System.out.println();

          j.waitForMessage("acquired lock on [resource1]", fb1);
        });
  }

  @Test
  public void testReserveOverRestart() {
    story.then(
        (JenkinsRule j) -> {
          LockableResourcesManager manager = LockableResourcesManager.get();
          manager.createResource("resource1");
          manager.reserve(Collections.singletonList(manager.fromName("resource1")), "user");

          WorkflowJob p = j.jenkins.createProject(WorkflowJob.class, "p");
          p.setDefinition(
              new CpsFlowDefinition(
                  "lock('resource1') {\n" + "  echo 'inside'\n" + "}\n" + "echo 'Finish'", true));
          WorkflowRun b1 = p.scheduleBuild2(0).waitForStart();
          j.waitForMessage("[resource1] is locked, waiting...", b1);
          isPaused(b1, 1, 1);

          FreeStyleProject f = j.createFreeStyleProject("f");
          f.addProperty(new RequiredResourcesProperty("resource1", null, null, null, null));

          f.scheduleBuild2(0);
          TestHelpers.waitForQueue(j.jenkins, f);
        });

    story.then(
        (JenkinsRule j) -> {
          WorkflowJob p = j.jenkins.getItemByFullName("p", WorkflowJob.class);
          FreeStyleProject f = j.jenkins.getItemByFullName("f", FreeStyleProject.class);
          WorkflowRun b1 = p.getBuildByNumber(1);

          LockableResourcesManager manager = LockableResourcesManager.get();
          manager.createResource("resource1");
          manager.unreserve(Collections.singletonList(manager.fromName("resource1")));

          j.waitForMessage("Lock released on resource [resource1]", b1);
          isPaused(b1, 1, 0);
          j.waitForMessage("Finish", b1);
          isPaused(b1, 1, 0);

          j.waitUntilNoActivity();
        });
  }
}

{'fraction_non_alphanumeric': 0.1321099459802987, 'fraction_numerical': 0.02009850651414045, 'mean_word_length': 2.5145170295924064, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 16, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1569021', 'n_tokens_mistral': 3999, 'n_tokens_neox': 3821, 'n_words': 722}