dharma 0.9.0
Sign up to get free protection for your applications and to get access to all the features.
- data/Gemfile +8 -0
- data/LICENSE +21 -0
- data/README.md +18 -0
- data/Rakefile +155 -0
- data/dharma.gemspec +78 -0
- data/lib/dharma.rb +43 -0
- data/lib/dharma/future.rb +19 -0
- data/lib/dharma/inline_executor.rb +7 -0
- data/lib/dharma/kept_promise.rb +40 -0
- data/lib/dharma/promise.rb +152 -0
- data/lib/dharma/promise_actions.rb +75 -0
- data/lib/dharma/thread_executor.rb +20 -0
- data/test/future_test.rb +11 -0
- data/test/promise_actions_test.rb +61 -0
- data/test/promise_test.rb +32 -0
- data/test/test_helper.rb +5 -0
- metadata +78 -0
data/Gemfile
ADDED
data/LICENSE
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
The MIT License
|
2
|
+
|
3
|
+
Copyright (c) Eric Lindvall
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
7
|
+
in the Software without restriction, including without limitation the rights
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
10
|
+
furnished to do so, subject to the following conditions:
|
11
|
+
|
12
|
+
The above copyright notice and this permission notice shall be included in
|
13
|
+
all copies or substantial portions of the Software.
|
14
|
+
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
21
|
+
THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,18 @@
|
|
1
|
+
Dharma
|
2
|
+
=======
|
3
|
+
|
4
|
+
# Description
|
5
|
+
|
6
|
+
A futures and promises library [modeled after Scala].
|
7
|
+
|
8
|
+
![Dharma Initiative](http://wallpaperstock.net/lost-dharma-initiative_wallpapers_17255_1920x1440.jpg)
|
9
|
+
|
10
|
+
|
11
|
+
# License
|
12
|
+
|
13
|
+
Copyright (c) 2012 Eric Lindvall
|
14
|
+
|
15
|
+
Published under the MIT License, see LICENSE
|
16
|
+
|
17
|
+
|
18
|
+
[modeled after Scala]: http://docs.scala-lang.org/overviews/core/futures.html
|
data/Rakefile
ADDED
@@ -0,0 +1,155 @@
|
|
1
|
+
require 'rubygems'
|
2
|
+
require 'rake'
|
3
|
+
require 'date'
|
4
|
+
|
5
|
+
#############################################################################
|
6
|
+
#
|
7
|
+
# Helper functions
|
8
|
+
#
|
9
|
+
#############################################################################
|
10
|
+
|
11
|
+
def name
|
12
|
+
@name ||= Dir['*.gemspec'].first.split('.').first
|
13
|
+
end
|
14
|
+
|
15
|
+
def version
|
16
|
+
line = File.read("lib/#{name}.rb")[/^\s*VERSION\s*=\s*.*/]
|
17
|
+
line.match(/.*VERSION\s*=\s*['"](.*)['"]/)[1]
|
18
|
+
end
|
19
|
+
|
20
|
+
def date
|
21
|
+
Date.today.to_s
|
22
|
+
end
|
23
|
+
|
24
|
+
def rubyforge_project
|
25
|
+
name
|
26
|
+
end
|
27
|
+
|
28
|
+
def gemspec_file
|
29
|
+
"#{name}.gemspec"
|
30
|
+
end
|
31
|
+
|
32
|
+
def gem_file
|
33
|
+
"#{name}-#{version}.gem"
|
34
|
+
end
|
35
|
+
|
36
|
+
def replace_header(head, header_name)
|
37
|
+
head.sub!(/(\.#{header_name}\s*= ').*'/) { "#{$1}#{send(header_name)}'"}
|
38
|
+
end
|
39
|
+
|
40
|
+
#############################################################################
|
41
|
+
#
|
42
|
+
# Standard tasks
|
43
|
+
#
|
44
|
+
#############################################################################
|
45
|
+
|
46
|
+
task :default => :test
|
47
|
+
|
48
|
+
require 'rake/testtask'
|
49
|
+
Rake::TestTask.new(:test) do |test|
|
50
|
+
test.libs << 'lib' << 'test'
|
51
|
+
test.pattern = 'test/**/*_test.rb'
|
52
|
+
test.verbose = true
|
53
|
+
end
|
54
|
+
|
55
|
+
desc "Generate RCov test coverage and open in your browser"
|
56
|
+
task :coverage do
|
57
|
+
require 'rcov'
|
58
|
+
sh "rm -fr coverage"
|
59
|
+
sh "rcov test/test_*.rb"
|
60
|
+
sh "open coverage/index.html"
|
61
|
+
end
|
62
|
+
|
63
|
+
begin
|
64
|
+
require 'rake/rdoctask'
|
65
|
+
rescue
|
66
|
+
require 'rdoc/task'
|
67
|
+
end
|
68
|
+
|
69
|
+
Rake::RDocTask.new do |rdoc|
|
70
|
+
rdoc.rdoc_dir = 'rdoc'
|
71
|
+
rdoc.title = "#{name} #{version}"
|
72
|
+
rdoc.rdoc_files.include('README*')
|
73
|
+
rdoc.rdoc_files.include('lib/**/*.rb')
|
74
|
+
end
|
75
|
+
|
76
|
+
desc "Open an irb session preloaded with this library"
|
77
|
+
task :console do
|
78
|
+
sh "irb -rubygems -r ./lib/#{name}.rb"
|
79
|
+
end
|
80
|
+
|
81
|
+
#############################################################################
|
82
|
+
#
|
83
|
+
# Custom tasks (add your own tasks here)
|
84
|
+
#
|
85
|
+
#############################################################################
|
86
|
+
|
87
|
+
|
88
|
+
|
89
|
+
#############################################################################
|
90
|
+
#
|
91
|
+
# Packaging tasks
|
92
|
+
#
|
93
|
+
#############################################################################
|
94
|
+
|
95
|
+
desc "Create tag v#{version} and build and push #{gem_file} to Rubygems"
|
96
|
+
task :release => :build do
|
97
|
+
unless `git branch` =~ /^\* master$/
|
98
|
+
puts "You must be on the master branch to release!"
|
99
|
+
exit!
|
100
|
+
end
|
101
|
+
sh "git commit --allow-empty -a -m 'Release #{version}'"
|
102
|
+
sh "git tag v#{version}"
|
103
|
+
sh "git push origin master"
|
104
|
+
sh "git push origin v#{version}"
|
105
|
+
sh "gem push pkg/#{name}-#{version}.gem"
|
106
|
+
end
|
107
|
+
|
108
|
+
desc "Build #{gem_file} into the pkg directory"
|
109
|
+
task :build => :gemspec do
|
110
|
+
sh "mkdir -p pkg"
|
111
|
+
sh "gem build #{gemspec_file}"
|
112
|
+
sh "mv #{gem_file} pkg"
|
113
|
+
end
|
114
|
+
|
115
|
+
desc "Generate #{gemspec_file}"
|
116
|
+
task :gemspec => :validate do
|
117
|
+
# read spec file and split out manifest section
|
118
|
+
spec = File.read(gemspec_file)
|
119
|
+
head, manifest, tail = spec.split(" # = MANIFEST =\n")
|
120
|
+
|
121
|
+
# replace name version and date
|
122
|
+
replace_header(head, :name)
|
123
|
+
replace_header(head, :version)
|
124
|
+
replace_header(head, :date)
|
125
|
+
#comment this out if your rubyforge_project has a different name
|
126
|
+
replace_header(head, :rubyforge_project)
|
127
|
+
|
128
|
+
# determine file list from git ls-files
|
129
|
+
files = `git ls-files`.
|
130
|
+
split("\n").
|
131
|
+
sort.
|
132
|
+
reject { |file| file =~ /^\./ }.
|
133
|
+
reject { |file| file =~ /^(rdoc|pkg)/ }.
|
134
|
+
map { |file| " #{file}" }.
|
135
|
+
join("\n")
|
136
|
+
|
137
|
+
# piece file back together and write
|
138
|
+
manifest = " s.files = %w[\n#{files}\n ]\n"
|
139
|
+
spec = [head, manifest, tail].join(" # = MANIFEST =\n")
|
140
|
+
File.open(gemspec_file, 'w') { |io| io.write(spec) }
|
141
|
+
puts "Updated #{gemspec_file}"
|
142
|
+
end
|
143
|
+
|
144
|
+
desc "Validate #{gemspec_file}"
|
145
|
+
task :validate do
|
146
|
+
libfiles = Dir['lib/*'] - ["lib/#{name}.rb", "lib/#{name}"]
|
147
|
+
unless libfiles.empty?
|
148
|
+
puts "Directory `lib` should only contain a `#{name}.rb` file and `#{name}` dir."
|
149
|
+
exit!
|
150
|
+
end
|
151
|
+
unless Dir['VERSION*'].empty?
|
152
|
+
puts "A `VERSION` file at root level violates Gem best practices."
|
153
|
+
exit!
|
154
|
+
end
|
155
|
+
end
|
data/dharma.gemspec
ADDED
@@ -0,0 +1,78 @@
|
|
1
|
+
## This is the rakegem gemspec template. Make sure you read and understand
|
2
|
+
## all of the comments. Some sections require modification, and others can
|
3
|
+
## be deleted if you don't need them. Once you understand the contents of
|
4
|
+
## this file, feel free to delete any comments that begin with two hash marks.
|
5
|
+
## You can find comprehensive Gem::Specification documentation, at
|
6
|
+
## http://docs.rubygems.org/read/chapter/20
|
7
|
+
Gem::Specification.new do |s|
|
8
|
+
s.specification_version = 2 if s.respond_to? :specification_version=
|
9
|
+
s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
|
10
|
+
s.rubygems_version = '1.3.5'
|
11
|
+
|
12
|
+
## Leave these as is they will be modified for you by the rake gemspec task.
|
13
|
+
## If your rubyforge_project name is different, then edit it and comment out
|
14
|
+
## the sub! line in the Rakefile
|
15
|
+
s.name = 'dharma'
|
16
|
+
s.version = '0.9.0'
|
17
|
+
s.date = '2013-05-24'
|
18
|
+
|
19
|
+
## Make sure your summary is short. The description may be as long
|
20
|
+
## as you like.
|
21
|
+
s.summary = "Ruby futures and promises library in the spirit of Scala"
|
22
|
+
s.description = "Ruby futures and promises library in the spirit of Scala"
|
23
|
+
|
24
|
+
## List the primary authors. If there are a bunch of authors, it's probably
|
25
|
+
## better to set the email to an email list or something. If you don't have
|
26
|
+
## a custom homepage, consider using your GitHub URL or the like.
|
27
|
+
s.authors = ["Eric Lindvall"]
|
28
|
+
s.email = 'eric@5stops.com'
|
29
|
+
s.homepage = 'https://github.com/eric/dharma'
|
30
|
+
|
31
|
+
## This gets added to the $LOAD_PATH so that 'lib/NAME.rb' can be required as
|
32
|
+
## require 'NAME.rb' or'/lib/NAME/file.rb' can be as require 'NAME/file.rb'
|
33
|
+
s.require_paths = %w[lib]
|
34
|
+
|
35
|
+
## If your gem includes any executables, list them here.
|
36
|
+
# s.executables = ["name"]
|
37
|
+
|
38
|
+
## Specify any RDoc options here. You'll want to add your README and
|
39
|
+
## LICENSE files to the extra_rdoc_files list.
|
40
|
+
s.rdoc_options = ["--charset=UTF-8"]
|
41
|
+
s.extra_rdoc_files = %w[README.md LICENSE]
|
42
|
+
|
43
|
+
## List your runtime dependencies here. Runtime dependencies are those
|
44
|
+
## that are needed for an end user to actually USE your code.
|
45
|
+
# s.add_dependency('DEPNAME', [">= 1.1.0", "< 2.0.0"])
|
46
|
+
|
47
|
+
## List your development dependencies here. Development dependencies are
|
48
|
+
## those that are only needed during development
|
49
|
+
# s.add_development_dependency('DEVDEPNAME', [">= 1.1.0", "< 2.0.0"])
|
50
|
+
|
51
|
+
## Leave this section as-is. It will be automatically generated from the
|
52
|
+
## contents of your Git repository via the gemspec task. DO NOT REMOVE
|
53
|
+
## THE MANIFEST COMMENTS, they are used as delimiters by the task.
|
54
|
+
# = MANIFEST =
|
55
|
+
s.files = %w[
|
56
|
+
Gemfile
|
57
|
+
LICENSE
|
58
|
+
README.md
|
59
|
+
Rakefile
|
60
|
+
dharma.gemspec
|
61
|
+
lib/dharma.rb
|
62
|
+
lib/dharma/future.rb
|
63
|
+
lib/dharma/inline_executor.rb
|
64
|
+
lib/dharma/kept_promise.rb
|
65
|
+
lib/dharma/promise.rb
|
66
|
+
lib/dharma/promise_actions.rb
|
67
|
+
lib/dharma/thread_executor.rb
|
68
|
+
test/future_test.rb
|
69
|
+
test/promise_actions_test.rb
|
70
|
+
test/promise_test.rb
|
71
|
+
test/test_helper.rb
|
72
|
+
]
|
73
|
+
# = MANIFEST =
|
74
|
+
|
75
|
+
## Test files will be grabbed from the file list. Make sure the path glob
|
76
|
+
## matches what you actually use.
|
77
|
+
s.test_files = s.files.select { |path| path =~ /^test\/test_.*\.rb/ }
|
78
|
+
end
|
data/lib/dharma.rb
ADDED
@@ -0,0 +1,43 @@
|
|
1
|
+
module Dharma
|
2
|
+
VERSION = '0.9.0'
|
3
|
+
|
4
|
+
class PromiseFailure < RuntimeError; end
|
5
|
+
class IllegalStateException < RuntimeError; end
|
6
|
+
class TimeoutException < RuntimeError; end
|
7
|
+
|
8
|
+
def self.default_executor
|
9
|
+
@default_executor ||= Dharma::ThreadExecutor.new
|
10
|
+
end
|
11
|
+
|
12
|
+
def self.default_executor=(executor)
|
13
|
+
@default_executor = executor
|
14
|
+
end
|
15
|
+
|
16
|
+
def self.promise
|
17
|
+
Dharma::Promise.new
|
18
|
+
end
|
19
|
+
|
20
|
+
def self.future(executor = default_executor, &work)
|
21
|
+
Dharma::Future.call(work, executor)
|
22
|
+
end
|
23
|
+
|
24
|
+
def self.sequence(promises)
|
25
|
+
if promises.is_a?(Array) && promises.length == 1
|
26
|
+
return promises.first
|
27
|
+
end
|
28
|
+
|
29
|
+
future do
|
30
|
+
promises.map { |p| p.result }
|
31
|
+
end
|
32
|
+
end
|
33
|
+
|
34
|
+
def self.trace_completion(tag, promise, logger = Rails.logger)
|
35
|
+
promise.on_complete do |value, as|
|
36
|
+
logger.info "#{tag}: #{as}: #{value.inspect}"
|
37
|
+
end
|
38
|
+
end
|
39
|
+
end
|
40
|
+
|
41
|
+
require 'dharma/promise'
|
42
|
+
require 'dharma/future'
|
43
|
+
require 'dharma/thread_executor'
|
@@ -0,0 +1,19 @@
|
|
1
|
+
module Dharma
|
2
|
+
class Future
|
3
|
+
def self.call(body, executor)
|
4
|
+
promise = Dharma.promise
|
5
|
+
|
6
|
+
work = proc do
|
7
|
+
begin
|
8
|
+
promise.success(body.call)
|
9
|
+
rescue => e
|
10
|
+
promise.failure(e)
|
11
|
+
end
|
12
|
+
end
|
13
|
+
|
14
|
+
executor.execute(work)
|
15
|
+
|
16
|
+
promise
|
17
|
+
end
|
18
|
+
end
|
19
|
+
end
|
@@ -0,0 +1,40 @@
|
|
1
|
+
module Dharma
|
2
|
+
class KeptPromise
|
3
|
+
include Dharma::PromiseActions
|
4
|
+
|
5
|
+
def initialize(value, as)
|
6
|
+
@value, @as = value, as
|
7
|
+
end
|
8
|
+
|
9
|
+
def failure?
|
10
|
+
@as == :failure
|
11
|
+
end
|
12
|
+
|
13
|
+
def result(at_most = nil)
|
14
|
+
if @as == :failure
|
15
|
+
raise @value
|
16
|
+
else
|
17
|
+
return @value
|
18
|
+
end
|
19
|
+
end
|
20
|
+
|
21
|
+
def ready(at_most = nil)
|
22
|
+
return self
|
23
|
+
end
|
24
|
+
|
25
|
+
def value
|
26
|
+
@value
|
27
|
+
end
|
28
|
+
|
29
|
+
def completed?
|
30
|
+
true
|
31
|
+
end
|
32
|
+
|
33
|
+
def on_complete(cb = nil, &block)
|
34
|
+
cb ||= block
|
35
|
+
|
36
|
+
cb.call(@value, @as)
|
37
|
+
return
|
38
|
+
end
|
39
|
+
end
|
40
|
+
end
|
@@ -0,0 +1,152 @@
|
|
1
|
+
require 'monitor'
|
2
|
+
require 'dharma/promise_actions'
|
3
|
+
require 'dharma/kept_promise'
|
4
|
+
|
5
|
+
module Dharma
|
6
|
+
class Promise
|
7
|
+
include Dharma::PromiseActions
|
8
|
+
|
9
|
+
def self.successful(value)
|
10
|
+
KeptPromise.new(value, :success)
|
11
|
+
end
|
12
|
+
|
13
|
+
def self.failed(value)
|
14
|
+
KeptPromise.new(value, :failure)
|
15
|
+
end
|
16
|
+
|
17
|
+
def initialize
|
18
|
+
@mutex = Monitor.new
|
19
|
+
@condition = @mutex.new_cond
|
20
|
+
end
|
21
|
+
|
22
|
+
def failure?
|
23
|
+
@mutex.synchronize do
|
24
|
+
@as == :failure
|
25
|
+
end
|
26
|
+
end
|
27
|
+
|
28
|
+
def result(at_most = nil)
|
29
|
+
ready(at_most)
|
30
|
+
|
31
|
+
@mutex.synchronize do
|
32
|
+
if @as == :failure
|
33
|
+
raise @value
|
34
|
+
else
|
35
|
+
return @value
|
36
|
+
end
|
37
|
+
end
|
38
|
+
end
|
39
|
+
|
40
|
+
def ready(at_most = nil)
|
41
|
+
@mutex.synchronize do
|
42
|
+
if @as
|
43
|
+
return self
|
44
|
+
else
|
45
|
+
@condition.wait(at_most ? at_most.to_f : nil)
|
46
|
+
|
47
|
+
if @as
|
48
|
+
return self
|
49
|
+
else
|
50
|
+
raise Dharma::TimeoutException.new("Futures timed out after [#{at_most}]")
|
51
|
+
end
|
52
|
+
end
|
53
|
+
end
|
54
|
+
end
|
55
|
+
|
56
|
+
def value
|
57
|
+
@mutex.synchronize do
|
58
|
+
if @as
|
59
|
+
@value
|
60
|
+
else
|
61
|
+
nil
|
62
|
+
end
|
63
|
+
end
|
64
|
+
end
|
65
|
+
|
66
|
+
def completed?
|
67
|
+
@mutex.synchronize do
|
68
|
+
@as != nil
|
69
|
+
end
|
70
|
+
end
|
71
|
+
|
72
|
+
def complete(value, as = nil)
|
73
|
+
if try_complete(value, as)
|
74
|
+
self
|
75
|
+
else
|
76
|
+
raise Dharma::IllegalStateException.new("Promise already completed.")
|
77
|
+
end
|
78
|
+
end
|
79
|
+
|
80
|
+
def try_complete(value, as = nil)
|
81
|
+
on_completed = nil
|
82
|
+
|
83
|
+
@mutex.synchronize do
|
84
|
+
if !@as
|
85
|
+
resolve_and_assign(value, as)
|
86
|
+
|
87
|
+
# Grab the value from what we stored
|
88
|
+
value, as = @value, @as
|
89
|
+
|
90
|
+
# Clear callbacks
|
91
|
+
on_completed = @on_completed
|
92
|
+
@on_completed = nil
|
93
|
+
|
94
|
+
# Wake everyone up
|
95
|
+
@condition.broadcast
|
96
|
+
|
97
|
+
# We don't need the condition anymore
|
98
|
+
@condition = nil
|
99
|
+
else
|
100
|
+
return false
|
101
|
+
end
|
102
|
+
end
|
103
|
+
|
104
|
+
if on_completed
|
105
|
+
on_completed.each { |cb| cb.call(value, as) }
|
106
|
+
end
|
107
|
+
|
108
|
+
return true
|
109
|
+
end
|
110
|
+
|
111
|
+
def on_complete(cb = nil, &block)
|
112
|
+
cb ||= block
|
113
|
+
value, as = nil
|
114
|
+
|
115
|
+
@mutex.synchronize do
|
116
|
+
if @as
|
117
|
+
value, as = @value, @as
|
118
|
+
else
|
119
|
+
(@on_completed ||= []) << cb
|
120
|
+
return
|
121
|
+
end
|
122
|
+
end
|
123
|
+
|
124
|
+
cb.call(value, as)
|
125
|
+
return
|
126
|
+
end
|
127
|
+
|
128
|
+
def resolve_and_assign(value, as = nil)
|
129
|
+
case as
|
130
|
+
when :failure
|
131
|
+
case value
|
132
|
+
when Exception
|
133
|
+
@value, @as = value, as
|
134
|
+
else
|
135
|
+
@value, @as = Dharma::PromiseFailure.new(value), as
|
136
|
+
@value.set_backtrace(caller)
|
137
|
+
end
|
138
|
+
when :success
|
139
|
+
@value, @as = value, as
|
140
|
+
else
|
141
|
+
case value
|
142
|
+
when LocalJumpError
|
143
|
+
@value, @as = value.exit_value, :success
|
144
|
+
when Exception
|
145
|
+
@value, @as = value, :failure
|
146
|
+
else
|
147
|
+
@value, @as = value, :success
|
148
|
+
end
|
149
|
+
end
|
150
|
+
end
|
151
|
+
end
|
152
|
+
end
|
@@ -0,0 +1,75 @@
|
|
1
|
+
module Dharma
|
2
|
+
module PromiseActions
|
3
|
+
def on_success(cb = nil, &block)
|
4
|
+
cb ||= block
|
5
|
+
|
6
|
+
on_complete do |value, as|
|
7
|
+
case as
|
8
|
+
when :success
|
9
|
+
cb.call(value)
|
10
|
+
end
|
11
|
+
end
|
12
|
+
end
|
13
|
+
|
14
|
+
def on_failure(cb = nil, &block)
|
15
|
+
cb ||= block
|
16
|
+
|
17
|
+
on_complete do |value, as|
|
18
|
+
case as
|
19
|
+
when :failure
|
20
|
+
cb.call(value)
|
21
|
+
end
|
22
|
+
end
|
23
|
+
end
|
24
|
+
|
25
|
+
def success(value)
|
26
|
+
complete(value, :success)
|
27
|
+
end
|
28
|
+
|
29
|
+
def failure(value)
|
30
|
+
complete(value, :failure)
|
31
|
+
end
|
32
|
+
|
33
|
+
def try_success(value)
|
34
|
+
try_complete(value, :success)
|
35
|
+
end
|
36
|
+
|
37
|
+
def try_failure(value)
|
38
|
+
try_complete(value, :failure)
|
39
|
+
end
|
40
|
+
|
41
|
+
def complete_with(other)
|
42
|
+
other.on_complete { |value, as| complete(value, as) }
|
43
|
+
self
|
44
|
+
end
|
45
|
+
|
46
|
+
def recover(cb = nil, &block)
|
47
|
+
cb ||= block
|
48
|
+
p = Dharma.promise
|
49
|
+
|
50
|
+
on_complete { |value| p.complete(cb.call(value)) }
|
51
|
+
|
52
|
+
p
|
53
|
+
end
|
54
|
+
|
55
|
+
def recover_with(cb = nil, &block)
|
56
|
+
cb ||= block
|
57
|
+
p = Dharma.promise
|
58
|
+
|
59
|
+
on_complete do |value, as|
|
60
|
+
case as
|
61
|
+
when :failure
|
62
|
+
begin
|
63
|
+
p.complete_with(cb.call(value))
|
64
|
+
rescue => e
|
65
|
+
p.failure(e)
|
66
|
+
end
|
67
|
+
else
|
68
|
+
p.complete(value)
|
69
|
+
end
|
70
|
+
end
|
71
|
+
|
72
|
+
p
|
73
|
+
end
|
74
|
+
end
|
75
|
+
end
|
@@ -0,0 +1,20 @@
|
|
1
|
+
module Dharma
|
2
|
+
class ThreadExecutor
|
3
|
+
def initialize
|
4
|
+
end
|
5
|
+
|
6
|
+
def execute(work)
|
7
|
+
Thread.new(work) do |work|
|
8
|
+
begin
|
9
|
+
work.call
|
10
|
+
rescue Exception => e
|
11
|
+
report_failure(e)
|
12
|
+
end
|
13
|
+
end
|
14
|
+
end
|
15
|
+
|
16
|
+
def report_failure(exception)
|
17
|
+
puts "Dharma: ThreadExecutor failure: #{exception.message} (#{exception.class})\n\t#{exception.backtrace.join("\n\t")}"
|
18
|
+
end
|
19
|
+
end
|
20
|
+
end
|
data/test/future_test.rb
ADDED
@@ -0,0 +1,61 @@
|
|
1
|
+
require 'test_helper'
|
2
|
+
|
3
|
+
class PromiseActionsTest < Test::Unit::TestCase
|
4
|
+
def test_on_complete_assigned_before
|
5
|
+
promise = Dharma.promise
|
6
|
+
|
7
|
+
result = nil
|
8
|
+
|
9
|
+
promise.on_complete do |value, as|
|
10
|
+
result = value
|
11
|
+
end
|
12
|
+
|
13
|
+
promise.try_complete(1)
|
14
|
+
|
15
|
+
assert promise.completed?
|
16
|
+
assert_equal 1, result
|
17
|
+
assert_equal 1, promise.result
|
18
|
+
end
|
19
|
+
|
20
|
+
def test_on_complete_assigned_after
|
21
|
+
promise = Dharma.promise
|
22
|
+
|
23
|
+
result = nil
|
24
|
+
|
25
|
+
promise.try_complete(1)
|
26
|
+
|
27
|
+
promise.on_complete do |value, as|
|
28
|
+
result = value
|
29
|
+
end
|
30
|
+
|
31
|
+
assert promise.completed?
|
32
|
+
assert_equal 1, result
|
33
|
+
assert_equal 1, promise.result
|
34
|
+
end
|
35
|
+
|
36
|
+
def test_on_success_callback
|
37
|
+
promise = Dharma.promise
|
38
|
+
|
39
|
+
result = nil
|
40
|
+
|
41
|
+
promise.on_success do |value|
|
42
|
+
result = value
|
43
|
+
end
|
44
|
+
|
45
|
+
promise.try_complete(1)
|
46
|
+
|
47
|
+
assert promise.completed?
|
48
|
+
assert_equal 1, result
|
49
|
+
end
|
50
|
+
|
51
|
+
def try_complete_with
|
52
|
+
p1 = Dharma.promise
|
53
|
+
p2 = Dharma.promise
|
54
|
+
|
55
|
+
p2.complete_with(p1)
|
56
|
+
|
57
|
+
p1.complete(1)
|
58
|
+
|
59
|
+
assert_equal 1, p2.result
|
60
|
+
end
|
61
|
+
end
|
@@ -0,0 +1,32 @@
|
|
1
|
+
require 'test_helper'
|
2
|
+
|
3
|
+
class PromiseTest < Test::Unit::TestCase
|
4
|
+
def test_new_promise_is_not_completed
|
5
|
+
promise = Dharma.promise
|
6
|
+
|
7
|
+
assert !promise.completed?
|
8
|
+
end
|
9
|
+
|
10
|
+
def test_completed_promise_is_completed
|
11
|
+
promise = Dharma.promise
|
12
|
+
|
13
|
+
assert promise.try_complete(1)
|
14
|
+
assert promise.completed?
|
15
|
+
end
|
16
|
+
|
17
|
+
def test_try_complete_twice
|
18
|
+
promise = Dharma.promise
|
19
|
+
|
20
|
+
assert promise.try_complete(1)
|
21
|
+
assert !promise.try_complete(2)
|
22
|
+
assert_equal 1, promise.result
|
23
|
+
end
|
24
|
+
|
25
|
+
def test_completing_with_an_exception
|
26
|
+
promise = Dharma.promise
|
27
|
+
|
28
|
+
promise.try_complete(Exception.new('broken'))
|
29
|
+
|
30
|
+
assert promise.failure?
|
31
|
+
end
|
32
|
+
end
|
data/test/test_helper.rb
ADDED
metadata
ADDED
@@ -0,0 +1,78 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: dharma
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
prerelease: false
|
5
|
+
segments:
|
6
|
+
- 0
|
7
|
+
- 9
|
8
|
+
- 0
|
9
|
+
version: 0.9.0
|
10
|
+
platform: ruby
|
11
|
+
authors:
|
12
|
+
- Eric Lindvall
|
13
|
+
autorequire:
|
14
|
+
bindir: bin
|
15
|
+
cert_chain: []
|
16
|
+
|
17
|
+
date: 2013-05-24 00:00:00 -07:00
|
18
|
+
default_executable:
|
19
|
+
dependencies: []
|
20
|
+
|
21
|
+
description: Ruby futures and promises library in the spirit of Scala
|
22
|
+
email: eric@5stops.com
|
23
|
+
executables: []
|
24
|
+
|
25
|
+
extensions: []
|
26
|
+
|
27
|
+
extra_rdoc_files:
|
28
|
+
- README.md
|
29
|
+
- LICENSE
|
30
|
+
files:
|
31
|
+
- Gemfile
|
32
|
+
- LICENSE
|
33
|
+
- README.md
|
34
|
+
- Rakefile
|
35
|
+
- dharma.gemspec
|
36
|
+
- lib/dharma.rb
|
37
|
+
- lib/dharma/future.rb
|
38
|
+
- lib/dharma/inline_executor.rb
|
39
|
+
- lib/dharma/kept_promise.rb
|
40
|
+
- lib/dharma/promise.rb
|
41
|
+
- lib/dharma/promise_actions.rb
|
42
|
+
- lib/dharma/thread_executor.rb
|
43
|
+
- test/future_test.rb
|
44
|
+
- test/promise_actions_test.rb
|
45
|
+
- test/promise_test.rb
|
46
|
+
- test/test_helper.rb
|
47
|
+
has_rdoc: true
|
48
|
+
homepage: https://github.com/eric/dharma
|
49
|
+
licenses: []
|
50
|
+
|
51
|
+
post_install_message:
|
52
|
+
rdoc_options:
|
53
|
+
- --charset=UTF-8
|
54
|
+
require_paths:
|
55
|
+
- lib
|
56
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
57
|
+
requirements:
|
58
|
+
- - ">="
|
59
|
+
- !ruby/object:Gem::Version
|
60
|
+
segments:
|
61
|
+
- 0
|
62
|
+
version: "0"
|
63
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
64
|
+
requirements:
|
65
|
+
- - ">="
|
66
|
+
- !ruby/object:Gem::Version
|
67
|
+
segments:
|
68
|
+
- 0
|
69
|
+
version: "0"
|
70
|
+
requirements: []
|
71
|
+
|
72
|
+
rubyforge_project:
|
73
|
+
rubygems_version: 1.3.6
|
74
|
+
signing_key:
|
75
|
+
specification_version: 2
|
76
|
+
summary: Ruby futures and promises library in the spirit of Scala
|
77
|
+
test_files:
|
78
|
+
- test/test_helper.rb
|