beanstalk-client-rspec 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
data/.rspec ADDED
@@ -0,0 +1 @@
1
+ --colour
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in beanstalk-client-rspec.gemspec
4
+ gemspec
data/Gemfile.lock ADDED
@@ -0,0 +1,26 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ beanstalk-client-rspec (0.0.1)
5
+ beanstalk-client
6
+ rspec
7
+
8
+ GEM
9
+ remote: https://rubygems.org/
10
+ specs:
11
+ beanstalk-client (1.1.1)
12
+ diff-lcs (1.1.3)
13
+ rspec (2.8.0)
14
+ rspec-core (~> 2.8.0)
15
+ rspec-expectations (~> 2.8.0)
16
+ rspec-mocks (~> 2.8.0)
17
+ rspec-core (2.8.0)
18
+ rspec-expectations (2.8.0)
19
+ diff-lcs (~> 1.1.2)
20
+ rspec-mocks (2.8.0)
21
+
22
+ PLATFORMS
23
+ ruby
24
+
25
+ DEPENDENCIES
26
+ beanstalk-client-rspec!
data/LICENSE ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2012 John Ferlito
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,53 @@
1
+ # Beanstalk::Client::Rspec
2
+
3
+ Allows easy mocking of Beanstalk::Client
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ gem 'beanstalk-client-rspec'
10
+
11
+ And then execute:
12
+
13
+ $ bundle
14
+
15
+ Or install it yourself as:
16
+
17
+ $ gem install beanstalk-client-rspec
18
+
19
+ ## Usage
20
+
21
+ TODO: Write usage instructions here
22
+
23
+ ## Caveats
24
+
25
+ * No timeout logic is implemented. i.e. jobs stay reserved forever
26
+
27
+ ## TODO
28
+
29
+ * Write more tests
30
+ * Implement job delay
31
+ * Implement job priority
32
+ * Implement
33
+ - bury
34
+ - touch
35
+ - peek
36
+ - peek-ready
37
+ - peek-delayed
38
+ - peek-buried
39
+ - kick
40
+ - stats-tube
41
+ - stats
42
+ - list-tubes
43
+ - list-tubes-used
44
+ - quit
45
+ - pause-tube
46
+
47
+ ## Contributing
48
+
49
+ 1. Fork it
50
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
51
+ 3. Commit your changes (`git commit -am 'Added some feature'`)
52
+ 4. Push to the branch (`git push origin my-new-feature`)
53
+ 5. Create new Pull Request
data/Rakefile ADDED
@@ -0,0 +1,2 @@
1
+ #!/usr/bin/env rake
2
+ require "bundler/gem_tasks"
@@ -0,0 +1,20 @@
1
+ # -*- encoding: utf-8 -*-
2
+ require File.expand_path('../lib/beanstalk-client-rspec/version', __FILE__)
3
+
4
+ Gem::Specification.new do |gem|
5
+ gem.authors = ["John Ferlito"]
6
+ gem.email = ["johnf@inodes.org"]
7
+ gem.description = %q{Mock and RSpec for beanstalk-client}
8
+ gem.summary = %q{Provides a fairly complete mock for beanstalk-client by imitating beanstalkd using arrays. Also provides some rspec matchers for testing the state of the system.}
9
+ gem.homepage = "https://github.com/johnf/beanstalk-client-rspec"
10
+
11
+ gem.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
12
+ gem.files = `git ls-files`.split("\n")
13
+ gem.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
14
+ gem.name = "beanstalk-client-rspec"
15
+ gem.require_paths = ["lib"]
16
+ gem.version = Beanstalk::Client::Rspec::VERSION
17
+
18
+ gem.add_dependency('rspec')
19
+ gem.add_dependency('beanstalk-client')
20
+ end
@@ -0,0 +1,126 @@
1
+ require "beanstalk-client-rspec/version"
2
+ require "beanstalk-client-rspec/spec"
3
+ require 'beanstalk-client'
4
+
5
+ module Beanstalk
6
+ class MockConnection < Beanstalk::Connection
7
+ def initialize(addr, default_tube=nil)
8
+ super
9
+ @id_mutex = Mutex.new
10
+ @tube_mutex = Mutex.new
11
+ @tubes = {}
12
+ @id = 0
13
+ end
14
+
15
+ # Tests use this to rest stuff
16
+ def flush!
17
+ initialize(nil, @default_tube)
18
+ end
19
+
20
+ def connect
21
+ # We don't want to actually connect to anything
22
+ end
23
+
24
+ # TODO Put on to reservation queue and deal with bury etc
25
+ def reserve(timeout=nil)
26
+ job = nil
27
+ @watch_list.each do |tube_name|
28
+ begin
29
+ job = @tubes[tube_name]['ready'].pop(false)
30
+ rescue ThreadError
31
+ next
32
+ end
33
+
34
+ job['reserves'] += 1
35
+ (@tubes[tube_name]['reserved'] ||= []).push job
36
+ end
37
+
38
+ if job.nil?
39
+ if timeout
40
+ return nil
41
+ else
42
+ raise Beanstalk::TimedOut if job.nil?
43
+ end
44
+ end
45
+
46
+ Job.new(self, job['id'], job['body'])
47
+ end
48
+
49
+ private
50
+ def interact(cmd, rfmt)
51
+ case cmd
52
+ when /^watch/
53
+ [@watch_list.size]
54
+ when /^ignore/
55
+ [@watch_list.size]
56
+ when /^use (\S+)/
57
+ [$1]
58
+ when /^list-tubes-watched/
59
+ @watch_list
60
+ when /^put (\d+) (\d+) (\d+) \d+\r\n(.*)\r\n/m
61
+ pri = $1
62
+ delay = $2
63
+ ttr = $3
64
+ body = $4
65
+
66
+ id = @id_mutex.synchronize { @id += 1 }
67
+ job = {
68
+ 'id' => id,
69
+ 'pri' => pri,
70
+ 'delay' => delay,
71
+ 'ttr' => ttr,
72
+ 'body' => body.to_s,
73
+ 'created_at' => Time.now,
74
+ 'file' => 0,
75
+ 'reserves' => 0,
76
+ 'timeouts' => 0,
77
+ 'releases' => 0,
78
+ 'buries' => 0,
79
+ 'kicks' => 0,
80
+ }
81
+ @tubes[@last_used] ||= {}
82
+ (@tubes[@last_used]['ready'] ||= Queue.new) << job
83
+ [id]
84
+ when /^delete (\d+)/
85
+ id = $1
86
+ @tubes.each_pair do |tube_name, states|
87
+ states['reserved'].delete_if {|job| job[:id] == id }
88
+ end
89
+ when /^stats-job (\d+)/
90
+ id = $1
91
+ @tubes.each_pair do |tube_name, states|
92
+ job = states['reserved'].find {|job| job[:id] == id }
93
+ if job
94
+ return job.merge(
95
+ 'state' => 'reserved',
96
+ 'age' => (Time.now - job[:created_at]),
97
+ 'file' => 0,
98
+ 'time-left' => 1,
99
+ )
100
+ end
101
+ end
102
+ when /^release (\d+) (\d+) (\d+)/
103
+ id = $1
104
+ @tubes.each_pair do |tube_name, states|
105
+ job = states['reserved'].delete_if {|job| job[:id] == id }.first
106
+ if job
107
+ job['releases'] += 1
108
+ (@tubes[tube_name]['ready'] ||= Queue.new) << job
109
+ end
110
+ end
111
+ else
112
+ raise "Need to define #{cmd} #{rfmt} for interact"
113
+ end
114
+ end
115
+ end
116
+
117
+ class MockPool < Pool
118
+ def connect
119
+ @connections ||= {}
120
+ if !@connections.include?('default')
121
+ @connections['default'] = MockConnection.new('default', @default_tube)
122
+ end
123
+ @connections.size
124
+ end
125
+ end
126
+ end
@@ -0,0 +1,49 @@
1
+ require 'rspec/expectations'
2
+
3
+
4
+ module TubeHelper
5
+ def self.extended(klass)
6
+ klass.instance_eval do
7
+ chain :for do |tube_name|
8
+ self.tube_name = tube_name
9
+ end
10
+ end
11
+ end
12
+
13
+ private
14
+
15
+ attr_accessor :tube_name
16
+
17
+ def tubes(beanstalk)
18
+ beanstalk.instance_variable_get(:@connections)['default'].instance_variable_get(:@tubes)
19
+ end
20
+
21
+ def tube_name
22
+ @tube_name || 'default'
23
+ end
24
+
25
+ def tube_size(beanstalk)
26
+ states = tubes(beanstalk)[tube_name] || {}
27
+ (states['ready'] || []).size
28
+ end
29
+ end
30
+
31
+ RSpec::Matchers.define :have_tube_size_of do |size|
32
+ extend TubeHelper
33
+ match do |actual|
34
+ tube_size(actual) == size
35
+ end
36
+
37
+ failure_message_for_should do |actual|
38
+ "expected that tube #{actual} would have #{size} jobs, but got #{tube_size(actual)} jobs instead"
39
+ end
40
+
41
+ failure_message_for_should_not do |actual|
42
+ "expected that tube #{actual} would not have #{size} jobs, but got #{tube_size(actual)} jobs instead"
43
+ end
44
+
45
+ description do
46
+ "have #{size} jobs in tube"
47
+ end
48
+ end
49
+
@@ -0,0 +1,8 @@
1
+ require 'beanstalk-client-rspec/matchers'
2
+
3
+ module BeanstalkSpec
4
+ extend self
5
+ def tube(tube_name)
6
+ tube_name
7
+ end
8
+ end
@@ -0,0 +1,7 @@
1
+ module Beanstalk
2
+ module Client
3
+ module Rspec
4
+ VERSION = "0.0.1"
5
+ end
6
+ end
7
+ end
@@ -0,0 +1,43 @@
1
+ require 'spec_helper'
2
+
3
+ require 'beanstalk-client-rspec'
4
+
5
+ describe Beanstalk::MockPool do
6
+ before do
7
+ @beanstalk = Beanstalk::MockPool.new ['127.0.0.1:11301']
8
+ end
9
+
10
+ it 'should accept objects on the default' do
11
+ @beanstalk.put 'cow'
12
+ job = @beanstalk.reserve
13
+ job.body.should == 'cow'
14
+
15
+ job.release
16
+
17
+ job = @beanstalk.reserve
18
+ job.body.should == 'cow'
19
+
20
+ job.delete
21
+ end
22
+
23
+ it 'should accept objects on a named tube' do
24
+ @beanstalk.use 'moo'
25
+ @beanstalk.put 'cow'
26
+
27
+ @beanstalk.watch 'moo'
28
+ @beanstalk.ignore 'default'
29
+ job = @beanstalk.reserve
30
+ job.body.should == 'cow'
31
+ end
32
+
33
+ it 'should deal with on_tube' do
34
+ @beanstalk.on_tube 'foo' do |c|
35
+ c.put 'cow'
36
+ end
37
+
38
+ @beanstalk.watch 'foo'
39
+ @beanstalk.ignore 'default'
40
+ job = @beanstalk.reserve
41
+ job.body.should == 'cow'
42
+ end
43
+ end
@@ -0,0 +1,17 @@
1
+ require 'spec_helper'
2
+
3
+ describe 'RSpec' do
4
+ before do
5
+ @beanstalk = Beanstalk::MockPool.new ['localhost:11300']
6
+ end
7
+
8
+ it 'should match empty tube size' do
9
+ @beanstalk.should have_tube_size_of(0)
10
+ end
11
+
12
+ it 'should match queue size' do
13
+ @beanstalk.use 'oath'
14
+ @beanstalk.yput :foo => 'bar'
15
+ @beanstalk.should have_tube_size_of(1).for('oath')
16
+ end
17
+ end
@@ -0,0 +1,10 @@
1
+ $LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
2
+
3
+ require 'rspec'
4
+
5
+ Dir[File.expand_path(File.join(File.dirname(__FILE__),'support','**','*.rb'))].each {|f| require f}
6
+
7
+ RSpec.configure do |config|
8
+ config.mock_with :rspec
9
+ end
10
+
metadata ADDED
@@ -0,0 +1,83 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: beanstalk-client-rspec
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - John Ferlito
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2012-02-04 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: rspec
16
+ requirement: &22384140 !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ! '>='
20
+ - !ruby/object:Gem::Version
21
+ version: '0'
22
+ type: :runtime
23
+ prerelease: false
24
+ version_requirements: *22384140
25
+ - !ruby/object:Gem::Dependency
26
+ name: beanstalk-client
27
+ requirement: &22444920 !ruby/object:Gem::Requirement
28
+ none: false
29
+ requirements:
30
+ - - ! '>='
31
+ - !ruby/object:Gem::Version
32
+ version: '0'
33
+ type: :runtime
34
+ prerelease: false
35
+ version_requirements: *22444920
36
+ description: Mock and RSpec for beanstalk-client
37
+ email:
38
+ - johnf@inodes.org
39
+ executables: []
40
+ extensions: []
41
+ extra_rdoc_files: []
42
+ files:
43
+ - .rspec
44
+ - Gemfile
45
+ - Gemfile.lock
46
+ - LICENSE
47
+ - README.md
48
+ - Rakefile
49
+ - beanstalk-client-rspec.gemspec
50
+ - lib/beanstalk-client-rspec.rb
51
+ - lib/beanstalk-client-rspec/matchers.rb
52
+ - lib/beanstalk-client-rspec/spec.rb
53
+ - lib/beanstalk-client-rspec/version.rb
54
+ - spec/beanstalk_spec.rb
55
+ - spec/rspec_spec.rb
56
+ - spec/spec_helper.rb
57
+ homepage: https://github.com/johnf/beanstalk-client-rspec
58
+ licenses: []
59
+ post_install_message:
60
+ rdoc_options: []
61
+ require_paths:
62
+ - lib
63
+ required_ruby_version: !ruby/object:Gem::Requirement
64
+ none: false
65
+ requirements:
66
+ - - ! '>='
67
+ - !ruby/object:Gem::Version
68
+ version: '0'
69
+ required_rubygems_version: !ruby/object:Gem::Requirement
70
+ none: false
71
+ requirements:
72
+ - - ! '>='
73
+ - !ruby/object:Gem::Version
74
+ version: '0'
75
+ requirements: []
76
+ rubyforge_project:
77
+ rubygems_version: 1.8.11
78
+ signing_key:
79
+ specification_version: 3
80
+ summary: Provides a fairly complete mock for beanstalk-client by imitating beanstalkd
81
+ using arrays. Also provides some rspec matchers for testing the state of the system.
82
+ test_files: []
83
+ has_rdoc: