[Groonga-commit] droonga/fluent-plugin-droonga at 1dc22ef [master] Add HandlerMessage and HandlerMessenger

Back to archive index

Kouhei Sutou null+****@clear*****
Tue Dec 24 19:08:19 JST 2013


Kouhei Sutou	2013-12-24 19:08:19 +0900 (Tue, 24 Dec 2013)

  New Revision: 1dc22effe1bd900862b1503668ec5dca2b0ae3c6
  https://github.com/droonga/fluent-plugin-droonga/commit/1dc22effe1bd900862b1503668ec5dca2b0ae3c6

  Message:
    Add HandlerMessage and HandlerMessenger
    
    Handler plugin API is changed. Now, handler plugin receives whole
    message (instead of body only) and messenger. Handler plugin uses
    messenger to emit response instead of plugin's method.

  Added files:
    lib/droonga/handler_messenger.rb
  Copied files:
    lib/droonga/handler_message.rb
      (from lib/droonga/farm.rb)
    lib/droonga/test/stub_handler_message.rb
      (from lib/droonga/plugin/handler/search.rb)
    lib/droonga/test/stub_handler_messenger.rb
      (from lib/droonga/test.rb)
  Modified files:
    lib/droonga/dispatcher.rb
    lib/droonga/farm.rb
    lib/droonga/handler.rb
    lib/droonga/handler_plugin.rb
    lib/droonga/plugin/handler/add.rb
    lib/droonga/plugin/handler/groonga.rb
    lib/droonga/plugin/handler/search.rb
    lib/droonga/plugin/handler/watch.rb
    lib/droonga/test.rb
    lib/droonga/worker.rb
    test/unit/plugin/handler/groonga/test_column_create.rb
    test/unit/plugin/handler/groonga/test_table_create.rb
    test/unit/plugin/handler/test_add.rb
    test/unit/plugin/handler/test_groonga.rb
    test/unit/plugin/handler/test_search.rb
    test/unit/plugin/handler/test_watch.rb

  Modified: lib/droonga/dispatcher.rb (+1 -1)
===================================================================
--- lib/droonga/dispatcher.rb    2013-12-24 18:12:17 +0900 (8e753e6)
+++ lib/droonga/dispatcher.rb    2013-12-24 19:08:19 +0900 (1b8eb80)
@@ -42,7 +42,7 @@ module Droonga
       @output_adapter =
         OutputAdapter.new(self, :plugins => Droonga.catalog.option("plugins"))
       @loop = EventLoop.new
-      @farm = Farm.new(name, @loop)
+      @farm = Farm.new(name, @loop, :dispatcher => self)
       @forwarder = Forwarder.new(@loop)
       @replier = Replier.new(@forwarder)
       @distributor = Distributor.new(self, @options)

  Modified: lib/droonga/farm.rb (+6 -3)
===================================================================
--- lib/droonga/farm.rb    2013-12-24 18:12:17 +0900 (9593b57)
+++ lib/droonga/farm.rb    2013-12-24 19:08:19 +0900 (21b4787)
@@ -19,12 +19,15 @@ require "droonga/partition"
 
 module Droonga
   class Farm
-    def initialize(name, loop)
+    def initialize(name, loop, options={})
       @name = name
       @loop = loop
+      @options = options
       @partitions = {}
-      Droonga.catalog.get_partitions(name).each do |partition_name, options|
-        partition = Droonga::Partition.new(@loop, options)
+      partitions = Droonga.catalog.get_partitions(name)
+      partitions.each do |partition_name, partition_options|
+        partition = Droonga::Partition.new(@loop,
+                                           @options.merge(partition_options))
         @partitions[partition_name] = partition
       end
     end

  Modified: lib/droonga/handler.rb (+7 -37)
===================================================================
--- lib/droonga/handler.rb    2013-12-24 18:12:17 +0900 (d9ff776)
+++ lib/droonga/handler.rb    2013-12-24 19:08:19 +0900 (d6f072f)
@@ -18,7 +18,8 @@
 require "groonga"
 
 require "droonga/forwarder"
-require "droonga/replier"
+require "droonga/handler_message"
+require "droonga/handler_messenger"
 require "droonga/pluggable"
 require "droonga/handler_plugin"
 
@@ -66,32 +67,11 @@ module Droonga
         $log.trace("#{log_tag}: process: done: no plugin: <#{command}>")
         return
       end
-      process_command(plugin, command, body, arguments)
+      process_command(plugin, command, envelope, arguments)
       $log.trace("#{log_tag}: process: done: <#{command}>",
                  :plugin => plugin.class)
     end
 
