going 0.0.3

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 ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: d65446460582cc05ff1e4b2a7f2dd75fa8e2de02
4
+ data.tar.gz: d3bef60cf07e6f7142194c3a524aa7451e8fc46a
5
+ SHA512:
6
+ metadata.gz: e7ada7d6cfc7e6bbf816e546f84c1c5b42f0901d4d8ac6740dd13219c8c73ebecb7d30eaba45f4a08ff4e194edd12f3856253cb267200983d55a649b1eeb510f
7
+ data.tar.gz: 5b353932fcd9107fddd2edde8fbcf2b950dbd5cc798100ccb1b4fbd7e2f58acf9ba2b6e6179665082232b46f0b55bc12ea6ed1949badffc501c97926a92d9ef9
data/.gitignore ADDED
@@ -0,0 +1,15 @@
1
+ /bin/
2
+ /.bundle/
3
+ /.yardoc
4
+ /Gemfile.lock
5
+ /_yardoc/
6
+ /coverage/
7
+ /doc/
8
+ /pkg/
9
+ /spec/reports/
10
+ /tmp/
11
+ *.bundle
12
+ *.so
13
+ *.o
14
+ *.a
15
+ mkmf.log
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --color
2
+ --require spec_helper
data/.travis.yml ADDED
@@ -0,0 +1,9 @@
1
+ language: ruby
2
+ rvm:
3
+ - 2.1.2
4
+ - 2.0.0
5
+ - 1.9.3
6
+ - jruby-18mode # JRuby in 1.8 mode
7
+ - jruby-19mode # JRuby in 1.9 mode
8
+ - rbx-2.2.7
9
+ - rbx-2.2.6
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in going.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2014 Justin Ridgewell
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,68 @@
1
+ # Going [![Build Status](https://travis-ci.org/jridgewell/Going.svg)](https://travis-ci.org/jridgewell/Going)
2
+
3
+ Go for Ruby
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ ```ruby
10
+ gem 'going'
11
+ ```
12
+
13
+ And then execute:
14
+
15
+ $ bundle
16
+
17
+ Or install it yourself as:
18
+
19
+ $ gem install going
20
+
21
+ ## Usage
22
+
23
+ Brings Go's Channel and Goroutines to Ruby.
24
+
25
+ ```ruby
26
+ require 'going'
27
+ # Require 'going/kernel' to get the unnamespaced `go` function
28
+ # require 'going/kernel'
29
+
30
+ class ConcurrentSieve
31
+ def generator
32
+ ch = Going::Channel.new
33
+ Going.go do
34
+ i = 1
35
+ loop { ch.push(i += 1) }
36
+ end
37
+ ch
38
+ end
39
+
40
+ def filter(prime, from)
41
+ ch = Going::Channel.new
42
+ Going.go do
43
+ loop do
44
+ i = from.receive
45
+ ch.push(i) if i % prime != 0
46
+ end
47
+ end
48
+ ch
49
+ end
50
+
51
+ def initialize(n)
52
+ ch = generator
53
+ n.times do
54
+ prime = ch.receive
55
+ puts prime
56
+ ch = filter(prime, ch)
57
+ end
58
+ end
59
+ end
60
+ ```
61
+
62
+ ## Contributing
63
+
64
+ 1. Fork it ( https://github.com/jridgewell/going/fork )
65
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
66
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
67
+ 4. Push to the branch (`git push origin my-new-feature`)
68
+ 5. Create a new Pull Request
data/Rakefile ADDED
@@ -0,0 +1,6 @@
1
+ require 'bundler/gem_tasks'
2
+ require 'rspec/core/rake_task'
3
+
4
+ RSpec::Core::RakeTask.new(:spec)
5
+
6
+ task :default => :spec
data/going.gemspec ADDED
@@ -0,0 +1,22 @@
1
+ lib = File.expand_path('../lib', __FILE__)
2
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
3
+ require 'going/version'
4
+
5
+ Gem::Specification.new do |spec|
6
+ spec.name = "going"
7
+ spec.version = Going::VERSION
8
+ spec.authors = ["Justin Ridgewell"]
9
+ spec.email = ["justin@ridgewell.name"]
10
+ spec.summary = %q{Go for Ruby}
11
+ spec.homepage = "https://github.com/jridgewell/going"
12
+ spec.license = "MIT"
13
+
14
+ spec.files = `git ls-files -z`.split("\x0")
15
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
16
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
17
+ spec.require_paths = ["lib"]
18
+
19
+ spec.add_development_dependency "bundler", "~> 1.6"
20
+ spec.add_development_dependency "rake", "~> 10.0"
21
+ spec.add_development_dependency "rspec", "~> 3.0"
22
+ end
@@ -0,0 +1,141 @@
1
+ require 'going'
2
+
3
+ module Going
4
+ #
5
+ # This class represents message channels of specified capacity.
6
+ # The push operation may be blocked if the capacity is full.
7
+ # The pop operation may be blocked if no messages have been sent.
8
+ #
9
+ class Channel
10
+ extend Forwardable
11
+
12
+ #
13
+ # Creates a fixed-length channel with a capacity of +capacity+.
14
+ #
15
+ def initialize(capacity = 0)
16
+ fail ArgumentError, 'channel capacity must be 0 or greater' unless capacity >= 0
17
+ @capacity = capacity
18
+ @closed = false
19
+ @mutex = Mutex.new
20
+ @push_semaphore = ConditionVariable.new
21
+ @pop_semaphore = ConditionVariable.new
22
+
23
+ yield self if block_given?
24
+ end
25
+
26
+ #
27
+ # Returns the capacity of the channel.
28
+ #
29
+ attr_reader :capacity
30
+
31
+ #
32
+ # Returns whether or not the channel is closed.
33
+ #
34
+ def closed?
35
+ @closed
36
+ end
37
+
38
+ #
39
+ # Closes the channel. Any data in the buffer may still be retrieved.
40
+ #
41
+ def close
42
+ synchronize do
43
+ return false if closed?
44
+ @messages = messages.first(capacity)
45
+ broadcast_close
46
+ @closed = true
47
+ end
48
+ end
49
+
50
+ #
51
+ # Pushes +obj+ to the channel. If the channel is already full, waits
52
+ # until a thread pops from it.
53
+ #
54
+ def push(obj)
55
+ synchronize do
56
+ fail 'cannot push to a closed channel' if closed?
57
+ messages.push obj
58
+ signal_push
59
+ wait_for_pop if messages.length > capacity
60
+ check_for_close
61
+ self
62
+ end
63
+ end
64
+
65
+ #
66
+ # Alias of push
67
+ #
68
+ alias_method :<<, :push
69
+ alias_method :yield, :push
70
+
71
+ #
72
+ # Receives data from the channel. If the channel is already empty,
73
+ # waits until a thread pushes to it.
74
+ #
75
+ def pop
76
+ synchronize do
77
+ return if closed?
78
+ wait_for_push if messages.empty?
79
+ signal_pop
80
+ check_for_close
81
+ messages.shift
82
+ end
83
+ end
84
+
85
+ #
86
+ # Alias of pop
87
+ #
88
+ alias_method :receive, :pop
89
+ alias_method :next, :pop
90
+
91
+ #
92
+ # Delegate size, length, and empty? to the messages queue
93
+ #
94
+ def_delegators :messages, :size, :empty?
95
+
96
+ #
97
+ # Alias of size
98
+ #
99
+ alias_method :length, :size
100
+
101
+ def inspect
102
+ inspection = [:capacity, :messages].map do |attr|
103
+ "#{attr}: #{send(attr).inspect}"
104
+ end
105
+ "#<#{self.class} #{inspection.join(', ')}>"
106
+ end
107
+
108
+ private
109
+
110
+ def_delegators :@mutex, :synchronize
111
+
112
+ def messages
113
+ @messages ||= []
114
+ end
115
+
116
+ def signal_pop
117
+ @push_semaphore.signal
118
+ end
119
+
120
+ def wait_for_pop
121
+ @push_semaphore.wait(@mutex)
122
+ end
123
+
124
+ def signal_push
125
+ @pop_semaphore.signal
126
+ end
127
+
128
+ def wait_for_push
129
+ @pop_semaphore.wait(@mutex)
130
+ end
131
+
132
+ def broadcast_close
133
+ @push_semaphore.broadcast
134
+ @pop_semaphore.broadcast
135
+ end
136
+
137
+ def check_for_close
138
+ throw :close if closed?
139
+ end
140
+ end
141
+ end
@@ -0,0 +1,7 @@
1
+ require 'going'
2
+
3
+ module Kernel
4
+ def go(*args, &blk)
5
+ Going.go(*args, &blk)
6
+ end
7
+ end
@@ -0,0 +1,3 @@
1
+ module Going
2
+ VERSION = '0.0.3'
3
+ end
data/lib/going.rb ADDED
@@ -0,0 +1,12 @@
1
+ require 'thread'
2
+ require 'going/channel'
3
+ require 'going/version'
4
+
5
+ module Going
6
+ #
7
+ # Creates an async thread to run the block
8
+ #
9
+ def self.go(*args, &blk)
10
+ Thread.new(*args, &blk)
11
+ end
12
+ end
@@ -0,0 +1,214 @@
1
+ describe Going::Channel do
2
+ subject(:channel) { Going::Channel.new }
3
+ def elapsed_time(original_time)
4
+ (Time.now - original_time)
5
+ end
6
+
7
+ describe '.new' do
8
+ it 'defaults capacity to 0' do
9
+ expect(channel.capacity).to eq(0)
10
+ end
11
+
12
+ it 'throws error if capacity is less than 0' do
13
+ expect { Going::Channel.new(-1) }.to raise_error
14
+ end
15
+
16
+ it 'is not closed' do
17
+ expect(channel).not_to be_closed
18
+ end
19
+
20
+ it 'yields itself if block given' do
21
+ yielded = nil
22
+ channel = Going::Channel.new { |y| yielded = y }
23
+ expect(yielded).to be(channel)
24
+ end
25
+ end
26
+
27
+ describe '#capacity' do
28
+ it 'returns capacity of channel' do
29
+ channel = Going::Channel.new(5)
30
+ expect(channel.capacity).to eq(5)
31
+ end
32
+ end
33
+
34
+ describe '#close' do
35
+ it 'closes channel' do
36
+ channel.close
37
+ expect(channel).to be_closed
38
+ end
39
+
40
+ it 'returns true after closing channel' do
41
+ expect(channel.close).to be(true)
42
+ end
43
+
44
+ it 'returns false if channel already closed' do
45
+ channel.close
46
+ expect(channel.close).to be(false)
47
+ end
48
+
49
+ it 'will wake a blocked push' do
50
+ Going.go do
51
+ sleep 0.1
52
+ channel.close
53
+ end
54
+ expect { channel.push 1 }.to throw_symbol(:close)
55
+ end
56
+
57
+ it 'will wake a blocked push' do
58
+ Going.go do
59
+ sleep 0.1
60
+ channel.close
61
+ end
62
+ expect { channel.receive }.to throw_symbol(:close)
63
+ end
64
+
65
+ it 'will reject all but the first #capacity pushes' do
66
+ channel = Going::Channel.new 2
67
+ Going.go do
68
+ sleep 0.1
69
+ channel.close
70
+ end
71
+ catch :close do
72
+ 3.times { |i| channel.push i }
73
+ end
74
+ expect(channel.size).to eq(2)
75
+ end
76
+ end
77
+
78
+ describe '#push' do
79
+ subject(:channel) { Going::Channel.new 1 }
80
+
81
+ it 'is aliased as #<<' do
82
+ expect(channel.method(:<<)).to eq(channel.method(:push))
83
+ end
84
+
85
+ it 'is aliased as #yield' do
86
+ expect(channel.method(:yield)).to eq(channel.method(:push))
87
+ end
88
+
89
+ it 'raises error if channel is closed' do
90
+ channel.close
91
+ expect { channel.push 1 }.to raise_error
92
+ end
93
+
94
+ it 'will not block if channel is under capacity' do
95
+ now = Time.now
96
+ channel.push 1
97
+ expect(elapsed_time(now)).to be < 0.2
98
+ end
99
+
100
+ it 'will block if channel is over capacity' do
101
+ channel.push 1
102
+ Going.go do
103
+ sleep 0.25
104
+ channel.receive
105
+ end
106
+ now = Time.now
107
+ channel.push 1
108
+ expect(elapsed_time(now)).to be > 0.2
109
+ end
110
+
111
+ it 'will push messages in order' do
112
+ channel.push 1
113
+ Going.go do
114
+ channel.push 2
115
+ end
116
+ Going.go do
117
+ sleep 0.05
118
+ channel.push 3
119
+ end
120
+ sleep 0.1
121
+ 1.upto(3).each do |i|
122
+ expect(channel.receive).to eq(i)
123
+ end
124
+ end
125
+
126
+ it 'returns the channel' do
127
+ expect(channel.push(1)).to be(channel)
128
+ end
129
+ end
130
+
131
+ describe '#pop' do
132
+ subject(:channel) { Going::Channel.new 1 }
133
+
134
+ it 'is aliased as #receive' do
135
+ expect(channel.method(:receive)).to eq(channel.method(:pop))
136
+ end
137
+
138
+ it 'is aliased as #next' do
139
+ expect(channel.method(:next)).to eq(channel.method(:pop))
140
+ end
141
+
142
+ it 'returns the next message' do
143
+ channel.push 1
144
+ expect(channel.receive).to eq(1)
145
+ end
146
+
147
+ it 'will not block if channel is not empty' do
148
+ channel.push 1
149
+ now = Time.now
150
+ channel.receive
151
+ expect(elapsed_time(now)).to be < 0.2
152
+ end
153
+
154
+ it 'will block if channel is empty' do
155
+ Going.go do
156
+ sleep 0.25
157
+ channel.push 1
158
+ end
159
+ now = Time.now
160
+ channel.receive
161
+ expect(elapsed_time(now)).to be > 0.2
162
+ end
163
+
164
+ it 'returns nil if closed' do
165
+ channel.close
166
+ expect(channel.receive).to be_nil
167
+ end
168
+
169
+ it 'does not block if closed' do
170
+ channel.close
171
+ now = Time.now
172
+ channel.receive
173
+ expect(elapsed_time(now)).to be < 0.2
174
+ end
175
+ end
176
+
177
+ describe '#size' do
178
+ it 'is aliased as #length' do
179
+ expect(channel.method(:length)).to eq(channel.method(:size))
180
+ end
181
+
182
+ it 'returns the number of messages in channel' do
183
+ channel = Going::Channel.new 2
184
+ expect(channel.size).to eq(0)
185
+ channel.push 1
186
+ expect(channel.size).to eq(1)
187
+ channel.push 1
188
+ expect(channel.size).to eq(2)
189
+ channel.receive
190
+ expect(channel.size).to eq(1)
191
+ channel.receive
192
+ expect(channel.size).to eq(0)
193
+ end
194
+ end
195
+
196
+ describe '#empty?' do
197
+ it 'returns true when no messages in channel' do
198
+ expect(channel).to be_empty
199
+ end
200
+
201
+ context 'when capacity is greater than 0' do
202
+ subject(:channel) { Going::Channel.new 1 }
203
+
204
+ it 'returns true when messages in channel' do
205
+ expect(channel).to be_empty
206
+ end
207
+
208
+ it 'returns false when messages in channel' do
209
+ channel.push 1
210
+ expect(channel).not_to be_empty
211
+ end
212
+ end
213
+ end
214
+ end
@@ -0,0 +1,18 @@
1
+ require 'going/kernel'
2
+
3
+ describe Kernel do
4
+ it 'defines #go function' do
5
+ expect(method(:go)).not_to be(nil)
6
+ end
7
+
8
+ it 'delegates to Going.go' do
9
+ expect(Going).to receive(:go)
10
+ go
11
+ end
12
+
13
+ it "returns Going.go's return value" do
14
+ thread = double
15
+ expect(Going).to receive(:go) { thread }
16
+ expect(go).to be(thread)
17
+ end
18
+ end
@@ -0,0 +1,28 @@
1
+ describe Going do
2
+ describe '.go' do
3
+ it 'creates new thread' do
4
+ expect(Thread).to receive(:new)
5
+ Going.go
6
+ end
7
+
8
+ it 'passes args to block' do
9
+ args = nil
10
+ Going.go(1, 2, 3) { |*a| args = a }.join
11
+ expect(args).to eq([1, 2, 3])
12
+ end
13
+
14
+ it 'passes block to thread' do
15
+ block = proc {}
16
+ expect(Thread).to receive(:new) do |&blk|
17
+ expect(blk).to be(block)
18
+ end
19
+ Going.go(&block)
20
+ end
21
+
22
+ it 'returns thread' do
23
+ thread = double
24
+ expect(Thread).to receive(:new) { thread }
25
+ expect(Going.go).to be(thread)
26
+ end
27
+ end
28
+ end
@@ -0,0 +1,40 @@
1
+ # This file was generated by the `rspec --init` command. Conventionally, all
2
+ # specs live under a `spec` directory, which RSpec adds to the `$LOAD_PATH`.
3
+ # The generated `.rspec` file contains `--require spec_helper` which will cause this
4
+ # file to always be loaded, without a need to explicitly require it in any files.
5
+ #
6
+ # Given that it is always loaded, you are encouraged to keep this file as
7
+ # light-weight as possible. Requiring heavyweight dependencies from this file
8
+ # will add to the boot time of your test suite on EVERY test run, even for an
9
+ # individual file that may not need all of that loaded. Instead, make a
10
+ # separate helper file that requires this one and then use it only in the specs
11
+ # that actually need it.
12
+ #
13
+ # The `.rspec` file also contains a few flags that are not defaults but that
14
+ # users commonly want.
15
+ #
16
+ # See http://rubydoc.info/gems/rspec-core/RSpec/Core/Configuration
17
+
18
+ require 'going'
19
+
20
+ RSpec.configure do |config|
21
+ config.filter_run :focus
22
+ config.run_all_when_everything_filtered = true
23
+
24
+ config.default_formatter = 'doc' if config.files_to_run.one?
25
+
26
+ config.profile_examples = 10
27
+
28
+ config.order = :random
29
+
30
+ Kernel.srand config.seed
31
+
32
+ config.expect_with :rspec do |expectations|
33
+ expectations.syntax = :expect
34
+ end
35
+
36
+ config.mock_with :rspec do |mocks|
37
+ mocks.syntax = :expect
38
+ mocks.verify_partial_doubles = true
39
+ end
40
+ end
metadata ADDED
@@ -0,0 +1,106 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: going
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.3
5
+ platform: ruby
6
+ authors:
7
+ - Justin Ridgewell
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2014-07-22 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '1.6'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.6'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '10.0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '10.0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rspec
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '3.0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '3.0'
55
+ description:
56
+ email:
57
+ - justin@ridgewell.name
58
+ executables: []
59
+ extensions: []
60
+ extra_rdoc_files: []
61
+ files:
62
+ - ".gitignore"
63
+ - ".rspec"
64
+ - ".travis.yml"
65
+ - Gemfile
66
+ - LICENSE.txt
67
+ - README.md
68
+ - Rakefile
69
+ - going.gemspec
70
+ - lib/going.rb
71
+ - lib/going/channel.rb
72
+ - lib/going/kernel.rb
73
+ - lib/going/version.rb
74
+ - spec/going/channel_spec.rb
75
+ - spec/going/kernel_spec.rb
76
+ - spec/going_spec.rb
77
+ - spec/spec_helper.rb
78
+ homepage: https://github.com/jridgewell/going
79
+ licenses:
80
+ - MIT
81
+ metadata: {}
82
+ post_install_message:
83
+ rdoc_options: []
84
+ require_paths:
85
+ - lib
86
+ required_ruby_version: !ruby/object:Gem::Requirement
87
+ requirements:
88
+ - - ">="
89
+ - !ruby/object:Gem::Version
90
+ version: '0'
91
+ required_rubygems_version: !ruby/object:Gem::Requirement
92
+ requirements:
93
+ - - ">="
94
+ - !ruby/object:Gem::Version
95
+ version: '0'
96
+ requirements: []
97
+ rubyforge_project:
98
+ rubygems_version: 2.2.2
99
+ signing_key:
100
+ specification_version: 4
101
+ summary: Go for Ruby
102
+ test_files:
103
+ - spec/going/channel_spec.rb
104
+ - spec/going/kernel_spec.rb
105
+ - spec/going_spec.rb
106
+ - spec/spec_helper.rb