Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Add current_thread option into #start instead of #run
  • Loading branch information
pocke committed Aug 29, 2016
1 parent 0568714 commit 0742bd2
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 59 deletions.
6 changes: 3 additions & 3 deletions README.md
Expand Up @@ -149,7 +149,7 @@ end

### synchronous execution

Use `Expeditor::Command#run` instead of `#start`, Command executes synchronous on current thread.
Use `current_thread` option of `#start`, command executes synchronous on current thread.

```ruby
command1 = Expeditor::Command.new do
Expand All @@ -160,8 +160,8 @@ command2 = Expeditor::Command.new do
...
end

command1.run # blocking
command2.run # blocking
command1.start(current_thread: true) # blocking
command2.start(current_thread: true) # blocking

command1.get
command2.get
Expand Down
18 changes: 7 additions & 11 deletions lib/expeditor/command.rb
Expand Up @@ -22,18 +22,14 @@ def initialize(opts = {}, &block)
@ivar = Concurrent::IVar.new
end

def start
# @param current_thread [Boolean] Execute the task on current thread(blocking)
def start(current_thread: false)
if not started?
prepare
@normal_future.safe_execute
end
self
end

# run is similar to the `start`, but the method is executed on the current thread.
def run
if not started?
prepare(Concurrent::ImmediateExecutor.new)
if current_thread
prepare(Concurrent::ImmediateExecutor.new)
else
prepare
end
@normal_future.safe_execute
end
self
Expand Down
90 changes: 45 additions & 45 deletions spec/expeditor/command_spec.rb
Expand Up @@ -79,63 +79,63 @@
expect(command.get).to eq(1000)
end
end
end

describe '#run' do
context 'with normal' do
it 'should run on current thread' do
Thread.current.thread_variable_set('foo', 'bar')
command = Expeditor::Command.new do
Thread.current.thread_variable_get('foo')
context 'current thread mode' do
context 'with normal' do
it 'should execute on current thread' do
Thread.current.thread_variable_set('foo', 'bar')
command = Expeditor::Command.new do
Thread.current.thread_variable_get('foo')
end
expect(command.start(current_thread: true).get).to eq('bar')
end
expect(command.run.get).to eq('bar')
end

it 'should return self' do
command = simple_command(42)
expect(command.run).to eq(command)
end
it 'should return self' do
command = simple_command(42)
expect(command.start(current_thread: true)).to eq(command)
end

it 'should ignore from the second time' do
count = 0
command = Expeditor::Command.new do
count += 1
count
it 'should ignore from the second time' do
count = 0
command = Expeditor::Command.new do
count += 1
count
end
command.start(current_thread: true)
command.start(current_thread: true)
command.start(current_thread: true)
expect(command.get).to eq(1)
expect(count).to eq(1)
end
command.run
command.run
command.run
expect(command.get).to eq(1)
expect(count).to eq(1)
end
end

context 'with fallback' do
it 'should work fallback proc' do
command = error_command(error_in_command, nil)
command.set_fallback do
42
context 'with fallback' do
it 'should work fallback proc' do
command = error_command(error_in_command, nil)
command.set_fallback do
42
end

expect(command.start(current_thread: true).get).to eq(42)
end

expect(command.run.get).to eq(42)
end
it 'should work fallback on current thread' do
Thread.current.thread_variable_set("count", 1)
command = Expeditor::Command.new do
count = Thread.current.thread_variable_get("count")
count += 1
Thread.current.thread_variable_set("count", count) # => 2
raise error_in_command
end

it 'should work fallback on current thread' do
Thread.current.thread_variable_set("count", 1)
command = Expeditor::Command.new do
count = Thread.current.thread_variable_get("count")
count += 1
Thread.current.thread_variable_set("count", count) # => 2
raise error_in_command
end
command.set_fallback do
count = Thread.current.thread_variable_get("count")
count += 1
count # => 3
end

command.set_fallback do
count = Thread.current.thread_variable_get("count")
count += 1
count # => 3
expect(command.start(current_thread: true).get).to eq(3)
end

expect(command.run.get).to eq(3)
end
end
end
Expand Down

0 comments on commit 0742bd2

Please sign in to comment.