eldritch 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.gitignore +75 -0
- data/.rspec +2 -0
- data/.travis.yml +3 -0
- data/Gemfile +8 -0
- data/LICENSE.txt +22 -0
- data/README.md +103 -0
- data/Rakefile +6 -0
- data/eldritch.gemspec +24 -0
- data/examples/async_block.rb +13 -0
- data/examples/async_method_with_class.rb +19 -0
- data/examples/simple_async_method.rb +15 -0
- data/examples/together_simple.rb +10 -0
- data/lib/eldritch.rb +3 -0
- data/lib/eldritch/dsl.rb +46 -0
- data/lib/eldritch/ext_core/thread.rb +7 -0
- data/lib/eldritch/safe.rb +11 -0
- data/lib/eldritch/task.rb +23 -0
- data/lib/eldritch/together.rb +15 -0
- data/lib/eldritch/version.rb +3 -0
- data/spec/dsl_spec.rb +165 -0
- data/spec/eldritch_spec.rb +33 -0
- data/spec/spec_helper.rb +5 -0
- data/spec/task_spec.rb +56 -0
- data/spec/thread_spec.rb +24 -0
- data/spec/together_spec.rb +16 -0
- metadata +120 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 421afdae683a86e47b2138e1c5b22ff833a7ef7a
|
4
|
+
data.tar.gz: e364c1a1030cf95dc5a3aa38efac6cf5a44111e2
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 0b558a3aa5e75a0a84f7d58dc292ea9afb49a8991442056d0e8afccbbd321d964d825d3fbab53d2e721a90fbee6722342f360948cf1440e82c9b674de5eb9e97
|
7
|
+
data.tar.gz: b7faec1e5ad653d78669e60a6ba0e11f4c9e16f029fd129b88575da4193edf5af1773ad98d7881d85d05d372bee941b7ed6fa20fded7f277be8b3a263293f569
|
data/.gitignore
ADDED
@@ -0,0 +1,75 @@
|
|
1
|
+
# Created by http://www.gitignore.io
|
2
|
+
|
3
|
+
### Ruby ###
|
4
|
+
*.gem
|
5
|
+
*.rbc
|
6
|
+
/.config
|
7
|
+
/coverage/
|
8
|
+
/InstalledFiles
|
9
|
+
/pkg/
|
10
|
+
/spec/reports/
|
11
|
+
/test/tmp/
|
12
|
+
/test/version_tmp/
|
13
|
+
/tmp/
|
14
|
+
|
15
|
+
## Documentation cache and generated files:
|
16
|
+
/.yardoc/
|
17
|
+
/_yardoc/
|
18
|
+
/doc/
|
19
|
+
/rdoc/
|
20
|
+
|
21
|
+
## Environment normalisation:
|
22
|
+
/.bundle/
|
23
|
+
/lib/bundler/man/
|
24
|
+
|
25
|
+
# for a library or gem, you might want to ignore these files since the code is
|
26
|
+
# intended to run in multiple environments; otherwise, check them in:
|
27
|
+
Gemfile.lock
|
28
|
+
.ruby-version
|
29
|
+
.ruby-gemset
|
30
|
+
|
31
|
+
# unless supporting rvm < 1.11.0 or doing something fancy, ignore this:
|
32
|
+
.rvmrc
|
33
|
+
|
34
|
+
|
35
|
+
### Intellij ###
|
36
|
+
# Covers JetBrains IDEs: IntelliJ, RubyMine, PhpStorm, AppCode
|
37
|
+
|
38
|
+
## Directory-based project format
|
39
|
+
.idea/
|
40
|
+
# if you remove the above rule, at least ignore user-specific stuff:
|
41
|
+
# .idea/workspace.xml
|
42
|
+
# .idea/tasks.xml
|
43
|
+
# and these sensitive or high-churn files:
|
44
|
+
# .idea/dataSources.ids
|
45
|
+
# .idea/dataSources.xml
|
46
|
+
# .idea/sqlDataSources.xml
|
47
|
+
# .idea/dynamic.xml
|
48
|
+
|
49
|
+
## File-based project format
|
50
|
+
*.ipr
|
51
|
+
*.iws
|
52
|
+
*.iml
|
53
|
+
|
54
|
+
## Additional for IntelliJ
|
55
|
+
out/
|
56
|
+
|
57
|
+
# generated by mpeltonen/sbt-idea plugin
|
58
|
+
.idea_modules/
|
59
|
+
|
60
|
+
# generated by JIRA plugin
|
61
|
+
atlassian-ide-plugin.xml
|
62
|
+
|
63
|
+
# generated by Crashlytics plugin (for Android Studio and Intellij)
|
64
|
+
com_crashlytics_export_strings.xml
|
65
|
+
|
66
|
+
# Created by http://www.gitignore.io
|
67
|
+
|
68
|
+
### vim ###
|
69
|
+
[._]*.s[a-w][a-z]
|
70
|
+
[._]s[a-w][a-z]
|
71
|
+
*.un~
|
72
|
+
Session.vim
|
73
|
+
.netrwhist
|
74
|
+
*~
|
75
|
+
|
data/.rspec
ADDED
data/.travis.yml
ADDED
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Copyright (c) 2014 Boris Bera, François Genois
|
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,103 @@
|
|
1
|
+
Eldritch
|
2
|
+
========
|
3
|
+
|
4
|
+
_The dark arts of concurrent programming._
|
5
|
+
|
6
|
+
**This gem is in development and in no way shape or form production ready.**
|
7
|
+
|
8
|
+
A ruby gem that adds parallel programming constructs to make your life a little easier.
|
9
|
+
|
10
|
+
Features
|
11
|
+
--------
|
12
|
+
|
13
|
+
### async methods
|
14
|
+
|
15
|
+
Async methods run concurrently when called. The caller is returned controlled right away and the method runs in the
|
16
|
+
background.
|
17
|
+
|
18
|
+
```ruby
|
19
|
+
require 'eldritch'
|
20
|
+
|
21
|
+
# define an async method
|
22
|
+
async def send_email(email)
|
23
|
+
# ...
|
24
|
+
end
|
25
|
+
|
26
|
+
# ...
|
27
|
+
send_email(some_email) # runs in the background
|
28
|
+
# ...
|
29
|
+
```
|
30
|
+
|
31
|
+
### async blocks
|
32
|
+
|
33
|
+
Async blocks are run concurrently.
|
34
|
+
|
35
|
+
```ruby
|
36
|
+
require 'eldritch'
|
37
|
+
|
38
|
+
# do some work
|
39
|
+
async do
|
40
|
+
# some long running task ...
|
41
|
+
end
|
42
|
+
# continue working
|
43
|
+
```
|
44
|
+
|
45
|
+
### tasks
|
46
|
+
|
47
|
+
Async blocks and async methods both return tasks. These can be used to interact with the async block/method. As of now,
|
48
|
+
you can wait for the task to finish or you can get its return value.
|
49
|
+
|
50
|
+
```ruby
|
51
|
+
require 'eldritch'
|
52
|
+
|
53
|
+
task = async do
|
54
|
+
# calculate something that will take a long time
|
55
|
+
end
|
56
|
+
|
57
|
+
# ...
|
58
|
+
|
59
|
+
# now we need to result of the task
|
60
|
+
res = 2 + task.value # waits for the task to finish
|
61
|
+
```
|
62
|
+
|
63
|
+
### together
|
64
|
+
|
65
|
+
Together blocks are used to control all async blocks and methods within them as a group. Right now, the together block
|
66
|
+
waits for all async calls be de done before exiting.
|
67
|
+
|
68
|
+
```ruby
|
69
|
+
require 'eldritch'
|
70
|
+
|
71
|
+
together do
|
72
|
+
1000.each do
|
73
|
+
async do
|
74
|
+
# do some work
|
75
|
+
end
|
76
|
+
end
|
77
|
+
end
|
78
|
+
# all 1000 tasks are done
|
79
|
+
```
|
80
|
+
|
81
|
+
Installation
|
82
|
+
------------
|
83
|
+
|
84
|
+
Add this line to your application's Gemfile:
|
85
|
+
|
86
|
+
gem 'eldritch'
|
87
|
+
|
88
|
+
And then execute:
|
89
|
+
|
90
|
+
$ bundle
|
91
|
+
|
92
|
+
Or install it yourself as:
|
93
|
+
|
94
|
+
$ gem install eldritch
|
95
|
+
|
96
|
+
Code quality
|
97
|
+
------------
|
98
|
+
|
99
|
+
[![Build Status](http://travis-ci.org/beraboris/eldritch.svg?branch=master)](http://travis-ci.org/beraboris/eldritch)
|
100
|
+
|
101
|
+
[![Coverage Status](http://coveralls.io/repos/beraboris/eldritch/badge.png)](http://coveralls.io/r/beraboris/eldritch)
|
102
|
+
|
103
|
+
[![Code Climate](http://codeclimate.com/github/beraboris/eldritch.png)](http://codeclimate.com/github/beraboris/eldritch)
|
data/Rakefile
ADDED
data/eldritch.gemspec
ADDED
@@ -0,0 +1,24 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'eldritch/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = 'eldritch'
|
8
|
+
spec.version = Eldritch::VERSION
|
9
|
+
spec.authors = ['Boris Bera', 'François Genois']
|
10
|
+
spec.email = %w(bboris@rsoft.ca frankgenerated@gmail.com)
|
11
|
+
spec.summary = %q{Adds tools to make parallelism easier.}
|
12
|
+
spec.description = %q{Adds support for async methods and async blocks. Adds a together block that allows async methods/blocks to be controlled as a group.}
|
13
|
+
spec.homepage = 'https://github.com/beraboris/eldritch'
|
14
|
+
spec.license = 'MIT'
|
15
|
+
|
16
|
+
spec.files = `git ls-files -z`.split("\x0")
|
17
|
+
spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
|
18
|
+
spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
|
19
|
+
spec.require_paths = ['lib']
|
20
|
+
|
21
|
+
spec.add_development_dependency 'bundler', '~> 1.5'
|
22
|
+
spec.add_development_dependency 'rake'
|
23
|
+
spec.add_development_dependency 'rspec', '~> 2.14'
|
24
|
+
end
|
@@ -0,0 +1,13 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
require 'eldritch'
|
3
|
+
|
4
|
+
async do
|
5
|
+
puts 'starting long running task'
|
6
|
+
sleep(1)
|
7
|
+
puts 'long running task done'
|
8
|
+
end
|
9
|
+
|
10
|
+
puts 'doing something else'
|
11
|
+
|
12
|
+
# waiting for everyone to finish
|
13
|
+
(Thread.list - [Thread.current]).each &:join
|
@@ -0,0 +1,19 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
require 'eldritch'
|
3
|
+
|
4
|
+
class BabysFirstClass
|
5
|
+
async def foo(arg)
|
6
|
+
puts "starting long running task with #{arg}"
|
7
|
+
sleep(1)
|
8
|
+
puts 'long running task done'
|
9
|
+
end
|
10
|
+
end
|
11
|
+
|
12
|
+
obj = BabysFirstClass.new
|
13
|
+
|
14
|
+
puts 'calling foo'
|
15
|
+
obj.foo('stuff')
|
16
|
+
puts 'doing something else'
|
17
|
+
|
18
|
+
# waiting for everyone to stop
|
19
|
+
(Thread.list - [Thread.current]).each &:join
|
@@ -0,0 +1,15 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
require 'eldritch'
|
3
|
+
|
4
|
+
async def foo
|
5
|
+
puts 'starting long running task'
|
6
|
+
sleep(1)
|
7
|
+
puts 'long running task done'
|
8
|
+
end
|
9
|
+
|
10
|
+
puts 'calling foo'
|
11
|
+
foo
|
12
|
+
puts 'doing something else'
|
13
|
+
|
14
|
+
# waiting for everybody to stop
|
15
|
+
(Thread.list - [Thread.current]).each &:join
|
data/lib/eldritch.rb
ADDED
data/lib/eldritch/dsl.rb
ADDED
@@ -0,0 +1,46 @@
|
|
1
|
+
module Eldritch
|
2
|
+
module DSL
|
3
|
+
def async(method=nil, &block)
|
4
|
+
if block
|
5
|
+
async_block(&block)
|
6
|
+
else
|
7
|
+
async_method(method)
|
8
|
+
end
|
9
|
+
end
|
10
|
+
|
11
|
+
def sync(task)
|
12
|
+
task.value
|
13
|
+
end
|
14
|
+
|
15
|
+
def together
|
16
|
+
t = Together.new
|
17
|
+
Thread.current.together = t
|
18
|
+
yield
|
19
|
+
t.wait_all
|
20
|
+
Thread.current.together = nil
|
21
|
+
end
|
22
|
+
|
23
|
+
private
|
24
|
+
|
25
|
+
def async_block(&block)
|
26
|
+
task = Task.new {|t| t.value = block.call }
|
27
|
+
Thread.current.together << task if Thread.current.together?
|
28
|
+
task.start
|
29
|
+
task
|
30
|
+
end
|
31
|
+
|
32
|
+
def async_method(method)
|
33
|
+
new_method = async_method_name(method)
|
34
|
+
target = self.kind_of?(Module) ? self : self.class
|
35
|
+
|
36
|
+
target.send :alias_method, new_method, method
|
37
|
+
target.send :define_method, method do |*args|
|
38
|
+
async { send(new_method, *args) }
|
39
|
+
end
|
40
|
+
end
|
41
|
+
|
42
|
+
def async_method_name(method)
|
43
|
+
"__async_#{method}".to_sym
|
44
|
+
end
|
45
|
+
end
|
46
|
+
end
|
@@ -0,0 +1,23 @@
|
|
1
|
+
module Eldritch
|
2
|
+
class Task
|
3
|
+
attr_writer :value
|
4
|
+
attr_reader :thread
|
5
|
+
|
6
|
+
def initialize(&block)
|
7
|
+
@block = block
|
8
|
+
end
|
9
|
+
|
10
|
+
def start
|
11
|
+
@thread = Thread.new self, &@block
|
12
|
+
end
|
13
|
+
|
14
|
+
def wait
|
15
|
+
@thread.join
|
16
|
+
end
|
17
|
+
|
18
|
+
def value
|
19
|
+
wait
|
20
|
+
@value
|
21
|
+
end
|
22
|
+
end
|
23
|
+
end
|
data/spec/dsl_spec.rb
ADDED
@@ -0,0 +1,165 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
require 'eldritch/dsl'
|
3
|
+
require 'eldritch/task'
|
4
|
+
require 'eldritch/together'
|
5
|
+
|
6
|
+
describe Eldritch::DSL do
|
7
|
+
let(:klass) do Class.new do
|
8
|
+
extend Eldritch::DSL
|
9
|
+
include Eldritch::DSL
|
10
|
+
end
|
11
|
+
end
|
12
|
+
|
13
|
+
describe '#sync' do
|
14
|
+
it 'should call task.value' do
|
15
|
+
task = double(:task)
|
16
|
+
expect(task).to receive(:value)
|
17
|
+
klass.sync(task)
|
18
|
+
end
|
19
|
+
end
|
20
|
+
|
21
|
+
describe '#together' do
|
22
|
+
it 'should create a new together' do
|
23
|
+
expect(Eldritch::Together).to receive(:new).and_return(double('together').as_null_object)
|
24
|
+
|
25
|
+
klass.together {}
|
26
|
+
end
|
27
|
+
|
28
|
+
it 'should set the current thread together' do
|
29
|
+
together = double('together').as_null_object
|
30
|
+
allow(Eldritch::Together).to receive(:new).and_return(together)
|
31
|
+
allow(Thread.current).to receive(:together=).with(nil)
|
32
|
+
|
33
|
+
expect(Thread.current).to receive(:together=).with(together)
|
34
|
+
|
35
|
+
klass.together {}
|
36
|
+
end
|
37
|
+
|
38
|
+
it 'should wait on all tasks' do
|
39
|
+
together = double('together').as_null_object
|
40
|
+
allow(Eldritch::Together).to receive(:new).and_return(together)
|
41
|
+
|
42
|
+
expect(together).to receive(:wait_all)
|
43
|
+
|
44
|
+
klass.together {}
|
45
|
+
end
|
46
|
+
|
47
|
+
it 'should leave current thread together nil' do
|
48
|
+
together = double('together').as_null_object
|
49
|
+
allow(Eldritch::Together).to receive(:new).and_return(together)
|
50
|
+
|
51
|
+
klass.together {}
|
52
|
+
|
53
|
+
expect(Thread.current.together).to be_nil
|
54
|
+
end
|
55
|
+
end
|
56
|
+
|
57
|
+
describe '#async' do
|
58
|
+
context 'with 0 arguments' do
|
59
|
+
|
60
|
+
it 'should add itself to the together block if together?' do
|
61
|
+
together = double('together')
|
62
|
+
allow(Thread.current).to receive(:together?).and_return(true)
|
63
|
+
allow(Thread.current).to receive(:together).and_return(together)
|
64
|
+
|
65
|
+
expect(together).to receive(:<<).with(kind_of(Eldritch::Task))
|
66
|
+
|
67
|
+
klass.async {}
|
68
|
+
end
|
69
|
+
|
70
|
+
it 'should return a task' do
|
71
|
+
expect(klass.async {}).to be_a(Eldritch::Task)
|
72
|
+
end
|
73
|
+
|
74
|
+
it 'should start a task' do
|
75
|
+
task = double(:task)
|
76
|
+
allow(Eldritch::Task).to receive(:new).and_return(task)
|
77
|
+
expect(task).to receive(:start)
|
78
|
+
|
79
|
+
klass.async {}
|
80
|
+
end
|
81
|
+
|
82
|
+
it 'should set the task value' do
|
83
|
+
task = double(:task)
|
84
|
+
allow(Thread).to receive(:new).and_yield(task)
|
85
|
+
expect(task).to receive(:value=).with('something')
|
86
|
+
|
87
|
+
klass.async { 'something' }
|
88
|
+
end
|
89
|
+
end
|
90
|
+
|
91
|
+
context 'with 1 argument' do
|
92
|
+
before do
|
93
|
+
klass.class_eval do
|
94
|
+
async def foo; end
|
95
|
+
end
|
96
|
+
end
|
97
|
+
|
98
|
+
it 'should create a __async method' do
|
99
|
+
expect(klass.new).to respond_to(:__async_foo)
|
100
|
+
end
|
101
|
+
|
102
|
+
it 'should redefine the method' do
|
103
|
+
expect(klass).to receive(:define_method).with(:foo)
|
104
|
+
|
105
|
+
klass.class_eval do
|
106
|
+
async def foo; end
|
107
|
+
end
|
108
|
+
end
|
109
|
+
|
110
|
+
describe 'async method' do
|
111
|
+
it 'should call the original' do
|
112
|
+
allow(Thread).to receive(:new).and_yield(double(:task).as_null_object)
|
113
|
+
|
114
|
+
instance = klass.new
|
115
|
+
expect(instance).to receive(:__async_foo)
|
116
|
+
|
117
|
+
instance.foo
|
118
|
+
end
|
119
|
+
|
120
|
+
it 'should pass all arguments' do
|
121
|
+
allow(Thread).to receive(:new).and_yield(double(:task).as_null_object)
|
122
|
+
|
123
|
+
klass.class_eval do
|
124
|
+
async def foo(_,_,_); end
|
125
|
+
end
|
126
|
+
instance = klass.new
|
127
|
+
expect(instance).to receive(:__async_foo).with(1,2,3)
|
128
|
+
|
129
|
+
instance.foo(1,2,3)
|
130
|
+
end
|
131
|
+
|
132
|
+
it 'should set the task value' do
|
133
|
+
task = double(:task)
|
134
|
+
expect(task).to receive(:value=).with(42)
|
135
|
+
allow(Thread).to receive(:new).and_yield(task)
|
136
|
+
|
137
|
+
klass.class_eval do
|
138
|
+
async def foo; 42; end
|
139
|
+
end
|
140
|
+
instance = klass.new
|
141
|
+
|
142
|
+
instance.foo
|
143
|
+
end
|
144
|
+
|
145
|
+
it 'should return a task' do
|
146
|
+
allow(Eldritch::DSL).to receive(:new).and_return(double(:task).as_null_object)
|
147
|
+
|
148
|
+
instance = klass.new
|
149
|
+
|
150
|
+
expect(instance.foo).to be_a(Eldritch::Task)
|
151
|
+
end
|
152
|
+
|
153
|
+
it 'should start the task' do
|
154
|
+
task = double(:task)
|
155
|
+
expect(task).to receive(:start).once
|
156
|
+
allow(Eldritch::Task).to receive(:new).and_return(task)
|
157
|
+
|
158
|
+
instance = klass.new
|
159
|
+
|
160
|
+
instance.foo
|
161
|
+
end
|
162
|
+
end
|
163
|
+
end
|
164
|
+
end
|
165
|
+
end
|
@@ -0,0 +1,33 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe Eldritch do
|
4
|
+
it 'should have a version number' do
|
5
|
+
Eldritch::VERSION.should_not be_nil
|
6
|
+
end
|
7
|
+
|
8
|
+
describe '#inject_dsl' do
|
9
|
+
it 'should include the dsl in Object' do
|
10
|
+
expect(Object).to receive(:include).with(Eldritch::DSL)
|
11
|
+
|
12
|
+
Eldritch.inject_dsl
|
13
|
+
end
|
14
|
+
|
15
|
+
it 'should allow classes to respond to dsl methods' do
|
16
|
+
Eldritch.inject_dsl
|
17
|
+
klass = Class.new
|
18
|
+
|
19
|
+
expect(klass).to respond_to(:async)
|
20
|
+
expect(klass).to respond_to(:sync)
|
21
|
+
expect(klass).to respond_to(:together)
|
22
|
+
end
|
23
|
+
|
24
|
+
it 'should allow objects to respond to dsl methods' do
|
25
|
+
Eldritch.inject_dsl
|
26
|
+
obj = Object.new
|
27
|
+
|
28
|
+
expect(obj).to respond_to(:async)
|
29
|
+
expect(obj).to respond_to(:sync)
|
30
|
+
expect(obj).to respond_to(:together)
|
31
|
+
end
|
32
|
+
end
|
33
|
+
end
|
data/spec/spec_helper.rb
ADDED
data/spec/task_spec.rb
ADDED
@@ -0,0 +1,56 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
require 'eldritch/task'
|
3
|
+
|
4
|
+
describe Eldritch::Task do
|
5
|
+
let(:task) { Eldritch::Task.new {} }
|
6
|
+
|
7
|
+
it 'should not start a thread on init' do
|
8
|
+
expect(task.thread).to be_nil
|
9
|
+
end
|
10
|
+
|
11
|
+
describe '#start' do
|
12
|
+
it 'should create a thread' do
|
13
|
+
task.start
|
14
|
+
|
15
|
+
expect(task.thread).not_to be_nil
|
16
|
+
end
|
17
|
+
|
18
|
+
it 'should call the block' do
|
19
|
+
allow(Thread).to receive(:new).and_yield
|
20
|
+
expect do |b|
|
21
|
+
task = Eldritch::Task.new &b
|
22
|
+
task.start
|
23
|
+
end.to yield_control
|
24
|
+
end
|
25
|
+
|
26
|
+
it 'should start a thread' do
|
27
|
+
expect(Thread).to receive(:new).with(task)
|
28
|
+
|
29
|
+
task.start
|
30
|
+
end
|
31
|
+
end
|
32
|
+
|
33
|
+
describe '#wait' do
|
34
|
+
it 'should join the thread' do
|
35
|
+
task.start
|
36
|
+
|
37
|
+
expect(task.thread).to receive(:join)
|
38
|
+
task.wait
|
39
|
+
end
|
40
|
+
end
|
41
|
+
|
42
|
+
describe '#value' do
|
43
|
+
it 'should join the thread' do
|
44
|
+
task.start
|
45
|
+
|
46
|
+
expect(task.thread).to receive(:join)
|
47
|
+
task.value
|
48
|
+
end
|
49
|
+
|
50
|
+
it 'should return the value' do
|
51
|
+
task.value = 42
|
52
|
+
task.start
|
53
|
+
expect(task.value).to eql(42)
|
54
|
+
end
|
55
|
+
end
|
56
|
+
end
|
data/spec/thread_spec.rb
ADDED
@@ -0,0 +1,24 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
require 'eldritch/ext_core/thread'
|
3
|
+
|
4
|
+
describe Thread do
|
5
|
+
it 'should have together accessor' do
|
6
|
+
t = Thread.new {}
|
7
|
+
expect(t).to respond_to(:together)
|
8
|
+
expect(t).to respond_to(:together=)
|
9
|
+
end
|
10
|
+
|
11
|
+
describe '#together?' do
|
12
|
+
it 'should be false when together is nil' do
|
13
|
+
t = Thread.new {}
|
14
|
+
t.together = nil
|
15
|
+
expect(t.together?).to be_false
|
16
|
+
end
|
17
|
+
|
18
|
+
it 'should be true when together is set' do
|
19
|
+
t = Thread.new {}
|
20
|
+
t.together = 2
|
21
|
+
expect(t.together?).to be_true
|
22
|
+
end
|
23
|
+
end
|
24
|
+
end
|
@@ -0,0 +1,16 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
require 'eldritch/together'
|
3
|
+
|
4
|
+
describe Eldritch::Together do
|
5
|
+
describe '#wait_all' do
|
6
|
+
it 'should call wait on all tasks' do
|
7
|
+
together = Eldritch::Together.new
|
8
|
+
task = double('task')
|
9
|
+
together << task
|
10
|
+
|
11
|
+
expect(task).to receive(:wait)
|
12
|
+
|
13
|
+
together.wait_all
|
14
|
+
end
|
15
|
+
end
|
16
|
+
end
|
metadata
ADDED
@@ -0,0 +1,120 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: eldritch
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Boris Bera
|
8
|
+
- François Genois
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
date: 2014-04-11 00:00:00.000000000 Z
|
13
|
+
dependencies:
|
14
|
+
- !ruby/object:Gem::Dependency
|
15
|
+
name: bundler
|
16
|
+
requirement: !ruby/object:Gem::Requirement
|
17
|
+
requirements:
|
18
|
+
- - "~>"
|
19
|
+
- !ruby/object:Gem::Version
|
20
|
+
version: '1.5'
|
21
|
+
type: :development
|
22
|
+
prerelease: false
|
23
|
+
version_requirements: !ruby/object:Gem::Requirement
|
24
|
+
requirements:
|
25
|
+
- - "~>"
|
26
|
+
- !ruby/object:Gem::Version
|
27
|
+
version: '1.5'
|
28
|
+
- !ruby/object:Gem::Dependency
|
29
|
+
name: rake
|
30
|
+
requirement: !ruby/object:Gem::Requirement
|
31
|
+
requirements:
|
32
|
+
- - ">="
|
33
|
+
- !ruby/object:Gem::Version
|
34
|
+
version: '0'
|
35
|
+
type: :development
|
36
|
+
prerelease: false
|
37
|
+
version_requirements: !ruby/object:Gem::Requirement
|
38
|
+
requirements:
|
39
|
+
- - ">="
|
40
|
+
- !ruby/object:Gem::Version
|
41
|
+
version: '0'
|
42
|
+
- !ruby/object:Gem::Dependency
|
43
|
+
name: rspec
|
44
|
+
requirement: !ruby/object:Gem::Requirement
|
45
|
+
requirements:
|
46
|
+
- - "~>"
|
47
|
+
- !ruby/object:Gem::Version
|
48
|
+
version: '2.14'
|
49
|
+
type: :development
|
50
|
+
prerelease: false
|
51
|
+
version_requirements: !ruby/object:Gem::Requirement
|
52
|
+
requirements:
|
53
|
+
- - "~>"
|
54
|
+
- !ruby/object:Gem::Version
|
55
|
+
version: '2.14'
|
56
|
+
description: Adds support for async methods and async blocks. Adds a together block
|
57
|
+
that allows async methods/blocks to be controlled as a group.
|
58
|
+
email:
|
59
|
+
- bboris@rsoft.ca
|
60
|
+
- frankgenerated@gmail.com
|
61
|
+
executables: []
|
62
|
+
extensions: []
|
63
|
+
extra_rdoc_files: []
|
64
|
+
files:
|
65
|
+
- ".gitignore"
|
66
|
+
- ".rspec"
|
67
|
+
- ".travis.yml"
|
68
|
+
- Gemfile
|
69
|
+
- LICENSE.txt
|
70
|
+
- README.md
|
71
|
+
- Rakefile
|
72
|
+
- eldritch.gemspec
|
73
|
+
- examples/async_block.rb
|
74
|
+
- examples/async_method_with_class.rb
|
75
|
+
- examples/simple_async_method.rb
|
76
|
+
- examples/together_simple.rb
|
77
|
+
- lib/eldritch.rb
|
78
|
+
- lib/eldritch/dsl.rb
|
79
|
+
- lib/eldritch/ext_core/thread.rb
|
80
|
+
- lib/eldritch/safe.rb
|
81
|
+
- lib/eldritch/task.rb
|
82
|
+
- lib/eldritch/together.rb
|
83
|
+
- lib/eldritch/version.rb
|
84
|
+
- spec/dsl_spec.rb
|
85
|
+
- spec/eldritch_spec.rb
|
86
|
+
- spec/spec_helper.rb
|
87
|
+
- spec/task_spec.rb
|
88
|
+
- spec/thread_spec.rb
|
89
|
+
- spec/together_spec.rb
|
90
|
+
homepage: https://github.com/beraboris/eldritch
|
91
|
+
licenses:
|
92
|
+
- MIT
|
93
|
+
metadata: {}
|
94
|
+
post_install_message:
|
95
|
+
rdoc_options: []
|
96
|
+
require_paths:
|
97
|
+
- lib
|
98
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
99
|
+
requirements:
|
100
|
+
- - ">="
|
101
|
+
- !ruby/object:Gem::Version
|
102
|
+
version: '0'
|
103
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
104
|
+
requirements:
|
105
|
+
- - ">="
|
106
|
+
- !ruby/object:Gem::Version
|
107
|
+
version: '0'
|
108
|
+
requirements: []
|
109
|
+
rubyforge_project:
|
110
|
+
rubygems_version: 2.2.2
|
111
|
+
signing_key:
|
112
|
+
specification_version: 4
|
113
|
+
summary: Adds tools to make parallelism easier.
|
114
|
+
test_files:
|
115
|
+
- spec/dsl_spec.rb
|
116
|
+
- spec/eldritch_spec.rb
|
117
|
+
- spec/spec_helper.rb
|
118
|
+
- spec/task_spec.rb
|
119
|
+
- spec/thread_spec.rb
|
120
|
+
- spec/together_spec.rb
|