-    def emit(value)
-      if****@desce*****?
-        @replier.reply(envelope.merge("body" => value))
-      else
-        @descendants.each do |name, dests|
-          message = {
-            "id" => @id,
-            "input" => name,
-            "value" => value[name],
-          }
-          dests.each do |dest|
-            forward(message, "to" => dest, "type" => "dispatcher")
-          end
-        end
-      end
-    end
-
-    def forward(message, destination)
-      @forwarder.forward(envelope.merge("body" => message), destination)
-    end
-
     private
     def parse_envelope(envelope)
       @envelope = envelope
@@ -105,7 +85,6 @@ module Droonga
       end
       load_plugins(@options[:handlers] || [])
       @forwarder = Forwarder.new(@loop)
-      @replier = Replier.new(@forwarder)
     end
 
     def instantiate_plugin(name)
@@ -113,20 +92,11 @@ module Droonga
     end
 
     def process_command(plugin, command, request, arguments)
-      return false unless request.is_a? Hash
-
-      @task = request["task"]
-      return false unles****@task*****_a? Hash
-
-      @component = @task["component"]
-      return false unles****@compo*****_a? Hash
-
-      @output_values = @task["values"]
-      @body = @component["body"]
-      @id = request["id"]
-      @descendants = request["descendants"]
+      handler_message = HandlerMessage.new(request)
+      handler_message.validate
 
-      plugin.process(command, @body, *arguments)
+      messenger = HandlerMessenger.new(@forwarder, handler_message, @options)
+      plugin.process(command, handler_message, messenger, *arguments)
     end
 
     def log_tag

  Copied: lib/droonga/handler_message.rb (+35 -22) 53%
===================================================================
--- lib/droonga/farm.rb    2013-12-24 18:12:17 +0900 (9593b57)
+++ lib/droonga/handler_message.rb    2013-12-24 19:08:19 +0900 (005bf6d)
@@ -1,5 +1,3 @@
-# -*- coding: utf-8 -*-
-#
 # Copyright (C) 2013 Droonga Project
 #
 # This library is free software; you can redistribute it and/or
@@ -15,34 +13,49 @@
 # 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 "droonga/partition"
-
 module Droonga
-  class Farm
-    def initialize(name, loop)
-      @name = name
-      @loop = loop
-      @partitions = {}
-      Droonga.catalog.get_partitions(name).each do |partition_name, options|
-        partition = Droonga::Partition.new(@loop, options)
-        @partitions[partition_name] = partition
-      end
+  class HandlerMessage
+    attr_reader :raw
+    def initialize(raw)
+      @raw = raw
     end
 
-    def start
-      @partitions.each_value do |partition|
-        partition.start
+    def validate
+      unless task.is_a?(Hash)
+        raise "<task> value isn't object: <#{@raw.inspect}>"
       end
-    end
 
-    def shutdown
-      @partitions.each_value do |partition|
-        partition.shutdown
+      unless component.is_a?(Hash)
+        raise "<task/component> value isn't object: <#{@raw.inspect}>"
       end
     end
 
-    def process(partition_name, message)
-      @partitions[partition_name].process(message)
+    def [](name)
+      @raw[name]
+    end
+
+    def body
+      @body ||= self["body"]
+    end
+
+    def task
+      @task ||= body["task"]
+    end
+
+    def component
+      @component ||= task["component"]
+    end
+
+    def request
+      @request ||= component["body"]
+    end
+
+    def id
+      @id ||= body["id"]
+    end
+
+    def descendants
+      @descendants ||= body["descendants"]
     end
   end
 end

  Added: lib/droonga/handler_messenger.rb (+66 -0) 100644
