thread_queues 0.0.0 → 0.0.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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 9ea8098a4b776f92b6830ca986b09277cd5ecc43
4
- data.tar.gz: 8d07b550c6eaa33739303921c0503a6eaebab004
3
+ metadata.gz: 4b38032825492e127d0224677e7413270630db36
4
+ data.tar.gz: ee89762a8e71d274892be46b485a30b0e5a52693
5
5
  SHA512:
6
- metadata.gz: 57e4074340c6f1edb561bcef380d2eecebd7aeccc8e61f02ebbbc5a27a4adc5b9a1510e23b5fb3d960b612fe9bb126f6522ec7aa5597df6c1664ee8c192c67f1
7
- data.tar.gz: e04edca9a185a43bf926da9bb7a21e9a9e85828b866f71f6fd2f7aa079ee767d85667304b492dc5fd60e64a7a0a609a3cac15bcf6f1446752e88eb6b2f3394f1
6
+ metadata.gz: 4cc527684a025a7994699846b33861a9ba2c059692ab679e76b3f3ab47dd542fb7a21371bdeff4047f79375c5d319f3a598a2ff3f178620d682048fb6c775fff
7
+ data.tar.gz: 1e8eba3f9bd51c90fedb14d1800dd304cbf3a7f80a7b4fec49e9bc9b267115eb0865bdd3cd7e00b11b373d9012c34ded0490c676775d9bdf328d0dbf7525ec9b
data/README.md CHANGED
@@ -7,7 +7,7 @@
7
7
  ## Installation
8
8
 
9
9
  ```ruby
10
- gem "thead_queues"
10
+ gem "thread_queues"
11
11
  ```
12
12
 
13
13
  ## Usage
@@ -54,6 +54,39 @@ end
54
54
  queue.pop #=> EOFError
