Skip to content

Commit

Permalink
Continuing initialization refactoring, creating fs event stream.
Browse files Browse the repository at this point in the history
The actual fs event stream itself (from OSX.FSEventStreamCreate) is now made outside of initialization. This should free initialization to be simple.
  • Loading branch information
ymendel committed Jun 14, 2008
1 parent 7a2d590 commit ca63f97
Show file tree
Hide file tree
Showing 2 changed files with 96 additions and 0 deletions.
5 changes: 5 additions & 0 deletions lib/fsevents/stream.rb
Expand Up @@ -26,6 +26,11 @@ def initialize(*paths, &callback)
raise StreamError, 'Unable to create FSEvents stream.' unless @stream
end

def create
@stream = OSX.FSEventStreamCreate(allocator, stream_callback, context, paths, since, latency, flags)
raise StreamError, 'Unable to create FSEvents stream.' unless @stream
end

def stream_callback
lambda do |stream, context, event_count, paths, event_flags, event_IDs|
paths.regard_as('*')
Expand Down
91 changes: 91 additions & 0 deletions spec/stream_spec.rb
Expand Up @@ -271,6 +271,97 @@
end
end

it 'should create a stream' do
@stream.should respond_to(:create)
end

describe 'when creating the stream' do
before :each do
@args = {}
[:allocator, :context, :paths, :since, :latency, :flags].each do |arg|
val = stub(arg.to_s)

@stream.stubs(arg).returns(val)
@args[arg] = val
end

@arg_placeholders = Array.new(7) { anything }

@stream_val = stub('stream')
OSX.stubs(:FSEventStreamCreate).returns(@stream_val)
end

it 'should create an FSEvent stream' do
OSX.expects(:FSEventStreamCreate).returns(@stream_val)
@stream.create
end

it 'should pass the allocator' do
args = @arg_placeholders
args[0] = @args[:allocator]
OSX.expects(:FSEventStreamCreate).with(*args).returns(@stream_val)
@stream.create
end

it 'should pass the stream callback' do
# stream_callback returns a different proc every time it's called
@stream.stubs(:stream_callback).returns(stub('stream callback'))
args = @arg_placeholders
args[1] = @stream.stream_callback
OSX.expects(:FSEventStreamCreate).with(*args).returns(@stream_val)
@stream.create
end

it 'should pass the context' do
args = @arg_placeholders
args[2] = @args[:context]
OSX.expects(:FSEventStreamCreate).with(*args).returns(@stream_val)
@stream.create
end

it 'should pass the paths' do
args = @arg_placeholders
args[3] = @args[:paths]
OSX.expects(:FSEventStreamCreate).with(*args).returns(@stream_val)
@stream.create
end

it "should pass 'since' (event ID)" do
args = @arg_placeholders
args[4] = @args[:since]
OSX.expects(:FSEventStreamCreate).with(*args).returns(@stream_val)
@stream.create
end

it 'should pass the latency' do
args = @arg_placeholders
args[5] = @args[:latency]
OSX.expects(:FSEventStreamCreate).with(*args).returns(@stream_val)
@stream.create
end

it 'should pass the flags' do
args = @arg_placeholders
args[6] = @args[:flags]
OSX.expects(:FSEventStreamCreate).with(*args).returns(@stream_val)
@stream.create
end

it 'should store the stream' do
@stream.create
@stream.stream.should == @stream_val
end

it 'should raise a StreamError exception if the stream could not be created' do
OSX.stubs(:FSEventStreamCreate).returns(nil)
lambda { @stream.create }.should raise_error(FSEvents::Stream::StreamError)
end

it 'should not raise a StreamError exception if the stream could be created' do
lambda { @stream.create }.should_not raise_error(FSEvents::Stream::StreamError)
end
end

it 'should have a stream callback' do
@stream.should respond_to(:stream_callback)
end
Expand Down

0 comments on commit ca63f97

Please sign in to comment.