===================================================================
--- /dev/null
+++ lib/droonga/handler_messenger.rb    2013-12-24 19:08:19 +0900 (55f4eaf)
@@ -0,0 +1,66 @@
+# Copyright (C) 2013 Droonga Project
+#
+# 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 "droonga/replier"
+require "droonga/forwarder"
+
+module Droonga
+  class HandlerMessenger
+    def initialize(forwarder, message, options={})
+      @forwarder = forwarder
+      @message = message
+      @options = options
+      @replier = Replier.new(@forwarder)
+      @dispatcher = @options[:dispatcher]
+    end
+
+    def emit(value)
+      descendants =****@messa*****
+      raw_message =****@messa*****
+      if descendants.empty?
+        @replier.reply(raw_message.merge("body" => value))
+      else
+        descendants.each do |name, dests|
+          body = {
+            "id"    => @message.id,
+            "input" => name,
+            "value" => value[name],
+          }
+          dests.each do |dest|
+            if @dispatcher
+              @dispatcher.dispatch(body, dest)
+            else
+              message = raw_message.merge("body" => body)
+              forward(message, "to" => dest, "type" => "dispatcher")
+            end
+          end
+        end
+      end
+    end
+
+    def forward(message, destination)
+      @forwarder.forward(message, destination)
+    end
+
+    def inspect
+      "\#<#{self.class} id=#{object_id}>"
+    end
+
+    private
+    def log_tag
+      "[#{Process.ppid}][#{Process.pid}] handler_messenger"
+    end
+  end
+end

  Modified: lib/droonga/handler_plugin.rb (+0 -12)
===================================================================
--- lib/droonga/handler_plugin.rb    2013-12-24 18:12:17 +0900 (7534a81)
+++ lib/droonga/handler_plugin.rb    2013-12-24 19:08:19 +0900 (f621cd2)
@@ -27,18 +27,6 @@ module Droonga
       @context =****@handl*****
     end
 
-    def envelope
-      @handler.envelope
-    end
-
-    def emit(value)
-      @handler.emit(value)
-    end
-
-    def forward(body, destination)
-      @handler.forward(body, destination)
-    end
-
     def prefer_synchronous?(command)
       false
     end

  Modified: lib/droonga/plugin/handler/add.rb (+3 -3)
===================================================================
--- lib/droonga/plugin/handler/add.rb    2013-12-24 18:12:17 +0900 (38afeb5)
+++ lib/droonga/plugin/handler/add.rb    2013-12-24 19:08:19 +0900 (6890aa9)
@@ -24,9 +24,9 @@ module Droonga
     repository.register("add", self)
 
     command :add
-    def add(request)
-      outputs = process_add(request)
-      emit(outputs)
+    def add(message, messenger)
+      outputs = process_add(message.request)
+      messenger.emit(outputs)
     end
 
     private

  Modified: lib/droonga/plugin/handler/groonga.rb (+6 -6)
===================================================================
--- lib/droonga/plugin/handler/groonga.rb    2013-12-24 18:12:17 +0900 (5db4ede)
+++ lib/droonga/plugin/handler/groonga.rb    2013-12-24 19:08:19 +0900 (73a5927)
@@ -24,17 +24,17 @@ module Droonga
     repository.register("groonga", self)
 
     command :table_create
-    def table_create(request)
+    def table_create(message, messenger)
       command = TableCreate.new(@context)
-      outputs = command.execute(request)
-      emit(outputs)
+      outputs = command.execute(message.request)
+      messenger.emit(outputs)
     end
 
     command :column_create
-    def column_create(request)
+    def column_create(message, messenger)
       command = ColumnCreate.new(@context)
-      outputs = command.execute(request)
-      emit(outputs)
+      outputs = command.execute(message.request)
+      messenger.emit(outputs)
     end
 
     def prefer_synchronous?(command)

  Modified: lib/droonga/plugin/handler/search.rb (+3 -3)
===================================================================
--- lib/droonga/plugin/handler/search.rb    2013-12-24 18:12:17 +0900 (82b5432)
+++ lib/droonga/plugin/handler/search.rb    2013-12-24 19:08:19 +0900 (0e33cc5)
@@ -23,13 +23,13 @@ module Droonga
     repository.register("search", self)
 
     command :search
-    def search(request)
+    def search(message, messenger)
       searcher = Droonga::Searcher.new(@context)
       values = {}
-      searcher.search(request["queries"]).each do |output, value|
+      searcher.search(message.request["queries"]).each do |output, value|
         values[output] = value
       end
-      emit(values)
+      messenger.emit(values)
     end
   end
 end

  Modified: lib/droonga/plugin/handler/watch.rb (+14 -12)
===================================================================
--- lib/droonga/plugin/handler/watch.rb    2013-12-24 18:12:17 +0900 (d5e8c8d)
+++ lib/droonga/plugin/handler/watch.rb    2013-12-24 19:08:19 +0900 (c31adc8)
@@ -47,8 +47,8 @@ module Droonga
     end
 
     command "watch.subscribe" => :subscribe
-    def subscribe(request)
-      subscriber, condition, query, route = parse_request(request)
+    def subscribe(message, messenger)
+      subscriber, condition, query, route = parse_message(message)
       normalized_request = {
         :subscriber => subscriber,
         :condition  => condition,
@@ -56,39 +56,41 @@ module Droonga
         :route      => route,
       }
       @watcher.subscribe(normalized_request)
