bean_counter 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,17 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ Gemfile.lock
7
+ InstalledFiles
8
+ _yardoc
9
+ coverage
10
+ doc/
11
+ lib/bundler/man
12
+ pkg
13
+ rdoc
14
+ spec/reports
15
+ test/tmp
16
+ test/version_tmp
17
+ tmp
@@ -0,0 +1,9 @@
1
+ --- !ruby/object:RDoc::Options
2
+ encoding: UTF-8
3
+ static_path: []
4
+ rdoc_include:
5
+ - lib/
6
+ charset: UTF-8
7
+ exclude: !ruby/regexp /(?:\/(?:test|coverage|doc))|(?:bean_counter.gemspec|(?:Gem|Rake)file|LICENSE|README\.md)/
8
+ main_page: BeanCounter
9
+ root: lib
data/.rspec ADDED
@@ -0,0 +1 @@
1
+ --debug
@@ -0,0 +1,21 @@
1
+ language: ruby
2
+
3
+ rvm:
4
+ - 1.9.3
5
+ - 2.0.0
6
+ - jruby-19mode
7
+ - rbx-19mode
8
+
9
+ before_install:
10
+ - curl -L https://github.com/kr/beanstalkd/archive/v1.9.tar.gz | tar xz -C /tmp
11
+ - cd /tmp/beanstalkd-1.9/
12
+ - make
13
+ - ./beanstalkd &
14
+ - cd $TRAVIS_BUILD_DIR
15
+
16
+ script: bundle exec rake test_all
17
+
18
+ matrix:
19
+ allow_failures:
20
+ - rvm: jruby-19mode
21
+ - rvm: rbx-19mode
data/Gemfile ADDED
@@ -0,0 +1,11 @@
1
+ source 'https://rubygems.org'
2
+
3
+ gemspec
4
+
5
+ gem 'debugger', :platforms => [:mri]
6
+ group :test do
7
+ gem 'coveralls', :require => false
8
+ gem 'mocha', :require => false
9
+ gem 'minitest_should'
10
+ gem 'rspec', :require => false
11
+ end
data/LICENSE ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2013 Freewrite.org
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.
@@ -0,0 +1,306 @@
1
+ # BeanCounter
2
+
3
+ [![Build Status](https://secure.travis-ci.org/gemeraldbeanstalk/bean_counter.png)](http://travis-ci.org/gemeraldbeanstalk/bean_counter)
4
+ [![Dependency Status](https://gemnasium.com/gemeraldbeanstalk/bean_counter.png)](https://gemnasium.com/gemeraldbeanstalk/bean_counter)
5
+ [![Coverage Status](https://coveralls.io/repos/gemeraldbeanstalk/bean_counter/badge.png?branch=master)](https://coveralls.io/r/gemeraldbeanstalk/bean_counter)
6
+ [![Code Climate](https://codeclimate.com/github/gemeraldbeanstalk/bean_counter.png)](https://codeclimate.com/github/gemeraldbeanstalk/bean_counter)
7
+
8
+ BeanCounter provides additional TestUnit/MiniTest assertions and/or RSpec matchers for testing Ruby code that relies on
9
+ [Beaneater](https://github.com/beanstalkd/beaneater) and [Beanstalkd](https://github.com/kr/beanstalkd).
10
+
11
+ ##### TestUnit/MiniTest Assertions
12
+ For TestUnit/MiniTest, BeanCounter provides 6 assertions/refutations
13
+ for use in your tests:
14
+ - ```assert_enqueued/refute_enqueued``` - Searches all jobs in the beanstalkd
15
+ pool for jobs with attributes matching the keys/values of the Hash given
16
+ - ```assert_enqueues/refute_enqueues``` - Searches only those jobs in the
17
+ beanstalkd pool enqueued during the execution of the provided block for jobs
18
+ with attributes matching the keys/values of the Hash given
19
+ - ```assert_tube/refute_tube``` - Searches all tubes in the beanstalkd pool
20
+ for a tube with attributes matching the keys/values of a given Hash
21
+
22
+ BeanCounter also provides a helper, ```BeanCounter.reset!``` to reset a given tube or
23
+ the entire beanstalkd pool by deleting the appropriate jobs.
24
+
25
+ ##### RSpec Matchers
26
+ For RSpec, BeanCounter provides 2 equivalent should/should_not matchers for use in your specs:
27
+ - ```have_enqueued``` - Searches for jobs in the beanstalkd
28
+ pool with attributes matching the keys/values of the Hash given.
29
+ If called on a block only inspects jobs enqueued during the execution of
30
+ the block. Otherwise, inspects all jobs.
31
+ - ```have_tube``` - Searches all tubes in the beanstalkd pool
32
+ for a tube with attributes matching the keys/values of a given Hash
33
+
34
+ BeanCounter also provides a helper, ```BeanCounter.reset!``` to reset a given tube or
35
+ the entire beanstalkd pool by deleting the appropriate jobs.
36
+
37
+ ## Installation
38
+
39
+ Add this line to your application's Gemfile:
40
+
41
+ gem 'bean_counter'
42
+
43
+ And then execute:
44
+
45
+ $ bundle
46
+
47
+ Or install it without bundler:
48
+
49
+ $ gem install bean_counter
50
+
51
+ ## Test Framework Configuration
52
+ #### TestUnit/MiniTest
53
+ In order to use BeanCounter in your tests you'll need to require and configure
54
+ it in your test_helper:
55
+ ```ruby
56
+ # To make the assertions available to all test cases you can require one of the
57
+ # following in test/test_helper.rb:
58
+
59
+ # For TestUnit, adds assertions to Test::Unit::TestCase and any derived classes:
60
+ require 'bean_counter/test_unit'
61
+
62
+ # For MiniTest, adds assertions to MiniTest::Unit::TestCase and any derived classes:
63
+ require 'bean_counter/mini_test'
64
+
65
+
66
+ # To maintain greater control over where the assertions are available, require
67
+ # bean_counter/test_assertions directly then include BeanCounter::TestAssertions
68
+ # in any test classes where you want to make use of the assertions:
69
+
70
+ # test/test_helper.rb
71
+ require 'bean_counter/test_assertions'
72
+
73
+ # test/beaneater_test.rb
74
+ require 'test_helper'
75
+
76
+ # For TestUnit:
77
+ class BeaneaterTest < Test::Unit::TestCase
78
+ include BeanCounter::TestAssertions
79
+
80
+ # assertions will be available to all tests in this class
81
+ end
82
+
83
+ # Or for MiniTest:
84
+ class BeaneaterTest < MiniTest::Unit::TestCase
85
+ include BeanCounter::TestAssertions
86
+
87
+ # assertions will be available to all tests in this class
88
+ end
89
+ ```
90
+
91
+ #### RSpec
92
+ In order to use BeanCounter in your specs you'll need to require and configure
93
+ it in your spec_helper:
94
+ ```ruby
95
+ # To make the BeanCounter matchers available to all specs, require
96
+ # bean_counter/spec in spec/spec_helper.rb:
97
+ require 'bean_counter/spec'
98
+
99
+
100
+ # To maintain more control over where the matchers are available, require
101
+ # bean_counter/spec_matchers directly and include BeanCounter::SpecMatchers in
102
+ # any spec where you want to use the matchers:
103
+
104
+ # spec/spec_helper.rb
105
+ require 'bean_counter/spec_matchers'
106
+
107
+ # Then include BeanCounter::SpecMatchers in any test class that needs access to the
108
+ # matchers:
109
+
110
+ # spec/beaneater_client_spec.rb
111
+ require 'spec_helper'
112
+
113
+ describe BeaneaterClient do
114
+ include BeanCounter::SpecMatchers
115
+
116
+ # matchers will be available to all test cases inside this block
117
+ end
118
+ ```
119
+
120
+ ## General Configuration
121
+ Beyond the configuration required to utilize BeanCounter with your test
122
+ framework, BeanCounter may also require other test framework agnostic
123
+ configuration to work properly or as desired.
124
+
125
+ #####BeanCounter.beanstalkd_url
126
+ ```BeanCounter.beanstalkd_url``` allows you to directly provide a string or an
127
+ Array of strings that will be used by BeanCounter when communicating with the
128
+ beanstalkd pool. By default, BeanCounter will try to intelligently determine
129
+ the location of beanstalkd servers by checking for configuration in a few
130
+ different places, though in some cases special configuration may be required.
131
+
132
+ First, and of higest precedence, BeanCounter will check
133
+ ```ENV['BEANSTALKD_URL']``` for a comma separated list of beanstalkd
134
+ servers. If no evironment variable is found, any value provided for
135
+ ```BeanCounter.beanstalkd_url``` will be used to connect to the the beanstalkd pool.
136
+ Finally, if no environment variable is found and no value has been provided for
137
+ BeanCounter.beanstalkd_url, BeanCounter will try to use any configuration
138
+ provided to Beaneater. If no beanstalkd_url can be determined, a Runtime error
139
+ will be raised.
140
+
141
+ Beanstalkd urls can be provided in any of the variety of formats shown below:
142
+ ```
143
+ $ BEANSTALKD_URL='127.0.0.1,beanstalk://localhost:11300,localhost:11301' rake test
144
+ BeanCounter.beanstalkd_url
145
+ #=> ['127.0.0.1', 'beanstalk://localhost:11300', 'localhost:11301']
146
+
147
+ BeanCounter.beanstalkd_url = 'beanstalk://localhost'
148
+ BeanCounter.beanstalkd_url
149
+ #=> 'beanstalk://localhost'
150
+
151
+ BeanCounter.beanstalkd_url = ['127.0.0.1', 'beanstalk://localhost:11300', 'localhost:11301']
152
+ BeanCounter.beanstalkd_url
153
+ #=> ['127.0.0.1', 'beanstalk://localhost:11300', 'localhost:11301']
154
+ ```
155
+
156
+ #####BeanCounter.strategy
157
+ ```BeanCounter.strategy``` allows you to choose and configure the strategy
158
+ BeanCounter uses for accessing and interacting with the beanstalkd pool. At
159
+ present, there is only a single strategy available, but at least one other is
160
+ in the works.
161
+
162
+ The strategy currently available, BeanCounter::Strategy::StalkClimber, utilizes
163
+ [StalkClimber](https://github.com/gemeraldbeanstalk/stalk_climber.git) to
164
+ navigate the beanstalkd pool. The job traversal method employed by StalkClimber
165
+ suffers from some inefficiencies that come with trying to sequential access a
166
+ queue, but overall it is a powerful strategy that supports multiple servers and
167
+ offers solid performance given the design of beanstalkd.
168
+
169
+ ## Usage
170
+ Whether you are using the TestUnit/MiniTest or RSpec matchers, the usage is the
171
+ same, the only difference is the invocation.
172
+
173
+ Each assertion/matcher takes a Hash of options that will be used to find
174
+ matching jobs. Each key in the options Hash is a String or a Symbol that
175
+ identifies an attribute of a job that the corresponding Hash value should be
176
+ compared against. All attribute comparisons are performed using the triple-equal
177
+ (===) operator/method of the given value.
178
+
179
+ #####assert_enqueued/have_enqueued
180
+ The attributes available on a job for comparison are: ```age```, ```body```,
181
+ ```buries```, ```connection```, ```delay```, ```id```, ```kicks```, ```pri```,
182
+ ```releases```, ```reserves```, ```state```, ```time-left```, ```timeouts```,
183
+ ```ttr```, and ```tube```.
184
+
185
+ To assert or set the expectation that a job with the body of 'foo'
186
+ should have been enqueued on the default tube you could use the following:
187
+ ```
188
+ # TestUnit/MiniTest
189
+ assert_enqueued(:tube => 'default', :body => 'foo')
190
+
191
+ # You could also use a Regexp if you prefer
192
+ # RSpec
193
+ should have_enqueued(:tube => 'default', :body => /foo/)
194
+ ```
195
+
196
+ #####assert_enqueues/have_enqueued
197
+ The assert_enqueues assertion and the have_enqueued matcher also support a
198
+ block form that will only check jobs enqueued during the execution of the given
199
+ block for matches. For example, if you wanted to assert or set the expectation
200
+ that a particular method caused a job to be enqueued to the exports tube in the
201
+ ready state, you could use something like the following:
202
+ ```
203
+ # TestUnit/MiniTest
204
+ assert_enqueues(:tube => 'exports', :state => 'ready') do
205
+ method_that_should_enqueue_a_job
206
+ end
207
+
208
+ # The form is a little different in RSpec:
209
+ proc do
210
+ method_that_should_enqueue_a_job
211
+ end.should have_enqueued(:tube => 'exports', :state => 'ready')
212
+ ```
213
+
214
+ The refutations/negative matches work the same way:
215
+ ```
216
+ # TestUnit/MiniTest
217
+ refute_enqueues(:tube => 'exports', :state => 'ready') do
218
+ method_that_should_not_enqueue_a_job
219
+ end
220
+
221
+ # RSpec:
222
+ proc do
223
+ method_that_should_enqueue_a_job
224
+ end.should_not have_enqueued(:tube => 'exports', :state => 'ready')
225
+ ```
226
+
227
+ The options Hash for enqueued/enqueues assertions/matchers may additionally
228
+ include a count key ('count' or :count) that can be used to verify that a
229
+ particular number of matching jobs are found.
230
+
231
+ If no count option is provided, the assertion/match succeeds if any job is found
232
+ that matches all of the options given. If no jobs are found that match the
233
+ options given, the assertion fails.
234
+
235
+ If a count option is provided the assertion/matcher only succeeds if the
236
+ triple-equal (===) operator/method of the value of the provided count evaluates
237
+ to true when given the total number of matching jobs. Otherwise the assertion
238
+ fails. The use of === allows for more advanced comparisons using Procs, Ranges,
239
+ Regexps, etc.
240
+ ```
241
+ # TestUnit/MiniTest
242
+ assert_enqueues(:tube => 'default', :body => 'foo', :count => 1) do
243
+ method_that_should_enqueue_exactly_one_job
244
+ end
245
+
246
+ # You could also use a Range if you prefer
247
+ # RSpec
248
+ proc do
249
+ method_that_should_enqueue_between_1_and_3_jobs
250
+ end.should have_enqueued(:tube => 'default', :body => /foo/, :count => 1..3)
251
+ ```
252
+
253
+ A count key can also be used with the refutations/negative matches, but tends
254
+ to be better stated as an assertion/positive match with a count.
255
+
256
+ #####assert_tube/have_tube
257
+ Similar to the other assertions/matchers, assert_tube and have_tube take a Hash
258
+ of options that will be used to find matching tubes. Each key in the options
259
+ Hash is a String or a Symbol that identifies an attribute of a tube that the
260
+ corresponding Hash value should be compared against. All attribute comparisons
261
+ are performed using the triple-equal (===) operator/method of the given value.
262
+
263
+ The attributes available on a tube for matching are: ```cmd-delete```,
264
+ ```cmd-pause-tube```, ```current-jobs-buried```, ```current-jobs-delayed```,
265
+ ```current-jobs-ready```, ```current-jobs-reserved```,
266
+ ```current-jobs-urgent```, ```current-using```, ```current-waiting```,
267
+ ```current-watching```, ```name```, ```pause```, ```pause-time-left```,
268
+ and ```total-jobs```.
269
+
270
+ For example to assert that no connections are waiting on the default tube
271
+ something like the following could be used:
272
+ ```
273
+ # TestUnit/MiniTest
274
+ assert_tube(:name => 'default', 'current-waiting' => 0)
275
+
276
+ # RSpec
277
+ should have_tube(:name => 'default', 'current-waiting' => 0)
278
+ ```
279
+
280
+ Similarly one could use refute_tube or the negative matcher for have_tube to
281
+ verify that the exports tube is paused:
282
+ ```
283
+ # TestUnit/MiniTest
284
+ refute_tube(:name => 'exports', :pause => 0)
285
+
286
+ #RSpec
287
+ should_not have_tube(:name => 'exports', :pause => 0)
288
+ ```
289
+
290
+ For more detailed explanations and more examples make sure to check out the
291
+ docs, expectations, and respective tests:
292
+ [docs](http://rubydoc.info/gems/bean_counter/0.0.1/frames)
293
+ [enqueued_expectation](https://github.com/gemeraldbeanstalk/bean_counter/tree/master/lib/bean_counter/enqueued_expectation.rb)
294
+ [tube_expectation](https://github.com/gemeraldbeanstalk/bean_counter/tree/master/lib/bean_counter/tube_expectation.rb)
295
+ [test_assertions](https://github.com/gemeraldbeanstalk/bean_counter/tree/master/lib/bean_counter/test_assertions.rb)
296
+ [spec_matchers](https://github.com/gemeraldbeanstalk/bean_counter/tree/master/lib/bean_counter/spec_matchers.rb)
297
+ [test_assertions_test](https://github.com/gemeraldbeanstalk/bean_counter/tree/master/test/unit/test_assertions_test.rb)
298
+ [spec_spec](https://github.com/gemeraldbeanstalk/bean_counter/tree/master/spec/spec_spec.rb)
299
+
300
+ ## Contributing
301
+
302
+ 1. Fork it
303
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
304
+ 3. Commit your changes (`git commit -am 'Added some feature'`)
305
+ 4. Push to the branch (`git push origin my-new-feature`)
306
+ 5. Create new Pull Request
@@ -0,0 +1,38 @@
1
+ require 'bundler/gem_tasks'
2
+ require 'rake/testtask'
3
+ require 'rspec/core/rake_task'
4
+ require 'coveralls/rake/task'
5
+ require 'English'
6
+
7
+ Rake::TestTask.new do |t|
8
+ t.libs << 'test'
9
+ t.pattern = 'test/**/*_test.rb'
10
+ end
11
+
12
+ RSpec::Core::RakeTask.new(:spec)
13
+
14
+ Coveralls::RakeTask.new
15
+
16
+ task :test_all do
17
+ failures = []
18
+
19
+ fork do
20
+ Rake::Task[:test].invoke
21
+ end
22
+ Process.wait
23
+ failures << 'TestUnit' unless $CHILD_STATUS.exitstatus == 0
24
+
25
+ fork do
26
+ Rake::Task[:spec].invoke
27
+ end
28
+ Process.wait
29
+ failures << 'RSpec' unless $CHILD_STATUS.exitstatus == 0
30
+
31
+ Rake::Task['coveralls:push'].invoke if ENV['CI']
32
+
33
+ if failures.any?
34
+ raise RuntimeError, "\n\nTest failures occured in test suite(s): #{failures.join(', ')}\n", []
35
+ end
36
+ end
37
+
38
+ task :default => :test_all
@@ -0,0 +1,26 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'bean_counter/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = 'bean_counter'
8
+ spec.version = BeanCounter::VERSION
9
+ spec.authors = ['gemeraldbeanstalk']
10
+ spec.email = ['gemeraldbeanstalk@gmail.com ']
11
+ spec.description = %q{Test::Unit assertions for Beaneater}
12
+ spec.summary = %q{BeanCounter provides additional assertions for testing Ruby code that relies on Beaneater}
13
+ spec.homepage = 'https://github.com/gemeraldbeanstalk/bean_counter'
14
+ spec.license = 'MIT'
15
+
16
+ spec.files = `git ls-files`.split($/)
17
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
18
+ spec.test_files = spec.files.grep(%r{^test/})
19
+ spec.require_paths = ['lib']
20
+
21
+ spec.add_dependency 'beaneater'
22
+ spec.add_dependency 'stalk_climber', '>= 0.1.0'
23
+
24
+ spec.add_development_dependency 'bundler', '~> 1.3'
25
+ spec.add_development_dependency 'rake'
26
+ end