Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

timer.rb tests passing

  • Loading branch information...
commit d15603c27946ec49db05450b0ff64b9a0154adeb 1 parent 2330efb
@tallakt authored
View
2  lib/machines/timedomain/analog_base.rb
@@ -139,7 +139,7 @@ def #{op}(other)
# it will also notify
def calc_and_notify(old_value)
new_val = yield
- notify_change if (new_val != old_val)
+ notify_change if (new_val != old_value)
new_val
end
end
View
14 lib/machines/timedomain/timer.rb
@@ -33,9 +33,13 @@ def elapsed
end
def start
- reset
- @start_time = Time.now
- do_wait
+ if EM::reactor_running?
+ reset
+ @start_time = Time.now
+ do_wait
+ else
+ EM::next_tick { start }
+ end
end
def reset
@@ -45,11 +49,11 @@ def reset
end
def active?
- @start_time
+ !!@start_time
end
def idle?
- not active?
+ !active?
end
private
View
103 spec/timer_spec.rb
@@ -4,89 +4,112 @@
include Machines::Timedomain
-describe Timer do
+describe Machines::Timedomain::Timer do
include EM::SpecHelper
before(:each) do
@t0 = Time.now
- @timer = Timer.new 1
+ end
+
+
+ def elapsed
+ #puts (Time.now - @t0).to_s + ' <-- elapsed'
+ Time.now - @t0
end
it 'should run for the desired time then stop' do
- em 4 do
- @timer.start
- @timer.on_finish do
- puts 'JUHUUU'
- raise 'test'
- (Time.now - @t0).should be_close(1.0, 0.010)
- raise 'test'
+ timer = Timer.new 0.1
+ em 1.0 do
+ timer.start
+ timer.on_finish do
+ elapsed.should be_close(0.1, 0.1)
+ done
end
end
end
it 'should report the elapsed time' do
- em 1.5 do
+ timer = Timer.new 1.0
+ timer.start
+ em 1.0 do
EM::add_timer 0.5 do
- @timer.elapsed.should be_close(0.5, 0.010)
+ timer.should be_active
+ timer.elapsed.should be_close(0.5, 0.1)
done
end
- @timer.start
end
end
it 'should report active and idle' do
- em 3.0 do
- EM::add_timer 0.5 do
- @timer.should_not be_active
- @timer.should be_idle
+ timer = Timer.new 0.2
+ em 1.0 do
+ EM::add_timer 0.1 do
+ timer.should_not be_active
+ timer.should be_idle
- EM::add_timer 0.5, proc { @timer.start }
+ timer.start
- EM::add_timer 0.5 do
- @timer.should be_active
- @timer.should_not be_idle
+ EM::add_timer 0.1 do
+ timer.should be_active
+ timer.should_not be_idle
- EM::add_timer 1.0 do
- @timer.should_not be_active
- @timer.should be_idle
+ EM::add_timer 0.2 do
+ timer.should_not be_active
+ timer.should be_idle
done
end
end
end
- @timer.start
end
end
it 'should not finish if beeing reset during timing' do
+ timer = Timer.new 0.5
timer.on_finish { fail }
- em 2.0 do
- EM::add_timer 0.5, proc { @timer.reset }
- EM::add_timer 1.5, proc { done }
- @timer.start
+ em 1.0 do
+ EM::add_timer 0.1, proc { timer.reset }
+ EM::add_timer 0.8, proc { done }
+ timer.start
end
end
it 'should accept changes in time while running' do
- time = Analog.new 1.0
- timer = Timer.new t
- times = [1.0, 3.0, 7.0]
+ param = Analog.new 0.4
+ timer = Timer.new param
+ times = [0.4, 1.0, 1.5]
- em 10.0 do
+ em 2.0 do
timer.on_finish do
- (Time.now - @t0).should be_close(time.shift, 0.01)
- if times.empty?
- done
- else
- timer.start
- end
+ elapsed.should be_close(times.shift, 0.1)
+ done if times.empty?
+ timer.start
end
- EM::add_timer 1.5, proc { time.v = 2.0 }
- EM::add_timer 3.5, proc { time.v = 4.0 }
+ EM::add_timer 0.6, proc { param.v = 0.6 }
+ EM::add_timer 1.2, proc { param.v = 0.5 }
timer.start
end
end
+
+ it 'should not start by itself' do
+ timer = Timer.new 0.1
+ em 1.0 do
+ EM::add_timer 0.5, proc { done }
+ timer.on_finish { fail }
+ end
+ end
+
+ it 'may be started outside the EM loop and inside' do
+ t0 = Timer.new 0.1
+ t1 = Timer.new 0.4
+ t0.start
+ em 1.0 do
+ t0.on_finish { elapsed.should be_close(0.1, 0.1) }
+ t1.on_finish { elapsed.should be_close(0.5, 0.2); done }
+ EM::add_timer(0.1) { t1.start }
+ end
+ end
end
Please sign in to comment.
Something went wrong with that request. Please try again.