klarlack 0.0.3 → 0.0.4

Sign up to get free protection for your applications and to get access to all the features.
data/VERSION.yml CHANGED
@@ -1,4 +1,4 @@
1
1
  ---
2
2
  :minor: 0
3
- :patch: 0
3
+ :patch: 4
4
4
  :major: 0
data/lib/klarlack.rb CHANGED
@@ -3,5 +3,5 @@ require 'varnish/socket_factory'
3
3
  require 'varnish/client'
4
4
 
5
5
  module Varnish
6
- VERSION = '0.0.3'
6
+ VERSION = '0.0.4'
7
7
  end
@@ -1,5 +1,7 @@
1
1
  module Varnish
2
2
  class Error < StandardError; end
3
+ class BrokenConnection < Error; end
4
+ class CommandFailed < Error; end
3
5
  class Client
4
6
  # Default management port of varnishd
5
7
  DEFAULT_PORT = 6082
@@ -215,16 +217,16 @@ module Varnish
215
217
  private
216
218
 
217
219
  # Sends a command to varnishd.
218
- # Raises an Varnish::Error when a non-200 status is returned
220
+ # Raises a Varnish::CommandFailed error when a non-200 status is returned
219
221
  # Returns the response text
220
222
  def cmd(name, *params)
221
223
  @mutex.synchronize do
222
224
  connect unless connected?
223
225
  @conn.write "#{name} #{params.join(' ').gsub('\\', '\\\\\\')}\n"
224
- status, length = @conn.gets.split # <status> <content_length>\n
225
- content = @conn.read(length.to_i + 1) # +1 = \n
226
+ status, length = conn_gets.split # <status> <content_length>\n
227
+ content = conn_read(length.to_i + 1) # +1 = \n
226
228
  content.chomp!
227
- raise Error, "Command #{name} returned with status #{status}: #{content}" if status.to_i != 200
229
+ raise CommandFailed, "Command #{name} returned with status #{status}: #{content}" if status.to_i != 200
228
230
  content
229
231
  end
230
232
  end
@@ -250,6 +252,14 @@ module Varnish
250
252
  @conn
251
253
  end
252
254
 
255
+ def conn_gets
256
+ @conn.gets || raise(BrokenConnection, "Expected to read one line from Varnish, got nil")
257
+ end
258
+
259
+ def conn_read(bytes)
260
+ @conn.read(bytes) || raise(BrokenConnection, "Expected to read #{bytes} bytes from Varnish, got nil")
261
+ end
262
+
253
263
  # converts +value+ into a boolean
254
264
  def bool(value)
255
265
  !!value
@@ -128,6 +128,24 @@ describe Varnish::Client do
128
128
  end
129
129
 
130
130
  end
131
+
132
+ describe '(broken connection)' do
133
+
134
+ before(:each) do
135
+ fake_connection!(:connected => true, :return => "200 1\n")
136
+ end
137
+
138
+ it 'should fail with a Varnish::Error when the connection does return nil for gets' do
139
+ @conn.stub!(:gets).and_return(nil)
140
+ lambda { @varnish.ping }.should raise_error(Varnish::BrokenConnection)
141
+ end
142
+
143
+ it 'should fail with a Varnish::Error when the connection does return nil for read' do
144
+ @conn.stub!(:read).and_return(nil)
145
+ lambda { @varnish.ping }.should raise_error(Varnish::BrokenConnection)
146
+ end
147
+
148
+ end
131
149
 
132
150
  describe '(daemon lifecycle)' do
133
151
 
@@ -142,12 +160,12 @@ describe Varnish::Client do
142
160
 
143
161
  it 'starting an already started daemon should raise an error' do
144
162
  ensure_started
145
- lambda { @varnish.start }.should raise_error(Varnish::Error)
163
+ lambda { @varnish.start }.should raise_error(Varnish::CommandFailed)
146
164
  end
147
165
 
148
166
  it 'stopping an already stopped daemon should raise an error' do
149
167
  ensure_stopped
150
- lambda { @varnish.stop }.should raise_error(Varnish::Error)
168
+ lambda { @varnish.stop }.should raise_error(Varnish::CommandFailed)
151
169
  end
152
170
 
153
171
  end
@@ -161,5 +179,19 @@ describe Varnish::Client do
161
179
  @varnish.stop if @varnish.running?
162
180
  while(!@varnish.stopped?) do sleep 0.1 end
163
181
  end
182
+
183
+ class FakeConn
184
+ attr_accessor :retval
185
+ def read(*args) @retval end
186
+ def gets(*args) @retval end
187
+ def write(str) str.to_s.size end
188
+ end
189
+
190
+ def fake_connection!(opts = {})
191
+ @conn = FakeConn.new
192
+ @conn.retval = opts[:return] || ''
193
+ @varnish.stub!(:connected?).and_return(opts[:connected] || false)
194
+ @varnish.instance_variable_set('@conn', @conn)
195
+ end
164
196
 
165
197
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: klarlack
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.3
4
+ version: 0.0.4
5
5
  platform: ruby
6
6
  authors:
7
7
  - "Max Sch\xC3\xB6fmann"
@@ -9,7 +9,7 @@ autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
11
 
12
- date: 2009-12-08 00:00:00 +01:00
12
+ date: 2010-01-03 00:00:00 +01:00
13
13
  default_executable:
14
14
  dependencies: []
15
15
 
@@ -58,7 +58,7 @@ requirements: []
58
58
  rubyforge_project:
59
59
  rubygems_version: 1.3.5
60
60
  signing_key:
61
- specification_version: 2
61
+ specification_version: 3
62
62
  summary: ruby client for varnishd's admin interface
63
63
  test_files:
64
64
  - spec/klarlack_spec.rb