Kouhei Sutou
null+****@clear*****
Tue Mar 22 18:36:21 JST 2016
Kouhei Sutou 2016-03-22 18:36:21 +0900 (Tue, 22 Mar 2016) New Revision: a243fcbcf01ba571a1304a7d8dc9d09095949962 https://github.com/ranguba/groonga-client-rails/commit/a243fcbcf01ba571a1304a7d8dc9d09095949962 Message: Add test helper Added files: lib/groonga/client/rails/fixture.rb lib/groonga/client/rails/groonga_server_runner.rb lib/groonga/client/rails/test_help.rb Added: lib/groonga/client/rails/fixture.rb (+47 -0) 100644 =================================================================== --- /dev/null +++ lib/groonga/client/rails/fixture.rb 2016-03-22 18:36:21 +0900 (47c53f7) @@ -0,0 +1,47 @@ +# Copyright (C) 2016 Kouhei Sutou <kou �� clear-code.com> +# +# This library is free software; you can redistribute it and/or +# modify it under the terms of the GNU Lesser General Public +# License as published by the Free Software Foundation; either +# version 2.1 of the License, or (at your option) any later version. +# +# This library is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +# Lesser General Public License for more details. +# +# You should have received a copy of the GNU Lesser General Public +# License along with this library; if not, write to the Free Software +# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA + +require "groonga/client/rails/groonga_server_runner" + +module Groonga + class Client + module Rails + module Fixture + extend ActiveSupport::Concern + + included do + setup do + setup_groonga + end + + teardown do + teardown_groonga + end + end + + def setup_groonga + @groonga_server_runner = GroongaServerRunner.new + @groonga_server_runner.run + end + + def teardown_groonga + return if @groonga_server_runner.nil? + @groonga_server_runner.stop + end + end + end + end +end Added: lib/groonga/client/rails/groonga_server_runner.rb (+137 -0) 100644 =================================================================== --- /dev/null +++ lib/groonga/client/rails/groonga_server_runner.rb 2016-03-22 18:36:21 +0900 (2e55acc) @@ -0,0 +1,137 @@ +# Copyright (C) 2016 Kouhei Sutou <kou �� clear-code.com> +# +# This library is free software; you can redistribute it and/or +# modify it under the terms of the GNU Lesser General Public +# License as published by the Free Software Foundation; either +# version 2.1 of the License, or (at your option) any later version. +# +# This library is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +# Lesser General Public License for more details. +# +# You should have received a copy of the GNU Lesser General Public +# License along with this library; if not, write to the Free Software +# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA + +require "rbconfig" +require "fileutils" + +module Groonga + class Client + module Rails + class GroongaServerRunner + def initialize + @pid = nil + @url = build_url + @groonga = find_groonga + @tmp_dir = nil + end + + def run + return if****@groon*****? + @tmp_dir = create_tmp_dir + db_path = @tmp_dir + "db" + @pid = spawn(@groonga, + "--port", @url.port.to_s, + "--log-path", (@tmp_dir + "groonga.log").to_s, + "--query-log-path", (@tmp_dir + "query.log").to_s, + "--protocol", "http", + "-s", + "-n", db_path.to_s) + wait_groonga_ready + end + + def stop + if @pid + Groonga::Client.open do |client| + client.shutdown + end + wait_groonga_shutdown + end + if @tmp_dir + FileUtils.rm_rf(@tmp_dir) + end + end + + private + def build_url + default_options = Groonga::Client.default_options + url = default_options[:url] + if url.nil? + host = default_options[:host] || default_options[:address] + port = default_options[:port] || 10041 + path = default_options[:path] + url = URI("http://#{host}:#{port}#{path}") + end + url + end + + def find_groonga + paths = ENV["PATH"].split(File::PATH_SEPARATOR) + exeext = RbConfig::CONFIG["EXEEXT"] + paths.each do |path| + groonga = File.join(path, "groonga#{exeext}") + return groonga if File.executable?(groonga) + end + nil + end + + def create_tmp_dir + tmpfs_dir = "/dev/shm" + if File.directory?(tmpfs_dir) + base_tmp_dir = Pathname(tmpfs_dir) + else + base_tmp_dir = ::Rails.root + "tmp" + end + tmp_dir = base_tmp_dir + "groonga-client-rails" + FileUtils.rm_rf(tmp_dir) + FileUtils.mkdir_p(tmp_dir) + tmp_dir + end + + def wait_groonga_ready + n_retried = 0 + while n_retried <= 20 + n_retried += 1 + sleep(0.05) + begin + TCPSocket.open(@url.host, @url.port) do + end + rescue SystemCallError + begin + pid = Process.waitpid(@pid, Process::WNOHANG) + rescue SystemCallError + @pid = nil + break + end + else + break + end + end + end + + def wait_groonga_shutdown + # TODO: Remove me when Groonga 6.0.1 has been released. + # Workaround to shutdown as soon as possible. + begin + TCPSocket.open(@url.host, @url.port) do + end + rescue SystemCallError + end + + n_retried = 0 + while n_retried <= 20 + n_retried += 1 + sleep(0.05) + pid = Process.waitpid(@pid, Process::WNOHANG) + return if pid + end + + Process.kill(:KILL, @pid) + Process.waitpid(@pid) + end + end + end + end +end Added: lib/groonga/client/rails/test_help.rb (+17 -0) 100644 =================================================================== --- /dev/null +++ lib/groonga/client/rails/test_help.rb 2016-03-22 18:36:21 +0900 (4195e68) @@ -0,0 +1,17 @@ +# Copyright (C) 2016 Kouhei Sutou <kou �� clear-code.com> +# +# This library is free software; you can redistribute it and/or +# modify it under the terms of the GNU Lesser General Public +# License as published by the Free Software Foundation; either +# version 2.1 of the License, or (at your option) any later version. +# +# This library is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +# Lesser General Public License for more details. +# +# You should have received a copy of the GNU Lesser General Public +# License along with this library; if not, write to the Free Software +# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA + +require "groonga/client/rails/fixture" -------------- next part -------------- HTML����������������������������...下载