concurrent-ruby 0.2.1 → 0.2.2
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/LICENSE +21 -21
- data/README.md +276 -275
- data/lib/concurrent.rb +28 -28
- data/lib/concurrent/agent.rb +114 -114
- data/lib/concurrent/cached_thread_pool.rb +131 -131
- data/lib/concurrent/defer.rb +65 -65
- data/lib/concurrent/event.rb +60 -60
- data/lib/concurrent/event_machine_defer_proxy.rb +23 -23
- data/lib/concurrent/executor.rb +96 -96
- data/lib/concurrent/fixed_thread_pool.rb +99 -99
- data/lib/concurrent/functions.rb +120 -120
- data/lib/concurrent/future.rb +42 -42
- data/lib/concurrent/global_thread_pool.rb +24 -16
- data/lib/concurrent/goroutine.rb +29 -29
- data/lib/concurrent/null_thread_pool.rb +22 -22
- data/lib/concurrent/obligation.rb +67 -67
- data/lib/concurrent/promise.rb +174 -174
- data/lib/concurrent/reactor.rb +166 -166
- data/lib/concurrent/reactor/drb_async_demux.rb +83 -83
- data/lib/concurrent/reactor/tcp_sync_demux.rb +131 -131
- data/lib/concurrent/supervisor.rb +105 -105
- data/lib/concurrent/thread_pool.rb +76 -76
- data/lib/concurrent/utilities.rb +32 -32
- data/lib/concurrent/version.rb +3 -3
- data/lib/concurrent_ruby.rb +1 -1
- data/md/agent.md +123 -123
- data/md/defer.md +174 -174
- data/md/event.md +32 -32
- data/md/executor.md +187 -187
- data/md/future.md +83 -83
- data/md/goroutine.md +52 -52
- data/md/obligation.md +32 -32
- data/md/promise.md +227 -227
- data/md/thread_pool.md +224 -224
- data/spec/concurrent/agent_spec.rb +390 -386
- data/spec/concurrent/cached_thread_pool_spec.rb +125 -125
- data/spec/concurrent/defer_spec.rb +199 -195
- data/spec/concurrent/event_machine_defer_proxy_spec.rb +256 -256
- data/spec/concurrent/event_spec.rb +134 -134
- data/spec/concurrent/executor_spec.rb +200 -200
- data/spec/concurrent/fixed_thread_pool_spec.rb +83 -83
- data/spec/concurrent/functions_spec.rb +217 -217
- data/spec/concurrent/future_spec.rb +112 -108
- data/spec/concurrent/global_thread_pool_spec.rb +11 -38
- data/spec/concurrent/goroutine_spec.rb +67 -67
- data/spec/concurrent/null_thread_pool_spec.rb +57 -57
- data/spec/concurrent/obligation_shared.rb +132 -132
- data/spec/concurrent/promise_spec.rb +316 -312
- data/spec/concurrent/reactor/drb_async_demux_spec.rb +196 -196
- data/spec/concurrent/reactor/tcp_sync_demux_spec.rb +410 -410
- data/spec/concurrent/reactor_spec.rb +364 -364
- data/spec/concurrent/supervisor_spec.rb +269 -269
- data/spec/concurrent/thread_pool_shared.rb +204 -204
- data/spec/concurrent/uses_global_thread_pool_shared.rb +64 -0
- data/spec/concurrent/utilities_spec.rb +74 -74
- data/spec/spec_helper.rb +32 -32
- metadata +17 -19
@@ -1,125 +1,125 @@
|
|
1
|
-
require 'spec_helper'
|
2
|
-
require_relative 'thread_pool_shared'
|
3
|
-
|
4
|
-
module Concurrent
|
5
|
-
|
6
|
-
describe CachedThreadPool do
|
7
|
-
|
8
|
-
subject { CachedThreadPool.new }
|
9
|
-
|
10
|
-
it_should_behave_like 'Thread Pool'
|
11
|
-
|
12
|
-
context '#initialize' do
|
13
|
-
|
14
|
-
it 'aliases Concurrent#new_cached_thread_pool' do
|
15
|
-
pool = Concurrent.new_cached_thread_pool
|
16
|
-
pool.should be_a(CachedThreadPool)
|
17
|
-
pool.size.should eq 0
|
18
|
-
end
|
19
|
-
end
|
20
|
-
|
21
|
-
context '#kill' do
|
22
|
-
|
23
|
-
it 'kills all threads' do
|
24
|
-
Thread.should_receive(:kill).at_least(5).times
|
25
|
-
pool = CachedThreadPool.new
|
26
|
-
5.times{ sleep(0.1); pool << proc{ sleep(1) } }
|
27
|
-
sleep(1)
|
28
|
-
pool.kill
|
29
|
-
sleep(0.1)
|
30
|
-
end
|
31
|
-
end
|
32
|
-
|
33
|
-
context '#size' do
|
34
|
-
|
35
|
-
it 'returns zero for a new thread pool' do
|
36
|
-
subject.size.should eq 0
|
37
|
-
end
|
38
|
-
|
39
|
-
it 'returns the size of the subject when running' do
|
40
|
-
5.times{ sleep(0.1); subject << proc{ sleep(1) } }
|
41
|
-
subject.size.should eq 5
|
42
|
-
end
|
43
|
-
|
44
|
-
it 'returns zero once shut down' do
|
45
|
-
subject.shutdown
|
46
|
-
subject.size.should eq 0
|
47
|
-
end
|
48
|
-
end
|
49
|
-
|
50
|
-
context 'worker creation and caching' do
|
51
|
-
|
52
|
-
it 'creates new workers when there are none available' do
|
53
|
-
subject.size.should eq 0
|
54
|
-
5.times{ sleep(0.1); subject << proc{ sleep(1000) } }
|
55
|
-
sleep(1)
|
56
|
-
subject.size.should eq 5
|
57
|
-
end
|
58
|
-
|
59
|
-
it 'uses existing idle threads' do
|
60
|
-
5.times{ sleep(0.05); subject << proc{ sleep(0.5) } }
|
61
|
-
sleep(1)
|
62
|
-
3.times{ sleep(0.1); subject << proc{ sleep(0.5) } }
|
63
|
-
subject.size.should eq 5
|
64
|
-
end
|
65
|
-
end
|
66
|
-
|
67
|
-
context 'garbage collection' do
|
68
|
-
|
69
|
-
subject{ CachedThreadPool.new(gc_interval: 1, thread_idleime: 1) }
|
70
|
-
|
71
|
-
it 'starts when the first thread is added to the pool' do
|
72
|
-
subject.should_receive(:collect_garbage)
|
73
|
-
subject << proc{ nil }
|
74
|
-
sleep(0.1)
|
75
|
-
end
|
76
|
-
|
77
|
-
it 'removes from pool any thread that has been idle too long' do
|
78
|
-
subject << proc{ nil }
|
79
|
-
subject.size.should eq 1
|
80
|
-
sleep(1.5)
|
81
|
-
subject.size.should eq 0
|
82
|
-
end
|
83
|
-
|
84
|
-
it 'removed from pool any dead thread' do
|
85
|
-
subject << proc{ raise StandardError }
|
86
|
-
subject.size.should eq 1
|
87
|
-
sleep(1.5)
|
88
|
-
subject.size.should eq 0
|
89
|
-
end
|
90
|
-
|
91
|
-
it 'resets the working count appropriately' do
|
92
|
-
subject << proc{ sleep(1000) }
|
93
|
-
sleep(0.1)
|
94
|
-
subject << proc{ raise StandardError }
|
95
|
-
sleep(0.1)
|
96
|
-
subject << proc{ nil }
|
97
|
-
|
98
|
-
sleep(0.1)
|
99
|
-
subject.working.should eq 2
|
100
|
-
|
101
|
-
sleep(1.5)
|
102
|
-
subject.working.should eq 1
|
103
|
-
end
|
104
|
-
|
105
|
-
it 'stops collection when the pool size becomes zero' do
|
106
|
-
3.times{ sleep(0.1); subject << proc{ sleep(0.5) } }
|
107
|
-
subject.instance_variable_get(:@collector).status.should eq 'sleep'
|
108
|
-
sleep(1.5)
|
109
|
-
subject.instance_variable_get(:@collector).status.should be_false
|
110
|
-
end
|
111
|
-
end
|
112
|
-
|
113
|
-
context '#status' do
|
114
|
-
|
115
|
-
it 'returns an empty collection when the pool is empty' do
|
116
|
-
subject.status.should be_empty
|
117
|
-
end
|
118
|
-
|
119
|
-
it 'returns one status object for each thread in the pool' do
|
120
|
-
3.times{ sleep(0.1); subject << proc{ sleep(0.5) } }
|
121
|
-
subject.status.length.should eq 3
|
122
|
-
end
|
123
|
-
end
|
124
|
-
end
|
125
|
-
end
|
1
|
+
require 'spec_helper'
|
2
|
+
require_relative 'thread_pool_shared'
|
3
|
+
|
4
|
+
module Concurrent
|
5
|
+
|
6
|
+
describe CachedThreadPool do
|
7
|
+
|
8
|
+
subject { CachedThreadPool.new }
|
9
|
+
|
10
|
+
it_should_behave_like 'Thread Pool'
|
11
|
+
|
12
|
+
context '#initialize' do
|
13
|
+
|
14
|
+
it 'aliases Concurrent#new_cached_thread_pool' do
|
15
|
+
pool = Concurrent.new_cached_thread_pool
|
16
|
+
pool.should be_a(CachedThreadPool)
|
17
|
+
pool.size.should eq 0
|
18
|
+
end
|
19
|
+
end
|
20
|
+
|
21
|
+
context '#kill' do
|
22
|
+
|
23
|
+
it 'kills all threads' do
|
24
|
+
Thread.should_receive(:kill).at_least(5).times
|
25
|
+
pool = CachedThreadPool.new
|
26
|
+
5.times{ sleep(0.1); pool << proc{ sleep(1) } }
|
27
|
+
sleep(1)
|
28
|
+
pool.kill
|
29
|
+
sleep(0.1)
|
30
|
+
end
|
31
|
+
end
|
32
|
+
|
33
|
+
context '#size' do
|
34
|
+
|
35
|
+
it 'returns zero for a new thread pool' do
|
36
|
+
subject.size.should eq 0
|
37
|
+
end
|
38
|
+
|
39
|
+
it 'returns the size of the subject when running' do
|
40
|
+
5.times{ sleep(0.1); subject << proc{ sleep(1) } }
|
41
|
+
subject.size.should eq 5
|
42
|
+
end
|
43
|
+
|
44
|
+
it 'returns zero once shut down' do
|
45
|
+
subject.shutdown
|
46
|
+
subject.size.should eq 0
|
47
|
+
end
|
48
|
+
end
|
49
|
+
|
50
|
+
context 'worker creation and caching' do
|
51
|
+
|
52
|
+
it 'creates new workers when there are none available' do
|
53
|
+
subject.size.should eq 0
|
54
|
+
5.times{ sleep(0.1); subject << proc{ sleep(1000) } }
|
55
|
+
sleep(1)
|
56
|
+
subject.size.should eq 5
|
57
|
+
end
|
58
|
+
|
59
|
+
it 'uses existing idle threads' do
|
60
|
+
5.times{ sleep(0.05); subject << proc{ sleep(0.5) } }
|
61
|
+
sleep(1)
|
62
|
+
3.times{ sleep(0.1); subject << proc{ sleep(0.5) } }
|
63
|
+
subject.size.should eq 5
|
64
|
+
end
|
65
|
+
end
|
66
|
+
|
67
|
+
context 'garbage collection' do
|
68
|
+
|
69
|
+
subject{ CachedThreadPool.new(gc_interval: 1, thread_idleime: 1) }
|
70
|
+
|
71
|
+
it 'starts when the first thread is added to the pool' do
|
72
|
+
subject.should_receive(:collect_garbage)
|
73
|
+
subject << proc{ nil }
|
74
|
+
sleep(0.1)
|
75
|
+
end
|
76
|
+
|
77
|
+
it 'removes from pool any thread that has been idle too long' do
|
78
|
+
subject << proc{ nil }
|
79
|
+
subject.size.should eq 1
|
80
|
+
sleep(1.5)
|
81
|
+
subject.size.should eq 0
|
82
|
+
end
|
83
|
+
|
84
|
+
it 'removed from pool any dead thread' do
|
85
|
+
subject << proc{ raise StandardError }
|
86
|
+
subject.size.should eq 1
|
87
|
+
sleep(1.5)
|
88
|
+
subject.size.should eq 0
|
89
|
+
end
|
90
|
+
|
91
|
+
it 'resets the working count appropriately' do
|
92
|
+
subject << proc{ sleep(1000) }
|
93
|
+
sleep(0.1)
|
94
|
+
subject << proc{ raise StandardError }
|
95
|
+
sleep(0.1)
|
96
|
+
subject << proc{ nil }
|
97
|
+
|
98
|
+
sleep(0.1)
|
99
|
+
subject.working.should eq 2
|
100
|
+
|
101
|
+
sleep(1.5)
|
102
|
+
subject.working.should eq 1
|
103
|
+
end
|
104
|
+
|
105
|
+
it 'stops collection when the pool size becomes zero' do
|
106
|
+
3.times{ sleep(0.1); subject << proc{ sleep(0.5) } }
|
107
|
+
subject.instance_variable_get(:@collector).status.should eq 'sleep'
|
108
|
+
sleep(1.5)
|
109
|
+
subject.instance_variable_get(:@collector).status.should be_false
|
110
|
+
end
|
111
|
+
end
|
112
|
+
|
113
|
+
context '#status' do
|
114
|
+
|
115
|
+
it 'returns an empty collection when the pool is empty' do
|
116
|
+
subject.status.should be_empty
|
117
|
+
end
|
118
|
+
|
119
|
+
it 'returns one status object for each thread in the pool' do
|
120
|
+
3.times{ sleep(0.1); subject << proc{ sleep(0.5) } }
|
121
|
+
subject.status.length.should eq 3
|
122
|
+
end
|
123
|
+
end
|
124
|
+
end
|
125
|
+
end
|
@@ -1,195 +1,199 @@
|
|
1
|
-
require 'spec_helper'
|
2
|
-
|
3
|
-
|
4
|
-
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
Defer.
|
34
|
-
|
35
|
-
|
36
|
-
|
37
|
-
|
38
|
-
|
39
|
-
|
40
|
-
|
41
|
-
|
42
|
-
|
43
|
-
|
44
|
-
|
45
|
-
|
46
|
-
|
47
|
-
|
48
|
-
|
49
|
-
|
50
|
-
|
51
|
-
|
52
|
-
|
53
|
-
|
54
|
-
|
55
|
-
|
56
|
-
|
57
|
-
|
58
|
-
|
59
|
-
|
60
|
-
|
61
|
-
|
62
|
-
|
63
|
-
|
64
|
-
|
65
|
-
|
66
|
-
|
67
|
-
|
68
|
-
|
69
|
-
|
70
|
-
|
71
|
-
|
72
|
-
|
73
|
-
|
74
|
-
|
75
|
-
|
76
|
-
|
77
|
-
|
78
|
-
|
79
|
-
|
80
|
-
|
81
|
-
|
82
|
-
|
83
|
-
|
84
|
-
|
85
|
-
|
86
|
-
|
87
|
-
|
88
|
-
|
89
|
-
|
90
|
-
|
91
|
-
|
92
|
-
|
93
|
-
|
94
|
-
|
95
|
-
|
96
|
-
|
97
|
-
|
98
|
-
|
99
|
-
|
100
|
-
|
101
|
-
|
102
|
-
|
103
|
-
|
104
|
-
|
105
|
-
|
106
|
-
|
107
|
-
|
108
|
-
|
109
|
-
|
110
|
-
|
111
|
-
|
112
|
-
|
113
|
-
|
114
|
-
|
115
|
-
|
116
|
-
|
117
|
-
|
118
|
-
|
119
|
-
|
120
|
-
|
121
|
-
|
122
|
-
|
123
|
-
|
124
|
-
|
125
|
-
|
126
|
-
|
127
|
-
deferred
|
128
|
-
|
129
|
-
|
130
|
-
|
131
|
-
|
132
|
-
|
133
|
-
|
134
|
-
|
135
|
-
|
136
|
-
|
137
|
-
|
138
|
-
|
139
|
-
|
140
|
-
|
141
|
-
|
142
|
-
|
143
|
-
|
144
|
-
|
145
|
-
|
146
|
-
|
147
|
-
|
148
|
-
|
149
|
-
|
150
|
-
|
151
|
-
|
152
|
-
|
153
|
-
@expected
|
154
|
-
|
155
|
-
|
156
|
-
|
157
|
-
|
158
|
-
|
159
|
-
|
160
|
-
@expected
|
161
|
-
|
162
|
-
|
163
|
-
|
164
|
-
|
165
|
-
|
166
|
-
|
167
|
-
@expected
|
168
|
-
|
169
|
-
|
170
|
-
|
171
|
-
|
172
|
-
|
173
|
-
|
174
|
-
@expected
|
175
|
-
|
176
|
-
|
177
|
-
|
178
|
-
|
179
|
-
|
180
|
-
|
181
|
-
@expected
|
182
|
-
|
183
|
-
|
184
|
-
|
185
|
-
|
186
|
-
|
187
|
-
|
188
|
-
@expected
|
189
|
-
|
190
|
-
|
191
|
-
|
192
|
-
|
193
|
-
|
194
|
-
|
195
|
-
|
1
|
+
require 'spec_helper'
|
2
|
+
require_relative 'uses_global_thread_pool_shared'
|
3
|
+
|
4
|
+
module Concurrent
|
5
|
+
|
6
|
+
describe Defer do
|
7
|
+
|
8
|
+
let!(:thread_pool_user){ Defer }
|
9
|
+
it_should_behave_like Concurrent::UsesGlobalThreadPool
|
10
|
+
|
11
|
+
before(:each) do
|
12
|
+
Defer.thread_pool = FixedThreadPool.new(1)
|
13
|
+
end
|
14
|
+
|
15
|
+
context '#initialize' do
|
16
|
+
|
17
|
+
it 'raises an exception if no block or operation given' do
|
18
|
+
lambda {
|
19
|
+
Defer.new
|
20
|
+
}.should raise_error(ArgumentError)
|
21
|
+
end
|
22
|
+
|
23
|
+
it 'raises an exception if both a block and an operation given' do
|
24
|
+
lambda {
|
25
|
+
operation = proc{ nil }
|
26
|
+
Defer.new(op: operation){ nil }
|
27
|
+
}.should raise_error(ArgumentError)
|
28
|
+
end
|
29
|
+
|
30
|
+
it 'starts the thread if an operation is given' do
|
31
|
+
Defer.thread_pool.should_receive(:post).once.with(any_args())
|
32
|
+
operation = proc{ nil }
|
33
|
+
Defer.new(op: operation)
|
34
|
+
end
|
35
|
+
|
36
|
+
it 'does not start the thread if neither a callback or errorback is given' do
|
37
|
+
Defer.thread_pool.should_not_receive(:post)
|
38
|
+
Defer.new{ nil }
|
39
|
+
end
|
40
|
+
end
|
41
|
+
|
42
|
+
context '#then' do
|
43
|
+
|
44
|
+
it 'raises an exception if no block given' do
|
45
|
+
lambda {
|
46
|
+
Defer.new{ nil }.then
|
47
|
+
}.should raise_error(ArgumentError)
|
48
|
+
end
|
49
|
+
|
50
|
+
it 'raises an exception if called twice' do
|
51
|
+
lambda {
|
52
|
+
Defer.new{ nil }.then{|result| nil }.then{|result| nil }
|
53
|
+
}.should raise_error(IllegalMethodCallError)
|
54
|
+
end
|
55
|
+
|
56
|
+
it 'raises an exception if an operation was provided at construction' do
|
57
|
+
lambda {
|
58
|
+
operation = proc{ nil }
|
59
|
+
Defer.new(op: operation).then{|result| nil }
|
60
|
+
}.should raise_error(IllegalMethodCallError)
|
61
|
+
end
|
62
|
+
|
63
|
+
it 'raises an exception if a callback was provided at construction' do
|
64
|
+
lambda {
|
65
|
+
callback = proc{|result|nil }
|
66
|
+
Defer.new(callback: callback){ nil }.then{|result| nil }
|
67
|
+
}.should raise_error(IllegalMethodCallError)
|
68
|
+
end
|
69
|
+
|
70
|
+
it 'returns self' do
|
71
|
+
deferred = Defer.new{ nil }
|
72
|
+
deferred.then{|result| nil }.should eq deferred
|
73
|
+
end
|
74
|
+
end
|
75
|
+
|
76
|
+
context '#rescue' do
|
77
|
+
|
78
|
+
it 'raises an exception if no block given' do
|
79
|
+
lambda {
|
80
|
+
Defer.new{ nil }.rescue
|
81
|
+
}.should raise_error(ArgumentError)
|
82
|
+
end
|
83
|
+
|
84
|
+
it 'raises an exception if called twice' do
|
85
|
+
lambda {
|
86
|
+
Defer.new{ nil }.rescue{ nil }.rescue{ nil }
|
87
|
+
}.should raise_error(IllegalMethodCallError)
|
88
|
+
end
|
89
|
+
|
90
|
+
it 'raises an exception if an operation was provided at construction' do
|
91
|
+
lambda {
|
92
|
+
operation = proc{ nil }
|
93
|
+
Defer.new(op: operation).rescue{|ex| nil }
|
94
|
+
}.should raise_error(IllegalMethodCallError)
|
95
|
+
end
|
96
|
+
|
97
|
+
it 'raises an exception if an errorback was provided at construction' do
|
98
|
+
lambda {
|
99
|
+
errorback = proc{|ex| nil }
|
100
|
+
Defer.new(errorback: errorback){ nil }.rescue{|ex| nil }
|
101
|
+
}.should raise_error(IllegalMethodCallError)
|
102
|
+
end
|
103
|
+
|
104
|
+
it 'returns self' do
|
105
|
+
deferred = Defer.new{ nil }
|
106
|
+
deferred.rescue{|ex| nil }.should eq deferred
|
107
|
+
end
|
108
|
+
|
109
|
+
it 'aliases #catch' do
|
110
|
+
lambda {
|
111
|
+
Defer.new{ nil }.catch{|ex| nil }
|
112
|
+
}.should_not raise_error
|
113
|
+
end
|
114
|
+
|
115
|
+
it 'aliases #on_error' do
|
116
|
+
lambda {
|
117
|
+
Defer.new{ nil }.on_error{|ex| nil }
|
118
|
+
}.should_not raise_error
|
119
|
+
end
|
120
|
+
end
|
121
|
+
|
122
|
+
context '#go' do
|
123
|
+
|
124
|
+
it 'starts the thread if not started' do
|
125
|
+
deferred = Defer.new{ nil }
|
126
|
+
Defer.thread_pool.should_receive(:post).once.with(any_args())
|
127
|
+
deferred.go
|
128
|
+
end
|
129
|
+
|
130
|
+
it 'does nothing if called more than once' do
|
131
|
+
deferred = Defer.new{ nil }
|
132
|
+
deferred.go
|
133
|
+
Defer.thread_pool.should_not_receive(:post)
|
134
|
+
deferred.go
|
135
|
+
end
|
136
|
+
|
137
|
+
it 'does nothing if thread started at construction' do
|
138
|
+
operation = proc{ nil }
|
139
|
+
callback = proc{|result| nil }
|
140
|
+
errorback = proc{|ex| nil }
|
141
|
+
deferred = Defer.new(op: operation, callback: callback, errorback: errorback)
|
142
|
+
Defer.thread_pool.should_not_receive(:post)
|
143
|
+
deferred.go
|
144
|
+
end
|
145
|
+
end
|
146
|
+
|
147
|
+
context 'fulfillment' do
|
148
|
+
|
149
|
+
it 'runs the operation' do
|
150
|
+
@expected = false
|
151
|
+
Defer.new{ @expected = true }.go
|
152
|
+
sleep(0.1)
|
153
|
+
@expected.should be_true
|
154
|
+
end
|
155
|
+
|
156
|
+
it 'calls the callback when the operation is successful' do
|
157
|
+
@expected = false
|
158
|
+
Defer.new{ true }.then{|result| @expected = true }.go
|
159
|
+
sleep(0.1)
|
160
|
+
@expected.should be_true
|
161
|
+
end
|
162
|
+
|
163
|
+
it 'passes the result of the block to the callback' do
|
164
|
+
@expected = false
|
165
|
+
Defer.new{ 'w00t' }.then{|result| @expected = result }.go
|
166
|
+
sleep(0.1)
|
167
|
+
@expected.should eq 'w00t'
|
168
|
+
end
|
169
|
+
|
170
|
+
it 'does not call the errorback when the operation is successful' do
|
171
|
+
@expected = true
|
172
|
+
Defer.new{ nil }.rescue{|ex| @expected = false }.go
|
173
|
+
sleep(0.1)
|
174
|
+
@expected.should be_true
|
175
|
+
end
|
176
|
+
|
177
|
+
it 'calls the errorback if the operation throws an exception' do
|
178
|
+
@expected = false
|
179
|
+
Defer.new{ raise StandardError }.rescue{|ex| @expected = true }.go
|
180
|
+
sleep(0.1)
|
181
|
+
@expected.should be_true
|
182
|
+
end
|
183
|
+
|
184
|
+
it 'passes the exception object to the errorback' do
|
185
|
+
@expected = nil
|
186
|
+
Defer.new{ raise StandardError }.rescue{|ex| @expected = ex }.go
|
187
|
+
sleep(0.1)
|
188
|
+
@expected.should be_a(StandardError)
|
189
|
+
end
|
190
|
+
|
191
|
+
it 'does not call the callback when the operation fails' do
|
192
|
+
@expected = true
|
193
|
+
Defer.new{ raise StandardError }.then{|result| @expected = false }.go
|
194
|
+
sleep(0.1)
|
195
|
+
@expected.should be_true
|
196
|
+
end
|
197
|
+
end
|
198
|
+
end
|
199
|
+
end
|