-      emit([true])
+      messenger.emit([true])
     end
 
     command "watch.unsubscribe" => :unsubscribe
-    def unsubscribe(request)
-      subscriber, condition, query, route = parse_request(request)
+    def unsubscribe(message, messenger)
+      subscriber, condition, query, route = parse_message(message)
       normalized_request = {
         :subscriber => subscriber,
         :condition  => condition,
         :query      => query,
       }
       @watcher.unsubscribe(normalized_request)
-      emit([true])
+      messenger.emit([true])
     end
 
     command "watch.feed" => :feed
-    def feed(request)
+    def feed(message, messenger)
+      request = message.request
       @watcher.feed(:targets => request["targets"]) do |route, subscribers|
-        message = request # return request itself
-        forward(message, "to" => route, "type" => "watch.notification")
+        messenger.forward(message.raw, # return request itself
+                          "to" => route, "type" => "watch.notification")
       end
     end
 
     command "watch.sweep" => :sweep
-    def sweep(request)
+    def sweep(message, messenger)
       @sweeper.sweep_expired_subscribers
     end
 
     private
-    def parse_request(request)
+    def parse_message(message)
+      request = message.request
       subscriber = request["subscriber"]
       condition = request["condition"]
-      route = request["route"] || envelope["from"]
+      route = request["route"] || message["from"]
       query = condition && condition.to_json
       [subscriber, condition, query, route]
     end

  Modified: lib/droonga/test.rb (+2 -0)
===================================================================
--- lib/droonga/test.rb    2013-12-24 18:12:17 +0900 (9fc4ac7)
+++ lib/droonga/test.rb    2013-12-24 19:08:19 +0900 (3b579b7)
@@ -17,3 +17,5 @@
 
 require "droonga/test/stub_distributor"
 require "droonga/test/stub_handler"
+require "droonga/test/stub_handler_message"
+require "droonga/test/stub_handler_messenger"

  Copied: lib/droonga/test/stub_handler_message.rb (+14 -14) 64%
===================================================================
--- lib/droonga/plugin/handler/search.rb    2013-12-24 18:12:17 +0900 (82b5432)
+++ lib/droonga/test/stub_handler_message.rb    2013-12-24 19:08:19 +0900 (57f1887)
@@ -1,5 +1,3 @@
-# -*- coding: utf-8 -*-
-#
 # Copyright (C) 2013 Droonga Project
 #
 # This library is free software; you can redistribute it and/or
@@ -15,21 +13,23 @@
 # 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 "droonga/handler_plugin"
-require "droonga/searcher"
+require "droonga/handler_message"
 
 module Droonga
-  class SearchHandler < Droonga::HandlerPlugin
-    repository.register("search", self)
-
-    command :search
-    def search(request)
-      searcher = Droonga::Searcher.new(@context)
-      values = {}
-      searcher.search(request["queries"]).each do |output, value|
-        values[output] = value
+  module Test
+    class StubHandlerMessage < HandlerMessage
+      def initialize(request, headers={})
+        raw = {
+          "body" => {
+            "task" => {
+              "component" => {
+                "body" => request,
+              },
+            },
+          },
+        }
+        super(headers.merge(raw))
       end
-      emit(values)
     end
   end
 end

  Copied: lib/droonga/test/stub_handler_messenger.rb (+19 -4) 67%
===================================================================
--- lib/droonga/test.rb    2013-12-24 18:12:17 +0900 (9fc4ac7)
+++ lib/droonga/test/stub_handler_messenger.rb    2013-12-24 19:08:19 +0900 (e8fdeba)
@@ -1,5 +1,3 @@
-# -*- coding: utf-8 -*-
-#
 # Copyright (C) 2013 Droonga Project
 #
 # This library is free software; you can redistribute it and/or
@@ -15,5 +13,22 @@
 # 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 "droonga/test/stub_distributor"
-require "droonga/test/stub_handler"
+module Droonga
+  module Test
+    class StubHandlerMessenger
+      attr_reader :values, :messages
+      def initialize
+        @values = []
+        @messages = []
+      end
+
+      def emit(value)
+        @values << value
+      end
+
+      def forward(message, destination)
+        @messages << [message, destination]
+      end
+    end
+  end
+end

  Modified: lib/droonga/worker.rb (+1 -1)
