turnpike 0.7.1 → 0.7.2

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 1bef65c2725cc692f91f86c187db23d3dc7250e7
4
- data.tar.gz: 3fddd1e0fc075d820b9ae6c00b006e42bfe0e599
3
+ metadata.gz: 6ee7b6e304b2d256bc1d0712c8b2bf806cd466b9
4
+ data.tar.gz: 8d0df7c09cb7e9960ea276f4a08ee50b20274497
5
5
  SHA512:
6
- metadata.gz: 18f0f8e0481d419b622a86d708850694f6254063a954d4b4523dfeafda8f8fd48eef7699a89419f7675a0f0bde9b881dc43302b0927f8fcdd2050ca9aa234c5b
7
- data.tar.gz: d6358963988ee4e42a24c6b7bc887c2d147cd65c776361296746c662a94a2e329c2d6596459966b9b7b76a3d6f543dd948caea8836c418be2d9973cb3f96c87c
6
+ metadata.gz: 2ab3f5a0ff811be7a0bc8472b84ffcee417b6983c96d32e5399de0656230e5f457c1e26674cf6738432e9ba82166c7de1424ebc5e841558a5287a78bbeb49e99
7
+ data.tar.gz: e56acce950f90bd741d388a8f2974193ca2aaaaa379162d42ef9e936fb2530aebc6133edf8e4a431b9fb040fcfc715141658d7a5494b02340438bc9923af2bae
@@ -1,5 +1,4 @@
1
1
  rvm:
2
- - 1.9.3
3
2
  - 2.0.0
4
3
  services:
5
4
  - redis-server
data/LICENSE CHANGED
@@ -1,6 +1,6 @@
1
1
  (The MIT License)
2
2
 
3
- Copyright (c) 2012 Hakan Ensari
3
+ Copyright (c) 2013 Hakan Ensari
4
4
 
5
5
  Permission is hereby granted, free of charge, to any person obtaining
