Skip to content

Commit 892bf83

Browse files
committed
added: ImmediateExecutor, a simple executor where tasks are executed by thread that posted them
added: ImmediateExecutor specs future_spec, context fulfillment, changed to use ImmediateExecutor, so it can run faster
1 parent 62d06ed commit 892bf83

File tree

4 files changed

+58
-9
lines changed

4 files changed

+58
-9
lines changed

‎lib/concurrent.rb‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121

2222
require 'concurrent/cached_thread_pool'
2323
require 'concurrent/fixed_thread_pool'
24+
require 'concurrent/immediate_executor'
2425

2526
require 'concurrent/event_machine_defer_proxy' if defined?(EventMachine)
2627

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
module Concurrent
2+
class ImmediateExecutor
3+
4+
def post(*args, &block)
5+
block.call(*args)
6+
end
7+
8+
def <<(block)
9+
post(&block)
10+
self
11+
end
12+
13+
end
14+
end

‎spec/concurrent/future_spec.rb‎

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -58,36 +58,37 @@ module Concurrent
5858

5959
context 'fulfillment' do
6060

61+
before(:each) do
62+
Future.thread_pool = ImmediateExecutor.new
63+
end
64+
6165
it 'passes all arguments to handler' do
62-
@a = @b = @c = nil
63-
f = Future.new(1, 2, 3) do |a, b, c|
64-
@a, @b, @c = a, b, c
66+
result = nil
67+
68+
Future.new(1, 2, 3) do |a, b, c|
69+
result = [a, b, c]
6570
end
66-
sleep(0.1)
67-
[@a, @b, @c].should eq [1, 2, 3]
71+
72+
result.should eq [1, 2, 3]
6873
end
6974

7075
it 'sets the value to the result of the handler' do
7176
f = Future.new(10){|a| a * 2 }
72-
sleep(0.1)
7377
f.value.should eq 20
7478
end
7579

7680
it 'sets the state to :fulfilled when the block completes' do
7781
f = Future.new(10){|a| a * 2 }
78-
sleep(0.1)
7982
f.should be_fulfilled
8083
end
8184

8285
it 'sets the value to nil when the handler raises an exception' do
8386
f = Future.new{ raise StandardError }
84-
sleep(0.1)
8587
f.value.should be_nil
8688
end
8789

8890
it 'sets the state to :rejected when the handler raises an exception' do
8991
f = Future.new{ raise StandardError }
90-
sleep(0.1)
9192
f.should be_rejected
9293
end
9394

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
require 'spec_helper'
2+
3+
module Concurrent
4+
5+
describe ImmediateExecutor do
6+
7+
let(:executor) { ImmediateExecutor.new }
8+
9+
context "#post" do
10+
it 'executes the block using the arguments as parameters' do
11+
result = executor.post(1, 2, 3, 4) { |a, b, c, d| [a, b, c, d] }
12+
result.should eq [1, 2, 3, 4]
13+
end
14+
end
15+
16+
context "#<<" do
17+
18+
it "returns true" do
19+
result = executor << proc { false }
20+
result.should be_true
21+
end
22+
23+
it "executes the passed callable" do
24+
x = 0
25+
26+
executor << proc { x = 5 }
27+
28+
x.should eq 5
29+
end
30+
31+
end
32+
end
33+
end

0 commit comments

Comments
 (0)