timeout_queue 0.1.0 → 0.1.1
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.
- checksums.yaml +4 -4
- data/lib/timeout_queue.rb +47 -12
- data/lib/timeout_queue/version.rb +1 -1
- data/test/close.rb +38 -0
- data/test/pop_closed.rb +69 -0
- metadata +5 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 29d93c187aaecf84b0dcfb32c5a05ae16bc6aa4b82636ffa43438ac731fd3ca7
|
4
|
+
data.tar.gz: c24440e09eab0ac50c295123b5faf95a9bf06757c2740ca633bb234a9e994c4d
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 41127cc7cd3cfc8e4b6fecd6d9b10a1e8c7023f95b1251450a29aacdc52902a22a541bd8128d1ede25550d864798cdbea93110c29b3aa4f37eb98f896f899bb0
|
7
|
+
data.tar.gz: 591f454b9cdfd8f9f882339f1c0711cd3f460df334da756af30b65343eb10d7f3b20e3b46965e70fff00f5728c25bfbe931e1581e447e5c9f1f2aea80efd1666
|
data/lib/timeout_queue.rb
CHANGED
@@ -5,6 +5,8 @@ class TimeoutQueue
|
|
5
5
|
@queue = []
|
6
6
|
@mutex = Mutex.new
|
7
7
|
@received = ConditionVariable.new
|
8
|
+
@closed = false
|
9
|
+
@waiting = []
|
8
10
|
|
9
11
|
end
|
10
12
|
|
@@ -14,7 +16,7 @@ class TimeoutQueue
|
|
14
16
|
# @return object
|
15
17
|
#
|
16
18
|
def push(object, **opt)
|
17
|
-
__push do
|
19
|
+
__push(object) do
|
18
20
|
@queue.send(__method__, object)
|
19
21
|
end
|
20
22
|
end
|
@@ -25,7 +27,7 @@ class TimeoutQueue
|
|
25
27
|
# @return object
|
26
28
|
#
|
27
29
|
def unshift(object)
|
28
|
-
__push do
|
30
|
+
__push(object) do
|
29
31
|
@queue.send(__method__, object)
|
30
32
|
end
|
31
33
|
end
|
@@ -58,15 +60,15 @@ class TimeoutQueue
|
|
58
60
|
|
59
61
|
timeout = opts[:timeout]
|
60
62
|
|
61
|
-
if timeout
|
62
|
-
|
63
|
-
end_time = Time.now + timeout.to_f
|
64
|
-
|
65
|
-
end
|
66
|
-
|
67
63
|
with_mutex do
|
64
|
+
|
65
|
+
@waiting << Thread.current
|
66
|
+
|
67
|
+
if timeout
|
68
|
+
end_time = Time.now + timeout.to_f
|
69
|
+
end
|
68
70
|
|
69
|
-
while @queue.empty? and not(non_block)
|
71
|
+
while @queue.empty? and not(non_block) and not(@closed)
|
70
72
|
|
71
73
|
if timeout
|
72
74
|
|
@@ -82,13 +84,45 @@ class TimeoutQueue
|
|
82
84
|
|
83
85
|
end
|
84
86
|
|
85
|
-
|
87
|
+
@waiting.delete(Thread.current)
|
88
|
+
|
89
|
+
if @queue.empty?
|
90
|
+
|
91
|
+
if @closed
|
92
|
+
raise ClosedQueueError
|
93
|
+
else
|
94
|
+
raise ThreadError
|
95
|
+
end
|
96
|
+
|
97
|
+
else
|
86
98
|
|
87
|
-
|
99
|
+
@queue.shift
|
100
|
+
|
101
|
+
end
|
88
102
|
|
89
103
|
end
|
90
104
|
|
91
105
|
end
|
106
|
+
|
107
|
+
def num_waiting
|
108
|
+
with_mutex do
|
109
|
+
@waiting.size
|
110
|
+
end
|
111
|
+
end
|
112
|
+
|
113
|
+
def closed?
|
114
|
+
@closed
|
115
|
+
end
|
116
|
+
|
117
|
+
def close
|
118
|
+
with_mutex do
|
119
|
+
if not @closed
|
120
|
+
@closed = true
|
121
|
+
@waiting.each(&:wakeup)
|
122
|
+
end
|
123
|
+
end
|
124
|
+
self
|
125
|
+
end
|
92
126
|
|
93
127
|
def empty?
|
94
128
|
@queue.send __method__
|
@@ -112,7 +146,8 @@ class TimeoutQueue
|
|
112
146
|
end
|
113
147
|
|
114
148
|
def __push(object)
|
115
|
-
with_mutex do
|
149
|
+
with_mutex do
|
150
|
+
raise ClosedQueueError if closed?
|
116
151
|
yield
|
117
152
|
@received.signal
|
118
153
|
end
|
data/test/close.rb
ADDED
@@ -0,0 +1,38 @@
|
|
1
|
+
require 'minitest/autorun'
|
2
|
+
require 'timeout_queue'
|
3
|
+
|
4
|
+
describe "close a queue" do
|
5
|
+
|
6
|
+
let(:q){ TimeoutQueue.new }
|
7
|
+
|
8
|
+
it "is not closed to start" do
|
9
|
+
|
10
|
+
refute q.closed?
|
11
|
+
|
12
|
+
end
|
13
|
+
|
14
|
+
describe "with waiters" do
|
15
|
+
|
16
|
+
before do
|
17
|
+
|
18
|
+
q.closed?
|
19
|
+
|
20
|
+
Thread.new do
|
21
|
+
|
22
|
+
assert_raises ClosedQueueError do
|
23
|
+
q.pop
|
24
|
+
end
|
25
|
+
|
26
|
+
end
|
27
|
+
|
28
|
+
q.close
|
29
|
+
|
30
|
+
end
|
31
|
+
|
32
|
+
it "is closed" do
|
33
|
+
assert q.closed?
|
34
|
+
end
|
35
|
+
|
36
|
+
end
|
37
|
+
|
38
|
+
end
|
data/test/pop_closed.rb
ADDED
@@ -0,0 +1,69 @@
|
|
1
|
+
require 'minitest/autorun'
|
2
|
+
require 'timeout_queue'
|
3
|
+
|
4
|
+
describe "pop a closed queue" do
|
5
|
+
|
6
|
+
describe "queue is empty" do
|
7
|
+
|
8
|
+
let(:q){ TimeoutQueue.new.close }
|
9
|
+
|
10
|
+
describe "without a timeout" do
|
11
|
+
|
12
|
+
it "raises ClosedQueueError" do
|
13
|
+
|
14
|
+
assert_raises ClosedQueueError do
|
15
|
+
q.pop
|
16
|
+
end
|
17
|
+
|
18
|
+
end
|
19
|
+
|
20
|
+
end
|
21
|
+
|
22
|
+
describe "with a timeout" do
|
23
|
+
|
24
|
+
it "raises ClosedQueueError" do
|
25
|
+
|
26
|
+
assert_raises ClosedQueueError do
|
27
|
+
q.pop(timeout: 1)
|
28
|
+
end
|
29
|
+
|
30
|
+
end
|
31
|
+
|
32
|
+
end
|
33
|
+
|
34
|
+
describe "with non_blocking" do
|
35
|
+
|
36
|
+
it "raises ClosedQueueError" do
|
37
|
+
|
38
|
+
assert_raises ClosedQueueError do
|
39
|
+
q.pop(true)
|
40
|
+
end
|
41
|
+
|
42
|
+
end
|
43
|
+
|
44
|
+
end
|
45
|
+
|
46
|
+
end
|
47
|
+
|
48
|
+
describe "queue is not empty" do
|
49
|
+
|
50
|
+
let(:obj) { {} }
|
51
|
+
let(:q){ TimeoutQueue.new }
|
52
|
+
|
53
|
+
before do
|
54
|
+
|
55
|
+
q.push obj
|
56
|
+
|
57
|
+
q.close
|
58
|
+
|
59
|
+
end
|
60
|
+
|
61
|
+
it "returns obj" do
|
62
|
+
|
63
|
+
assert_equal obj, q.pop
|
64
|
+
|
65
|
+
end
|
66
|
+
|
67
|
+
end
|
68
|
+
|
69
|
+
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: timeout_queue
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.1.
|
4
|
+
version: 0.1.1
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Cameron Harper
|
@@ -46,6 +46,8 @@ extra_rdoc_files: []
|
|
46
46
|
files:
|
47
47
|
- lib/timeout_queue.rb
|
48
48
|
- lib/timeout_queue/version.rb
|
49
|
+
- test/close.rb
|
50
|
+
- test/pop_closed.rb
|
49
51
|
- test/timeout.rb
|
50
52
|
homepage: https://github.com/cjhdev/timeout_queue
|
51
53
|
licenses:
|
@@ -72,4 +74,6 @@ signing_key:
|
|
72
74
|
specification_version: 4
|
73
75
|
summary: Like Queue but with a pop timeout and a few other things
|
74
76
|
test_files:
|
77
|
+
- test/close.rb
|
78
|
+
- test/pop_closed.rb
|
75
79
|
- test/timeout.rb
|