[Groonga-commit] groonga/fluent-plugin-groonga [master] out: add a test

Back to archive index

Kouhei Sutou null+****@clear*****
Fri Nov 9 00:20:10 JST 2012


Kouhei Sutou	2012-11-09 00:20:10 +0900 (Fri, 09 Nov 2012)

  New Revision: 0f38d8f2e5d643abf7f9d2f9ebf5966f8a4ada96
  https://github.com/groonga/fluent-plugin-groonga/commit/0f38d8f2e5d643abf7f9d2f9ebf5966f8a4ada96

  Log:
    out: add a test

  Added files:
    test/test_output.rb
  Modified files:
    lib/fluent/plugin/out_groonga.rb

  Modified: lib/fluent/plugin/out_groonga.rb (+35 -9)
===================================================================
--- lib/fluent/plugin/out_groonga.rb    2012-11-08 22:43:45 +0900 (63d3dfc)
+++ lib/fluent/plugin/out_groonga.rb    2012-11-09 00:20:10 +0900 (8ba8d24)
@@ -19,7 +19,7 @@ require "fileutils"
 require "cgi/util"
 
 module Fluent
-  class GroongaOutput < ObjectBufferedOutput
+  class GroongaOutput < BufferedOutput
     Plugin.register_output("groonga", self)
 
     def initialize
@@ -27,6 +27,7 @@ module Fluent
     end
 
     config_param :protocol, :string, :default => "http"
+    config_param :table, :string, :default => nil
 
     def configure(conf)
       super
@@ -51,11 +52,34 @@ module Fluent
       @client.shutdown
     end
 
-    def write_objects(tag, chunk)
-      command = tag.split(/\./)[2]
+    def format(tag, time, record)
+      [time, record].to_msgpack
+    end
+
+    def write(chunk)
+      if /\Agroonga\.command\./ =~ chunk.key
+        command = $POSTMATCH
+        chunk.msgpack_each do |time, parameters|
+          @client.send(command, parameters)
+        end
+      else
+        store_chunk(chunk)
+      end
+    end
+
+    private
+    def store_chunk(chunk)
+      return if****@table*****?
+
+      data = []
       chunk.each do |time, parameters|
-        @client.send(command, parameters)
+        data << parameters
       end
+      parameters = {
+        "table" => @table,
+        "data" => Yajl::Enocder.encode(data),
+      }
+      @client.send("load", parameters)
     end
 
     class HTTPClient
@@ -76,7 +100,10 @@ module Fluent
       include Configurable
 
       config_param :groonga, :string, :default => "groonga"
-      config_param :database, :string, :default => nil
+      config_param :database, :string
+      config_param :arguments, :default => [] do |value|
+        Shellwords.split(value)
+      end
 
       def initialize
         super
@@ -98,8 +125,8 @@ module Fluent
           output_fd => output_fd,
           :err => @error[1],
         }
-        arguments = [
-          "--log-path", "/tmp/groonga.log",
+        arguments = @arguments
+        arguments += [
           "--input-fd", input_fd.to_s,
           "--output-fd", output_fd.to_s,
         ]
@@ -133,7 +160,6 @@ module Fluent
         unless url_encoded_parameters.empty?
           path << "?#{url_encoded_parameters.join('&')}"
         end
-        p path
         @input[1].write("#{path}\n")
         if body
           body.each_line do |line|
@@ -141,7 +167,7 @@ module Fluent
           end
         end
         @input[1].flush
-        p @output[0].gets
+        # p @output[0].gets
       end
     end
   end

  Added: test/test_output.rb (+132 -0) 100644
===================================================================
--- /dev/null
+++ test/test_output.rb    2012-11-09 00:20:10 +0900 (dede1de)
@@ -0,0 +1,132 @@
+# -*- coding: utf-8 -*-
+#
+# Copyright (C) 2012  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 version 2.1 as published by the Free Software Foundation.
+#
+# 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., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
+
+require "time"
+require "cgi/util"
+require "net/http"
+require "webrick/config"
+require "webrick/httpresponse"
+
+require "fluent/test"
+require "fluent/plugin/out_groonga"
+
+require "http_parser"
+
+class GroongaOutputTest < Test::Unit::TestCase
+  setup
+  def setup_fluent
+    Fluent::Test.setup
+  end
+
+  private
+  def create_driver(tag)
+    driver = Fluent::Test::BufferedOutputTestDriver.new(Fluent::GroongaOutput,
+                                                        tag)
+    driver.configure(configuration)
+    driver
+  end
+
+  def configuration
+    <<-EOC
+EOC
+  end
+
+  class CommandLineTest < self
+    setup :before => :append
+    def setup_command
+      @temporary_directory = File.expand_path("tmp", File.dirname(__FILE__))
+      FileUtils.rm_rf(@temporary_directory)
+      FileUtils.mkdir_p(@temporary_directory)
+
+      @groonga_stub_path = File.join(@temporary_directory, "groonga")
+      @command_line_path = File.join(@temporary_directory, "command-line")
+      @input_path = File.join(@temporary_directory, "input")
+      @database_path = File.join(@temporary_directory, "database")
+
+      File.open(@groonga_stub_path, "w") do |groonga_stub|
+        groonga_stub.puts(<<-EOR)
+#!#{Gem.ruby}
+
+File.open(#{@command_line_path.inspect}, "a") do |file|
+  file.puts(ARGV)
+end
+
+input_fd = ARGV[ARGV.index("--input-fd") + 1]
+input = IO.new(input_fd.to_i)
+
+File.open(#{@input_path.inspect}, "a") do |file|
+  input.each_line do |line|
+    file.print(line)
+  end
+end
+
+output_fd = ARGV[ARGV.index("--output-fd") + 1]
+output = IO.new(output_fd.to_i)
+output.puts("done")
+output.flush
+EOR
+      end
+      FileUtils.chmod(0755, @groonga_stub_path)
+
+      FileUtils.touch(@command_line_path)
+      FileUtils.touch(@input_path)
+    end
+
+    teardown
+    def teardown_command
+      FileUtils.rm_rf(@temporary_directory)
+    end
+
+    def configuration
+      <<-EOC
+      protocol command
+      groonga #{@groonga_stub_path}
+      database #{@database_path}
+EOC
+    end
+
+    private
+    def actual_command_line
+      File.read(@command_line_path).split(/\n/)
+    end
+
+    def actual_input
+      File.read(@input_path)
+    end
+
+    class CommandTest < self
+      def test_basic_command
+        driver = create_driver("groonga.command.table_create")
+        time = Time.parse("2012-10-26T08:45:42Z")
+        driver.emit({"name" => "Users"}, time)
+        driver.run
+        assert_equal([
+                       [
+                         "--input-fd", "5",
+                         "--output-fd", "8",
+                         "-n", @database_path,
+                       ],
+                       "/d/table_create?name=Users\n",
+                     ],
+                     [
+                       actual_command_line,
+                       actual_input,
+                     ])
+      end
+    end
+  end
+end
-------------- next part --------------
HTML����������������������������...
Download 



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