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

Allow Monitor#interests to be nil #183

Merged
merged 6 commits into from
Mar 16, 2018
Merged
Show file tree
Hide file tree
Changes from all 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
16 changes: 11 additions & 5 deletions ext/nio4r/monitor.c
Original file line number Diff line number Diff line change
Expand Up @@ -163,7 +163,11 @@ static VALUE NIO_Monitor_interests(VALUE self)

static VALUE NIO_Monitor_set_interests(VALUE self, VALUE interests)
{
NIO_Monitor_update_interests(self, NIO_Monitor_symbol2interest(interests));
if(NIL_P(interests)) {
NIO_Monitor_update_interests(self, 0);
} else {
NIO_Monitor_update_interests(self, NIO_Monitor_symbol2interest(interests));
}

return rb_ivar_get(self, rb_intern("interests"));
}
Expand Down Expand Up @@ -292,9 +296,11 @@ static void NIO_Monitor_update_interests(VALUE self, int interests)
rb_ivar_set(self, rb_intern("interests"), Qnil);
}

monitor->interests = interests;
if(monitor->interests != interests) {
monitor->interests = interests;

ev_io_stop(monitor->selector->ev_loop, &monitor->ev_io);
ev_io_set(&monitor->ev_io, monitor->ev_io.fd, monitor->interests);
ev_io_start(monitor->selector->ev_loop, &monitor->ev_io);
ev_io_stop(monitor->selector->ev_loop, &monitor->ev_io);
ev_io_set(&monitor->ev_io, monitor->ev_io.fd, monitor->interests);
ev_io_start(monitor->selector->ev_loop, &monitor->ev_io);
}
}
7 changes: 6 additions & 1 deletion ext/nio4r/org/nio4r/Monitor.java
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,12 @@ public IRubyObject setInterests(ThreadContext context, IRubyObject interests) {
Ruby ruby = context.getRuntime();
SelectableChannel channel = (SelectableChannel)io.getChannel();

key.interestOps(Nio4r.symbolToInterestOps(ruby, channel, interests));
if(interests != context.nil) {
key.interestOps(Nio4r.symbolToInterestOps(ruby, channel, interests));
} else {
key.interestOps(0);
}

this.interests = interests;

return this.interests;
Expand Down
4 changes: 2 additions & 2 deletions lib/nio/monitor.rb
Original file line number Diff line number Diff line change
Expand Up @@ -26,12 +26,12 @@ def initialize(io, interests, selector)

# Replace the existing interest set with a new one
#
# @param interests [:r, :w, :rw] I/O readiness we're interested in (read/write/readwrite)
# @param interests [:r, :w, :rw, nil] I/O readiness we're interested in (read/write/readwrite)
#
# @return [Symbol] new interests
def interests=(interests)
raise EOFError, "monitor is closed" if closed?
raise ArgumentError, "bad interests: #{interests}" unless %i[r w rw].include?(interests)
raise ArgumentError, "bad interests: #{interests}" unless [:r, :w, :rw, nil].include?(interests)

@interests = interests
end
Expand Down
6 changes: 6 additions & 0 deletions spec/nio/monitor_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,12 @@
end

describe "#interests=" do
it "can set interests to nil" do
expect(monitor.interests).not_to eq(nil)
monitor.interests = nil
expect(monitor.interests).to eq(nil)
end

it "changes the interest set" do
expect(monitor.interests).not_to eq(:w)
monitor.interests = :w
Expand Down