-
Notifications
You must be signed in to change notification settings - Fork 4
/
stream_spec.rb
585 lines (474 loc) · 17.6 KB
/
stream_spec.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
require File.dirname(__FILE__) + '/spec_helper.rb'
describe FSEvents::Stream do
before :each do
@path = '/tmp'
@stream = FSEvents::Stream.new(@path) {}
end
describe 'when initialized' do
it 'should accept a path and callback block' do
lambda { FSEvents::Stream.new(@path) {} }.should_not raise_error(ArgumentError)
end
it 'should not require a path' do
lambda { FSEvents::Stream.new() {} }.should_not raise_error(ArgumentError)
end
it 'should require a callback block' do
lambda { FSEvents::Stream.new(@path) }.should raise_error(ArgumentError)
end
it 'should accept a hash of options' do
lambda { FSEvents::Stream.new(@path, :flags => 27 ) {} }.should_not raise_error(ArgumentError)
end
it 'should accept an array of paths' do
lambda { FSEvents::Stream.new([@path, '/other/path']) {} }.should_not raise_error
end
it 'should accept an array of paths with options' do
lambda { FSEvents::Stream.new([@path, '/other/path'], :flags => 27) {} }.should_not raise_error
end
it 'should accept multiple paths' do
lambda { FSEvents::Stream.new(@path, '/other/path') {} }.should_not raise_error
end
it 'should accept multiple paths with options' do
lambda { FSEvents::Stream.new(@path, '/other/path', :flags => 27) {} }.should_not raise_error
end
it 'should store the callback block' do
callback = lambda {}
FSEvents::Stream.new(@path, &callback).callback.should == callback
end
describe 'handling options' do
before :each do
@options = {}
[:allocator, :context, :since, :latency, :flags].each do |opt|
@options[opt] = stub(opt.to_s)
end
@options[:mode] = :cache
@other_path = '/other/path'
end
it 'should store the allocator' do
FSEvents::Stream.new(@path, @options) {}.allocator.should == @options[:allocator]
end
it 'should default the allocator to KCFAllocatorDefault' do
@options.delete(:allocator)
FSEvents::Stream.new(@path, @options) {}.allocator.should == OSX::KCFAllocatorDefault
end
it 'should store the context' do
FSEvents::Stream.new(@path, @options) {}.context.should == @options[:context]
end
it 'should default the context to nil' do
@options.delete(:context)
FSEvents::Stream.new(@path, @options) {}.context.should == nil
end
it 'should store the path as an array' do
FSEvents::Stream.new(@path, @options) {}.paths.should == [@path]
end
it 'should store an array of paths as-is' do
FSEvents::Stream.new([@path, @other_path], @options) {}.paths.should == [@path, @other_path]
end
it 'should store multiple paths as an array' do
FSEvents::Stream.new(@path, @other_path, @options) {}.paths.should == [@path, @other_path]
end
it 'should default the path to the present working directory' do
FSEvents::Stream.new(@options) {}.paths.should == [Dir.pwd]
end
it "should store 'since' (event ID)" do
FSEvents::Stream.new(@path, @options) {}.since.should == @options[:since]
end
it "should default 'since' to KFSEventStreamEventIdSinceNow" do
@options.delete(:since)
FSEvents::Stream.new(@path, @options) {}.since.should == OSX::KFSEventStreamEventIdSinceNow
end
it 'should store the latency' do
FSEvents::Stream.new(@path, @options) {}.latency.should == @options[:latency]
end
it 'should default the latency to 1.0' do
@options.delete(:latency)
FSEvents::Stream.new(@path, @options) {}.latency.should == 1.0
end
it 'should store the flags' do
FSEvents::Stream.new(@path, @options) {}.flags.should == @options[:flags]
end
it 'should default the flags to 0' do
@options.delete(:flags)
FSEvents::Stream.new(@path, @options) {}.flags.should == 0
end
it 'should store mode' do
FSEvents::Stream.new(@path, @options) {}.mode.should == @options[:mode]
end
it 'should default mode to mtime' do
@options.delete(:mode)
FSEvents::Stream.new(@path, @options) {}.mode.should == :mtime
end
it 'should not accept any mode other than mtime or cache' do
lambda { FSEvents::Stream.new(@path, @options.merge(:mode => :something_else)) {} }.should raise_error(ArgumentError)
end
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
describe 'stream callback' do
it 'should return a proc' do
@stream.stream_callback.should be_kind_of(Proc)
end
describe 'proc' do
before :each do
@callback_arg_order = [:stream, :context, :event_count, :paths, :event_flags, :event_IDs]
@args_hash = {}
[:stream, :context].each do |arg|
@args_hash[arg] = stub(arg.to_s)
end
@args_hash[:event_count] = 0
[:paths, :event_flags, :event_IDs].each do |arg|
@args_hash[arg] = []
end
@args_hash[:paths].stubs(:regard_as)
@args = @args_hash.values_at(*@callback_arg_order)
@callback = stub('callback', :call => nil)
@stream.stubs(:callback).returns(@callback)
@proc = @stream.stream_callback
end
it 'should accept stream, context, event count, paths, event flags, and event IDs' do
lambda { @proc.call(*@args) }.should_not raise_error(ArgumentError)
end
it "should regard the paths as '*'" do
@args_hash[:paths].expects(:regard_as).with('*')
@proc.call(*@args)
end
it 'should call the stored callback' do
@callback.expects(:call)
@proc.call(*@args)
end
it 'should collect the paths and IDs, create Event objects, and pass them to the stored callback' do
event_count = 3
@args_hash[:event_count] = event_count
events = []
event_count.times do |i|
path = "/some/path/to/dir/number/#{i+1}"
id = i + 1
@args_hash[:paths].push path
@args_hash[:event_IDs].push id
event = stub("event #{path}")
FSEvents::Event.stubs(:new).with(id, path, @stream).returns(event)
events.push event
end
@args = @args_hash.values_at(*@callback_arg_order)
@callback.expects(:call).with(events)
@proc.call(*@args)
end
it 'should extend the event array' do
@args = @args_hash.values_at(*@callback_arg_order)
@callback.expects(:call).with(kind_of(EventArray))
@proc.call(*@args)
end
it "should update the stream's last event" do
@stream.expects(:update_last_event)
@proc.call(*@args)
end
end
end
it 'should create' do
FSEvents::Stream.should respond_to(:create)
end
describe 'when creating' do
before :each do
@other_path = '/other/path'
end
# This is just here for organization and use of the before block.
# I'd like to ensure that the block is passed to new, but mocha expecation apparently doesn't support that.
# So instead I stub new for some testing and then have something that actually uses new and sees the callback
# is the expected block.
describe do
before :each do
@stream.stubs(:create)
FSEvents::Stream.stubs(:new).returns(@stream)
end
it 'should accept arguments and a block' do
lambda { FSEvents::Stream.create(@path, @other_path, :flags => 27) {} }.should_not raise_error(ArgumentError)
end
it 'should initialize a new stream object' do
FSEvents::Stream.expects(:new).returns(@stream)
FSEvents::Stream.create(@path, @other_path, :flags => 27) {}
end
it 'should pass the arguments to the initialization' do
FSEvents::Stream.expects(:new).with(@path, @other_path, :flags => 27).returns(@stream)
FSEvents::Stream.create(@path, @other_path, :flags => 27) {}
end
it 'should make the resultant stream object create a stream' do
@stream.expects(:create)
FSEvents::Stream.create(@path, @other_path, :flags => 27) {}
end
it 'should return the stream object' do
FSEvents::Stream.create.should == @stream
end
end
it 'should pass the callback block' do
callback = lambda {}
FSEvents::Stream.create(@path, @other_path, :flags => 27, &callback).callback.should == callback
end
end
it 'should schedule itself' do
@stream.should respond_to(:schedule)
end
describe 'when scheduling' do
before :each do
OSX.stubs(:FSEventStreamScheduleWithRunLoop)
end
it 'should schedule the stream' do
OSX.expects(:FSEventStreamScheduleWithRunLoop)
@stream.schedule
end
it 'should pass the stream' do
OSX.expects(:FSEventStreamScheduleWithRunLoop).with(@stream.stream, anything, anything)
@stream.schedule
end
it "should use the 'get current' run loop" do
OSX.expects(:CFRunLoopGetCurrent)
@stream.schedule
end
it "should pass the 'get current' run loop" do
# CFRunLoopGetCurrent returns a different value every time it's called, so it's like testing Time.now
get_current_run_loop = OSX.CFRunLoopGetCurrent
OSX.stubs(:CFRunLoopGetCurrent).returns(get_current_run_loop)
OSX.expects(:FSEventStreamScheduleWithRunLoop).with(anything, get_current_run_loop, anything)
@stream.schedule
end
it 'should use the default mode' do
OSX.expects(:FSEventStreamScheduleWithRunLoop).with(anything, anything, OSX::KCFRunLoopDefaultMode)
@stream.schedule
end
end
it 'should start itself' do
@stream.should respond_to(:start)
end
describe 'when starting' do
before :each do
OSX.stubs(:FSEventStreamStart).returns(true)
end
it 'should start the stream' do
OSX.expects(:FSEventStreamStart).with(@stream.stream).returns(true)
@stream.start
end
it 'should raise a StreamError exception if the stream could not be started' do
OSX.stubs(:FSEventStreamStart).returns(nil)
lambda { @stream.start }.should raise_error(FSEvents::Stream::StreamError)
end
it 'should not raise a StreamError exception if the stream could be started' do
lambda { @stream.start }.should_not raise_error(FSEvents::Stream::StreamError)
end
it 'should update its last event' do
@stream.expects(:update_last_event)
@stream.start
end
end
it 'should update its last event' do
@stream.should respond_to(:update_last_event)
end
describe 'updating its last event' do
it 'should store the last event time' do
now = Time.now
Time.stubs(:now).returns(now)
@stream.update_last_event
@stream.last_event.should == now
end
end
it 'should start up' do
@stream.should respond_to(:startup)
end
describe 'when starting up' do
before :each do
@stream.stubs(:schedule)
@stream.stubs(:start)
end
it 'should schedule' do
@stream.expects(:schedule)
@stream.startup
end
it 'should start' do
@stream.expects(:start)
@stream.startup
end
end
it 'should watch' do
FSEvents::Stream.should respond_to(:watch)
end
describe 'when watching' do
before :each do
@other_path = '/other/path'
end
# This is just here for organization and use of the before block.
# I'd like to ensure that the block is passed to create, but mocha expecation apparently doesn't support that.
# So instead I stub create for some testing and then have something that actually uses create and sees the callback
# is the expected block.
describe do
before :each do
@stream.stubs(:startup)
FSEvents::Stream.stubs(:create).returns(@stream)
end
it 'should accept arguments and a block' do
lambda { FSEvents::Stream.watch(@path, @other_path, :flags => 27) {} }.should_not raise_error(ArgumentError)
end
it 'should create a stream object' do
FSEvents::Stream.expects(:create).returns(@stream)
FSEvents::Stream.watch(@path, @other_path, :flags => 27) {}
end
it 'should pass the arguments to the creation' do
FSEvents::Stream.expects(:create).with(@path, @other_path, :flags => 27).returns(@stream)
FSEvents::Stream.watch(@path, @other_path, :flags => 27) {}
end
it 'should start up the resultant stream object' do
@stream.expects(:startup)
FSEvents::Stream.watch(@path, @other_path, :flags => 27) {}
end
it 'should return the stream object' do
FSEvents::Stream.watch.should == @stream
end
end
it 'should pass the callback block' do
callback = lambda {}
FSEvents::Stream.watch(@path, @other_path, :flags => 27, &callback).callback.should == callback
end
end
it 'should stop itself' do
@stream.should respond_to(:stop)
end
describe 'when stopping' do
it 'should stop the stream' do
OSX.expects(:FSEventStreamStop).with(@stream.stream)
@stream.stop
end
end
it 'should invalidate itself' do
@stream.should respond_to(:invalidate)
end
describe 'when invalidating' do
it 'should invalidate the stream' do
OSX.expects(:FSEventStreamInvalidate).with(@stream.stream)
@stream.invalidate
end
end
it 'should release itself' do
@stream.should respond_to(:release)
end
describe 'when releasing' do
before :each do
OSX.stubs(:FSEventStreamRelease)
end
it 'should release the stream' do
OSX.expects(:FSEventStreamRelease).with(@stream.stream)
@stream.release
end
it 'should clear the stream' do
@stream.release
@stream.stream.should be_nil
end
end
it 'should shut down' do
@stream.should respond_to(:shutdown)
end
describe 'when shutting down' do
before :each do
@stream.stubs(:stop)
@stream.stubs(:invalidate)
@stream.stubs(:release)
end
it 'should stop' do
@stream.expects(:stop)
@stream.shutdown
end
it 'should invalidate' do
@stream.expects(:invalidate)
@stream.shutdown
end
it 'should release' do
@stream.expects(:release)
@stream.shutdown
end
end
it 'should run' do
@stream.should respond_to(:run)
end
describe 'running' do
it 'should enter the run loop' do
OSX.expects(:CFRunLoopRun)
@stream.run
end
end
end
describe FSEvents::Stream::StreamError do
it 'should be a type of StandardError' do
FSEvents::Stream::StreamError.should < StandardError
end
end