Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

add retryable functionality to Bosh::Common module

also include profiling in rspec configuration
  • Loading branch information...
commit 1995379be092407a64c992d3be284475296c0818 1 parent e3a8dee
Amit Gupta and Glenn Oppegard authored
1  .rspec
View
@@ -1,2 +1,3 @@
--color
--format progress
+--profile
2  bosh_aws_cpi/lib/bosh_aws_cpi.rb
View
@@ -1,3 +1,3 @@
# Copyright (c) 2009-2012 VMware, Inc.
-require "cloud/aws"
+require "cloud/aws"
5 bosh_aws_cpi/lib/cloud/aws/instance_manager.rb
View
@@ -1,4 +1,4 @@
-require 'retryable'
+require "common/common"
module Bosh::AwsCloud
class InstanceManager
@@ -31,7 +31,7 @@ def create(agent_id, stemcell_id, resource_pool, networks_spec, disk_locality, e
@logger.info("Creating new instance with: #{instance_params.inspect}")
- Retryable.retryable(sleep: 30, tries: 10, on: [AWS::EC2::Errors::InvalidIPAddress::InUse]) do |tries, e|
+ Bosh::Common.retryable(sleep: instance_create_wait_time, tries: 10, on: [AWS::EC2::Errors::InvalidIPAddress::InUse]) do |tries, e|
@logger.warn("IP address was in use: #{e}") if tries > 0
@instance = @region.instances.create(instance_params)
end
@@ -146,5 +146,6 @@ def task_checkpoint
Bosh::Clouds::Config.task_checkpoint
end
+ def instance_create_wait_time; 30; end
end
end
2  bosh_aws_cpi/spec/unit/instance_manager_spec.rb
View
@@ -59,7 +59,7 @@
aws_instances.should_receive(:create).with(aws_instance_params).once
instance_manager = described_class.new(region, registry, availability_zone_selector)
- Retryable.stub(:sleep)
+ instance_manager.stub(instance_create_wait_time: 0)
agent_id = "agent-id"
stemcell_id = "stemcell-id"
2  bosh_common/lib/common/common.rb
View
@@ -1,9 +1,11 @@
# Copyright (c) 2012 VMware, Inc.
+require 'retryable'
module Bosh
# Module for common methods used throughout the BOSH code.
module Common
+ extend Retryable::Methods
# Converts all keys of a [Hash] to symbols. Performs deep conversion.
#
69 bosh_common/spec/unit/common_spec.rb
View
@@ -6,21 +6,21 @@
describe Bosh::Common do
- describe "#symbolize_keys" do
+ describe "::symbolize_keys" do
ORIGINAL = {
- "foo1" => "bar",
- :foo2 => "bar",
- "foo3" => {
- "foo4" => "bar"
- }
+ "foo1" => "bar",
+ :foo2 => "bar",
+ "foo3" => {
+ "foo4" => "bar"
+ }
}.freeze
EXPECTED = {
- :foo1 => "bar",
- :foo2 => "bar",
- :foo3 => {
- :foo4 => "bar"
- }
+ :foo1 => "bar",
+ :foo2 => "bar",
+ :foo3 => {
+ :foo4 => "bar"
+ }
}.freeze
it "should not modify the original hash" do
@@ -34,7 +34,7 @@
end
end
- describe "#which" do
+ describe "::which" do
let(:path) {
path = ENV["PATH"]
path += ":#{File.expand_path('../../assets', __FILE__)}"
@@ -56,4 +56,49 @@
Bosh::Common.which("foo1").should be_nil
end
end
+
+ describe "::retryable" do
+ it "should retry the given number of times" do
+ Bosh::Common.stub(:sleep)
+
+ count = 0
+
+ Bosh::Common.retryable(tries: 2) do |tries|
+ count += 1
+ raise StandardError if tries == 0
+ end
+
+ count.should == 2
+ end
+
+ it "should sleep on each retry the given number of seconds" do
+ Bosh::Common.should_receive(:sleep).with(5).twice
+
+ Bosh::Common.retryable(tries: 3, sleep: 5) do |tries|
+ raise StandardError if tries < 2
+ end
+ end
+
+ it "should retry when given error is raised" do
+ Bosh::Common.stub(:sleep)
+
+ count = 0
+
+ Bosh::Common.retryable(tries: 3, on: [ArgumentError, RuntimeError]) do |tries|
+ count += 1
+ raise ArgumentError if tries == 0
+ raise RuntimeError if tries == 1
+ end
+
+ count.should == 3
+ end
+
+ it "should raise an error if that error is raised and isn't in the specified list" do
+ expect {
+ Bosh::Common.retryable(on: [ArgumentError]) do
+ 1/0
+ end
+ }.to raise_error(ZeroDivisionError)
+ end
+ end
end
Please sign in to comment.
Something went wrong with that request. Please try again.