===================================================================
--- lib/droonga/worker.rb    2013-12-24 18:12:17 +0900 (e4583d8)
+++ lib/droonga/worker.rb    2013-12-24 19:08:19 +0900 (5087455)
@@ -23,7 +23,7 @@ module Droonga
   module Worker
     def initialize
       @loop = EventLoop.new
-      @handler = Handler.new(@loop, config)
+      @handler = Handler.new(@loop, config.merge(:dispatcher => nil))
       receiver_socket = config[:message_receiver]
       @message_receiver = MessageReceiver.new(@loop, receiver_socket) do |message|
         process(message)

  Modified: test/unit/plugin/handler/groonga/test_column_create.rb (+17 -13)
===================================================================
--- test/unit/plugin/handler/groonga/test_column_create.rb    2013-12-24 18:12:17 +0900 (0068663)
+++ test/unit/plugin/handler/groonga/test_column_create.rb    2013-12-24 19:08:19 +0900 (78b22ca)
@@ -15,9 +15,10 @@
 
 class ColumnCreateTest < GroongaHandlerTest
   def test_success
-    @plugin.table_create({"name" => "Books"})
-    @plugin.column_create({"table" => "Books", "name" => "title", "type" => "ShortText"})
-    response =****@messa*****
+    process(:table_create, {"name" => "Books"})
+    process(:column_create,
+            {"table" => "Books", "name" => "title", "type" => "ShortText"})
+    response =****@messe*****
     assert_equal(
       [[Droonga::GroongaHandler::Status::SUCCESS, NORMALIZED_START_TIME, NORMALIZED_ELAPSED_TIME], true],
       [normalize_header(response.first), response.last]
@@ -25,8 +26,9 @@ class ColumnCreateTest < GroongaHandlerTest
   end
 
   def test_name
-    @plugin.table_create({"name" => "Books"})
-    @plugin.column_create({"table" => "Books", "name" => "title", "type" => "ShortText"})
+    process(:table_create, {"name" => "Books"})
+    process(:column_create,
+            {"table" => "Books", "name" => "title", "type" => "ShortText"})
     assert_equal(<<-SCHEMA, dump)
 table_create Books TABLE_HASH_KEY --key_type ShortText
 column_create Books title COLUMN_SCALAR ShortText
@@ -34,8 +36,9 @@ column_create Books title COLUMN_SCALAR ShortText
   end
 
   def test_type
-    @plugin.table_create({"name" => "Books"})
-    @plugin.column_create({"table" => "Books", "name" => "main_text", "type" => "LongText"})
+    process(:table_create, {"name" => "Books"})
+    process(:column_create,
+            {"table" => "Books", "name" => "main_text", "type" => "LongText"})
     assert_equal(<<-SCHEMA, dump)
 table_create Books TABLE_HASH_KEY --key_type ShortText
 column_create Books main_text COLUMN_SCALAR LongText
@@ -59,8 +62,8 @@ column_create Books main_text COLUMN_SCALAR LongText
           "type"  => "ShortText",
           "flags" => data[:flags],
         }
-        @plugin.table_create({"name" => "Books"})
-        @plugin.column_create(request)
+        process(:table_create, {"name" => "Books"})
+        process(:column_create, request)
         assert_equal(<<-EXPECTED, dump)
 table_create Books TABLE_HASH_KEY --key_type ShortText
 column_create Books title #{data[:flags]} ShortText
@@ -71,8 +74,9 @@ column_create Books title #{data[:flags]} ShortText
     class IndexTest < self
       def setup
         super
-        @plugin.table_create({"name" => "Books"})
-        @plugin.column_create({"table" => "Books", "name" => "title", "type" => "ShortText"})
+        process(:table_create, {"name" => "Books"})
+        process(:column_create,
+                {"table" => "Books", "name" => "title", "type" => "ShortText"})
       end
 
       def test_index_column_type
@@ -86,7 +90,7 @@ column_create Books title #{data[:flags]} ShortText
           "source" => "title",
           "flags"  => data[:flags],
         }
-        @plugin.column_create(request)
+        process(:column_create, request)
         assert_equal(<<-EXPECTED, dump)
 table_create Books TABLE_HASH_KEY --key_type ShortText
 column_create Books title COLUMN_SCALAR ShortText
@@ -118,7 +122,7 @@ column_create Books entry_title #{data[:flags]} Books title
           "source" => "title",
           "flags"  => flags,
         }
-        @plugin.column_create(request)
+        process(:column_create, request)
         assert_equal(<<-EXPECTED, dump)
 table_create Books TABLE_HASH_KEY --key_type ShortText
 column_create Books title COLUMN_SCALAR ShortText

  Modified: test/unit/plugin/handler/groonga/test_table_create.rb (+10 -10)
