Kouhei Sutou
null+****@clear*****
Mon Dec 12 11:54:52 JST 2016
Kouhei Sutou 2016-12-12 11:54:52 +0900 (Mon, 12 Dec 2016) New Revision: ad83de3b1911131fd15811fb5d1013d939db6403 https://github.com/ranguba/groonga-client-rails/commit/ad83de3b1911131fd15811fb5d1013d939db6403 Message: Use test helper in groonga-client Added files: lib/groonga/client/rails/test_synchronizer.rb Removed files: lib/groonga/client/rails/fixture.rb lib/groonga/client/rails/groonga_server_runner.rb Modified files: lib/groonga/client/rails/spec_helper.rb lib/groonga/client/rails/test_helper.rb Deleted: lib/groonga/client/rails/fixture.rb (+0 -35) 100644 =================================================================== --- lib/groonga/client/rails/fixture.rb 2016-12-12 11:17:58 +0900 (9c48d85) +++ /dev/null @@ -1,35 +0,0 @@ -# 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 - 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 Deleted: lib/groonga/client/rails/groonga_server_runner.rb (+0 -164) 100644 =================================================================== --- lib/groonga/client/rails/groonga_server_runner.rb 2016-12-12 11:17:58 +0900 (fefb114) +++ /dev/null @@ -1,164 +0,0 @@ -# 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 - @using_running_server = false - @url = build_url - @groonga = find_groonga - @tmp_dir = nil - end - - def run - if groonga_server_running? - @using_running_server = true - else - 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 - sync_schema - end - - def stop - if @using_running_server - Groonga::Client.open do |client| - schema = client.schema - schema.tables.each do |name, _| - client.delete(table: name, - filter: "true") - end - end - else - if @pid - Groonga::Client.open do |client| - client.shutdown - end - wait_groonga_shutdown - end - if @tmp_dir - FileUtils.rm_rf(@tmp_dir) - end - 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 groonga_server_running? - begin - TCPSocket.open(@url.host, @url.port) do - end - rescue SystemCallError - false - else - true - end - end - - def sync_schema - ::Rails.application.eager_load! - ObjectSpace.each_object(Class) do |klass| - klass.sync_schema if klass < Searcher - end - 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.#{Process.pid}" - 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) - if groonga_server_running? - break - else - begin - pid = Process.waitpid(@pid, Process::WNOHANG) - rescue SystemCallError - @pid = nil - break - end - 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. - groonga_server_running? - - 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 Modified: lib/groonga/client/rails/spec_helper.rb (+6 -8) =================================================================== --- lib/groonga/client/rails/spec_helper.rb 2016-12-12 11:17:58 +0900 (0eaaec4) +++ lib/groonga/client/rails/spec_helper.rb 2016-12-12 11:54:52 +0900 (e3c1821) @@ -14,23 +14,21 @@ # 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" +require "groonga/client/spec-helper" +require "groonga/client/rails/test_synchronizer" module Groonga class Client module Rails module SpecHelper - include Fixture - extend ActiveSupport::Concern included do - before(:each) do - setup_groonga - end + inlcude Groonga::Client::SpecHelper - after(:each) do - teardown_groonga + before(:each) do + syncher = TestSynchronizer.new + syncher.sync end end end Modified: lib/groonga/client/rails/test_helper.rb (+11 -8) =================================================================== --- lib/groonga/client/rails/test_helper.rb 2016-12-12 11:17:58 +0900 (1cb843e) +++ lib/groonga/client/rails/test_helper.rb 2016-12-12 11:54:52 +0900 (f35601f) @@ -14,23 +14,26 @@ # 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" +require "groonga/client/test-helper" +require "groonga/client/rails/test_synchronizer" module Groonga class Client module Rails module TestHelper - include Fixture - extend ActiveSupport::Concern included do - setup do - setup_groonga - end + include Groonga::Client::TestHelper - teardown do - teardown_groonga + setup do + syncher = TestSynchronizer.new + options = {} + if self.class.respond_to?(:fixture_table_names) + fixture_table_names = self.fixture_table_names + options[:sync_records] = true unless fixture_table_names.empty? + end + syncher.sync(options) end end end Added: lib/groonga/client/rails/test_synchronizer.rb (+17 -0) 100644 =================================================================== --- /dev/null +++ lib/groonga/client/rails/test_synchronizer.rb 2016-12-12 11:54:52 +0900 (ce33cc0) @@ -0,0 +1,17 @@ +module Groonga + class Client + module Rails + class TestSynchronizer + def sync(options={}) + ::Rails.application.eager_load! + ObjectSpace.each_object(Class) do |klass| + if klass < Searcher + klass.sync_schema + klass.sync_records if options[:sync_records] + end + end + end + end + end + end +end -------------- next part -------------- HTML����������������������������...다운로드