-
Notifications
You must be signed in to change notification settings - Fork 245
/
loop_spec.rb
107 lines (84 loc) · 2.61 KB
/
loop_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
require 'thread'
require 'listen/event/config'
require 'listen/event/loop'
RSpec.describe Listen::Event::Loop do
let(:config) { instance_double(Listen::Event::Config, 'config') }
let(:processor) { instance_double(Listen::Event::Processor, 'processor') }
let(:thread) { instance_double(Thread) }
let(:reasons) { instance_double(::Queue, 'reasons') }
let(:ready) { instance_double(::Queue, 'ready') }
let(:blocks) do
{
thread_block: proc { fail 'thread block stub called' },
}
end
subject { described_class.new(config) }
# TODO: this is hideous
before do
allow(::Queue).to receive(:new).and_return(reasons, ready)
allow(Listen::Event::Processor).to receive(:new).with(config, reasons).
and_return(processor)
allow(Thread).to receive(:new) do |*args, &block|
fail 'Unstubbed call:'\
" Thread.new(#{args.map(&:inspect) * ','},&#{block.inspect})"
end
allow(config).to receive(:min_delay_between_events).and_return(1.234)
allow(Thread).to receive(:new) do |*_, &block|
blocks[:thread_block] = block
thread
end
allow(Kernel).to receive(:sleep) do |*args|
fail "stub called: sleep(#{args.map(&:inspect) * ','})"
end
allow(subject).to receive(:_nice_error) do |ex|
indent = "\n -- "
backtrace = ex.backtrace.reject { |line| line =~ %r{\/gems\/} }
fail "error called: #{ex}: #{indent}#{backtrace * indent}"
end
end
describe '#start' do
before do
expect(Thread).to receive(:new) do |&block|
block.call
thread
end
expect(processor).to receive(:loop_for).with(1.234)
subject.start
end
it 'is started' do
expect(subject).to be_started
end
context 'when start is called again' do
it 'returns silently' do
expect { subject.start }.to_not raise_exception
end
end
end
context 'when set up / started' do
before do
allow(thread).to receive(:alive?).and_return(true)
allow(config).to receive(:min_delay_between_events).and_return(1.234)
allow(processor).to receive(:loop_for).with(1.234)
expect(Thread).to receive(:new) do |&block|
block.call
thread
end
subject.start
end
describe '#stop' do
before do
allow(thread).to receive_message_chain(:join, :kill)
end
it 'frees the thread' do
subject.stop
end
it 'waits for the thread to finish' do
expect(thread).to receive_message_chain(:join, :kill)
subject.stop
end
it 'sets the reason for waking up' do
subject.stop
end
end
end
end