From 4111b1cee87ce8857558900b8df76a5bd6c3e985 Mon Sep 17 00:00:00 2001 From: Joao Fernandes Date: Mon, 4 Jan 2021 18:38:38 +0000 Subject: [PATCH] Allow pure Ruby and Java NIO::Selector to be initialized with nil Trying to make this consistent across all backends. Passing nil was only working with libev. --- ext/nio4r/org/nio4r/Selector.java | 2 +- lib/nio/selector.rb | 2 +- spec/nio/selector_spec.rb | 4 ++++ spec/spec_helper.rb | 2 ++ 4 files changed, 8 insertions(+), 2 deletions(-) diff --git a/ext/nio4r/org/nio4r/Selector.java b/ext/nio4r/org/nio4r/Selector.java index 14bfb62..016ec35 100644 --- a/ext/nio4r/org/nio4r/Selector.java +++ b/ext/nio4r/org/nio4r/Selector.java @@ -43,7 +43,7 @@ public class Selector extends RubyObject { @JRubyMethod public IRubyObject initialize(ThreadContext context, IRubyObject backend) { - if(backend != context.runtime.newSymbol("java")) { + if(backend != context.runtime.newSymbol("java") && !backend.isNil()) { throw context.runtime.newArgumentError(":java is the only supported backend"); } diff --git a/lib/nio/selector.rb b/lib/nio/selector.rb index 0e55088..b18031c 100644 --- a/lib/nio/selector.rb +++ b/lib/nio/selector.rb @@ -14,7 +14,7 @@ module NIO # Create a new NIO::Selector def initialize(backend = :ruby) - raise ArgumentError, "unsupported backend: #{backend}" unless backend == :ruby + raise ArgumentError, "unsupported backend: #{backend}" unless [:ruby, nil].include?(backend) @selectables = {} @lock = Mutex.new diff --git a/spec/nio/selector_spec.rb b/spec/nio/selector_spec.rb index 1211571..3060f75 100644 --- a/spec/nio/selector_spec.rb +++ b/spec/nio/selector_spec.rb @@ -24,6 +24,10 @@ RSpec.describe NIO::Selector do example.reporter.message "Supported backends: #{described_class.backends}" end + it "automatically selects a backend if none or nil is specified" do + expect(described_class.new.backend).to eq described_class.new(nil).backend + end + it "raises ArgumentError if given an invalid backend" do expect { described_class.new(:derp) }.to raise_error ArgumentError end diff --git a/spec/spec_helper.rb b/spec/spec_helper.rb index b9742d2..6403bd8 100644 --- a/spec/spec_helper.rb +++ b/spec/spec_helper.rb @@ -12,6 +12,8 @@ RSpec.configure do |config| # Enable flags like --only-failures and --next-failure config.example_status_persistence_file_path = ".rspec_status" + config.filter_run_when_matching :focus + config.expect_with :rspec do |c| c.syntax = :expect end