[Groonga-commit] ranguba/groonga-client-rails at b4d9359 [master] Support using existing Groonga server for test

Back to archive index

Kouhei Sutou null+****@clear*****
Tue Mar 22 19:09:14 JST 2016


Kouhei Sutou	2016-03-22 19:09:14 +0900 (Tue, 22 Mar 2016)

  New Revision: b4d9359daccfafc3f9b921435178dc7e441efd23
  https://github.com/ranguba/groonga-client-rails/commit/b4d9359daccfafc3f9b921435178dc7e441efd23

  Message:
    Support using existing Groonga server for test

  Modified files:
    lib/groonga/client/rails/groonga_server_runner.rb

  Modified: lib/groonga/client/rails/groonga_server_runner.rb (+55 -28)
===================================================================
--- lib/groonga/client/rails/groonga_server_runner.rb    2016-03-22 19:09:01 +0900 (2e55acc)
+++ lib/groonga/client/rails/groonga_server_runner.rb    2016-03-22 19:09:14 +0900 (176c4f4)
@@ -23,34 +23,50 @@ module Groonga
       class GroongaServerRunner
         def initialize
           @pid = nil
+          @using_running_server = false
           @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
+          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 @pid
+          if @using_running_server
             Groonga::Client.open do |client|
-              client.shutdown
+              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
-            wait_groonga_shutdown
-          end
-          if @tmp_dir
-            FileUtils.rm_rf(@tmp_dir)
           end
         end
 
@@ -67,6 +83,24 @@ module Groonga
           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"]
@@ -95,18 +129,15 @@ module Groonga
           while n_retried <= 20
             n_retried += 1
             sleep(0.05)
-            begin
-              TCPSocket.open(@url.host, @url.port) do
-              end
-            rescue SystemCallError
+            if groonga_server_running?
+              break
+            else
               begin
                 pid = Process.waitpid(@pid, Process::WNOHANG)
               rescue SystemCallError
                 @pid = nil
                 break
               end
-            else
-              break
             end
           end
         end
@@ -114,11 +145,7 @@ module Groonga
         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
+          groonga_server_running?
 
           n_retried = 0
           while n_retried <= 20
-------------- next part --------------
HTML����������������������������...
下载 



More information about the Groonga-commit mailing list
Back to archive index