From 964aa188b007fa9b22b7f55f0fe7d38ac6a25804 Mon Sep 17 00:00:00 2001 From: Kazuaki MATSUO Date: Sat, 3 Jun 2017 09:07:17 +0900 Subject: [PATCH 1/2] add executing by thread --- lib/droid/monitor.rb | 8 ++-- lib/droid/monitor/common/timer.rb | 52 ++++++++++++++++++++++++++ lib/droid/monitor/executor/executor.rb | 19 ++++++++++ test/monitor/timer_test.rb | 24 ++++++++++++ 4 files changed, 100 insertions(+), 3 deletions(-) create mode 100644 lib/droid/monitor/common/timer.rb create mode 100644 lib/droid/monitor/executor/executor.rb create mode 100644 test/monitor/timer_test.rb diff --git a/lib/droid/monitor.rb b/lib/droid/monitor.rb index 641f471..4e02957 100644 --- a/lib/droid/monitor.rb +++ b/lib/droid/monitor.rb @@ -1,4 +1,6 @@ require_relative "monitor/version" +require_relative "monitor/common/timer" +require_relative "monitor/executor/executor" require "Open3" @@ -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 @@ -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 diff --git a/lib/droid/monitor/common/timer.rb b/lib/droid/monitor/common/timer.rb new file mode 100644 index 0000000..4fa69e5 --- /dev/null +++ b/lib/droid/monitor/common/timer.rb @@ -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] :finish + def execute + sleep interval + yield + + :finish + end + + # Loop `execute` until stopping its process + # @param [Block] &block Yield the block after sleep @interval + # @return [Symbol] :finish + 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] :finish + 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 diff --git a/lib/droid/monitor/executor/executor.rb b/lib/droid/monitor/executor/executor.rb new file mode 100644 index 0000000..3ddb332 --- /dev/null +++ b/lib/droid/monitor/executor/executor.rb @@ -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 diff --git a/test/monitor/timer_test.rb b/test/monitor/timer_test.rb new file mode 100644 index 0000000..d8bb3de --- /dev/null +++ b/test/monitor/timer_test.rb @@ -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 From 2f445a12940dbd3b79e02d9cf4c41f12c61efb7d Mon Sep 17 00:00:00 2001 From: Kazuaki MATSUO Date: Sat, 3 Jun 2017 09:09:20 +0900 Subject: [PATCH 2/2] rename :finish symbol --- lib/droid/monitor/common/timer.rb | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/lib/droid/monitor/common/timer.rb b/lib/droid/monitor/common/timer.rb index 4fa69e5..250624b 100644 --- a/lib/droid/monitor/common/timer.rb +++ b/lib/droid/monitor/common/timer.rb @@ -10,17 +10,17 @@ def initialize(interval) end # @param [Block] &block Yield the block after sleep @interval - # @return [Symbol] :finish + # @return [Symbol] :finished def execute sleep interval yield - :finish + :finished end # Loop `execute` until stopping its process # @param [Block] &block Yield the block after sleep @interval - # @return [Symbol] :finish + # @return [Symbol] :finished def execute_loop(&block) loop { execute(&block) } end @@ -32,7 +32,7 @@ def execute_loop(&block) # # Loop `execute` until stopping its process on the other thread # @param [Block] &block Yield the block after sleep @interval - # @return [Symbol] :finish + # @return [Symbol] :finished def execute_loop_thread(&block) Thread.new { loop { execute(&block) } } end