mirror of
https://github.com/fog/fog.git
synced 2022-11-09 13:51:43 -05:00
simplify eventual stuff
This commit is contained in:
parent
b010bbf38b
commit
922966482a
9 changed files with 50 additions and 96 deletions
|
@ -7,7 +7,10 @@ describe 'S3.delete_bucket' do
|
|||
end
|
||||
|
||||
it 'should include fogdeletebucket in get_service before delete_bucket' do
|
||||
lambda { s3.get_service }.should eventually { |expected| expected.body[:buckets].collect { |bucket| bucket[:name] }.should include('fogdeletebucket') }
|
||||
eventually do
|
||||
actual = s3.get_service
|
||||
actual.body[:buckets].collect { |bucket| bucket[:name] }.should include('fogdeletebucket')
|
||||
end
|
||||
end
|
||||
|
||||
it 'should return proper attributes' do
|
||||
|
@ -16,7 +19,10 @@ describe 'S3.delete_bucket' do
|
|||
end
|
||||
|
||||
it 'should not include fogdeletebucket in get_service after delete_bucket' do
|
||||
lambda { s3.get_service }.should_not eventually { |expected| expected.body[:buckets].collect { |bucket| bucket[:name] }.should_not include('fogdeletebucket') }
|
||||
eventually do
|
||||
actual = s3.get_service
|
||||
expected.body[:buckets].collect { |bucket| bucket[:name] }.should_not include('fogdeletebucket')
|
||||
end
|
||||
end
|
||||
|
||||
end
|
||||
|
|
|
@ -22,7 +22,10 @@ describe 'S3.get_service' do
|
|||
end
|
||||
|
||||
it 'should include foggetservice in get_service' do
|
||||
lambda { s3.get_service }.should eventually { |expected| expected.body[:buckets].collect { |bucket| bucket[:name] }.should include('foggetservice') }
|
||||
eventually do
|
||||
actual = s3.get_service
|
||||
actual.body[:buckets].collect { |bucket| bucket[:name] }.should include('foggetservice')
|
||||
end
|
||||
end
|
||||
|
||||
end
|
||||
|
|
|
@ -7,7 +7,10 @@ describe 'S3.put_bucket' do
|
|||
end
|
||||
|
||||
it 'should not include fogputbucket in get_service buckets before put_bucket' do
|
||||
lambda { s3.get_service }.should_not eventually { |expected| expected.body[:buckets].collect { |bucket| bucket[:name] }.should_not include('fogputbucket') }
|
||||
eventually do
|
||||
actual = s3.get_service
|
||||
actual.body[:buckets].collect { |bucket| bucket[:name] }.should_not include('fogputbucket')
|
||||
end
|
||||
end
|
||||
|
||||
it 'should return proper attributes' do
|
||||
|
@ -16,7 +19,10 @@ describe 'S3.put_bucket' do
|
|||
end
|
||||
|
||||
it 'should include fogputbucket in get_service buckets after put_bucket' do
|
||||
lambda { s3.get_service }.should eventually { |expected| expected.body[:buckets].collect { |bucket| bucket[:name] }.should include('fogputbucket') }
|
||||
eventually do
|
||||
actual = s3.get_service
|
||||
expected.body[:buckets].collect { |bucket| bucket[:name] }.should include('fogputbucket')
|
||||
end
|
||||
end
|
||||
|
||||
end
|
||||
|
|
|
@ -12,7 +12,10 @@ describe 'SimpleDB.delete_attributes' do
|
|||
end
|
||||
|
||||
it 'should have attributes for foo before delete_attributes' do
|
||||
lambda { sdb.get_attributes('delete_attributes', 'foo') }.should eventually { |expected| expected.body[:attributes].should == { 'bar' => ['baz'] } }
|
||||
eventually do
|
||||
actual = sdb.get_attributes('delete_attributes', 'foo')
|
||||
actual.body[:attributes].should == { 'bar' => ['baz'] }
|
||||
end
|
||||
end
|
||||
|
||||
it 'should return proper attributes from delete_attributes' do
|
||||
|
@ -22,7 +25,10 @@ describe 'SimpleDB.delete_attributes' do
|
|||
end
|
||||
|
||||
it 'should have no attributes for foo after delete_attributes' do
|
||||
lambda { sdb.get_attributes('delete_attributes', 'foo') }.should eventually { |expected| expected.body[:attributes].should be_empty }
|
||||
eventually do
|
||||
actual = sdb.get_attributes('delete_attributes', 'foo')
|
||||
actual.body[:attributes].should be_empty
|
||||
end
|
||||
end
|
||||
|
||||
end
|
||||
|
|
|
@ -12,12 +12,18 @@ describe 'SimpleDB.get_attributes' do
|
|||
end
|
||||
|
||||
it 'should have no attributes for foo before put_attributes' do
|
||||
lambda { sdb.get_attributes(@domain_name, 'foo') }.should eventually { |expected| expected.body[:attributes].should be_empty }
|
||||
eventually do
|
||||
actual = sdb.get_attributes(@domain_name, 'foo')
|
||||
actual.body[:attributes].should be_empty
|
||||
end
|
||||
end
|
||||
|
||||
it 'should have attributes for foo after put_attributes' do
|
||||
sdb.put_attributes(@domain_name, 'foo', { :bar => :baz })
|
||||
lambda { sdb.get_attributes(@domain_name, 'foo') }.should eventually { |expected| expected.body[:attributes].should == { 'bar' => ['baz'] } }
|
||||
eventually do
|
||||
actual = sdb.get_attributes(@domain_name, 'foo')
|
||||
actual.body[:attributes].should == { 'bar' => ['baz'] }
|
||||
end
|
||||
end
|
||||
|
||||
end
|
||||
|
|
|
@ -19,7 +19,10 @@ describe 'SimpleDB.list_domains' do
|
|||
|
||||
it 'should include created domains' do
|
||||
sdb.create_domain(@domain_name)
|
||||
lambda { sdb.list_domains }.should eventually { |expected| expected.body[:domains].should include(@domain_name) }
|
||||
eventually do
|
||||
actual = sdb.list_domains
|
||||
actual.body[:domains].should include(@domain_name)
|
||||
end
|
||||
end
|
||||
|
||||
end
|
||||
|
|
|
@ -1,36 +0,0 @@
|
|||
module Spec
|
||||
module Matchers
|
||||
class Eventually #:nodoc:
|
||||
def initialize(&block)
|
||||
@block = block
|
||||
end
|
||||
|
||||
def matches?(given_proc)
|
||||
match = nil
|
||||
[0,2,4,8,16].each do |delay|
|
||||
begin
|
||||
sleep(delay)
|
||||
match = @block[given_proc.call]
|
||||
break
|
||||
rescue Spec::Expectations::ExpectationNotMetError => error
|
||||
raise error if delay == 16
|
||||
end
|
||||
end
|
||||
match
|
||||
end
|
||||
end
|
||||
|
||||
# :call-seq
|
||||
# should eventually() { |expected| ... }
|
||||
# Matches if block matches within 30 seconds
|
||||
#
|
||||
# == Examples
|
||||
#
|
||||
# lambda { do_something_eventually_returning_true }.should eventually {|expected| expected.should be_true }
|
||||
#
|
||||
# lambda { do_something_eventually_returning_false }.should_not eventually {|expected| expected.should_not be_true }
|
||||
def eventually(&block)
|
||||
Matchers::Eventually.new(&block)
|
||||
end
|
||||
end
|
||||
end
|
|
@ -1,39 +0,0 @@
|
|||
require File.dirname(__FILE__) + '/spec_helper'
|
||||
|
||||
describe "should eventually { block }" do
|
||||
|
||||
it "should pass if block returns true immediately" do
|
||||
lambda { true }.should eventually { |expected| expected.should == true }
|
||||
end
|
||||
|
||||
it "should pass if block returns true after a delay" do
|
||||
eventually = EventualMock.new(true, 1)
|
||||
lambda { true }.should eventually { |expected| expected.should == eventually.test }
|
||||
end
|
||||
|
||||
it "should fail if block returns false despite delay" do
|
||||
lambda {
|
||||
lambda { true }.should eventually { |expected| expected.should == false }
|
||||
}.should raise_error(Spec::Expectations::ExpectationNotMetError)
|
||||
end
|
||||
|
||||
end
|
||||
|
||||
describe "should_not eventually { block }" do
|
||||
|
||||
it "should pass if block returns false immediately" do
|
||||
lambda { true }.should_not eventually { |expected| expected.should_not == false }
|
||||
end
|
||||
|
||||
it "should pass if block returns false after a delay" do
|
||||
eventually = EventualMock.new(false, 1)
|
||||
lambda { true }.should_not eventually { |expected| expected.should_not == eventually.test }
|
||||
end
|
||||
|
||||
it "should fail if block returns true despite delay" do
|
||||
lambda {
|
||||
lambda { true }.should_not eventually { |expected| expected.should_not == true }
|
||||
}.should raise_error(Spec::Expectations::ExpectationNotMetError)
|
||||
end
|
||||
|
||||
end
|
|
@ -2,7 +2,6 @@ require 'spec'
|
|||
|
||||
current_directory = File.dirname(__FILE__)
|
||||
require "#{current_directory}/../lib/fog"
|
||||
require "#{current_directory}/eventually.rb"
|
||||
|
||||
Spec::Runner.configure do |config|
|
||||
end
|
||||
|
@ -40,14 +39,14 @@ def s3
|
|||
end
|
||||
end
|
||||
|
||||
class EventualMock
|
||||
def initialize(result, delay)
|
||||
@result = result
|
||||
@delay = delay
|
||||
def eventually(&block)
|
||||
[0,2,4,8,16].each do |delay|
|
||||
begin
|
||||
sleep(delay)
|
||||
yield
|
||||
break
|
||||
rescue Spec::Expectations::ExpectationNotMetError => error
|
||||
raise error if delay == 16
|
||||
end
|
||||
end
|
||||
|
||||
def test
|
||||
@start ||= Time.now
|
||||
(Time.now - @start <= @delay) ? !@result : @result
|
||||
end
|
||||
end
|
||||
end
|
Loading…
Reference in a new issue