<commit_before>require 'spec_helper'

module Concurrent

  describe Event do

    subject{ Event.new }

    context '#initialize' do

      it 'sets the state to unset' do
        subject.should_not be_set
      end
    end

    context '#set?' do

      it 'returns true when the event has been set' do
        subject.set
        subject.should be_set
      end

      it 'returns false if the event is unset' do
        #subject.reset
        subject.should_not be_set
      end
    end

    context '#set' do

      it 'triggers the event' do
        #subject.reset
        @expected = false
        Thread.new{ subject.wait; @expected = true }
        sleep(0.1)
        subject.set
        sleep(0.1)
        @expected.should be_true
      end

      it 'sets the state to set' do
        subject.set
        subject.should be_set
      end
    end

    context '#reset' do

      it 'does not change the state of an unset event' do
        subject.reset
        subject.should_not be_set
      end
 
      it 'does not trigger an unset event' do
        @expected = false
        Thread.new{ subject.wait; @expected = true }
        sleep(0.1)
        subject.reset
        sleep(0.1)
        @expected.should be_false
      end

      it 'does not interrupt waiting threads when event is unset' do
        @expected = false
        Thread.new{ subject.wait; @expected = true }
        sleep(0.1)
        subject.reset
        sleep(0.1)
        subject.set
        sleep(0.1)
        @expected.should be_true
      end

      it 'returns true when called on an unset event' do
        subject.reset.should be_true
      end

      it 'sets the state of a set event to unset' do
        subject.set
        subject.should be_set
        subject.reset
        subject.should_not be_set
      end

      it 'returns true when called on a set event' do
        subject.set
        subject.should be_set
        subject.reset.should be_true
      end
    end

    context '#wait' do

      it 'returns immediately when the event has been set' do
        subject.reset
        @expected = false
        subject.set
        Thread.new{ subject.wait(1000); @expected = true}
        sleep(1)
        @expected.should be_true
      end

      it 'returns true once the event is set' do
        subject.set
        subject.wait.should be_true
      end

      it 'blocks indefinitely when the timer is nil' do
        subject.reset
        @expected = false
        Thread.new{ subject.wait; @expected = true}
        subject.set
        sleep(1)
        @expected.should be_true
      end

      it 'stops waiting when the timer expires' do
        subject.reset
        @expected = false
        Thread.new{ subject.wait(0.5); @expected = true}
        sleep(2)
        @expected.should be_true
      end

      it 'returns false when the timer expires' do
        subject.reset
        subject.wait(1).should be_false
      end

      it 'triggers multiple waiting threads' do
        subject.reset
        @expected = []
        5.times{ Thread.new{ subject.wait; @expected << Thread.current.object_id } }
        subject.set
        sleep(1)
        @expected.length.should eq 5
      end

      it 'behaves appropriately if wait begins while #set is processing' do
        subject.reset
        @expected = []
        5.times{ Thread.new{ subject.wait(5) } }
        subject.set
        5.times{ Thread.new{ subject.wait; @expected << Thread.current.object_id } }
        sleep(1)
        @expected.length.should eq 5
      end
    end
  end
end<commit_msg>fixed non-deterministic event spec<commit_after>require 'spec_helper'

module Concurrent

  describe Event do

    subject{ Event.new }

    context '#initialize' do

      it 'sets the state to unset' do
        subject.should_not be_set
      end
    end

    context '#set?' do

      it 'returns true when the event has been set' do
        subject.set
        subject.should be_set
      end

      it 'returns false if the event is unset' do
        #subject.reset
        subject.should_not be_set
      end
    end

    context '#set' do

      it 'triggers the event' do
        #subject.reset
        @expected = false
        Thread.new{ subject.wait; @expected = true }
        sleep(0.1)
        subject.set
        sleep(0.1)
        @expected.should be_true
      end

      it 'sets the state to set' do
        subject.set
        subject.should be_set
      end
    end

    context '#reset' do

      it 'does not change the state of an unset event' do
        subject.reset
        subject.should_not be_set
      end
 
      it 'does not trigger an unset event' do
        @expected = false
        Thread.new{ subject.wait; @expected = true }
        sleep(0.1)
        subject.reset
        sleep(0.1)
        @expected.should be_false
      end

      it 'does not interrupt waiting threads when event is unset' do
        @expected = false
        Thread.new{ subject.wait; @expected = true }
        sleep(0.1)
        subject.reset
        sleep(0.1)
        subject.set
        sleep(0.1)
        @expected.should be_true
      end

      it 'returns true when called on an unset event' do
        subject.reset.should be_true
      end

      it 'sets the state of a set event to unset' do
        subject.set
        subject.should be_set
        subject.reset
        subject.should_not be_set
      end

      it 'returns true when called on a set event' do
        subject.set
        subject.should be_set
        subject.reset.should be_true
      end
    end

    context '#wait' do

      it 'returns immediately when the event has been set' do
        subject.reset
        @expected = false
        subject.set
        Thread.new{ subject.wait(1000); @expected = true}
        sleep(1)
        @expected.should be_true
      end

      it 'returns true once the event is set' do
        subject.set
        subject.wait.should be_true
      end

      it 'blocks indefinitely when the timer is nil' do
        subject.reset
        @expected = false
        Thread.new{ subject.wait; @expected = true}
        subject.set
        sleep(1)
        @expected.should be_true
      end

      it 'stops waiting when the timer expires' do
        subject.reset
        @expected = false
        Thread.new{ subject.wait(0.5); @expected = true}
        sleep(1)
        @expected.should be_true
      end

      it 'returns false when the timer expires' do
        subject.reset
        subject.wait(1).should be_false
      end

      it 'triggers multiple waiting threads' do
        latch = CountDownLatch.new(5)
        subject.reset
        5.times{ Thread.new{ subject.wait; latch.count_down } }
        subject.set
        latch.wait(0.2).should be_true
      end

      it 'behaves appropriately if wait begins while #set is processing' do
        subject.reset
        latch = CountDownLatch.new(5)
        5.times{ Thread.new{ subject.wait(5) } }
        subject.set
        5.times{ Thread.new{ subject.wait; latch.count_down } }
        latch.wait(0.2).should be_true
      end
    end
  end
end
{'fraction_non_alphanumeric': 0.07083509364878186, 'fraction_numerical': 0.009294465568229826, 'mean_word_length': 1.7200306396016851, 'pattern_counts': {'":': 0, '<': 7, '<?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': '4985604', 'n_tokens_mistral': 2053, 'n_tokens_neox': 1991, 'n_words': 672}