<commit_before>require "test/unit"
require "posixlock"

class PosilockTest < Test::Unit::TestCase
  def setup
    @file = File.new(File.expand_path('../testfile', __FILE__), File::CREAT|File::TRUNC|File::RDWR, 0644)
  end

  def teardown
    File.delete(@file.path)
  end

  def test_method_definition
    assert_respond_to @file, :lockf
    assert_respond_to @file, :posixlock
  end

  def test_lockf
    pid = fork {
      Signal.trap("USR1") { exit }

      @file.lockf(File::F_LOCK, 0)

      Process.kill('USR2', Process.ppid())
      loop { sleep(1) }
    }

    Signal.trap("USR2") {
      assert_equal pid, @file.lockf(File::F_TEST, 0)
      Process.kill('USR1', pid)
    }

    Process.wait
  end

  def test_posixlock
    pid = fork {
      Signal.trap("USR1") { exit }

      @file.posixlock(File::LOCK_EX)

      Process.kill('USR2', Process.ppid())
      loop { sleep(1) }
    }

    Signal.trap("USR2") {
      assert_equal pid, @file.lockf(File::F_TEST, 0)
      Process.kill('USR1', pid)
      assert @file.posixlock(File::LOCK_EX)
    }

    Process.wait
  end
end<commit_msg>Move trap signal on parent before fork in tests<commit_after>require "test/unit"
require "posixlock"

class PosilockTest < Test::Unit::TestCase
  def setup
    @file = File.new(File.expand_path('../testfile', __FILE__), File::CREAT|File::TRUNC|File::RDWR, 0644)
  end

  def teardown
    File.delete(@file.path)
  end

  def test_method_definition
    assert_respond_to @file, :lockf
    assert_respond_to @file, :posixlock
  end

  def test_lockf
    pid = 0

    Signal.trap("USR2") {
      assert_equal pid, @file.lockf(File::F_TEST, 0)
      Process.kill('USR1', pid)
    }

    pid = fork {
      Signal.trap("USR1") { exit }

      @file.lockf(File::F_LOCK, 0)

      Process.kill('USR2', Process.ppid())
      loop { sleep(1) }
    }

    Process.wait
  end

  def test_posixlock
    pid = 0

    Signal.trap("USR2") {
      assert_equal pid, @file.lockf(File::F_TEST, 0)
      Process.kill('USR1', pid)
      assert @file.posixlock(File::LOCK_EX)
    }

    pid = fork {
      Signal.trap("USR1") { exit }

      @file.posixlock(File::LOCK_EX)

      Process.kill('USR2', Process.ppid())
      loop { sleep(1) }
    }

    Process.wait
  end
end
{'fraction_non_alphanumeric': 0.14069456812110417, 'fraction_numerical': 0.016028495102404273, 'mean_word_length': 2.802030456852792, 'pattern_counts': {'":': 0, '<': 5, '<?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': '200896', 'n_tokens_mistral': 887, 'n_tokens_neox': 836, 'n_words': 155}