tickr_client 1.0.0 → 1.1.0

Sign up to get free protection for your applications and to get access to all the features.
data/README.md CHANGED
@@ -14,9 +14,9 @@ makes no guarantee about sequentiality.
14
14
 
15
15
  $tickr = TickrClient.new(
16
16
  servers: [
17
- {host: '192.168.1.1', port: 8080},
18
- {host: '192.168.1.2', port: 8080},
19
- {host: '192.168.1.3', port: 8080}
17
+ {host: '192.168.1.1', port: 8080, http_auth_password: 'lockd0wn'},
18
+ {host: '192.168.1.2', port: 8080, http_auth_password: 'lockd0wn'},
19
+ {host: '192.168.1.3', port: 8080, http_auth_password: 'lockd0wn'}
20
20
  ],
21
21
  timeout: 1000, # Try next host after 1 second.
22
22
  cache_size: 100, # Keep up to 100 tickets (unique IDs) at the ready.
data/VERSION CHANGED
@@ -1 +1 @@
1
- 1.0.0
1
+ 1.1.0
@@ -39,10 +39,16 @@ class TickrClient
39
39
 
40
40
  def fetch_tickets_from_server(index)
41
41
  new_ticket_group = begin
42
- uri = URI("http://#{servers[index][:host]}:#{servers[index][:port]}/tickets/create/#{replenish_capacity}")
43
- Timeout::timeout(timeout / 1000.0) do
44
- JSON.parse(Net::HTTP.get(uri))
42
+ uri = URI.parse("http://#{servers[index][:host]}:#{servers[index][:port]}/tickets/create/#{replenish_capacity}")
43
+ request = Net::HTTP::Get.new(uri.request_uri)
44
+ if servers[index][:http_auth_password] && servers[index][:http_auth_password] != ''
45
+ request.basic_auth '', servers[index][:http_auth_password]
45
46
  end
47
+
48
+ response = Timeout::timeout(timeout / 1000.0) do
49
+ Net::HTTP.new(uri.host, uri.port).start{|http| http.request(request)}
50
+ end
51
+ JSON.parse(response.body)
46
52
  rescue Timeout::Error
47
53
  return false
48
54
  end
@@ -5,18 +5,26 @@ require File.join(File.dirname(__FILE__), '..', 'lib', 'tickr_client')
5
5
  require 'json'
6
6
 
7
7
  describe TickrClient do
8
+ def create_client(num_servers = 4, http_password = nil, client_opts = {})
9
+ client_opts[:cache_size] ||= 10
10
+ ports = (8080..(8080 + num_servers - 1))
11
+ ports.each do |port|
12
+ FakeWeb.register_uri :get, "http://#{":#{http_password}@" if http_password}127.0.0.1:#{port}/tickets/create/#{client_opts[:cache_size]}", body: {'first' => 1, 'increment' => 1, 'count' => 5}.to_json
13
+ end
14
+ TickrClient.new(
15
+ client_opts.merge(servers: ports.inject([]) {|servers, port| servers.push({host: '127.0.0.1', port: port, http_auth_password: http_password})})
16
+ )
17
+ end
18
+
8
19
  after do
9
20
  # Make sure threads finish before we fail based on expectations
10
21
  Thread.list.each {|t| t.join unless t == Thread.current}
11
22
  end
12
- def get_client(opts = {})
13
- TickrClient.new(
14
- {servers: [{host: '127.0.0.1', port: 8080}, {host: '127.0.0.1', port: 8081}]}.merge(opts)
15
- )
16
- end
17
23
  describe '#initialize' do
18
24
  it 'sets configurable instance variables' do
19
- TickrClient.any_instance.stub :fetch_tickets
25
+ (8080..8081).each do |port|
26
+ FakeWeb.register_uri :get, "http://127.0.0.1:#{port}/tickets/create/500", body: {'first' => 1, 'increment' => 1, 'count' => 5}.to_json
27
+ end
20
28
  client = TickrClient.new(
21
29
  servers: [{host: '127.0.0.1', port: 8080}, {host: '127.0.0.1', port: 8081}],
22
30
  timeout: 150,
@@ -30,7 +38,10 @@ describe TickrClient do
30
38
  client.replenish_cache_at.should == 100
31
39
  end
32
40
  it 'applies sensible defaults' do
33
- TickrClient.any_instance.stub :fetch_tickets
41
+ (8080..8081).each do |port|
42
+ FakeWeb.register_uri :get, "http://127.0.0.1:#{port}/tickets/create/100", body: {'first' => 1, 'increment' => 1, 'count' => 5}.to_json
43
+ end
44
+
34
45
  client = TickrClient.new(
35
46
  servers: [{host: '127.0.0.1', port: 8080}, {host: '127.0.0.1', port: 8081}]
36
47
  )
@@ -44,22 +55,26 @@ describe TickrClient do
44
55
  servers: [{host: '127.0.0.1', port: 8080}, {host: '127.0.0.1', port: 8081}]
45
56
  )
46
57
  end
58
+ it 'supports http authentication enabled' do
59
+ # Notice that there is no register_uri set for a non-http authed request.
60
+ # Therefore if a non-http authed request is made, it will bomb.
61
+ FakeWeb.register_uri :get, 'http://:password@127.0.0.1:8080/tickets/create/100', body: {'first' => 1, 'increment' => 1, 'count' => 5}.to_json
62
+ TickrClient.new(
63
+ servers: [{host: '127.0.0.1', port: 8080, http_auth_password: 'password'}]
64
+ )
65
+ end
47
66
  end
48
67
 
49
68
  describe '#get_ticket' do
50
69
  it 'loads ticket from cache and removes it from the cache' do
51
- TickrClient.any_instance.stub :fetch_tickets
52
- client = get_client
53
-
70
+ client = create_client(1, nil, cache_size: 4, replenish_cache_at: 2)
54
71
  client.send(:tickets=, [1, 2, 3, 4])
55
72
  client.get_ticket.should == 1
56
73
  client.send(:tickets).should == [2, 3, 4]
57
74
  end
58
75
 
59
76
  it 'asynchronously fetches more tickets after falling below the replenesh_cache_at threshold' do
60
- TickrClient.any_instance.stub :fetch_tickets
61
-
62
- client = get_client(cache_size: 10, replenesh_cache_at: 2)
77
+ client = create_client(2, nil, cache_size: 10, replenesh_cache_at: 2)
63
78
  client.send(:tickets=, [5, 6, 7])
64
79
  client.should_receive :fetch_tickets_async
65
80
  client.get_ticket
@@ -68,16 +83,8 @@ describe TickrClient do
68
83
 
69
84
  describe 'private instance methods' do
70
85
  describe '#fetch_tickets' do
71
- it 'fetches tickets from servers one at a time' do
72
- Net::HTTP.should_receive(:get).and_return({'first' => 1, 'increment' => 1, 'count' => 5}.to_json)
73
- client = TickrClient.new(
74
- servers: [
75
- {host: '127.0.0.1', port: 8080},
76
- {host: '127.0.0.1', port: 8081},
77
- {host: '127.0.0.1', port: 8082},
78
- {host: '127.0.0.1', port: 8083}
79
- ]
80
- )
86
+ it 'fetches tickets from servers one at a time until it succeeds' do
87
+ client = create_client(4)
81
88
  Thread.list.each {|t| t.join unless t == Thread.current}
82
89
 
83
90
  client.send(:next_server_index=, 0)
@@ -91,13 +98,7 @@ describe TickrClient do
91
98
 
92
99
  describe '#fetch_tickets_async' do
93
100
  it 'fetches tickets in a separate thread' do
94
- FakeWeb.register_uri :get, 'http://127.0.0.1:8080/tickets/create/10', body: {'first' => 1, 'increment' => 1, 'count' => 2}.to_json
95
- client = TickrClient.new(
96
- servers: [
97
- {host: '127.0.0.1', port: 8080}
98
- ],
99
- cache_size: 10
100
- )
101
+ client = create_client(1, nil, cache_size: 10)
101
102
  client.send(:tickets=, [1, 2])
102
103
  FakeWeb.register_uri :get, 'http://127.0.0.1:8080/tickets/create/8', body: {'first' => 5, 'increment' => 1, 'count' => 8}.to_json
103
104
 
@@ -110,23 +111,13 @@ describe TickrClient do
110
111
 
111
112
  describe '#fetch_tickets_from_server' do
112
113
  it 'returns false on timeout error' do
113
- TickrClient.any_instance.stub :fetch_tickets
114
- client = get_client
115
-
116
- Net::HTTP.should_receive(:get).and_raise(Timeout::Error)
114
+ client = create_client(2)
115
+ Net::HTTP.should_receive(:new).and_raise(Timeout::Error)
117
116
  client.send(:fetch_tickets_from_server, 0).should be_false
118
117
  end
119
118
 
120
119
  it 'adds tickets to array and returns true' do
121
- TickrClient.any_instance.stub :fetch_tickets
122
- client = TickrClient.new(
123
- servers: [
124
- {host: '127.0.0.1', port: 8080},
125
- {host: '127.0.0.1', port: 8081},
126
- {host: '127.0.0.1', port: 8082}
127
- ],
128
- cache_size: 10
129
- )
120
+ client = create_client(3, nil, cache_size: 10)
130
121
  client.send(:tickets=, [1, 2])
131
122
  client.send(:next_server_index=, 1)
132
123
 
@@ -139,9 +130,7 @@ describe TickrClient do
139
130
 
140
131
  describe '#replenish_capacity' do
141
132
  it 'is the difference between the cache size and the number of tickets' do
142
- TickrClient.any_instance.stub :fetch_tickets
143
-
144
- client = get_client(cache_size: 10)
133
+ client = create_client(2, nil, cache_size: 10)
145
134
  client.send(:tickets=, [5, 6, 7])
146
135
  client.send(:replenish_capacity).should == 7
147
136
  end
@@ -149,9 +138,7 @@ describe TickrClient do
149
138
 
150
139
  describe '#create_tickets_from_ticket_group' do
151
140
  it 'should create an array of tickets' do
152
- TickrClient.any_instance.stub :fetch_tickets
153
-
154
- client = get_client
141
+ client = create_client(2)
155
142
  tickets = client.send(:create_tickets_from_ticket_group, {'first' => 100, 'increment' => 100, 'count' => 5})
156
143
  tickets.should == [100, 200, 300, 400, 500]
157
144
  end
@@ -5,7 +5,7 @@
5
5
 
6
6
  Gem::Specification.new do |s|
7
7
  s.name = "tickr_client"
8
- s.version = "1.0.0"
8
+ s.version = "1.1.0"
9
9
 
10
10
  s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
11
  s.authors = ["Robby Grossman"]
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: tickr_client
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.0
4
+ version: 1.1.0
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -177,7 +177,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
177
177
  version: '0'
178
178
  segments:
179
179
  - 0
180
- hash: 453379598713018140
180
+ hash: 130854216530279690
181
181
  required_rubygems_version: !ruby/object:Gem::Requirement
182
182
  none: false
183
183
  requirements: