Permalink
Browse files

Test specs for +Within

Specs for expected and unexpected raising of Timeout::Error.
  • Loading branch information...
Gurpartap committed Aug 7, 2012
1 parent ada2cd1 commit ee19306bac8f30341a373e36d1892d4009b31e0a
Showing with 43 additions and 0 deletions.
  1. +43 −0 spec/decorators/within_spec.rb
@@ -0,0 +1,43 @@
+require 'spec_helper'
+require 'method_decorators/decorators/within'
+
+describe Within do
+ let(:method) { double(:method, :call => false) }
+ subject { Within.new(2) }
+
+ describe "#call" do
+ it "raises when the timeout seconds have elapsed" do
+ method.stub(:call){ sleep 3 }
+ expect{ subject.call(method, nil) }.to raise_error(Timeout::Error)
+ end
+
+ it "does not raise when the method has finished execution before timeout" do
+ method.stub(:call){ sleep 1 }
+ expect{ subject.call(method, nil) }.to_not raise_error(Timeout::Error)
+ end
+ end
+
+ describe "acceptance" do
+ let(:klass) do
+ Class.new Base do
+ +Within.new(2)
+ def do_it(execution_period)
+ sleep(execution_period)
+ end
+ end
+ end
+ subject { klass.new }
+
+ context "with longer execution period" do
+ it "raises if the timeout period has elapsed" do
+ expect{ subject.do_it(3) }.to raise_error(Timeout::Error)
+ end
+ end
+
+ context "with shorter execution period" do
+ it "finishes within the timeout period" do
+ expect{ subject.do_it(1) }.to_not raise_error(Timeout::Error)
+ end
+ end
+ end
+end

0 comments on commit ee19306

Please sign in to comment.