===================================================================
--- test/unit/plugin/handler/groonga/test_table_create.rb    2013-12-24 18:12:17 +0900 (ca8afc8)
+++ test/unit/plugin/handler/groonga/test_table_create.rb    2013-12-24 19:08:19 +0900 (744486c)
@@ -15,8 +15,8 @@
 
 class TableCreateTest < GroongaHandlerTest
   def test_success
-    @plugin.table_create({"name" => "Books"})
-    response =****@messa*****
+    process(:table_create, {"name" => "Books"})
+    response =****@messe*****
     assert_equal(
       [[Droonga::GroongaHandler::Status::SUCCESS, NORMALIZED_START_TIME, NORMALIZED_ELAPSED_TIME], true],
       [normalize_header(response.first), response.last]
@@ -24,8 +24,8 @@ class TableCreateTest < GroongaHandlerTest
   end
 
   def test_failure
-    @plugin.table_create({})
-    response =****@messa*****
+    process(:table_create, {})
+    response =****@messe*****
     assert_equal(
       [[Droonga::GroongaHandler::Status::INVALID_ARGUMENT, NORMALIZED_START_TIME, NORMALIZED_ELAPSED_TIME], false],
       [normalize_header(response.first), response.last]
@@ -33,7 +33,7 @@ class TableCreateTest < GroongaHandlerTest
   end
 
   def test_name
-    @plugin.table_create({"name" => "Books"})
+    process(:table_create, {"name" => "Books"})
     assert_equal(<<-SCHEMA, dump)
 table_create Books TABLE_HASH_KEY --key_type ShortText
     SCHEMA
@@ -83,7 +83,7 @@ table_create Books TABLE_NO_KEY
         "name" => "Books",
         "flags" => data[:flags],
       }
-      @plugin.table_create(request)
+      process(:table_create, request)
       assert_equal(data[:schema], dump)
     end
   end
@@ -94,7 +94,7 @@ table_create Books TABLE_NO_KEY
         "name"  => "Books",
         "key_type" => "Int32",
       }
-      @plugin.table_create(request)
+      process(:table_create, request)
       assert_equal(<<-SCHEMA, dump)
 table_create Books TABLE_HASH_KEY --key_type Int32
       SCHEMA
@@ -107,7 +107,7 @@ table_create Books TABLE_HASH_KEY --key_type Int32
         "name"  => "Books",
         "value_type" => "Int32",
       }
-      @plugin.table_create(request)
+      process(:table_create, request)
       assert_equal(<<-SCHEMA, dump)
 table_create Books TABLE_HASH_KEY --key_type ShortText --value_type Int32
       SCHEMA
@@ -120,7 +120,7 @@ table_create Books TABLE_HASH_KEY --key_type ShortText --value_type Int32
         "name"  => "Books",
         "default_tokenizer" => "TokenBigram",
       }
-      @plugin.table_create(request)
+      process(:table_create, request)
       assert_equal(<<-SCHEMA, dump)
 table_create Books TABLE_HASH_KEY --key_type ShortText --default_tokenizer TokenBigram
       SCHEMA
@@ -133,7 +133,7 @@ table_create Books TABLE_HASH_KEY --key_type ShortText --default_tokenizer Token
         "name"  => "Books",
         "normalizer" => "NormalizerAuto",
       }
-      @plugin.table_create(request)
+      process(:table_create, request)
       assert_equal(<<-SCHEMA, dump)
 table_create Books TABLE_HASH_KEY|KEY_NORMALIZE --key_type ShortText
       SCHEMA

  Modified: test/unit/plugin/handler/test_add.rb (+16 -10)
===================================================================
--- test/unit/plugin/handler/test_add.rb    2013-12-24 18:12:17 +0900 (3945a68)
+++ test/unit/plugin/handler/test_add.rb    2013-12-24 19:08:19 +0900 (54a9336)
@@ -34,12 +34,18 @@ class AddHandlerTest < Test::Unit::TestCase
   def setup_handler
     @worker = StubWorker.new
     @handler = Droonga::AddHandler.new(@worker)
+    @messenger = Droonga::Test::StubHandlerMessenger.new
   end
 
   def teardown_handler
     @handler = nil
   end
 
+  def process(request)
+    message = Droonga::Test::StubHandlerMessage.new(request)
+    @handler.add(message, @messenger)
+  end
+
   public
   class HasKeyTest < self
     def setup_schema