55
55
  ```
56
56
 
57
+ ### StringBuffer
58
+
59
+ ```ruby
60
+ queue = ThreadQueues::BufferedQueue.new(5)
61
+ string_buffer = ThreadQueues::StringBuffer.new(queue)
62
+
63
+ Thread.new do
64
+ queue.push("hel")
65
+ queue.push("lo\n")
66
+ queue.push("my\r\n")
67
+ queue.push("nam")
68
+ queue.push("e\n")
69
+ queue.push("is kasper\n")
70
+ queue.close
71
+ end
72
+ ```
73
+
74
+ Get each line
75
+ ```ruby
76
+ string_buffer.gets #=> "hello\n"
77
+ string_buffer.gets #=> "my\r\n"
78
+ string_buffer.gets #=> "name\n"
79
+ string_buffer.gets #=> "is kasper\n"
80
+ ```
81
+
82
+ Get specified lengths
83
+ ```ruby
84
+ string_buffer.read(6) #=> "hello\n"
85
+ string_buffer.read(4) #=> "my\r\n"
86
+ string_buffer.read(5) #=> "name\n"
87
+ string_buffer.read(10) #=> "is kasper\n"
88
+ ```
89
+
57
90
  ## Contributing to thread_queues
58
91
 
59
92
  * Check out the latest master to make sure the feature hasn't been implemented or the bug hasn't been fixed yet.
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.0.0
1
+ 0.0.1
@@ -0,0 +1,57 @@
1
+ require "monitor"
2
+
3
+ class ThreadQueues::StringBuffer
4
+ def initialize(queue)
5
+ @queue = queue
6
+ @buffer = ""
7
+ @mutex = Monitor.new
8
+ end
9
+
10
+ def gets
11
+ @mutex.synchronize do
12
+ loop do
13
+ if match = @buffer.match(/\A(.+)\n/)
14
+ @buffer.gsub!(/\A(.+)\n/, "")
15
+ return match[0]
16
+ end
17
+
18
+ begin
19
+ store_more_in_buffer
20
+ rescue EOFError => e
21
+ return @buffer if @buffer.length > 0
22
+ raise e
23
+ end
24
+ end
25
+ end
26
+ end
27
+
28
+ def read(length)
29
+ @mutex.synchronize do
30
+ loop do
31
+ if @buffer.length >= length
32
+ return @buffer.slice!(0, length)
33
+ end
34
+
35
+ begin
36
+ store_more_in_buffer
37
+ rescue EOFError => e
38
+ return @buffer if @buffer.length > 0
39
+ raise e
40
+ end
41
+ end
42
+ end
43
+ end
44
+
45
+ private
46
+
47
+ def store_more_in_buffer
48
+ @mutex.synchronize do
49
+ begin
50
+ @buffer << @queue.pop.to_s
51
+ rescue Exception => e
52
+ raise EOFError, "No live threads left. Deadlock?" if e.message == "No live threads left. Deadlock?"
53
+ raise e
54
+ end
55
+ end
56
+ end
57
+ end
@@ -0,0 +1,51 @@
1
+ require "spec_helper"
2
+
3
+ describe ThreadQueues::StringBuffer do
4
+ it "works with gets" do
5
+ queue = ThreadQueues::BlockingQueue.new
6
+ string_buffer = ThreadQueues::StringBuffer.new(queue)
7
+
8
+ Thread.new do
9
+ Thread.current.abort_on_exception = true
10
+
11
+ queue.push("hel")
12
+ queue.push("lo\n")
13
+ queue.push("my\r\n")
14
+ queue.push("nam")
15
+ queue.push("e\n")
16
+ queue.push("is kasper\n")
17
+ queue.close
18
+ end
19
+
20
+ string_buffer.gets.should eq "hello\n"
21
+ string_buffer.gets.should eq "my\r\n"
22
+ string_buffer.gets.should eq "name\n"
23
+ string_buffer.gets.should eq "is kasper\n"
24
+
25
+ expect { string_buffer.gets }.to raise_error(EOFError)
26
+ end
27
+
28
+ it "should read the right lengths" do
29
+ queue = ThreadQueues::BufferedQueue.new(5)
30
+ string_buffer = ThreadQueues::StringBuffer.new(queue)
31
+
32
+ Thread.new do
33
+ Thread.current.abort_on_exception = true
34
+
35
+ queue.push("hel")
36
+ queue.push("lo\n")
37
+ queue.push("my\r\n")
38
+ queue.push("nam")
39
+ queue.push("e\n")
40
+ queue.push("is kasper\n")
41
+ queue.close
42
+ end
43
+
44
+ string_buffer.read(6).should eq "hello\n"
45
+ string_buffer.read(4).should eq "my\r\n"
46
+ string_buffer.read(5).should eq "name\n"
47
+ string_buffer.read(10).should eq "is kasper\n"
48
+
49
+ expect { string_buffer.gets }.to raise_error(EOFError)
50
+ end
51
+ end
@@ -2,11 +2,11 @@
2
2
  # DO NOT EDIT THIS FILE DIRECTLY
3
3
  # Instead, edit Jeweler::Tasks in Rakefile, and run 'rake gemspec'
4
4
  # -*- encoding: utf-8 -*-
5
- # stub: thread_queues 0.0.0 ruby lib
5
+ # stub: thread_queues 0.0.1 ruby lib
6
6
 
7
7
  Gem::Specification.new do |s|
8
8
  s.name = "thread_queues"
9
- s.version = "0.0.0"
9
+ s.version = "0.0.1"
10
10
 
11
11
  s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
12
12
  s.require_paths = ["lib"]
@@ -30,10 +30,12 @@ Gem::Specification.new do |s|
30
30
  "lib/thread_queues.rb",
31
31
  "lib/thread_queues/blocking_queue.rb",
32
32
  "lib/thread_queues/buffered_queue.rb",
33
+ "lib/thread_queues/string_buffer.rb",
33
34
  "shippable.yml",
34
35
  "spec/spec_helper.rb",
35
36
  "spec/thread_queues/blocking_queue_spec.rb",
36
37
  "spec/thread_queues/buffered_queue_spec.rb",
38
+ "spec/thread_queues/string_buffer_spec.rb",
37
39
  "spec/thread_queues_spec.rb",
38
40
  "thread_queues.gemspec"
39
41
  ]
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: thread_queues
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.0
4
+ version: 0.0.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - kaspernj
@@ -113,10 +113,12 @@ files:
113
113
  - lib/thread_queues.rb
114
114
  - lib/thread_queues/blocking_queue.rb
115
115
  - lib/thread_queues/buffered_queue.rb
116
+ - lib/thread_queues/string_buffer.rb
116
117
  - shippable.yml
117
118
  - spec/spec_helper.rb
118
119
  - spec/thread_queues/blocking_queue_spec.rb
119
120
  - spec/thread_queues/buffered_queue_spec.rb
121
+ - spec/thread_queues/string_buffer_spec.rb
120
122
  - spec/thread_queues_spec.rb
121
123
  - thread_queues.gemspec
122
124
  homepage: http://github.com/kaspernj/thread_queues