qu-spec 0.1.2

Sign up to get free protection for your applications and to get access to all the features.
data/MIT-LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ Copyright 2011 YOURNAME
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,3 @@
1
+ # QuSpec
2
+
3
+ This project rocks and uses MIT-LICENSE.
data/Rakefile ADDED
@@ -0,0 +1,31 @@
1
+ #!/usr/bin/env rake
2
+ begin
3
+ require 'bundler/setup'
4
+ rescue LoadError
5
+ puts 'You must `gem install bundler` and `bundle install` to run rake tasks'
6
+ end
7
+ begin
8
+ require 'rdoc/task'
9
+ rescue LoadError
10
+ require 'rdoc/rdoc'
11
+ require 'rake/rdoctask'
12
+ RDoc::Task = Rake::RDocTask
13
+ end
14
+
15
+ require 'rspec/core/rake_task'
16
+
17
+ RDoc::Task.new(:rdoc) do |rdoc|
18
+ rdoc.rdoc_dir = 'rdoc'
19
+ rdoc.title = 'QuSpec'
20
+ rdoc.options << '--line-numbers'
21
+ rdoc.rdoc_files.include('README.rdoc')
22
+ rdoc.rdoc_files.include('lib/**/*.rb')
23
+ end
24
+
25
+ Bundler::GemHelper.install_tasks
26
+
27
+ RSpec::Core::RakeTask.new(:spec) do |spec|
28
+ spec.pattern = 'spec/**/*_spec.rb'
29
+ end
30
+
31
+ task :default => [ :spec ]
@@ -0,0 +1,99 @@
1
+ require 'qu'
2
+ require 'simple_uuid'
3
+
4
+ module Qu
5
+ module Backend
6
+ class Memory < Base
7
+ def initialize
8
+ @workers = Hash.new
9
+ @queues = Hash.new {|h,k| h[k] = (k == "failed" ? {} : [])}
10
+ end
11
+
12
+ def connection
13
+ @connection ||= true
14
+ end
15
+
16
+ def queues
17
+ @queues.keys.reject { |queue| @queues[queue].empty? || queue == "failed" }
18
+ end
19
+
20
+ def length(queue = 'default')
21
+ @queues[queue].length
22
+ end
23
+
24
+ def clear(queue = nil)
25
+ queue ||= queues + ['failed']
26
+ logger.info { "Clearing queues: #{queue.inspect}" }
27
+ Array(queue).each do |q|
28
+ logger.debug "Clearing queue #{q}"
29
+ @queues.delete(q)
30
+ end
31
+ end
32
+
33
+ def register_worker(worker)
34
+ logger.debug "Registering worker #{worker.id}"
35
+ @workers[worker.id] = worker.attributes.merge(:id => worker.id)
36
+ end
37
+
38
+ def unregister_worker(worker)
39
+ logger.debug "Unregistering worker #{worker.id}"
40
+ @workers.delete(worker.id)
41
+ end
42
+
43
+ def clear_workers
44
+ @workers = Hash.new
45
+ end
46
+
47
+ def workers
48
+ @workers.map do |w|
49
+ Qu::Worker.new(w)
50
+ end
51
+ end
52
+
53
+ def enqueue(payload)
54
+ payload.id = SimpleUUID::UUID.new.to_guid
55
+ @queues[payload.queue] << payload
56
+ logger.debug { "Enqueued job #{payload}" }
57
+ payload
58
+ end
59
+
60
+ def reserve(worker, options = {:block => true})
61
+ loop do
62
+ worker.queues.each do |queue|
63
+ logger.debug { "Reserving job in queue #{queue}" }
64
+ return @queues[queue].shift unless @queues[queue].empty?
65
+ end
66
+ end
67
+ end
68
+
69
+ def failed(payload, error)
70
+ @queues["failed"][payload.id] = payload
71
+ end
72
+
73
+ def requeue(id)
74
+ logger.debug "Requeuing job #{id}"
75
+ if payload = @queues["failed"].delete(id)
76
+ @queues[payload.queue] << payload
77
+ payload
78
+ else
79
+ false
80
+ end
81
+ end
82
+
83
+ def release(payload)
84
+ @queues[payload.queue] << payload
85
+ end
86
+
87
+ def completed(payload); end
88
+
89
+ def get_queue_by_name(queue = 'default')
90
+ @queues[queue.to_s]
91
+ end
92
+
93
+ def get_queue_by_klass(klass)
94
+ payload = Payload.new(:klass => klass)
95
+ get_queue_by_name(payload.queue)
96
+ end
97
+ end
98
+ end
99
+ end
@@ -0,0 +1,64 @@
1
+ require 'rspec'
2
+
3
+ module InQueueHelper
4
+ def self.extended(klass)
5
+ klass.instance_eval do
6
+ chain :in do |queue_name|
7
+ self.queue_name = queue_name
8
+ end
9
+ end
10
+ end
11
+
12
+ private
13
+
14
+ attr_accessor :queue_name
15
+
16
+ def queue(actual)
17
+ if @queue_name
18
+ Qu.backend.get_queue_by_name(@queue_name)
19
+ else
20
+ Qu.backend.get_queue_by_klass(actual)
21
+ end
22
+ end
23
+
24
+ end
25
+
26
+ RSpec::Matchers.define :have_queued do |*expected_args|
27
+ extend InQueueHelper
28
+
29
+ match do |actual|
30
+ queue(actual).any? { |entry| entry.klass.to_s == actual.to_s && entry.args == expected_args }
31
+ end
32
+
33
+ failure_message_for_should do |actual|
34
+ "expected that #{actual} would have [#{expected_args.join(', ')}] queued"
35
+ end
36
+
37
+ failure_message_for_should_not do |actual|
38
+ "expected that #{actual} would not have [#{expected_args.join(', ')}] queued"
39
+ end
40
+
41
+ description do
42
+ "have queued arguments of [#{expected_args.join(', ')}]"
43
+ end
44
+ end
45
+
46
+ RSpec::Matchers.define :have_queue_size_of do |size|
47
+ extend InQueueHelper
48
+
49
+ match do |actual|
50
+ queue(actual).size == size
51
+ end
52
+
53
+ failure_message_for_should do |actual|
54
+ "expected that #{actual} would have #{size} entries queued, but got #{queue(actual).size} instead"
55
+ end
56
+
57
+ failure_message_for_should_not do |actual|
58
+ "expected that #{actual} would not have #{size} entries queued, but got #{queue(actual).size} instead"
59
+ end
60
+
61
+ description do
62
+ "have a queue size of #{size}"
63
+ end
64
+ end
@@ -0,0 +1,3 @@
1
+ module QuSpec
2
+ VERSION = "0.1.2"
3
+ end
data/lib/qu-spec.rb ADDED
@@ -0,0 +1,15 @@
1
+ require 'qu/backend/memory'
2
+ require 'qu-spec/matchers'
3
+
4
+ module QuSpec
5
+ def self.setup!
6
+ Qu.backend = Qu::Backend::Memory.new
7
+ Qu.failure = nil
8
+ end
9
+
10
+ def self.reset!
11
+ Qu.backend.clear
12
+ end
13
+ end
14
+
15
+ QuSpec.setup!
@@ -0,0 +1,5 @@
1
+ require 'spec_helper'
2
+
3
+ describe Qu::Backend::Memory do
4
+ it_should_behave_like 'a backend'
5
+ end
@@ -0,0 +1,77 @@
1
+ require 'spec_helper'
2
+
3
+ describe "QuSpec Matchers" do
4
+ before do
5
+ QuSpec.reset!
6
+ end
7
+
8
+ let(:first_name) { 'Morton' }
9
+ let(:last_name) { 'Jonuschat' }
10
+
11
+ describe "#have_queued" do
12
+ context "queued with a class" do
13
+ before do
14
+ Qu.enqueue(Person, first_name, last_name)
15
+ end
16
+
17
+ subject { Person }
18
+
19
+ it { should have_queued(first_name, last_name) }
20
+ it { should_not have_queued(last_name, first_name) }
21
+ end
22
+
23
+ context "#in" do
24
+
25
+ before do
26
+ Qu.enqueue(Person, first_name, last_name)
27
+ end
28
+
29
+ subject { Person }
30
+
31
+ context "with #in(queue_name)" do
32
+ it { should have_queued(first_name, last_name).in(:people) }
33
+ it { should_not have_queued(last_name, first_name).in(:people) }
34
+ end
35
+ end
36
+ end
37
+
38
+ describe "#have_queue_size_of" do
39
+ context "when nothing is queued" do
40
+ subject { Person }
41
+
42
+ it "raises the approrpiate exception" do
43
+ expect do
44
+ should have_queue_size_of(1)
45
+ end.to raise_error(RSpec::Expectations::ExpectationNotMetError)
46
+ end
47
+ end
48
+
49
+ context "queued with a class" do
50
+ before do
51
+ Qu.enqueue(Person, first_name, last_name)
52
+ end
53
+
54
+ subject { Person }
55
+
56
+ it { should have_queue_size_of(1) }
57
+ end
58
+ end
59
+
60
+ describe "#in" do
61
+ before do
62
+ Qu.enqueue(Person, first_name, last_name)
63
+ end
64
+
65
+ subject { Person }
66
+
67
+ context "with #in(queue_name)" do
68
+ it { should have_queue_size_of(1).in(:people) }
69
+ it { should_not have_queue_size_of(1).in(:other_people) }
70
+ end
71
+
72
+ context "with #in('queue_name')" do
73
+ it { should have_queue_size_of(1).in('people') }
74
+ it { should_not have_queue_size_of(1).in('other_people') }
75
+ end
76
+ end
77
+ end
@@ -0,0 +1,14 @@
1
+ require 'bundler'
2
+ Bundler.require :default, :test
3
+ require 'qu'
4
+ require 'qu/backend/spec'
5
+
6
+ Dir[File.expand_path(File.join(File.dirname(__FILE__),'support','**','*.rb'))].each {|f| require f}
7
+
8
+ RSpec.configure do |config|
9
+ config.before do
10
+ QuSpec.setup!
11
+ end
12
+ end
13
+
14
+ Qu.logger.level = Logger::UNKNOWN
@@ -0,0 +1,3 @@
1
+ class Person
2
+ @queue = :people
3
+ end
metadata ADDED
@@ -0,0 +1,110 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: qu-spec
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.2
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Morton Jonuschat
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2011-12-04 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: qu
16
+ requirement: &70160136907080 !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ! '>='
20
+ - !ruby/object:Gem::Version
21
+ version: 0.1.3
22
+ type: :runtime
23
+ prerelease: false
24
+ version_requirements: *70160136907080
25
+ - !ruby/object:Gem::Dependency
26
+ name: rspec
27
+ requirement: &70160136906420 !ruby/object:Gem::Requirement
28
+ none: false
29
+ requirements:
30
+ - - ! '>='
31
+ - !ruby/object:Gem::Version
32
+ version: 2.5.0
33
+ type: :runtime
34
+ prerelease: false
35
+ version_requirements: *70160136906420
36
+ - !ruby/object:Gem::Dependency
37
+ name: simple_uuid
38
+ requirement: &70160136905640 !ruby/object:Gem::Requirement
39
+ none: false
40
+ requirements:
41
+ - - ! '>='
42
+ - !ruby/object:Gem::Version
43
+ version: 0.2.0
44
+ type: :runtime
45
+ prerelease: false
46
+ version_requirements: *70160136905640
47
+ - !ruby/object:Gem::Dependency
48
+ name: rake
49
+ requirement: &70160136904880 !ruby/object:Gem::Requirement
50
+ none: false
51
+ requirements:
52
+ - - ! '>='
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ type: :development
56
+ prerelease: false
57
+ version_requirements: *70160136904880
58
+ description: RSpec matchers and an in-memory backend for Qu
59
+ email:
60
+ - yabawock@gmail.com
61
+ executables: []
62
+ extensions: []
63
+ extra_rdoc_files: []
64
+ files:
65
+ - lib/qu/backend/memory.rb
66
+ - lib/qu-spec/matchers.rb
67
+ - lib/qu-spec/version.rb
68
+ - lib/qu-spec.rb
69
+ - MIT-LICENSE
70
+ - Rakefile
71
+ - README.md
72
+ - spec/qu/backend/memory_spec.rb
73
+ - spec/qu-spec/matchers_spec.rb
74
+ - spec/spec_helper.rb
75
+ - spec/support/test_classes.rb
76
+ homepage: https://github.com/yabawock/qu-spec
77
+ licenses: []
78
+ post_install_message:
79
+ rdoc_options: []
80
+ require_paths:
81
+ - lib
82
+ required_ruby_version: !ruby/object:Gem::Requirement
83
+ none: false
84
+ requirements:
85
+ - - ! '>='
86
+ - !ruby/object:Gem::Version
87
+ version: '0'
88
+ segments:
89
+ - 0
90
+ hash: 4087639463335953036
91
+ required_rubygems_version: !ruby/object:Gem::Requirement
92
+ none: false
93
+ requirements:
94
+ - - ! '>='
95
+ - !ruby/object:Gem::Version
96
+ version: '0'
97
+ segments:
98
+ - 0
99
+ hash: 4087639463335953036
100
+ requirements: []
101
+ rubyforge_project:
102
+ rubygems_version: 1.8.10
103
+ signing_key:
104
+ specification_version: 3
105
+ summary: RSpec matchers for Qu
106
+ test_files:
107
+ - spec/qu/backend/memory_spec.rb
108
+ - spec/qu-spec/matchers_spec.rb
109
+ - spec/spec_helper.rb
110
+ - spec/support/test_classes.rb