Skip to content

Commit

Permalink
Added Connection#timeout=(seconds), Issue #61.
Browse files Browse the repository at this point in the history
  • Loading branch information
mvidner committed Apr 30, 2018
1 parent 4fdeff4 commit a47185b
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 18 deletions.
8 changes: 7 additions & 1 deletion lib/dbus/bus.rb
Original file line number Diff line number Diff line change
Expand Up @@ -193,7 +193,7 @@ def sub_inspect
class Connection
# The unique name (by specification) of the message.
attr_reader :unique_name
# pop and push messages here
# @return [MessageQueue] pop and push messages here
attr_reader :message_queue

# Create a new connection to the bus for a given connect _path_. _path_
Expand All @@ -212,6 +212,12 @@ def initialize(path)
@object_root = Node.new("/")
end

# @param t [Numeric] Timeout for synchronous calls,
# in seconds, fractions possible. DBus::Error is raised on a timeout.
def timeout=(t)
message_queue.timeout = t
end

# Dispatch all messages that are available in the queue,
# but do not block on the queue.
# Called by a main loop when something is available in the queue
Expand Down
11 changes: 9 additions & 2 deletions lib/dbus/message_queue.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,15 @@ class MessageQueue
# The socket that is used to connect with the bus.
attr_reader :socket

# @return [Numeric] Timeout for synchronous calls,
# in seconds, fractions possible.
attr_accessor :timeout

def initialize(address)
@address = address
@buffer = ""
@is_tcp = false
@timeout = 365 * 24 * 60 * 60 # Float::INFINITY causes Range Error
connect
end

Expand All @@ -32,10 +37,12 @@ def pop(non_block = false)
unless non_block
# we can block
while message.nil?
r, _d, _d = IO.select([@socket], [], [], @timeout)
if r && r[0] == @socket
read, _write, _error = IO.select([@socket], [], [], @timeout)
if read && read[0] == @socket
buffer_from_socket_nonblock
message = message_from_buffer_nonblock
else
raise DBus.error("org.freedesktop.DBus.Error.NoReply")
end
end
end
Expand Down
4 changes: 4 additions & 0 deletions spec/service_newapi.rb
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,10 @@ def initialize(path)
dbus_method :mirror_byte_array, "in bytes:ay, out mirrored:ay" do |bytes|
[bytes]
end

dbus_method :Sleep, "in duration:d" do |duration|
Kernel.sleep duration
end
end

# closing and reopening the same interface
Expand Down
18 changes: 3 additions & 15 deletions spec/timeout_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,6 @@
require "dbus"

describe "TimeoutTest" do
around(:each) do |example|
with_private_bus do
with_service_by_activation(&example)
end
end

before(:each) do
@bus = DBus::ASessionBus.new
@svc = @bus.service("org.ruby.service")
Expand All @@ -20,23 +14,17 @@
end

it "tests default (infinite) timeout" do
# WTF, sleep works, via foo.method(:sleep).call(1.0)
# even 1.method(:sleep).call(1.0) works.
expect { @base.sleep(1.0) }.to_not raise_error
expect { @base.Sleep(1.0) }.to_not raise_error
end

it "tests a sufficient timeout" do
@bus.timeout = 10.0 # seconds
expect { @base.sleep(1.0) }.to_not raise_error
expect { @base.Sleep(1.0) }.to_not raise_error
end

it "tests an insufficient timeout" do
@bus.timeout = 0.5 # seconds
expect { @base.sleep(1.0) }.to raise_error # FIXME a specific exception? which?
# "org.freedesktop.DBus.Error.NoReply"

#"org.freedesktop.DBus.Error.Timeout"
#"org.freedesktop.DBus.Error.TimedOut"
expect { @base.Sleep(1.0) }.to raise_error(DBus::Error)
end

end

0 comments on commit a47185b

Please sign in to comment.