Skip to content

Commit

Permalink
Timers#after converts argument to float. (#109)
Browse files Browse the repository at this point in the history
  • Loading branch information
ioquatix authored Jun 13, 2024
1 parent 3f298fa commit b3b55bb
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 4 deletions.
1 change: 0 additions & 1 deletion lib/io/event/selector/select.rb
Original file line number Diff line number Diff line change
Expand Up @@ -421,7 +421,6 @@ def select(duration = nil)
error = nil

if duration
duration = duration/1.0
if duration > 0
start_time = Process.clock_gettime(Process::CLOCK_MONOTONIC)
end
Expand Down
7 changes: 4 additions & 3 deletions lib/io/event/timers.rb
Original file line number Diff line number Diff line change
Expand Up @@ -50,18 +50,19 @@ def size
end

# Schedule a block to be called at a specific time in the future.
# @parameter time [Time] The time at which the block should be called, relative to {#now}.
# @parameter time [Float] The time at which the block should be called, relative to {#now}.
def schedule(time, block)
handle = Handle.new(time, block)

@scheduled << handle

return handle
end

# Schedule a block to be called after a specific time offset, relative to the current time as returned by {#now}.
# @parameter offset [Time] The time offset from the current time at which the block should be called.
# @parameter offset [#to_f] The time offset from the current time at which the block should be called.
def after(offset, &block)
schedule(self.now + offset, block)
schedule(self.now + offset.to_f, block)
end

def wait_interval(now = self.now)
Expand Down
30 changes: 30 additions & 0 deletions test/io/event/timers.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,16 @@

require 'io/event/timers'

class FloatWrapper
def initialize(value)
@value = value
end

def to_f
@value
end
end

describe IO::Event::Timers do
let(:timers) {subject.new}

Expand Down Expand Up @@ -67,6 +77,26 @@
expect(timers.size).to be == 0
end

with '#schedule' do
it "raises an error if given an invalid time" do
expect do
timers.after(Object.new) {}
end.to raise_exception(NoMethodError, message: be =~ /to_f/)
end

it "converts the offset to a float" do
fired = false

timers.after(FloatWrapper.new(0.1)) do
fired = true
end

timers.fire(timers.now + 0.15)

expect(fired).to be == true
end
end

with '#wait_interval' do
it "should return nil if no timers are scheduled" do
expect(timers.wait_interval).to be_nil
Expand Down

0 comments on commit b3b55bb

Please sign in to comment.