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

import static org.junit.Assert.assertNotNull;

import hudson.model.Executor;
import hudson.model.Result;
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.Issue;
import org.jvnet.hudson.test.JenkinsRule;

public class LockStepHardKillTest extends LockStepTestBase {

  @Rule public JenkinsRule j = new JenkinsRule();

  @Issue("JENKINS-36479")
  @Test
  public void hardKillNewBuildClearsLock() throws Exception {
    LockableResourcesManager.get().createResource("resource1");

    WorkflowJob p1 = j.jenkins.createProject(WorkflowJob.class, "p");
    p1.setDefinition(
        new CpsFlowDefinition(
            "lock('resource1') { echo 'locked!'; semaphore 'wait-inside' }", true));
    WorkflowRun b1 = p1.scheduleBuild2(0).waitForStart();
    j.waitForMessage("locked!", b1);
    SemaphoreStep.waitForStart("wait-inside/1", b1);

    WorkflowJob p2 = j.jenkins.createProject(WorkflowJob.class, "p2");
    p2.setDefinition(
        new CpsFlowDefinition("lock('resource1') {\n" + "  semaphore 'wait-inside'\n" + "}", true));
    WorkflowRun b2 = p2.scheduleBuild2(0).waitForStart();

    // Make sure that b2 is blocked on b1's lock.
    j.waitForMessage("[resource1] is locked by " + b1.getFullDisplayName() + ", waiting...", b2);
    isPaused(b2, 1, 1);

    // Now b2 is still sitting waiting for a lock. Create b3 and launch it to clear the
    // lock.
    WorkflowJob p3 = j.jenkins.createProject(WorkflowJob.class, "p3");
    p3.setDefinition(
        new CpsFlowDefinition("lock('resource1') {\n" + "  semaphore 'wait-inside'\n" + "}", true));
    WorkflowRun b3 = p3.scheduleBuild2(0).waitForStart();
    j.waitForMessage("[resource1] is locked by " + b1.getFullDisplayName() + ", waiting...", b3);
    isPaused(b3, 1, 1);

    // Kill b1 hard.
    b1.doKill();
    j.waitForMessage("Hard kill!", b1);
    j.waitForCompletion(b1);
    j.assertBuildStatus(Result.ABORTED, b1);

    // Verify that b2 gets the lock.
    j.waitForMessage("Lock acquired on [resource1]", b2);
    SemaphoreStep.success("wait-inside/2", b2);
    // Verify that b2 releases the lock and finishes successfully.
    j.waitForMessage("Lock released on resource [resource1]", b2);
    j.assertBuildStatusSuccess(j.waitForCompletion(b2));
    isPaused(b2, 1, 0);

    // Now b3 should get the lock and do its thing.
    j.waitForMessage("Lock acquired on [resource1]", b3);
    SemaphoreStep.success("wait-inside/3", b3);
    j.assertBuildStatusSuccess(j.waitForCompletion(b3));
    isPaused(b3, 1, 0);
  }

  @Issue("JENKINS-40368")
  @Test
  public void hardKillWithWaitingRuns() throws Exception {
    LockableResourcesManager.get().createResource("resource1");
    WorkflowJob p = j.jenkins.createProject(WorkflowJob.class, "p");<commit_msg>LockStepHardKillTest.java: hardKillNewBuildClearsLock() reformat and add messages to help human tracing<commit_after>package org.jenkins.plugins.lockableresources;

import static org.junit.Assert.assertNotNull;

import hudson.model.Executor;
import hudson.model.Result;
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.Issue;
import org.jvnet.hudson.test.JenkinsRule;

public class LockStepHardKillTest extends LockStepTestBase {

  @Rule public JenkinsRule j = new JenkinsRule();

  @Issue("JENKINS-36479")
  @Test
  public void hardKillNewBuildClearsLock() throws Exception {
    LockableResourcesManager.get().createResource("resource1");

    WorkflowJob p1 = j.jenkins.createProject(WorkflowJob.class, "p1");
    p1.setDefinition(
        new CpsFlowDefinition(
            "lock('resource1') { echo 'locked!'; semaphore 'wait-inside'; echo 'Semaphore 1 unblocked'; }", true));
    WorkflowRun b1 = p1.scheduleBuild2(0).waitForStart();
    j.waitForMessage("locked!", b1);
    SemaphoreStep.waitForStart("wait-inside/1", b1);

    WorkflowJob p2 = j.jenkins.createProject(WorkflowJob.class, "p2");
    p2.setDefinition(
        new CpsFlowDefinition(
            "lock('resource1') {\n" +
            "  semaphore 'wait-inside'\n" +
            "  echo 'Semaphore 2 unblocked'\n" +
            "}", true));
    WorkflowRun b2 = p2.scheduleBuild2(0).waitForStart();

    // Make sure that b2 is blocked on b1's lock.
    j.waitForMessage("[resource1] is locked by " + b1.getFullDisplayName() + ", waiting...", b2);
    isPaused(b2, 1, 1);

    // Now b2 is still sitting waiting for a lock. Create b3 and launch it to clear the
    // lock.
    WorkflowJob p3 = j.jenkins.createProject(WorkflowJob.class, "p3");
    p3.setDefinition(
        new CpsFlowDefinition(
            "lock('resource1') {\n" +
            "  semaphore 'wait-inside'\n" +
            "  echo 'Semaphore 3 unblocked'\n" +
            "}", true));
    WorkflowRun b3 = p3.scheduleBuild2(0).waitForStart();

    // Make sure that b3 is also blocked still on b1's lock.
    j.waitForMessage("[resource1] is locked by " + b1.getFullDisplayName() + ", waiting...", b3);
    isPaused(b3, 1, 1);

    // Kill b1 hard.
    b1.doKill();
    j.waitForMessage("Hard kill!", b1);
    j.waitForCompletion(b1);
    j.assertBuildStatus(Result.ABORTED, b1);

    // Verify that b2 gets the lock.
    j.waitForMessage("Lock acquired on [resource1]", b2);
    SemaphoreStep.success("wait-inside/2", b2);
    // Verify that b2 releases the lock and finishes successfully.
    j.waitForMessage("Lock released on resource [resource1]", b2);
    j.assertBuildStatusSuccess(j.waitForCompletion(b2));
    isPaused(b2, 1, 0);

    // Now b3 should get the lock and do its thing.
    j.waitForMessage("Lock acquired on [resource1]", b3);
    SemaphoreStep.success("wait-inside/3", b3);
    j.assertBuildStatusSuccess(j.waitForCompletion(b3));
    isPaused(b3, 1, 0);
  }

  @Issue("JENKINS-40368")
  @Test
  public void hardKillWithWaitingRuns() throws Exception {
    LockableResourcesManager.get().createResource("resource1");
    WorkflowJob p = j.jenkins.createProject(WorkflowJob.class, "p");
{'fraction_non_alphanumeric': 0.13397277227722773, 'fraction_numerical': 0.02568069306930693, 'mean_word_length': 3.799554565701559, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, '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': '3636329', 'n_tokens_mistral': 2256, 'n_tokens_neox': 2145, 'n_words': 461}