Skip to content

Commit

Permalink
Merge 86d636e into 25ac90d
Browse files Browse the repository at this point in the history
  • Loading branch information
e2 committed Mar 18, 2016
2 parents 25ac90d + 86d636e commit d0726af
Show file tree
Hide file tree
Showing 5 changed files with 92 additions and 3 deletions.
2 changes: 2 additions & 0 deletions Gemfile
Expand Up @@ -7,10 +7,12 @@ group :development do
gem "guard-rspec", require: false
gem "guard-rubocop", require: false
gem "hound-tools", '~> 0.0.4', require: false
gem 'timecop'
end

group :test do
gem "coveralls", require: false
gem "rake", require: false
gem 'rspec', '~> 3.1'
gem 'timecop'
end
3 changes: 2 additions & 1 deletion lib/guard/livereload.rb
Expand Up @@ -5,6 +5,7 @@ class LiveReload < Plugin
require 'guard/livereload/websocket'
require 'guard/livereload/reactor'
require 'guard/livereload/snippet'
require 'guard/livereload/guaranteed_sleep'

attr_accessor :reactor, :options

Expand Down Expand Up @@ -37,7 +38,7 @@ def stop
end

def run_on_modifications(paths)
sleep options[:grace_period]
GuaranteedSleep.new.sleep(options[:grace_period])
reactor.reload_browser(paths)
end
end
Expand Down
14 changes: 14 additions & 0 deletions lib/guard/livereload/guaranteed_sleep.rb
@@ -0,0 +1,14 @@
require 'guard/compat/plugin'

module Guard
class LiveReload < Plugin
class GuaranteedSleep
def sleep(seconds)
deadline = Time.now + seconds
while (remaining = deadline - Time.now) > 0
Kernel.sleep(remaining)
end
end
end
end
end
64 changes: 64 additions & 0 deletions spec/lib/guard/livereload/guaranteed_sleep_spec.rb
@@ -0,0 +1,64 @@
require 'guard/compat/test/helper'

require 'timecop'

RSpec.describe Guard::LiveReload::GuaranteedSleep do
let(:start_time) { Time.local(2006, 3, 18, 2, 0) }

let(:implementation) { Kernel }

around do |example|
Timecop.freeze(start_time) do
example.run
end
end

describe '.sleep' do
before do
@interruptions = interruptions
allow(implementation).to receive(:sleep) do |time|
t = (@interruptions > 0 ? (time.to_f / 2) : time)
@interruptions -= 1
Timecop.travel(Time.now + t)
t
end
end

context 'with 0' do
let(:interruptions) { 0 }

it 'never sleeps' do
subject.sleep(0)
expect(implementation).to_not receive(:sleep)
expect(Time.now).to be_within(0.05).of(start_time)
end
end

context 'when never interrupted' do
let(:interruptions) { 0 }

it 'waits before reloading the browser' do
subject.sleep(5)
expect(Time.now).to be_within(0.1).of(start_time + 5)
end
end

context 'when interrupted once' do
let(:interruptions) { 1 }

it 'waits before reloading the browser' do
subject.sleep(5)
expect(Time.now).to be_within(0.1).of(start_time + 5)
end
end

context 'when interrupted many times' do
let(:interruptions) { 3 }

it 'waits before reloading the browser' do
subject.sleep(5)
expect(Time.now).to be_within(0.1).of(start_time + 5)
end
end
end
end
12 changes: 10 additions & 2 deletions spec/lib/guard/livereload_spec.rb
Expand Up @@ -153,14 +153,22 @@
end

describe '#run_on_modifications' do
let(:sleeper) { instance_double(Guard::LiveReload::GuaranteedSleep) }

before do
allow(reactor).to receive(:reload_browser)
allow(Guard::LiveReload::GuaranteedSleep).to receive(:new).and_return(sleeper)
allow(sleeper).to receive(:sleep)
end

it 'reloads browser' do
expect(reactor).to receive(:reload_browser).with(['foo'])
plugin.run_on_modifications(['foo'])
end

it 'can wait 0.5 seconds before reloading the browser' do
it 'waits given time before reloading the browser' do
expect(reactor).to receive(:reload_browser).with(['foo'])
expect(plugin).to receive(:sleep).with(0.5)
allow(sleeper).to receive(:sleep).with(0.5)
plugin.options[:grace_period] = 0.5
plugin.run_on_modifications(['foo'])
end
Expand Down

0 comments on commit d0726af

Please sign in to comment.