Skip to content

Commit

Permalink
Merge pull request #21 from KazuCocoa/add_thread_utils
Browse files Browse the repository at this point in the history
add executing by thread
  • Loading branch information
KazuCocoa authored Jun 3, 2017
2 parents 4ca1caa + 2f445a1 commit 6729367
Show file tree
Hide file tree
Showing 4 changed files with 100 additions and 3 deletions.
8 changes: 5 additions & 3 deletions lib/droid/monitor.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
require_relative "monitor/version"
require_relative "monitor/common/timer"
require_relative "monitor/executor/executor"

require "Open3"

Expand All @@ -8,8 +10,8 @@ class Adb
attr_accessor :package, :device_serial, :api_level

def initialize(opts = {})
fail "opts must be a hash" unless opts.is_a? Hash
fail "Package name is required." unless opts[:package]
raise "opts must be a hash" unless opts.is_a? Hash
raise "Package name is required." unless opts[:package]
@package = opts[:package]
@device_serial = opts[:device_serial] || ""
@api_level = device_sdk_version
Expand Down Expand Up @@ -64,7 +66,7 @@ def dump_gfxinfo
private

def adb
fail "ANDROID_HOME is not set" unless ENV["ANDROID_HOME"]
raise "ANDROID_HOME is not set" unless ENV["ANDROID_HOME"]
"#{ENV["ANDROID_HOME"]}/platform-tools/adb"
end

Expand Down
52 changes: 52 additions & 0 deletions lib/droid/monitor/common/timer.rb
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
19 changes: 19 additions & 0 deletions lib/droid/monitor/executor/executor.rb
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
24 changes: 24 additions & 0 deletions test/monitor/timer_test.rb
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

0 comments on commit 6729367

Please sign in to comment.