title: RSpec: Testing with Threads
body: <p>In RSpec, I have function that creates a new thread, and inside that thread performs some action–in my case, calls <code>TCPSocket#readline</code>. Here's the function as it is right now:</p>
  
  <pre><code>def read
    Thread.new do
      while line = @socket.readline
        #TODO: stuff
      end
    end
  end
  </code></pre>
  
  <p>Due to thread scheduling, my test will fail if written as such:</p>
  
  <pre><code>it "reads from socket" do
    subject.socket.should_receive(:readline)
    subject.read
  end
  </code></pre>
  
  <p>Currently the only way I know to hack around this is to use <code>sleep 0.1</code>. Is there a way to properly delay the test until that thread is running?</p>