6
6
  a copy of this software and associated documentation files (the
data/README.md CHANGED
@@ -24,7 +24,7 @@ Push payload to the front of the queue:
24
24
 
25
25
  ```ruby
26
26
  q.unshift('foo') # => 2
27
- q.pop # => 'foo"'
27
+ q.pop # => 'foo'
28
28
  ```
29
29
 
30
30
  Use a queue with set-like properties to ensure uniqueness of queued items:
@@ -2,7 +2,14 @@ require 'turnpike/queue'
2
2
  require 'turnpike/unique_queue'
3
3
 
4
4
  module Turnpike
5
- def self.call(name = 'default', unique: false)
6
- (unique ? UniqueQueue : Queue).new(name)
5
+ class << self
6
+ attr_accessor :namespace
7
+
8
+ def call(name = 'default', unique: false)
9
+ (unique ? UniqueQueue : Queue).new(name)
10
+ end
11
+ alias [] call
7
12
  end
13
+
14
+ @namespace = 'turnpike'
8
15
  end
@@ -6,13 +6,13 @@ module Turnpike
6
6
  class Base
7
7
  extend Forwardable
8
8
 
9
- def_delegator :Redis, :current, :redis
10
9
  def_delegators :MessagePack, :pack, :unpack
11
10
 
12
- attr :name
11
+ attr :name, :redis
13
12
 
14
- def initialize(name)
15
- @name = "turnpike:#{name}"
13
+ def initialize(name, redis: Redis.current)
14
+ @name = "#{Turnpike.namespace}:#{name}"
15
+ @redis = redis
16
16
  end
17
17
 
18
18
  def clear
@@ -24,6 +24,7 @@ module Turnpike
24
24
  end
25
25
 
26
26
  def pop(*); raise NotImplementedError; end
27
+ def bpop(*); raise NotImplementedError; end
27
28
  def push(*); raise NotImplementedError; end
28
29
  def size; raise NotImplementedError; end
29
30
  def unshift(*); raise NotImplementedError; end
@@ -6,17 +6,26 @@ module Turnpike
6
6
  #
7
7
  # n - Integer number of items to pop.
8
8
  #
9
- # Returns a String item, an Array of items, or nil if the queue is empty.
9
+ # Returns an item, an Array of items, or nil if the queue is empty.
10
10
  def pop(n = 1)
11
- items = []
12
- n.times do
13
- break unless item = redis.lpop(name)
14
- items << unpack(item)
15
- end
11
+ items = n.times.reduce([]) { |ary, _|
12
+ break ary unless item = redis.lpop(name)
13
+ ary << unpack(item)
14
+ }
16
15
 
17
16
  n == 1 ? items.first : items
18
17
  end
19
18
 
19
+ # Pop one or more items from the queue and block if the queue is empty.
20
+ #
21
+ # n - Integer number of items to pop.
22
+ #
23
+ # Returns an item or an Array of items.
24
+ def bpop(n = 1)
25
+ items = n.times.map { unpack(redis.blpop(name).last) }
26
+ n == 1 ? items.first : items
27
+ end
28
+
20
29
  # Push items to the end of the queue.
21
30
  #
22
31
  # items - A splat Array of items.
@@ -15,15 +15,9 @@ module Turnpike
15
15
  #
16
16
  # n - Integer number of items to pop.
17
17
  #
18
- # Returns a String item, an Array of items, or nil if the queue is empty.
18
+ # Returns an item, an Array of items, or nil if the queue is empty.
19
19
  def pop(n = 1)
20
- items = begin
21
- redis.evalsha(ZPOP_SHA, [name, n])
22
- rescue Redis::CommandError
23
- redis.eval(ZPOP, [name, n])
24
- end
25
- items.map! { |i| unpack(i) }
26
-
20
+ items = zpop(n).map { |i| unpack(i) }
27
21
  n == 1 ? items.first : items
28
22
  end
29
23
 
@@ -52,5 +46,13 @@ module Turnpike
52
46
  _, score = redis.zrange(name, 0, 0, with_scores: true).pop
53
47
  score ? push(*items, score: score - 1) : push(*items)
54
48
  end
49
+
50
+ private
51
+
52
+ def zpop(n)
53
+ redis.evalsha(ZPOP_SHA, [name, n])
54
+ rescue Redis::CommandError
55
+ redis.eval(ZPOP, [name, n])
56
+ end
55
57
  end
56
58
  end
@@ -1,3 +1,3 @@
1
1
  module Turnpike
2
- VERSION = '0.7.1'
2
+ VERSION = '0.7.2'
3
3
  end
@@ -4,8 +4,13 @@ require 'minitest/benchmark' if ENV['BENCH']
4
4
  require 'turnpike'
5
5
 
6
6
  class TestTurnpike < MiniTest::Unit::TestCase
7
- def test_call
7
+ def test_lambdalike
8
8
  assert_kind_of Turnpike::Queue, Turnpike.call
9
+ assert_kind_of Turnpike::Queue, Turnpike.()
10
+ assert_kind_of Turnpike::Queue, Turnpike[]
11
+ end
12
+
13
+ def test_unique_call
9
14
  assert_kind_of Turnpike::UniqueQueue, Turnpike.call(unique: true)
10
15
  end
11
16
 
@@ -14,9 +19,17 @@ class TestTurnpike < MiniTest::Unit::TestCase
14
19
  assert_includes queue.name, 'foo'
15
20
  end
16
21
 
22
+ def test_namespace
23
+ orig = Turnpike.namespace
24
+ Turnpike.namespace = 'foo'
25
+ queue = Turnpike::Base.new('bar')
26
+ assert_equal queue.name, 'foo:bar'
27
+ Turnpike.namespace = orig
28
+ end
29
+
17
30
  def test_abstract_class
18
31
  queue = Turnpike::Base.new('foo')
19
- %w(push pop unshift size).each do |mth|
32
+ %w(push pop bpop unshift size).each do |mth|
20
33
  assert_raises(NotImplementedError) { queue.send(mth) }
21
34
  end
22
35
  end
@@ -109,8 +122,31 @@ class TestQueue < MiniTest::Unit::TestCase
109
122
  def peek(queue)
110
123
  queue.redis.lrange(queue.name, 0, -1).map { |i| queue.unpack(i) }
111
124
  end
112
- end
113
125
 
126
+ def test_blocking_pop
127
+ Thread.new {
128
+ sleep 0.1
129
+ klass.new('foo', redis: Redis.new).push(3)
130
+ queue.push(3)
131
+ }
132
+ queue.push(1, 2)
133
+ assert_equal [1, 2], queue.bpop(2)
134
+ assert_equal 3, queue.bpop
135
+ end
136
+
137
+ def test_non_blocking_enumeration
138
+ klass.class_eval do
139
+ include Enumerable
140
+ def each(&blk)
141
+ while x = pop
142
+ blk.call(x)
143
+ end
144
+ end
145
+ end
146
+ queue.push(1, 2)
147
+ assert_equal [1, 2], queue.to_a
148
+ end
149
+ end
114
150
 
115
151
  class TestUniqueQueue < MiniTest::Unit::TestCase
116
152
  include QueueTests
@@ -120,4 +156,8 @@ class TestUniqueQueue < MiniTest::Unit::TestCase
120
156
  def peek(queue)
121
157
  queue.redis.zrange(queue.name, 0, -1).map { |i| queue.unpack(i) }
122
158
  end
159
+
160
+ def test_blocking_pop
161
+ assert_raises(NotImplementedError) { queue.bpop }
162
+ end
123
163
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: turnpike
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.7.1
4
+ version: 0.7.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Hakan Ensari
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2013-05-15 00:00:00.000000000 Z
11
+ date: 2013-05-21 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: redis
@@ -91,7 +91,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
91
91
  version: '0'
92
92
  requirements: []
93
93
  rubyforge_project:
94
- rubygems_version: 2.0.0
94
+ rubygems_version: 2.0.2
95
95
  signing_key:
96
96
  specification_version: 4
97
97
  summary: A Redis-backed FIFO queue