<commit_before>require 'test_helper'

class SemaphoreTest < MiniTest::Unit::TestCase

  def test_should_create_semaphore
    semaphore = Semaphore.new 3
    assert semaphore
  end

  def test_should_synchronize_method
    var = false

    semaphore = Semaphore.new 3
    semaphore.synchronize {
      var = true
    }

    assert var
  end

  def test_check_two_threads_write
    var = []
    semaphore = Semaphore.new 1

    a = Thread.new{
      semaphore.synchronize {
        sleep 1
        100.times{ var << 1 }
        sleep 100
      }
    }

    sleep 1

    b = Thread.new{
      semaphore.synchronize {
        100.times{ var << 2 }
      }
    }

    sleep 1

    a.kill
    b.kill

    assert_equal 100, var.inject(:+)

  end

end<commit_msg>added multi threade test<commit_after>require 'test_helper'

class SemaphoreTest < MiniTest::Unit::TestCase

  def test_should_create_semaphore
    semaphore = Semaphore.new 3
    assert semaphore
  end

  def test_should_synchronize_method
    var = false

    semaphore = Semaphore.new 3
    semaphore.synchronize {
      var = true
    }

    assert var
  end

  def test_two_threads
    var = []
    threads = []
    semaphore = Semaphore.new 1

    threads << Thread.new{ semaphore.synchronize {
      100.times{ var << 1 }
      sleep 100
    }}

    sleep 1

    threads << Thread.new{ semaphore.synchronize {
      100.times{ var << 2 }
    }}

    sleep 1

    threads.map(&:kill)

    assert_equal 100, var.inject(:+)
  end

  def test_multi_threads
    var = { a: [], b: [], c: [], }
    threads = []

    semaphore = Semaphore.new 2

    threads << Thread.new{ semaphore.synchronize {
      100.times{ var[:a] << 1 }
      sleep 100
    }}

    threads << Thread.new{ semaphore.synchronize {
      100.times{ var[:b] << 2 }
      sleep 100
    }}

    sleep 1

    threads << Thread.new{ semaphore.synchronize {
      100.times{ var[:c] << 3 }
    }}

    sleep 1

    threads.map(&:kill)

    sum = (var[:a] + var[:b] + var[:c]).inject(:+)

    assert_equal 300, sum
  end

end
{'fraction_non_alphanumeric': 0.10415647921760392, 'fraction_numerical': 0.03080684596577017, 'mean_word_length': 2.138036809815951, 'pattern_counts': {'":': 0, '<': 29, '<?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': '7119800', 'n_tokens_mistral': 792, 'n_tokens_neox': 710, 'n_words': 163}