From: Stefano Crocco Date: 2011-09-01T15:35:25+09:00 Subject: Re: Testing threads in RSpec On Thursday 01 September 2011 04:18:48 James Lavin wrote: > Stefano Crocco wrote in post #1019453: > > You should try putting > > > > thread.join > > > > instead of sleep. That makes the main thread wait for the thread to > > return > > before going on. See the ri documentation for Thread#join for more > > information. > > > > I hope this helps > > > > Stefano > > Thanks for replying, Stefano. But I DON'T want the main thread to wait > for the named thread to return. I want the opposite. > > Thread#join keeps the named thread alive to completion. This would help > if the named thread died faster than the main thread's code executed. In > my case, it's the opposite. The named thread (playing a "song") > naturally takes longer than the main thread. So I'm not clear how > Thread#join would help. > > My intent (which I should have spelled out) is to have the Jukebox start > (but not finish) playing a "song" (for testing purposes, just a "sleep > 0.2" or something) and test that it's playing while the thread is > running. The "sleep 0.1" makes the main thread wait until the "song" > starts playing before testing that it's playing. > > --James Ok. Now I understand what you want. Unless I'm missing something, it should work. This is a simple Jukebox class with its play method with a test to check it. Not knowing what your Jukebox class does, I can't say whether there are key differences, but the test here passes. class Jukebox class << self attr_reader :current_song end def self.play @current_song = 'x' sleep 2 end end describe Jukebox do it "should have a current_song" do thread = Thread.new do Jukebox.play end sleep 0.1 Jukebox.current_song.should == 'x' end end Stefano