<commit_before>require File.dirname(__FILE__) + '/../../spec_helper'
require 'thread'

describe "Mutex#lock" do
  it "returns self" do
    m = Mutex.new
    m.lock.should == m
    m.unlock
  end

  it "waits if the lock is not available" do
    m = Mutex.new

    m.lock
    v = 0

    th = Thread.new do
      m.lock
      v = 1
    end

    Thread.pass while th.status and th.status != "sleep"

    v.should == 0
    m.unlock
    th.join
    v.should == 1
  end

  it "waits if the lock is not available, even by the same thread" do
    m = Mutex.new

    v = 0

    th = Thread.new do
      m.lock
      m.lock
      v = 1
    end

    Thread.pass while th.status and th.status != "sleep"

    v.should == 0
    th.kill
    th.join
  end
end<commit_msg>Updated Mutex#lock specs for recursive locking case.<commit_after>require File.dirname(__FILE__) + '/../../spec_helper'
require 'thread'

describe "Mutex#lock" do
  before :each do
    ScratchPad.clear
  end

  it "returns self" do
    m = Mutex.new
    m.lock.should == m
    m.unlock
  end

  it "waits if the lock is not available" do
    m = Mutex.new

    m.lock

    th = Thread.new do
      m.lock
      ScratchPad.record :after_lock
    end

    Thread.pass while th.status and th.status != "sleep"

    ScratchPad.recorded.should be_nil
    m.unlock
    th.join
    ScratchPad.recorded.should == :after_lock
  end

  # Unable to find a specific ticket but behavior change may be
  # related to this ML thread.
  ruby_bug "[ruby-core:23457]", "172.16.58.3" do
    it "raises a ThreadError when used recursively" do
      m = Mutex.new

      th = Thread.new do
        m.lock
        m.lock
        v = 1
      end

      Thread.pass while th.status and th.status != "sleep"

      ScratchPad.recorded.should be_nil

      lambda do
        th.kill
        th.join
      end.should raise_error(ThreadError)
    end
  end
end
{'fraction_non_alphanumeric': 0.09322033898305085, 'fraction_numerical': 0.011122881355932203, 'mean_word_length': 1.9748031496062992, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 1, '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': '5800942', 'n_tokens_mistral': 679, 'n_tokens_neox': 633, 'n_words': 205}