Skip to content

Commit

Permalink
issue #510: fix tests to stub MonotonicTime
Browse files Browse the repository at this point in the history
  • Loading branch information
ColinDKelley committed Dec 31, 2020
1 parent c2cce26 commit e2a20e2
Showing 1 changed file with 14 additions and 14 deletions.
28 changes: 14 additions & 14 deletions spec/lib/listen/event/processor_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
end

let(:state) do
{ time: 0.0 }
{ monotonic_time: 0.0 }
end

def status_for_time(time)
Expand All @@ -32,15 +32,15 @@ def status_for_time(time)
allow(config).to receive(:event_queue).and_return(event_queue)

allow(listener).to receive(:stopped?) do
status_for_time(state[:time]) == :stopped
status_for_time(state[:monotonic_time]) == :stopped
end

allow(listener).to receive(:paused?) do
status_for_time(state[:time]) == :paused
status_for_time(state[:monotonic_time]) == :paused
end

allow(Time).to receive(:now) do
state[:time]
allow(Listen::MonotonicTime).to receive(:now) do
state[:monotonic_time]
end
end

Expand Down Expand Up @@ -91,14 +91,14 @@ def status_for_time(time)
end

it 'sleeps, waiting to be woken up' do
expect(config).to receive(:sleep).once { state[:time] = 0.6 }
expect(config).to receive(:sleep).once { state[:monotonic_time] = 0.6 }
expect(event_queue).to receive(:pop).and_return(event)
subject.loop_for(1)
end

it 'breaks' do
expect(event_queue).to receive(:pop).and_return(event)
allow(config).to receive(:sleep).once { state[:time] = 0.6 }
allow(config).to receive(:sleep).once { state[:monotonic_time] = 0.6 }
expect(config).to_not receive(:call)
subject.loop_for(1)
end
Expand All @@ -113,11 +113,11 @@ def status_for_time(time)
it 'sleeps for latency to possibly later optimize some events' do
# pretend we were woken up at 0.6 seconds since start
allow(config).to receive(:sleep).
with(anything) { |*_args| state[:time] += 0.6 }
with(anything) { |*_args| state[:monotonic_time] += 0.6 }

# pretend we slept for latency (now: 1.6 seconds since start)
allow(config).to receive(:sleep).
with(1.0) { |*_args| state[:time] += 1.0 }
with(1.0) { |*_args| state[:monotonic_time] += 1.0 }

expect(event_queue).to receive(:pop).and_return(event)
subject.loop_for(1)
Expand All @@ -132,15 +132,15 @@ def status_for_time(time)
it 'still does not process events because it is paused' do
# pretend we were woken up at 0.6 seconds since start
allow(config).to receive(:sleep).
with(anything) { |*_args| state[:time] += 2.0 }
with(anything) { |*_args| state[:monotonic_time] += 2.0 }

# second loop starts here (no sleep, coz recent events, but no
# processing coz paused

# pretend we were woken up at 3.6 seconds since start
allow(listener).to receive(:wait_for_state).
with(:initializing, :backend_started, :processing_events, :stopped) do |*_args|
state[:time] += 3.0
state[:monotonic_time] += 3.0
raise ScriptError, 'done'
end

Expand Down Expand Up @@ -169,14 +169,14 @@ def status_for_time(time)
it 'sleeps, waiting to be woken up' do
expect(event_queue).to receive(:pop).and_return(event)
expect(config).to receive(:sleep).
once { |*_args| state[:time] = 0.6 }
once { |*_args| state[:monotonic_time] = 0.6 }

subject.loop_for(1)
end

it 'breaks' do
allow(config).to receive(:sleep).
once { |*_args| state[:time] = 0.6 }
once { |*_args| state[:monotonic_time] = 0.6 }

expect(config).to_not receive(:call)
expect(event_queue).to receive(:pop).and_return(event)
Expand Down Expand Up @@ -207,7 +207,7 @@ def status_for_time(time)

final_changes = [['foo'], [], []]
allow(config).to receive(:call) do |*changes|
state[:time] = 4.0 # stopped
state[:monotonic_time] = 4.0 # stopped
expect(changes).to eq(final_changes)
end

Expand Down

0 comments on commit e2a20e2

Please sign in to comment.