Permalink
Browse files

Fix InternetConnection.available? so that it memoizes correctly when …

…a connection is not available.
  • Loading branch information...
1 parent e305b26 commit 922f145df6fe28c428d4f6af1a23de43b47fdaac @myronmarston committed Nov 2, 2010
Showing with 28 additions and 8 deletions.
  1. +1 −0 CHANGELOG.md
  2. +2 −1 lib/vcr/internet_connection.rb
  3. +25 −7 spec/internet_connection_spec.rb
View
@@ -6,6 +6,7 @@
* Moved documentation from README to [Wiki](http://github.com/myronmarston/vcr/wiki).
* Refactoring and code cleanup.
+* Fix InternetConnection.available? so that it memoizes correctly when a connection is not available.
## 1.2.0 (October 13, 2010)
@@ -7,7 +7,8 @@ module InternetConnection
EXAMPLE_HOST = "example.com"
def available?
- @available ||= Ping.pingecho(EXAMPLE_HOST, 1, 80)
+ @available = Ping.pingecho(EXAMPLE_HOST, 1, 80) unless defined?(@available)
+ @available
end
end
end
@@ -3,17 +3,35 @@
describe VCR::InternetConnection do
describe '.available?' do
before(:each) do
- described_class.instance_variable_set(:@available, nil)
+ described_class.send(:remove_instance_variable, :@available) if described_class.instance_variable_defined?(:@available)
end
- it 'returns true when pinging example.com succeeds' do
- Ping.stub(:pingecho).with("example.com", anything, anything).and_return(true)
- described_class.available?.should be_true
+ def stub_pingecho_with(value)
+ Ping.stub(:pingecho).with("example.com", anything, anything).and_return(value)
end
- it 'returns false when pinging example.com fails' do
- Ping.stub(:pingecho).with("example.com", anything, anything).and_return(false)
- described_class.available?.should be_false
+ context 'when pinging example.com succeeds' do
+ it 'returns true' do
+ stub_pingecho_with(true)
+ described_class.should be_available
+ end
+
+ it 'memoizes the value so no extra pings are made' do
+ Ping.should_receive(:pingecho).once.and_return(true)
+ 3.times { described_class.available? }
+ end
+ end
+
+ context 'when pinging example.com fails' do
+ it 'returns false' do
+ stub_pingecho_with(false)
+ described_class.should_not be_available
+ end
+
+ it 'memoizes the value so no extra pings are made' do
+ Ping.should_receive(:pingecho).once.and_return(false)
+ 3.times { described_class.available? }
+ end
end
end
end

0 comments on commit 922f145

Please sign in to comment.