Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

WIP: Use NIO.2 WatchService for JRuby #463

Closed
wants to merge 6 commits into from
Closed
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 2 additions & 20 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,24 +3,6 @@ cache: bundler

matrix:
include:
- rvm: 2.3
- rvm: 2.4
- rvm: 2.5
- rvm: 2.6
- rvm: 2.5
os: osx
env:
# TODO: 0.8 is enough on Linux, but 2 seems needed for Travis/OSX
- LISTEN_TESTS_DEFAULT_LAG=2
- rvm: jruby
- rvm: truffleruby
- rvm: jruby-head
- rvm: ruby-head
- rvm: rbx-3
allow_failures:
- rvm: truffleruby
- rvm: jruby
- rvm: ruby-head
- rvm: jruby-head
- rvm: rbx-3
fast_finish: true
env:
- TEST_LISTEN_ADAPTER_MODES=native
9 changes: 7 additions & 2 deletions lib/listen/adapter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,11 @@
require 'listen/adapter/linux'
require 'listen/adapter/polling'
require 'listen/adapter/windows'
require 'listen/adapter/jruby'

module Listen
module Adapter
OPTIMIZED_ADAPTERS = [Darwin, Linux, BSD, Windows].freeze
OPTIMIZED_ADAPTERS = [Jruby, Darwin, Linux, BSD, Windows].freeze
POLLING_FALLBACK_MESSAGE = 'Listen will be polling for changes.'\
'Learn more at https://github.com/guard/listen#listen-adapters.'.freeze

Expand All @@ -16,7 +17,11 @@ def select(options = {})
_log :debug, 'Adapter: considering polling ...'
return Polling if options[:force_polling]
_log :debug, 'Adapter: considering optimized backend...'
return _usable_adapter_class if _usable_adapter_class
_log :debug, "Adapter: RUBY_ENGINE=#{RUBY_ENGINE}"
if _usable_adapter_class
_log :debug, "Adapter: using #{_usable_adapter_class}"
return _usable_adapter_class
end
_log :debug, 'Adapter: falling back to polling...'
_warn_polling_fallback(options)
Polling
Expand Down
57 changes: 57 additions & 0 deletions lib/listen/adapter/jruby.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
module Listen
module Adapter
# @see https://docs.oracle.com/javase/tutorial/essential/io/notification.html
class Jruby < Base
def self.usable?
RUBY_ENGINE == 'jruby'
end

private

def _configure(directory, &_callback)
require 'java'
java_import 'java.nio.file.FileSystems'
java_import 'java.nio.file.Paths'
java_import 'java.nio.file.StandardWatchEventKinds'

event_kinds = [
StandardWatchEventKinds::ENTRY_CREATE,
StandardWatchEventKinds::ENTRY_MODIFY,
StandardWatchEventKinds::ENTRY_DELETE
]

@watcher ||= FileSystems.getDefault.newWatchService
p @watcher.class.name
@keys ||= {}
path = Paths.get(directory.to_s)
key = path.register(@watcher, *event_kinds)
@keys[key] = path
end

def _run
loop do

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Metrics/BlockLength: Block has too many lines. [26/25]

key = @watcher.take
dir = @keys[key]
unless dir.nil?
key.pollEvents.each do |event|
kind = event.kind
next if kind == StandardWatchEventKinds::OVERFLOW
name = event.context
child = dir.resolve(name)
p child.to_s

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Layout/IndentationConsistency: Inconsistent indentation detected.

pathname = Pathname.new(child.to_s).dirname
_queue_change(:dir, pathname, '.', recursive: true)
end
end
valid = key.reset
unless valid
@keys.delete(key)
break if @keys.empty?
end
end
end

def _process_event(dir, event); end
end
end
end
11 changes: 11 additions & 0 deletions spec/lib/listen/adapter/jruby_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
RSpec.describe Listen::Adapter::Jruby do
describe 'class' do
subject { described_class }

if jruby?
it { should be_usable }
else
it { should_not be_usable }
end
end
end
7 changes: 7 additions & 0 deletions spec/lib/listen/adapter_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
allow(Listen::Adapter::Darwin).to receive(:usable?) { false }
allow(Listen::Adapter::Linux).to receive(:usable?) { false }
allow(Listen::Adapter::Windows).to receive(:usable?) { false }
allow(Listen::Adapter::Jruby).to receive(:usable?) { false }
end

describe '.select' do
Expand Down Expand Up @@ -37,6 +38,12 @@
expect(klass).to eq Listen::Adapter::Windows
end

it 'returns JRuby adapter when usable' do
allow(Listen::Adapter::Jruby).to receive(:usable?) { true }
klass = Listen::Adapter.select
expect(klass).to eq Listen::Adapter::Jruby
end

context 'no usable adapters' do
before { allow(Kernel).to receive(:warn) }

Expand Down
4 changes: 4 additions & 0 deletions spec/support/platform_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,7 @@ def bsd?
def windows?
RbConfig::CONFIG['target_os'] =~ /mswin|mingw|cygwin/i
end

def jruby?
RUBY_ENGINE == 'jruby'
end