beanpicker 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,168 @@
1
+ require File.expand_path("../../lib/beanpicker/job_server", __FILE__)
2
+
3
+ RSpec.configure do |c|
4
+ c.mock_with :rspec
5
+ end
6
+
7
+ Object.send(:remove_const, :TCPSocket)
8
+
9
+ class TCPSocket
10
+
11
+ class FakeBeanstalkSocketErrorGetsWithoutQueue < RuntimeError; end
12
+ class FakeBeanstalkSocketErrorGetsWithSepStringNotMatch < RuntimeError; end
13
+ class FakeBeanstalkSocketErrorReadWithoutQueue < RuntimeError; end
14
+ class FakeBeanstalkSocketErrorReadWithoutCorrectLength < RuntimeError; end
15
+ class FakeBeanstalkSocketErrorCantUnderstandCommand < RuntimeError; end
16
+
17
+ def self.instances
18
+ @instances ||= []
19
+ end
20
+
21
+ def self.fail
22
+ if fail_times > 0
23
+ true
24
+ else
25
+ false
26
+ end
27
+ end
28
+
29
+ def self.fail_times(n=nil)
30
+ @fail_times ||= 0
31
+ if n.nil?
32
+ if @fail_times > 0
33
+ n = @fail_times -=1
34
+ n+1
35
+ else
36
+ 0
37
+ end
38
+ else
39
+ @fail_times = n
40
+ end
41
+ end
42
+
43
+
44
+ attr_reader :host, :port, :_queue, :tube_used, :last_command
45
+ def initialize(h, p)
46
+ raise Errno::ECONNREFUSED if self.class.fail
47
+ @host = h
48
+ @port = p
49
+ @_queue = []
50
+ @tubes = ["default"]
51
+ @tube_used = "default"
52
+ @last_command = ""
53
+ @last_error = ""
54
+ @last_response = ""
55
+ @cmd_i = 0
56
+ @jobs = {}
57
+ self.class.instances << self
58
+ end
59
+
60
+ def write(s)
61
+ @last_command = s
62
+ @_queue << case s
63
+ when /^put (\d+) (\d+) (\d+) (\d+)\r\n(.*)\r\n$/m then
64
+ @jobs[@cmd_i+=1] = $5
65
+ "INSERTED #{@cmd_i}\r\n"
66
+ when /^use (.*)\r\n$/ then
67
+ @tube_used = $1
68
+ (@tubes << $1).uniq!
69
+ "USING #{$1}\r\n"
70
+ when /^reserve(-with-timeout \d+)?\r\n/
71
+ n = @jobs.keys.first
72
+ @_queue << "RESERVED #{n} #{@jobs[n].length}\r\n"
73
+ @jobs[n] + "\r\n"
74
+ when /^delete (\d+)\r\n$/ then
75
+ @jobs.delete $1
76
+ "DELETED\r\n"
77
+ when /^release (\d+) (\d+) (\d+)\r\n$/ then
78
+ "RELEASED\r\n"
79
+ when /^bury (\d+) (\d+)\r\n$/ then
80
+ "BURIED\r\n"
81
+ when /^touch (\d+)\r\n$/ then
82
+ "TOUCHED\r\n"
83
+ when /^watch (.*)\r\n$/ then
84
+ (@tubes << $1).uniq!
85
+ "WATCHING #{@tubes.size}\r\n"
86
+ when /^ignore (.*)\r\n$/ then
87
+ @tubes.select! { |x| x != $1 }
88
+ "WATCHING #{@tubes.size}\r\n"
89
+ when /^list-tubes(-watched)?\r\n$/ then
90
+ y = @tubes.to_yaml
91
+ @_queue << "OK #{y.size}\r\n"
92
+ "#{y}\r\n"
93
+ when /^list-tube-used\r\n$/ then
94
+ "USING #{@tube_used}\r\n"
95
+ else
96
+ raise FakeBeanstalkSocketErrorCantUnderstandCommand, s
97
+ end
98
+
99
+ s.length
100
+ end
101
+
102
+ def gets(s=nil)
103
+ raise FakeBeanstalkSocketErrorGetsWithoutQueue if @_queue.size == 0
104
+ if s.nil?
105
+ @last_response = @_queue.shift
106
+ else
107
+ if @_queue.join.scan(s).size > 0
108
+ i = 0
109
+ for m in @_queue
110
+ scan = m.scan(/^(.*#{s})(.*)$/m)
111
+ if scan.size == 0
112
+ i += 1
113
+ else
114
+ f = @_queue.shift(i).join
115
+ f << scan[0][0]
116
+ if scan[0][1].empty?
117
+ @_queue.shift
118
+ else
119
+ @_queue[0] = scan[0][1]
120
+ end
121
+ return f
122
+ end
123
+ end
124
+ raise FakeBeanstalkSocketErrorGetsWithSepStringNotMatch, s
125
+ else
126
+ raise FakeBeanstalkSocketErrorGetsWithSepStringNotMatch, s
127
+ end
128
+ end
129
+ end
130
+
131
+ def read(n=nil)
132
+ raise FakeBeanstalkSocketErrorReadWithoutQueue if @_queue.size == 0
133
+ raise FakeBeanstalkSocketErrorReadWithoutCorrectLength, n if not n.nil? and @_queue.join.size < n
134
+ sleep 0.1 while not n.nil? and @_queue.join.size < n
135
+
136
+ o = ""
137
+ i = 0
138
+ l = 0
139
+ for m in @_queue
140
+ if l + m.length < n
141
+ i += 1
142
+ l += m.length
143
+ else
144
+ o << @_queue.shift(i).join
145
+ if l+m.length == n
146
+ o << @_queue.shift
147
+ else
148
+ s = @_queue.first.scan(/^(.{#{n-l}})(.*)$/m)
149
+ o << s[0][0]
150
+ @_queue[0] = s[0][1]
151
+ end
152
+ @last_response = o
153
+ return o
154
+ end
155
+ end
156
+ end
157
+
158
+ def fcntl(*a)
159
+ end
160
+
161
+ def close
162
+ end
163
+
164
+ end
165
+
166
+ for m in [:debug, :info, :warn, :error, :fatal]
167
+ Logger.module_eval("def #{m}(*a); true; end")
168
+ end
metadata ADDED
@@ -0,0 +1,103 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: beanpicker
3
+ version: !ruby/object:Gem::Version
4
+ prerelease: false
5
+ segments:
6
+ - 0
7
+ - 1
8
+ - 0
9
+ version: 0.1.0
10
+ platform: ruby
11
+ authors:
12
+ - Renan Fernandes
13
+ autorequire:
14
+ bindir: bin
15
+ cert_chain: []
16
+
17
+ date: 2010-12-28 00:00:00 -04:00
18
+ default_executable: combine
19
+ dependencies:
20
+ - !ruby/object:Gem::Dependency
21
+ name: beanstalk-client
22
+ prerelease: false
23
+ requirement: &id001 !ruby/object:Gem::Requirement
24
+ none: false
25
+ requirements:
26
+ - - ">="
27
+ - !ruby/object:Gem::Version
28
+ segments:
29
+ - 0
30
+ version: "0"
31
+ type: :runtime
32
+ version_requirements: *id001
33
+ - !ruby/object:Gem::Dependency
34
+ name: rspec
35
+ prerelease: false
36
+ requirement: &id002 !ruby/object:Gem::Requirement
37
+ none: false
38
+ requirements:
39
+ - - ">="
40
+ - !ruby/object:Gem::Version
41
+ segments:
42
+ - 2
43
+ - 0
44
+ version: "2.0"
45
+ type: :development
46
+ version_requirements: *id002
47
+ description: DSL for beanstalkd, similar to Stalker/Minion but uses subprocesses
48
+ email: renan@kauamanga.com.br
49
+ executables:
50
+ - combine
51
+ extensions: []
52
+
53
+ extra_rdoc_files:
54
+ - README.markdown
55
+ files:
56
+ - .rspec
57
+ - README.markdown
58
+ - Rakefile
59
+ - beanpicker.gemspec
60
+ - bin/combine
61
+ - examples/sandwich.rb
62
+ - lib/beanpicker.rb
63
+ - lib/beanpicker/job_server.rb
64
+ - lib/beanpicker/process.rb
65
+ - lib/beanpicker/version.rb
66
+ - spec/beanpicker_spec.rb
67
+ - spec/spec_helper.rb
68
+ has_rdoc: true
69
+ homepage: http://github.com/ShadowBelmolve/beanpicker
70
+ licenses:
71
+ - MIT
72
+ post_install_message:
73
+ rdoc_options: []
74
+
75
+ require_paths:
76
+ - lib
77
+ required_ruby_version: !ruby/object:Gem::Requirement
78
+ none: false
79
+ requirements:
80
+ - - ">="
81
+ - !ruby/object:Gem::Version
82
+ segments:
83
+ - 0
84
+ version: "0"
85
+ required_rubygems_version: !ruby/object:Gem::Requirement
86
+ none: false
87
+ requirements:
88
+ - - ">="
89
+ - !ruby/object:Gem::Version
90
+ segments:
91
+ - 0
92
+ version: "0"
93
+ requirements: []
94
+
95
+ rubyforge_project:
96
+ rubygems_version: 1.3.7
97
+ signing_key:
98
+ specification_version: 3
99
+ summary: DSL for beanstalkd, similar to Stalker/Minion
100
+ test_files:
101
+ - examples/sandwich.rb
102
+ - spec/beanpicker_spec.rb
103
+ - spec/spec_helper.rb