obligation 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 89ad94f0f02e4b89ea1a2b0a3d4edf3b93d53dfa
4
+ data.tar.gz: b30c77f10074e7101258c04e129064ed1139ce97
5
+ SHA512:
6
+ metadata.gz: 468c583bbedf38e2067ef85748940f65ac849ecf4e307deaea29faed1e26c7fc37df0e4e0bae26444846f71831ad81c19d7189658f87b05df7e78b65836c7acb
7
+ data.tar.gz: 4e8411787e1320da6c67ef2c32cc8129926bfdfc879c2926c1ece5d595273d2858f38b5f128dfc9f37639727d19f76e4305c1217aed3c0bd84fb178699b5fac4
data/LICENSE.txt ADDED
@@ -0,0 +1,165 @@
1
+ GNU LESSER GENERAL PUBLIC LICENSE
2
+ Version 3, 29 June 2007
3
+
4
+ Copyright (C) 2007 Free Software Foundation, Inc. <http://fsf.org/>
5
+ Everyone is permitted to copy and distribute verbatim copies
6
+ of this license document, but changing it is not allowed.
7
+
8
+
9
+ This version of the GNU Lesser General Public License incorporates
10
+ the terms and conditions of version 3 of the GNU General Public
11
+ License, supplemented by the additional permissions listed below.
12
+
13
+ 0. Additional Definitions.
14
+
15
+ As used herein, "this License" refers to version 3 of the GNU Lesser
16
+ General Public License, and the "GNU GPL" refers to version 3 of the GNU
17
+ General Public License.
18
+
19
+ "The Library" refers to a covered work governed by this License,
20
+ other than an Application or a Combined Work as defined below.
21
+
22
+ An "Application" is any work that makes use of an interface provided
23
+ by the Library, but which is not otherwise based on the Library.
24
+ Defining a subclass of a class defined by the Library is deemed a mode
25
+ of using an interface provided by the Library.
26
+
27
+ A "Combined Work" is a work produced by combining or linking an
28
+ Application with the Library. The particular version of the Library
29
+ with which the Combined Work was made is also called the "Linked
30
+ Version".
31
+
32
+ The "Minimal Corresponding Source" for a Combined Work means the
33
+ Corresponding Source for the Combined Work, excluding any source code
34
+ for portions of the Combined Work that, considered in isolation, are
35
+ based on the Application, and not on the Linked Version.
36
+
37
+ The "Corresponding Application Code" for a Combined Work means the
38
+ object code and/or source code for the Application, including any data
39
+ and utility programs needed for reproducing the Combined Work from the
40
+ Application, but excluding the System Libraries of the Combined Work.
41
+
42
+ 1. Exception to Section 3 of the GNU GPL.
43
+
44
+ You may convey a covered work under sections 3 and 4 of this License
45
+ without being bound by section 3 of the GNU GPL.
46
+
47
+ 2. Conveying Modified Versions.
48
+
49
+ If you modify a copy of the Library, and, in your modifications, a
50
+ facility refers to a function or data to be supplied by an Application
51
+ that uses the facility (other than as an argument passed when the
52
+ facility is invoked), then you may convey a copy of the modified
53
+ version:
54
+
55
+ a) under this License, provided that you make a good faith effort to
56
+ ensure that, in the event an Application does not supply the
57
+ function or data, the facility still operates, and performs
58
+ whatever part of its purpose remains meaningful, or
59
+
60
+ b) under the GNU GPL, with none of the additional permissions of
61
+ this License applicable to that copy.
62
+
63
+ 3. Object Code Incorporating Material from Library Header Files.
64
+
65
+ The object code form of an Application may incorporate material from
66
+ a header file that is part of the Library. You may convey such object
67
+ code under terms of your choice, provided that, if the incorporated
68
+ material is not limited to numerical parameters, data structure
69
+ layouts and accessors, or small macros, inline functions and templates
70
+ (ten or fewer lines in length), you do both of the following:
71
+
72
+ a) Give prominent notice with each copy of the object code that the
73
+ Library is used in it and that the Library and its use are
74
+ covered by this License.
75
+
76
+ b) Accompany the object code with a copy of the GNU GPL and this license
77
+ document.
78
+
79
+ 4. Combined Works.
80
+
81
+ You may convey a Combined Work under terms of your choice that,
82
+ taken together, effectively do not restrict modification of the
83
+ portions of the Library contained in the Combined Work and reverse
84
+ engineering for debugging such modifications, if you also do each of
85
+ the following:
86
+
87
+ a) Give prominent notice with each copy of the Combined Work that
88
+ the Library is used in it and that the Library and its use are
89
+ covered by this License.
90
+
91
+ b) Accompany the Combined Work with a copy of the GNU GPL and this license
92
+ document.
93
+
94
+ c) For a Combined Work that displays copyright notices during
95
+ execution, include the copyright notice for the Library among
96
+ these notices, as well as a reference directing the user to the
97
+ copies of the GNU GPL and this license document.
98
+
99
+ d) Do one of the following:
100
+
101
+ 0) Convey the Minimal Corresponding Source under the terms of this
102
+ License, and the Corresponding Application Code in a form
103
+ suitable for, and under terms that permit, the user to
104
+ recombine or relink the Application with a modified version of
105
+ the Linked Version to produce a modified Combined Work, in the
106
+ manner specified by section 6 of the GNU GPL for conveying
107
+ Corresponding Source.
108
+
109
+ 1) Use a suitable shared library mechanism for linking with the
110
+ Library. A suitable mechanism is one that (a) uses at run time
111
+ a copy of the Library already present on the user's computer
112
+ system, and (b) will operate properly with a modified version
113
+ of the Library that is interface-compatible with the Linked
114
+ Version.
115
+
116
+ e) Provide Installation Information, but only if you would otherwise
117
+ be required to provide such information under section 6 of the
118
+ GNU GPL, and only to the extent that such information is
119
+ necessary to install and execute a modified version of the
120
+ Combined Work produced by recombining or relinking the
121
+ Application with a modified version of the Linked Version. (If
122
+ you use option 4d0, the Installation Information must accompany
123
+ the Minimal Corresponding Source and Corresponding Application
124
+ Code. If you use option 4d1, you must provide the Installation
125
+ Information in the manner specified by section 6 of the GNU GPL
126
+ for conveying Corresponding Source.)
127
+
128
+ 5. Combined Libraries.
129
+
130
+ You may place library facilities that are a work based on the
131
+ Library side by side in a single library together with other library
132
+ facilities that are not Applications and are not covered by this
133
+ License, and convey such a combined library under terms of your
134
+ choice, if you do both of the following:
135
+
136
+ a) Accompany the combined library with a copy of the same work based
137
+ on the Library, uncombined with any other library facilities,
138
+ conveyed under the terms of this License.
139
+
140
+ b) Give prominent notice with the combined library that part of it
141
+ is a work based on the Library, and explaining where to find the
142
+ accompanying uncombined form of the same work.
143
+
144
+ 6. Revised Versions of the GNU Lesser General Public License.
145
+
146
+ The Free Software Foundation may publish revised and/or new versions
147
+ of the GNU Lesser General Public License from time to time. Such new
148
+ versions will be similar in spirit to the present version, but may
149
+ differ in detail to address new problems or concerns.
150
+
151
+ Each version is given a distinguishing version number. If the
152
+ Library as you received it specifies that a certain numbered version
153
+ of the GNU Lesser General Public License "or any later version"
154
+ applies to it, you have the option of following the terms and
155
+ conditions either of that published version or of any later version
156
+ published by the Free Software Foundation. If the Library as you
157
+ received it does not specify a version number of the GNU Lesser
158
+ General Public License, you may choose any version of the GNU Lesser
159
+ General Public License ever published by the Free Software Foundation.
160
+
161
+ If the Library as you received it specifies that a proxy can decide
162
+ whether future versions of the GNU Lesser General Public License shall
163
+ apply, that proxy's public statement of acceptance of any version is
164
+ permanent authorization for you to choose that version for the
165
+ Library.
data/README.md ADDED
@@ -0,0 +1,86 @@
1
+ # Obligation
2
+
3
+ Obligation is a support library to return promised, future results.
4
+
5
+ It is planned to support different concurrency models, like threads, EventMachine and EM + Fibers.
6
+
7
+ Note: Obliation DOES NOT implement concurrency, it can be used within library to return futures/promises that can then be used within different concurrency models.
8
+
9
+ Note: Pre-alpha library.
10
+
11
+ ## Installation
12
+
13
+ As Obligation is made to be used as a support library you will most likely add it to your gemspec:
14
+
15
+ ```ruby
16
+ spec.add_runtime_dependency 'obligation', '~> 0.1'
17
+ ```
18
+
19
+ ## Usage
20
+
21
+ Within your library you can create an Obligation:
22
+
23
+ ```ruby
24
+ def my_library_method
25
+ obligation, writer = Obligation.create
26
+ ```
27
+
28
+ Then return the `obligation` object and use the `writer` object to fulfill it somewhere in your concurrent library code:
29
+
30
+ ```ruby
31
+ Thread.new do
32
+ sleep 1
33
+ writer.fulfill 42
34
+ end
35
+
36
+ return obligation
37
+ end
38
+ ```
39
+
40
+ A user of your library using e.g. threads can now use it like a future object:
41
+
42
+ ```ruby
43
+ future = my_library_method
44
+
45
+ # Dome something else...
46
+
47
+ future.value # block until result is there
48
+ #=> 42
49
+ ```
50
+
51
+ Obligation also supports a promise-like API:
52
+
53
+ ```ruby
54
+ promise = my_library_method
55
+
56
+ p2 = promise.then do |p|
57
+ p ** 2
58
+ end
59
+
60
+ p2.value #=> 84
61
+ ```
62
+
63
+ It is also planned to implement more promise like API features like depending on multiple other promises like a data flow.
64
+
65
+ All these future and promise API functions will be made available on whatever concurrency model the user of your library uses, may it be threads or (not yet supported) EventMachine or Celluloid or Fibers.
66
+
67
+ See [Restify](https://github.com/jgraichen/restify) for a library using obligations.
68
+
69
+ ## Contributing
70
+
71
+ 1. Fork it ( http://github.com/jgraichen/obligation/fork )
72
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
73
+ 3. Commit specs for your feature so that I do not break it later
74
+ 4. Commit your changes (`git commit -am 'Add some feature'`)
75
+ 5. Push to the branch (`git push origin my-new-feature`)
76
+ 6. Create new Pull Request
77
+
78
+ ## License
79
+
80
+ Copyright (C) 2014 Jan Graichen
81
+
82
+ This program is free software: you can redistribute it and/or modify it under the terms of the GNU Lesser General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.
83
+
84
+ This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
85
+
86
+ You should have received a copy of the GNU Lesser General Public License along with this program. If not, see <http://www.gnu.org/licenses/>.
@@ -0,0 +1,169 @@
1
+ require 'concurrent/event'
2
+
3
+ module Obligation
4
+ #
5
+ class Base
6
+ include Obligation
7
+
8
+ def initialize
9
+ @state = :pending
10
+ @mutex = Mutex.new
11
+ @event = Concurrent::Event.new
12
+ @reason = nil
13
+ end
14
+
15
+ def pending?
16
+ state == :pending
17
+ end
18
+
19
+ def fulfilled?
20
+ state == :fulfilled
21
+ end
22
+
23
+ def rejected?
24
+ state == :rejected
25
+ end
26
+
27
+ def state
28
+ @mutex.synchronize { @state }
29
+ end
30
+
31
+ def reason
32
+ @mutex.synchronize { @reason }
33
+ end
34
+
35
+ def value(timeout = 30)
36
+ _value(timeout)
37
+ end
38
+
39
+ def then
40
+ return self unless block_given?
41
+
42
+ Dependent.new(self, &Proc.new)
43
+ end
44
+
45
+ protected
46
+
47
+ def _sync_pending?
48
+ @state == :pending
49
+ end
50
+
51
+ def _fulfill(result)
52
+ @mutex.synchronize { _sync_fulfill result }
53
+ end
54
+
55
+ def _sync_fulfill(result)
56
+ if @state == :pending
57
+ @state = :fulfilled
58
+ @result = result
59
+ @event.set
60
+ else
61
+ raise StateError.new "Obligation already changed to #{@state}."
62
+ end
63
+ end
64
+
65
+ def _reject(reason)
66
+ @mutex.synchronize { _sync_reject reason }
67
+ end
68
+
69
+ def _sync_reject(reason)
70
+ if @state == :pending
71
+ @state = :rejected
72
+ @reason = reason
73
+ @event.set
74
+ else
75
+ raise StateError.new "Obligation already changed to #{@state}."
76
+ end
77
+ end
78
+ end
79
+
80
+ #
81
+ class Value < Base
82
+ def initialize
83
+ super
84
+
85
+ @result = nil
86
+ end
87
+
88
+ protected
89
+
90
+ def _value(timeout)
91
+ @event.wait(timeout) if pending? && timeout != 0
92
+ @mutex.synchronize do
93
+ case @state
94
+ when :fulfilled then @result
95
+ when :rejected then raise RejectedError.new \
96
+ "Obligation rejected due to #{@reason}.", @reason
97
+ else raise TimeoutError.new
98
+ end
99
+ end
100
+ end
101
+
102
+ #
103
+ class Writer
104
+ def initialize(obligation)
105
+ @obligation = obligation
106
+ end
107
+
108
+ def fulfill(result)
109
+ @obligation.send :_fulfill, result
110
+ end
111
+
112
+ def reject(reason)
113
+ @obligation.send :_reject, reason
114
+ end
115
+ end
116
+ end
117
+
118
+ #
119
+ class Dependent < Base
120
+ def initialize(dependencies, &block)
121
+ super()
122
+ @dependencies = dependencies
123
+ @block = block
124
+ @reason = nil
125
+ @result = nil
126
+ end
127
+
128
+ protected
129
+
130
+ def _value(timeout)
131
+ @mutex.synchronize do
132
+ if _sync_pending?
133
+ begin
134
+ if @dependencies.is_a?(Array)
135
+ _sync_fulfill @block.call @dependencies.map(&:value)
136
+ else
137
+ _sync_fulfill @block.call @dependencies.value
138
+ end
139
+ rescue RejectedError => e
140
+ _sync_reject e.cause
141
+ raise RejectedError.new "Obligation rejected due to #{e.cause}."
142
+ end
143
+ end
144
+
145
+ case @state
146
+ when :fulfilled then @result
147
+ when :rejected then raise RejectedError.new \
148
+ "Obligation rejected due to #{@reason}.", @reason
149
+ end
150
+ end
151
+ end
152
+ end
153
+
154
+ #
155
+ module Impl
156
+ class << self
157
+ def create
158
+ if block_given?
159
+ Value.new.tap do |ob|
160
+ yield Value::Writer.new(ob), ob
161
+ end
162
+ else
163
+ ob = Value.new
164
+ [ob, Value::Writer.new(ob)]
165
+ end
166
+ end
167
+ end
168
+ end
169
+ end
@@ -0,0 +1,13 @@
1
+ module Obligation
2
+ module VERSION
3
+ MAJOR = 0
4
+ MINOR = 1
5
+ PATCH = 0
6
+ STAGE = nil
7
+ STRING = [MAJOR, MINOR, PATCH, STAGE].reject(&:nil?).join('.').freeze
8
+
9
+ def self.to_s
10
+ STRING
11
+ end
12
+ end
13
+ end
data/lib/obligation.rb ADDED
@@ -0,0 +1,187 @@
1
+ require 'obligation/version'
2
+
3
+ # # Obligation
4
+ #
5
+ # An {Obligation} represent a future result. It allows to do other
6
+ # things while something is calculated in the background and wait
7
+ # for it's value when needed.
8
+ #
9
+ # This implementation also supports various kinds of callbacks and
10
+ # data flow specification to integration into thread, fibered as well
11
+ # as evented environments.
12
+ #
13
+ module Obligation
14
+
15
+ # Execute given block with {Obligation} result.
16
+ #
17
+ # The moment of execution is not specified and MAY not be directly after
18
+ # {Obligation} was fulfilled. Execution of the callback SHALL be on
19
+ # same thread as the current.
20
+ #
21
+ # If callback returns an {Obligation} the outer returned {Obligation} MUST
22
+ # wait on the returned one from the callback. This allows seamless
23
+ # integration with any library using {Obligation}s to handle delayed
24
+ # return values (see example 3).
25
+ #
26
+ # @example Simple data flow
27
+ # o1 = Obligation.create do |w|
28
+ # Thread.new do
29
+ # sleep 1 # Heavy calculation
30
+ # w.fulfill 42
31
+ # end
32
+ # end
33
+ #
34
+ # o2 = o1.then do |result|
35
+ # result - 5 + 1300
36
+ # end
37
+ #
38
+ # o2.value #=> 1337
39
+ #
40
+ # @example Callback with another Obligation
41
+ # o1 = Obligation.create do |w|
42
+ # Thread.new do
43
+ # sleep 1 # Heavy calculation
44
+ # w.fulfill 42
45
+ # end
46
+ # end
47
+ #
48
+ # o2 = o1.then do |result|
49
+ # Obligation.create do |w|
50
+ # Thread.new do
51
+ # r.fulfill result.times.map do
52
+ # Net::HTTP.get('example.com', 'index.html')
53
+ # end
54
+ # end
55
+ # end
56
+ # end
57
+ #
58
+ # o2.value #=> ["<html>...", "<html>...", ...]
59
+ #
60
+ # @example Seamless integration with any library using {Obligation}
61
+ # o1 = Obligation.create do |w|
62
+ # Thread.new do
63
+ # sleep 1 # Heavy calculation
64
+ # w.fulfill 42
65
+ # end
66
+ # end
67
+ #
68
+ # o2 = o1.then do |result|
69
+ # HTTPLibUsingObligation.get("http://example.org/#{result}.html")
70
+ # end
71
+ #
72
+ # o2.value #=> ["<html>..."]
73
+ #
74
+ # @yield [result] Yield {Obligation}s result after it is fulfilled.
75
+ # @yieldparam result [Object] {Obligation} result.
76
+ # @yieldreturn [Object|Obligation] The returned {Obligation}s result.
77
+ # If result is a {Obligation} the returned {Obligation} will wait
78
+ # on it's result.
79
+ # @return [Obligation] {Obligation} for yielded result.
80
+ #
81
+ def then
82
+ throw NotImplementedError.new 'Obligation#then not implemented.'
83
+ end
84
+
85
+ # Wait for {Obligation}'s result.
86
+ #
87
+ # This method MUST block the thread of fiber until either
88
+ # the {Obligation} is fullfilled or the given timeout exceeds.
89
+ #
90
+ # If the {Obligation} is already fulfilled the result MUST
91
+ # be returned instantly. An {RejectedError} MUST be raised when
92
+ # {Obligation} is rejected and a {TimeoutError} when waiting
93
+ # has timed out.
94
+ #
95
+ # @param timeout [Integer] Timeout to wait in seconds.
96
+ # @return [Object] {Obligation}'s result if any. Nil if rejected
97
+ # or timed out.
98
+ # @raise [RejectedError] Raised when {Obligation} is rejected.
99
+ # Raised error will contain reason as `#cause`.
100
+ # @raise [TimeoutError] Raised when waiting on result has timed out.
101
+ #
102
+ def value(timeout)
103
+ throw NotImplementedError.new 'Obligation#value not implemented.'
104
+ end
105
+
106
+ # Check if {Obligation} is fulfilled.
107
+ #
108
+ # @example
109
+ # o = Obligation.create do |w|
110
+ # Thread.new { sleep 1; w.fulfill 42 }
111
+ # end
112
+ #
113
+ # o.fulfilled? #=> false
114
+ # o.value
115
+ # o.fulfilled? #=> true
116
+ #
117
+ # @return [Boolean] True if {Obligation} is fulfilled, false otherwise.
118
+ #
119
+ def fulfilled?
120
+ throw NotImplementedError.new 'Obligation#fulfilled? not implemented.'
121
+ end
122
+
123
+ # Check if {Obligation} is pending e.g. neither fulfilled or rejected.
124
+ #
125
+ # @example
126
+ # o = Obligation.create do |w|
127
+ # Thread.new { sleep 1; w.fulfill 42 }
128
+ # end
129
+ #
130
+ # o.pending? #=> true
131
+ # o.value
132
+ # o.pending? #=> false
133
+ #
134
+ # @return [Boolean] True if {Obligation} is pending, false otherwise.
135
+ #
136
+ def pending?
137
+ throw NotImplementedError.new 'Obligation#pending? not implemented.'
138
+ end
139
+
140
+ # Check if {Obligation} is rejected.
141
+ #
142
+ # @example
143
+ # o = Obligation.create do |w|
144
+ # Thread.new { sleep 1; w.reject StandardError.new }
145
+ # end
146
+ #
147
+ # o.rejected? #=> false
148
+ # o.value
149
+ # o.pending? #=> true
150
+ #
151
+ # @return [Boolean] True if {Obligation} is rejected, false otherwise.
152
+ #
153
+ def rejected?
154
+ throw NotImplementedError.new 'Obligation#rejected? not implemented.'
155
+ end
156
+
157
+ # A {RejectedError} will be raised when {Obligation} is rejected
158
+ # and someone tries to access the result.
159
+ #
160
+ class RejectedError < StandardError
161
+ attr_reader :cause
162
+ def initialize(msg, cause = $!)
163
+ super msg
164
+ @cause = cause
165
+ end
166
+ end
167
+
168
+ # A {TimeoutError} will be raised when waiting for a result
169
+ # has timed out.
170
+ class TimeoutError < StandardError; end
171
+
172
+ # A {StateError} will be raised when a obligation is tried to
173
+ # fulfill or reject twice.
174
+ class StateError < StandardError; end
175
+
176
+ class << self
177
+ def create(*args, &block)
178
+ Impl.create(*args, &block)
179
+ end
180
+
181
+ def on(*obligations)
182
+ Impl.on(*obligations)
183
+ end
184
+ end
185
+ end
186
+
187
+ require 'obligation/impl'
@@ -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 'obligation/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = 'obligation'
8
+ spec.version = Obligation::VERSION
9
+ spec.authors = ['Jan Graichen']
10
+ spec.email = ['jg@altimos.de']
11
+ spec.summary = %q{Support library to provide Futures and Promises for different concurrency models.}
12
+ spec.description = %q{Support library to provide Futures and Promises for different concurrency models.}
13
+ spec.homepage = 'https://github.com/jgraichen/obligation'
14
+ spec.license = 'LGPLv3'
15
+
16
+ spec.files = Dir['**/*'].grep(%r{^((bin|lib|test|spec|features)/|.*\.gemspec|.*LICENSE.*|.*README.*|.*CHANGELOG.*)})
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_runtime_dependency 'concurrent-ruby', '~> 0.5.0'
22
+
23
+ spec.add_development_dependency 'bundler', '~> 1.5'
24
+ end
@@ -0,0 +1,19 @@
1
+ require 'rspec'
2
+
3
+ if ENV['CI'] || (defined?(:RUBY_ENGINE) && RUBY_ENGINE != 'rbx')
4
+ require 'coveralls'
5
+ Coveralls.wear! do
6
+ add_filter 'spec'
7
+ end
8
+ end
9
+
10
+ require 'bundler'
11
+ Bundler.require :default, :test
12
+
13
+ require 'obligation'
14
+
15
+ Dir[File.expand_path('spec/support/**/*.rb')].each {|f| require f }
16
+
17
+ RSpec.configure do |config|
18
+ config.order = 'random'
19
+ end
@@ -0,0 +1,53 @@
1
+ require 'spec_helper'
2
+
3
+ describe Obligation do
4
+ context 'with threaded environment:' do
5
+ describe 'then dataflow:' do
6
+ it 'should let data flow' do
7
+ o1 = Obligation.create do |w|
8
+ Thread.new do
9
+ sleep 0.2
10
+ w.fulfill 42
11
+ end
12
+ end
13
+
14
+ o2 = o1.then do |result|
15
+ result - 5
16
+ end
17
+
18
+ o3 = o2.then do |result|
19
+ result + 1300
20
+ end
21
+
22
+ start = Time.now
23
+ result = o3.value
24
+
25
+ expect(Time.now - start).to be > 0.2
26
+ expect(result).to eq 1337
27
+ end
28
+
29
+ it 'should inherit error on reject' do
30
+ o1 = Obligation.create do |w|
31
+ Thread.new do
32
+ w.reject RuntimeError.new 'ERR'
33
+ end
34
+ end
35
+
36
+ o2 = o1.then do |result|
37
+ result - 5
38
+ end
39
+
40
+ expect { o2.value }.to raise_error Obligation::RejectedError
41
+
42
+ begin
43
+ o2.value
44
+ rescue Obligation::RejectedError => err
45
+ error = err
46
+ end
47
+
48
+ expect(error.cause).to be_a RuntimeError
49
+ expect(error.cause.message).to eq 'ERR'
50
+ end
51
+ end
52
+ end
53
+ end
@@ -0,0 +1,53 @@
1
+ require 'spec_helper'
2
+
3
+ describe Obligation do
4
+ context 'with threaded environment:' do
5
+ describe 'get value:' do
6
+ it 'should wait on fulfill' do
7
+ o, w = Obligation.create
8
+
9
+ Thread.new do
10
+ sleep 0.2
11
+ w.fulfill 42
12
+ end
13
+
14
+ start = Time.now
15
+ result = o.value
16
+
17
+ expect(result).to eq 42
18
+ expect(Time.now - start).to be_within(0.1).of(0.2)
19
+ end
20
+
21
+ it 'should be able to timeout' do
22
+ o, _ = Obligation.create
23
+ start = Time.now
24
+
25
+ expect { o.value(0.5) }.to raise_error
26
+ expect(Time.now - start).to be > 0.5
27
+ end
28
+
29
+ it 'should raise on reject' do
30
+ o = Obligation.create do |w|
31
+ Thread.new do
32
+ begin
33
+ raise RuntimeError.new 'ERR'
34
+ rescue => e
35
+ w.reject e
36
+ end
37
+ end
38
+ end
39
+
40
+ expect{ o.value }.to raise_error Obligation::RejectedError
41
+
42
+ begin
43
+ o.value
44
+ rescue Obligation::RejectedError => err
45
+ error = err
46
+ end
47
+
48
+ expect(error.cause).to be_a RuntimeError
49
+ expect(error.cause.message).to eq 'ERR'
50
+ end
51
+ end
52
+ end
53
+ end
metadata ADDED
@@ -0,0 +1,86 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: obligation
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Jan Graichen
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2014-05-29 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: concurrent-ruby
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: 0.5.0
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: 0.5.0
27
+ - !ruby/object:Gem::Dependency
28
+ name: bundler
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '1.5'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '1.5'
41
+ description: Support library to provide Futures and Promises for different concurrency
42
+ models.
43
+ email:
44
+ - jg@altimos.de
45
+ executables: []
46
+ extensions: []
47
+ extra_rdoc_files: []
48
+ files:
49
+ - LICENSE.txt
50
+ - README.md
51
+ - lib/obligation.rb
52
+ - lib/obligation/impl.rb
53
+ - lib/obligation/version.rb
54
+ - obligation.gemspec
55
+ - spec/spec_helper.rb
56
+ - spec/thread/then_spec.rb
57
+ - spec/thread/value_spec.rb
58
+ homepage: https://github.com/jgraichen/obligation
59
+ licenses:
60
+ - LGPLv3
61
+ metadata: {}
62
+ post_install_message:
63
+ rdoc_options: []
64
+ require_paths:
65
+ - lib
66
+ required_ruby_version: !ruby/object:Gem::Requirement
67
+ requirements:
68
+ - - ">="
69
+ - !ruby/object:Gem::Version
70
+ version: '0'
71
+ required_rubygems_version: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - ">="
74
+ - !ruby/object:Gem::Version
75
+ version: '0'
76
+ requirements: []
77
+ rubyforge_project:
78
+ rubygems_version: 2.2.2
79
+ signing_key:
80
+ specification_version: 4
81
+ summary: Support library to provide Futures and Promises for different concurrency
82
+ models.
83
+ test_files:
84
+ - spec/spec_helper.rb
85
+ - spec/thread/then_spec.rb
86
+ - spec/thread/value_spec.rb