-
-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #21 from KazuCocoa/add_thread_utils
add executing by thread
- Loading branch information
Showing
4 changed files
with
100 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
module Droid | ||
module Monitor | ||
module Timer | ||
class Executor | ||
attr_reader :interval | ||
|
||
# @param [int] interval Interval to wait | ||
def initialize(interval) | ||
@interval = interval | ||
end | ||
|
||
# @param [Block] &block Yield the block after sleep @interval | ||
# @return [Symbol] :finished | ||
def execute | ||
sleep interval | ||
yield | ||
|
||
:finished | ||
end | ||
|
||
# Loop `execute` until stopping its process | ||
# @param [Block] &block Yield the block after sleep @interval | ||
# @return [Symbol] :finished | ||
def execute_loop(&block) | ||
loop { execute(&block) } | ||
end | ||
|
||
# Start | ||
# t = timer.execute_loop_thread { puts "#{Time.now}" } | ||
# Stop | ||
# timer.kill_thread t | ||
# | ||
# Loop `execute` until stopping its process on the other thread | ||
# @param [Block] &block Yield the block after sleep @interval | ||
# @return [Symbol] :finished | ||
def execute_loop_thread(&block) | ||
Thread.new { loop { execute(&block) } } | ||
end | ||
|
||
# @param [Thread] thread Kill the thread | ||
def kill_thread(thread) | ||
Thread.kill thread | ||
end | ||
|
||
# @return [Array[Thread]] Return a list running thread on the process | ||
def thread_list | ||
Thread.list | ||
end | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
module Droid | ||
module Monitor | ||
class Executor | ||
class Base | ||
def execute(_timer) | ||
raise NotImplementedError | ||
end | ||
|
||
def save | ||
raise NotImplementedError | ||
end | ||
|
||
def kill | ||
raise NotImplementedError | ||
end | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
require 'test/unit' | ||
|
||
require './lib/droid/monitor/common/timer' | ||
|
||
module Droid | ||
module Monitor | ||
class TimerTest < Test::Unit::TestCase | ||
def test_timer | ||
timer = Timer::Executor.new(0.1) | ||
assert_equal(0.1, timer.interval) | ||
|
||
thread_size = Thread.list.size | ||
thread = timer.execute_loop_thread { 1 } | ||
assert_equal(thread_size + 1, Thread.list.size) | ||
assert_equal('run', thread.status) | ||
|
||
timer.kill_thread thread | ||
sleep 0.1 | ||
assert_equal(false, thread.status) | ||
assert_equal(thread_size, Thread.list.size) | ||
end | ||
end | ||
end | ||
end |