Created
January 14, 2016 22:05
-
-
Save marksim/154d8a8d544bbc2fccfb to your computer and use it in GitHub Desktop.
This file contains hidden or 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
require 'rspec' | |
def next_server_number(server_numbers) | |
server_numbers.sort.each.with_index do |s, i| | |
return i+1 if s > i+1 | |
end | |
return server_numbers.count + 1 | |
end | |
describe 'next_server_number' do | |
it 'returns 2 for [5, 3, 1]' do | |
expect(next_server_number([5, 3, 1])).to eql 2 | |
end | |
it 'returns 4 for [5, 3, 2, 1]' do | |
expect(next_server_number([5, 3, 2, 1])).to eql 4 | |
end | |
it 'returns 4 for [3, 2, 1]' do | |
expect(next_server_number([3, 2, 1])).to eql 4 | |
end | |
it 'returns 1 for []' do | |
expect(next_server_number([])).to eql 1 | |
end | |
end | |
class ServerAllocator | |
class << self | |
attr_reader :server_types | |
def allocate(name) | |
sa = get(name) | |
sa.get_next_server | |
end | |
def deallocate(server_name) | |
name = server_name.match(/[a-z]+/)[0] | |
number = server_name.match(/[0-9]+/)[0].to_i | |
sa = get(name) | |
sa.remove(number) | |
end | |
def get(name) | |
@server_types ||= {} | |
@server_types[name] ||= ServerAllocator.new(name) | |
end | |
def clear | |
@server_types = {} | |
end | |
end | |
attr_reader :server_numbers | |
def initialize(name) | |
@server_numbers = [] | |
@name = name; | |
end | |
def next_server_number | |
@server_numbers.sort.each.with_index do |s, i| | |
return i+1 if s > i+1 | |
end | |
return @server_numbers.count + 1 | |
end | |
def remove(number) | |
@server_numbers.delete(number) | |
true | |
end | |
def allocated?(number) | |
@server_numbers.include?(number) | |
end | |
def get_next_server | |
num = next_server_number | |
@server_numbers << num | |
"#{@name}#{num}" | |
end | |
end | |
describe ServerAllocator do | |
context '.allocate' do | |
before(:each) do | |
ServerAllocator.clear | |
end | |
it 'allocates based on one name one time properly' do | |
expect(ServerAllocator.allocate('first')).to eql 'first1' | |
end | |
it 'allocates based on one name multiple times properly' do | |
expect(ServerAllocator.allocate('first')).to eql 'first1' | |
expect(ServerAllocator.allocate('first')).to eql 'first2' | |
end | |
it 'allocates based on multiple names multiple times properly' do | |
expect(ServerAllocator.allocate('first')).to eql 'first1' | |
expect(ServerAllocator.allocate('first')).to eql 'first2' | |
expect(ServerAllocator.allocate('second')).to eql 'second1' | |
expect(ServerAllocator.allocate('second')).to eql 'second2' | |
end | |
end | |
context '.deallocate' do | |
before(:each) do | |
ServerAllocator.clear | |
end | |
it 'deallocates a just allocated item' do | |
ServerAllocator.allocate('first') | |
expect(ServerAllocator.get('first').allocated?(1)).to eql true | |
ServerAllocator.deallocate('first1') | |
expect(ServerAllocator.get('first').allocated?(1)).to eql false | |
end | |
end | |
it 'deallocates and allocates properly' do | |
ServerAllocator.allocate('first') | |
ServerAllocator.allocate('first') | |
ServerAllocator.allocate('first') | |
ServerAllocator.allocate('first') | |
ServerAllocator.deallocate('first2') | |
expect(ServerAllocator.get('first').allocated?(2)).to eql false | |
expect(ServerAllocator.allocate('first')).to eql 'first2' | |
expect(ServerAllocator.allocate('first')).to eql 'first5' | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment