-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Roy Razon
committed
Dec 27, 2018
1 parent
b008d9a
commit 83c0fbd
Showing
7 changed files
with
215 additions
and
20 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
module HubClient | ||
class ExponentialBackoffInterval | ||
attr_accessor :initial, :multiplier, :rand_factor, :max_count | ||
|
||
DEFAULT_OPTS = { | ||
initial: 0.5, | ||
multiplier: 1.2, | ||
rand_factor: 0.05, | ||
max_count: 10, | ||
} | ||
|
||
def initialize(opts = {}) | ||
opts = DEFAULT_OPTS.merge(opts) | ||
@initial = opts[:initial] | ||
@multiplier = opts[:multiplier] | ||
@rand_factor = opts[:rand_factor] | ||
@max_count = opts[:max_count] | ||
end | ||
|
||
def next(count) | ||
return nil if count > max_count | ||
result = @initial * (@multiplier ** count) | ||
r = Kernel.rand(-@rand_factor..@rand_factor) | ||
result + result * r | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -9,5 +9,6 @@ def method_missing(method_sym, *args, &block) | |
|
||
def self.logger | ||
@logger ||= Logger.new | ||
@logger | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
require 'spec_helper' | ||
require "hub_client/exponential_backoff_interval" | ||
|
||
describe HubClient::ExponentialBackoffInterval do | ||
let(:default_opts) {described_class.const_get(:DEFAULT_OPTS)} | ||
|
||
describe '#next' do | ||
subject {described_class.new(initial: 12, max_count: 3)} | ||
|
||
context 'when called with 2' do | ||
it 'returns a correct value' do | ||
multiplier = default_opts[:multiplier] | ||
med = 12 * (multiplier ** 2) | ||
rand_factor = default_opts[:rand_factor] | ||
expect(Kernel).to receive(:rand).with(-rand_factor..rand_factor) {0.1} | ||
expect(subject.next(2)).to eq(med + 0.1*med) | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters