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..250624b --- /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] :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 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