@@ -58,8 +64,8 @@ class AddHandlerTest < Test::Unit::TestCase
         "key"    => "mori",
         "values" => {},
       }
-      mock(@handler).emit([true])
-      @handler.add(request)
+      process(request)
+      assert_equal([[true]], @messenger.values)
       table =****@worke*****["Users"]
       assert_equal(["mori"], table.collect(&:key))
     end
@@ -70,8 +76,8 @@ class AddHandlerTest < Test::Unit::TestCase
         "key"    => "asami",
         "values" => {"country" => "japan"},
       }
-      mock(@handler).emit([true])
-      @handler.add(request)
+      process(request)
+      assert_equal([[true]], @messenger.values)
       table =****@worke*****["Users"]
       assert_equal(["japan"], table.collect(&:country))
     end
@@ -92,8 +98,8 @@ class AddHandlerTest < Test::Unit::TestCase
         "table"  => "Books",
         "values" => {},
       }
-      mock(@handler).emit([true])
-      @handler.add(request)
+      process(request)
+      assert_equal([[true]], @messenger.values)
       table =****@worke*****["Books"]
       assert_equal([nil], table.collect(&:title))
     end
@@ -103,8 +109,8 @@ class AddHandlerTest < Test::Unit::TestCase
         "table"  => "Books",
         "values" => {"title" => "CSS"},
       }
-      mock(@handler).emit([true])
-      @handler.add(request)
+      process(request)
+      assert_equal([[true]], @messenger.values)
       table =****@worke*****["Books"]
       assert_equal(["CSS"], table.collect(&:title))
     end
@@ -116,8 +122,8 @@ class AddHandlerTest < Test::Unit::TestCase
         "table"  => "Nonexistent",
         "values" => {},
       }
-      mock(@handler).emit([false])
-      @handler.add(request)
+      process(request)
+      assert_equal([[false]], @messenger.values)
     end
   end
 end

  Modified: test/unit/plugin/handler/test_groonga.rb (+15 -1)
===================================================================
--- test/unit/plugin/handler/test_groonga.rb    2013-12-24 18:12:17 +0900 (41a27e6)
+++ test/unit/plugin/handler/test_groonga.rb    2013-12-24 19:08:19 +0900 (5a0b8be)
@@ -20,7 +20,7 @@ class GroongaHandlerTest < Test::Unit::TestCase
 
   def setup
     setup_database
-    setup_plugin(Droonga::GroongaHandler)
+    setup_plugin
   end
 
   def teardown
@@ -29,11 +29,25 @@ class GroongaHandlerTest < Test::Unit::TestCase
   end
 
   private
+  def setup_plugin
+    @handler = Droonga::Test::StubHandler.new
+    @plugin = Droonga::GroongaHandler.new(@handler)
+    @messenger = Droonga::Test::StubHandlerMessenger.new
+  end
+
+  def teardown_plugin
+  end
+
   def dump
     database_dumper = Groonga::DatabaseDumper.new(:database => @database)
     database_dumper.dump
   end
 
+  def process(command, request)
+    message = Droonga::Test::StubHandlerMessage.new(request)
+    @plugin.send(command, message, @messenger)
+  end
+
   NORMALIZED_START_TIME = Time.parse("2013-07-11T16:04:28+0900").to_i
   NORMALIZED_ELAPSED_TIME = 1
   def normalize_header(header)

  Modified: test/unit/plugin/handler/test_search.rb (+5 -3)
===================================================================
--- test/unit/plugin/handler/test_search.rb    2013-12-24 18:12:17 +0900 (8141091)
+++ test/unit/plugin/handler/test_search.rb    2013-12-24 19:08:19 +0900 (c3384c5)
@@ -30,6 +30,7 @@ class SearchHandlerTest < Test::Unit::TestCase
   def setup_plugin
     @handler = Droonga::Test::StubHandler.new
     @plugin = Droonga::SearchHandler.new(@handler)
+    @messenger = Droonga::Test::StubHandlerMessenger.new
   end
 
   def teardown_plugin
@@ -38,9 +39,10 @@ class SearchHandlerTest < Test::Unit::TestCase
   end
 
   private
-  def search(request)
-    @plugin.search(request)
-    results_to_result_set(@handler.messages.first)
+  def search(request, headers={})
+    message = Droonga::Test::StubHandlerMessage.new(request, headers)
+    @plugin.search(message, @messenger)
+    results_to_result_set(@messenger.values.first)
   end
 
   def results_to_result_set(results)

  Modified: test/unit/plugin/handler/test_watch.rb (+33 -19)
