turnpike 0.0.2 → 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- data/lib/turnpike/version.rb +1 -1
- data/lib/turnpike.rb +20 -16
- data/test/turnpike_test.rb +18 -9
- metadata +5 -5
data/lib/turnpike/version.rb
CHANGED
data/lib/turnpike.rb
CHANGED
@@ -2,8 +2,14 @@ require 'redis'
|
|
2
2
|
|
3
3
|
# A Redis-backed queue.
|
4
4
|
class Turnpike
|
5
|
-
|
6
|
-
|
5
|
+
class << self
|
6
|
+
# Timeout, in seconds, for blocking `pop` or `shift`.
|
7
|
+
attr_accessor :timeout
|
8
|
+
|
9
|
+
def configure(&block)
|
10
|
+
yield self
|
11
|
+
end
|
12
|
+
end
|
7
13
|
|
8
14
|
# The name of the queue.
|
9
15
|
attr :name
|
@@ -31,6 +37,9 @@ class Turnpike
|
|
31
37
|
end
|
32
38
|
|
33
39
|
# Iterates the given block for each slice of `n` queued items.
|
40
|
+
#
|
41
|
+
# Takes an optional boolean argument to specify if the command should block
|
42
|
+
# the connection when the queue is empty. This argument defaults to false.
|
34
43
|
def each_slice(n, blocking = false, &block)
|
35
44
|
slice = []
|
36
45
|
|
@@ -73,7 +82,7 @@ class Turnpike
|
|
73
82
|
# the connection when the queue is empty. This argument defaults to false.
|
74
83
|
def pop(blocking = false)
|
75
84
|
if blocking
|
76
|
-
redis.brpop(name,
|
85
|
+
redis.brpop(name, timeout)[1] rescue nil
|
77
86
|
else
|
78
87
|
redis.rpop(name)
|
79
88
|
end
|
@@ -81,12 +90,7 @@ class Turnpike
|
|
81
90
|
|
82
91
|
# Pushes items to the end of the queue.
|
83
92
|
def push(*items)
|
84
|
-
|
85
|
-
if REDIS_VERSION < '2.3'
|
86
|
-
items.each { |item| redis.rpush(name, item) }
|
87
|
-
else
|
88
|
-
redis.rpush(name, items)
|
89
|
-
end
|
93
|
+
items.each { |item| redis.rpush(name, item) }
|
90
94
|
end
|
91
95
|
alias << push
|
92
96
|
|
@@ -96,7 +100,7 @@ class Turnpike
|
|
96
100
|
# the connection when the queue is empty. This argument defaults to false.
|
97
101
|
def shift(blocking = false)
|
98
102
|
if blocking
|
99
|
-
redis.blpop(name,
|
103
|
+
redis.blpop(name, timeout)[1] rescue nil
|
100
104
|
else
|
101
105
|
redis.lpop(name)
|
102
106
|
end
|
@@ -104,12 +108,7 @@ class Turnpike
|
|
104
108
|
|
105
109
|
# Pushes items to the front of the queue.
|
106
110
|
def unshift(*items)
|
107
|
-
|
108
|
-
if REDIS_VERSION < '2.3'
|
109
|
-
items.each { |item| redis.lpush(name, item) }
|
110
|
-
else
|
111
|
-
redis.lpush(name, items)
|
112
|
-
end
|
111
|
+
items.each { |item| redis.lpush(name, item) }
|
113
112
|
end
|
114
113
|
|
115
114
|
private
|
@@ -117,4 +116,9 @@ class Turnpike
|
|
117
116
|
def redis
|
118
117
|
Redis.current
|
119
118
|
end
|
119
|
+
|
120
|
+
def timeout
|
121
|
+
# Timeout will default to 0, which blocks indefinitely.
|
122
|
+
self.class.timeout.to_i
|
123
|
+
end
|
120
124
|
end
|
data/test/turnpike_test.rb
CHANGED
@@ -10,6 +10,13 @@ class TestTurnpike < Test::Unit::TestCase
|
|
10
10
|
Redis.current.flushall
|
11
11
|
end
|
12
12
|
|
13
|
+
def time_out_in(seconds, &block)
|
14
|
+
original_timeout = Turnpike.timeout
|
15
|
+
Turnpike.configure { |c| c.timeout = seconds }
|
16
|
+
block.call
|
17
|
+
Turnpike.configure { |c| c.timeout = original_timeout }
|
18
|
+
end
|
19
|
+
|
13
20
|
def test_emptiness
|
14
21
|
queue = Turnpike.new
|
15
22
|
assert(queue.empty?)
|
@@ -117,15 +124,17 @@ class TestTurnpike < Test::Unit::TestCase
|
|
117
124
|
end
|
118
125
|
|
119
126
|
def test_timeout
|
120
|
-
|
121
|
-
|
122
|
-
|
123
|
-
|
127
|
+
time_out_in 1 do
|
128
|
+
queue = Turnpike.new
|
129
|
+
thread = Thread.new do
|
130
|
+
sleep(2)
|
131
|
+
queue.push(1)
|
132
|
+
end
|
133
|
+
assert_equal(0, queue.length)
|
134
|
+
assert_equal(nil, queue.shift(true))
|
135
|
+
|
136
|
+
thread.join
|
137
|
+
assert_equal(1, queue.length)
|
124
138
|
end
|
125
|
-
assert_equal(0, queue.length)
|
126
|
-
assert_equal(nil, queue.shift(true))
|
127
|
-
|
128
|
-
thread.join
|
129
|
-
assert_equal(1, queue.length)
|
130
139
|
end
|
131
140
|
end
|
metadata
CHANGED
@@ -4,9 +4,9 @@ version: !ruby/object:Gem::Version
|
|
4
4
|
prerelease: false
|
5
5
|
segments:
|
6
6
|
- 0
|
7
|
+
- 1
|
7
8
|
- 0
|
8
|
-
|
9
|
-
version: 0.0.2
|
9
|
+
version: 0.1.0
|
10
10
|
platform: ruby
|
11
11
|
authors:
|
12
12
|
- Paper Cavalier
|
@@ -14,7 +14,7 @@ autorequire:
|
|
14
14
|
bindir: bin
|
15
15
|
cert_chain: []
|
16
16
|
|
17
|
-
date: 2011-07-
|
17
|
+
date: 2011-07-18 00:00:00 +01:00
|
18
18
|
default_executable:
|
19
19
|
dependencies:
|
20
20
|
- !ruby/object:Gem::Dependency
|
@@ -80,7 +80,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
80
80
|
requirements:
|
81
81
|
- - ">="
|
82
82
|
- !ruby/object:Gem::Version
|
83
|
-
hash:
|
83
|
+
hash: 1142159480707083776
|
84
84
|
segments:
|
85
85
|
- 0
|
86
86
|
version: "0"
|
@@ -89,7 +89,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
89
89
|
requirements:
|
90
90
|
- - ">="
|
91
91
|
- !ruby/object:Gem::Version
|
92
|
-
hash:
|
92
|
+
hash: 1142159480707083776
|
93
93
|
segments:
|
94
94
|
- 0
|
95
95
|
version: "0"
|