require File.dirname(__FILE__) + '/../../spec_helper'
require File.dirname(__FILE__) + '/fixtures/classes'

describe "ThreadGroup#list" do
  it "returns the list of threads in the group" do
    chan = Channel.new
    th1 = Thread.new { chan << :go; sleep }
    chan.receive.should == :go
    tg = ThreadGroup.new
    tg.add(th1)
    tg.list.should include(th1)
    
    th2 = Thread.new { chan << :go; sleep }
    chan.receive.should == :go
    
    tg.add(th2)    
    (tg.list & [th1, th2]).should include(th1, th2)

    th1.run; th1.join
    th2.run; th2.join
  end
end

require File.dirname(__FILE__) + '/../../spec_helper'
require File.dirname(__FILE__) + '/fixtures/classes'

describe "ThreadGroup#list" do
  it "returns the list of threads in the group" do
    chan = Channel.new
    th1 = Thread.new { chan << :go; sleep }
    chan.receive.should == :go
    tg = ThreadGroup.new
    tg.add(th1)
    tg.list.should include(th1)
    
    th2 = Thread.new { chan << :go; sleep }
    chan.receive.should == :go
    
    tg.add(th2)    
    (tg.list & [th1, th2]).should include(th1, th2)

    Thread.pass until th1.status == 'sleep' || !th1.alive?
    Thread.pass until th2.status == 'sleep' || !th2.alive?
    th1.run; th1.join
    th2.run; th2.join
  end
end

Fix yet another race condition in a threading spec:
Fix yet another race condition in a threading spec:

ThreadGroup's list_spec was making the assumption that immediately after
the two threads posted to the Channel, they would then be asleep. The
spec ran through its checks and then tried to #run and #join those
threads. But since there was no guarantee either thread had actually
reached the #sleep call, there was a real chance #run would be called too
early and #join would end up waiting for a sleeping thread. The fix was
simply to wait for both threads to either be sleeping or dead, and then
proceed to the #run and #wait calls.