===================================================================
--- test/unit/plugin/handler/test_watch.rb    2013-12-24 18:12:17 +0900 (6001263)
+++ test/unit/plugin/handler/test_watch.rb    2013-12-24 19:08:19 +0900 (6f8c6fb)
@@ -35,12 +35,18 @@ class WatchHandlerTest < Test::Unit::TestCase
   def setup_plugin
     @handler = Droonga::Test::StubHandler.new
     @plugin = Droonga::WatchHandler.new(@handler)
+    @messenger = Droonga::Test::StubHandlerMessenger.new
   end
 
   def teardown_plugin
     @plugin = nil
   end
 
+  def process(command, request, headers={})
+    message = Droonga::Test::StubHandlerMessage.new(request, headers)
+    @plugin.send(command, message, @messenger)
+  end
+
   public
   class SubscribeTest < self
     def test_subscribe
@@ -49,8 +55,8 @@ class WatchHandlerTest < Test::Unit::TestCase
         "condition" => "たいやき",
         "subscriber" => "localhost"
       }
-      mock(@plugin).emit([true])
-      @plugin.subscribe(request)
+      process(:subscribe, request)
+      assert_equal([[true]], @messenger.values)
 
       assert_equal(
         ["localhost:23003/output"],
@@ -63,9 +69,8 @@ class WatchHandlerTest < Test::Unit::TestCase
         "condition" => "たいやき",
         "subscriber" => "localhost"
       }
-      @handler.envelope["from"] = "localhost:23004/output"
-      mock(@plugin).emit([true])
-      @plugin.subscribe(request)
+      process(:subscribe, request, "from" => "localhost:23004/output")
+      assert_equal([[true]], @messenger.values)
 
       assert_equal(
         ["localhost:23004/output"],
@@ -79,9 +84,8 @@ class WatchHandlerTest < Test::Unit::TestCase
         "subscriber" => "localhost",
         "route" => "localhost:23003/output"
       }
-      @handler.envelope["from"] = "localhost:23004/output"
-      mock(@plugin).emit([true])
-      @plugin.subscribe(request)
+      process(:subscribe, request, "from" => "localhost:23004/output")
+      assert_equal([[true]], @messenger.values)
 
       assert_equal(
         ["localhost:23003/output"],
@@ -111,8 +115,8 @@ class WatchHandlerTest < Test::Unit::TestCase
         "condition" => "たいやき",
         "subscriber" => "localhost"
       }
-      mock(@plugin).emit([true])
-      @plugin.unsubscribe(request)
+      process(:unsubscribe, request)
+      assert_equal([[true]], @messenger.values)
     end
 
     private
@@ -122,8 +126,9 @@ class WatchHandlerTest < Test::Unit::TestCase
         "condition" => "たいやき",
         "subscriber" => "localhost"
       }
-      stub(@plugin).emit([true])
-      @plugin.subscribe(request)
+      process(:subscribe, request)
+      assert_equal([[true]], @messenger.values)
+      @messenger.values.clear
     end
   end
 
@@ -139,16 +144,25 @@ class WatchHandlerTest < Test::Unit::TestCase
           "text" => "たいやきおいしいです"
         }
       }
-      @plugin.feed(request)
+      process(:feed, request)
       assert_equal([
-                     [request,
+                     [
+                       {
+                         "body" => {
+                           "task" => {
+                             "component" => {
+                               "body" => request,
+                             },
+                           },
+                         },
+                       },
                        {
                          "to"   => "localhost:23003/output",
                          "type" => "watch.notification",
                        },
                      ],
                    ],
-                   @handler.messages)
+                   @messenger.messages)
     end
 
     def test_feed_not_match
@@ -157,8 +171,8 @@ class WatchHandlerTest < Test::Unit::TestCase
           "text" => "たこやきおいしいです"
         }
       }
-      @plugin.feed(request)
-      assert_equal([], @handler.messages)
+      process(:feed, request)
+      assert_equal([], @messenger.messages)
     end
 
     private
@@ -168,8 +182,8 @@ class WatchHandlerTest < Test::Unit::TestCase
         "condition" => "たいやき",
         "subscriber" => "localhost"
       }
-      stub(@plugin).emit([true])
-      @plugin.subscribe(request)
+      process(:subscribe, request)
+      assert_equal([[true]], @messenger.values)
     end
   end
 end
-------------- next part --------------
HTML����������������������������...
다운로드 



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