Browse files

Fixed all tests, major bug in Scheduler discovered

  • Loading branch information...
1 parent e699dcf commit 46a7674302843c08b40ef59718e2487dca8b7bf2 Tallak Tveide committed Mar 12, 2009
View
1 .gitignore
@@ -1,3 +1,4 @@
*.swp
secret/
tags
+*.swo
View
5 lib/ruby-plc/timedomain/discrete.rb
@@ -17,10 +17,10 @@ def v=(new_val)
old_v = @v
if new_val
@v = true
- data_change v unless old_v
+ data_change @v unless old_v
else
@v = false
- data_change v if old_v
+ data_change @v if old_v
end
end
@@ -31,7 +31,6 @@ def data_change(value)
notify_fe unless value
notify_change
end
-
end
end
end
View
4 lib/ruby-plc/timedomain/discrete_base.rb
@@ -26,9 +26,9 @@ def ton(time)
result = Discrete.new
on_fe do
timer.reset
- result.value = false
+ result.v = false
end
- timer.at_end { result.value = true }
+ timer.at_end { result.v = true }
result
end
end
View
2 lib/ruby-plc/timedomain/scheduler.rb
@@ -12,7 +12,7 @@ class Entry
@@current = nil
def initialize
- @scheduled = RBTree.new
+ @scheduled = MultiRBTree.new
@scheduled.extend(MonitorMixin)
@wait_cond = @scheduled.new_cond
@running = false
View
9 spec/discrete_spec.rb
@@ -4,6 +4,7 @@
include RubyPlc::TimeDomain
+
describe 'Discrete signals' do
before(:each) do
@a = Discrete.new
@@ -136,17 +137,11 @@ def now
end
it 'should support timed on returning a signal' do
- # This is what I know this far: When ever the ton
- # method is called on the double_pulse signal, two
- # of the change callbacks disappear from the original
- # signal
times = []
- @double_pulse.on_change { puts 'change: ' + now.to_s }
@double_pulse.ton(1).should be_a DiscreteBase
@double_pulse.ton(0.5).on_change { times << now }
@double_pulse.ton(2).on_change { fail }
-
- Scheduler.current.run_for 10
+ Scheduler.current.run_for 20000
check_time_offsets(times, [1.5, 2, 4.5, 5]);
end
View
8 spec/notify_spec.rb
@@ -83,6 +83,14 @@ class NotifyTestTwo
@test.should be_true
end
+ it "should not misbehave if an exception is thrown by a listener, even errors" do
+ @test = nil
+ @one.on_one { @test.call_undefined_method }
+ @one.on_one { @test = true }
+ lambda { @one.notify_one }.should_not raise_error(RuntimeError, /Boom/)
+ @test.should be_true
+ end
+
it "should call handle_notify_error if available" do
@one_handler.on_one { throw 'BOOM' }
lambda { @one_handler.notify_one }.should raise_error(Exception, /Notified/)
View
26 spec/scheduler_nowait_spec.rb
@@ -75,13 +75,11 @@ def perform_foo(arr, foo)
it 'should schedule tasks correctly that were scheduled during runtime' do
arr = []
- (1..10).each do |i|
- Scheduler.current.wait 2 do
- perform_foo arr, :foo
- end
- Scheduler.current.wait 1 do
- perform_foo arr, :bar
- end
+ Scheduler.current.wait 2 do
+ perform_foo arr, :foo
+ end
+ Scheduler.current.wait 1 do
+ perform_foo arr, :bar
end
Scheduler.current.run_for 6.5
arr.should eql([:bar, :foo] * 3)
@@ -97,6 +95,20 @@ def perform_foo(arr, foo)
it 'should return a Time object from the now function' do
Scheduler.current.now.should be_a(Time)
end
+
+ it 'should respond to many different events at the same time' do
+ t = Scheduler.current.now + 0.5
+ count = 0
+ (1..100).each do
+ Scheduler.current.wait_until(t) { count += 1 }
+ end
+ Scheduler.current.wait_until t do
+ count += 2
+ count -= 1
+ end
+ Scheduler.current.run_for 1
+ count.should == 101
+ end
end

0 comments on commit 46a7674

Please sign in to comment.