[Groonga-commit] ranguba/groonga-client at 9056291 [master] response: add reader for header contents

Back to archive index

Kouhei Sutou null+****@clear*****
Wed Nov 5 15:28:36 JST 2014


Kouhei Sutou	2014-11-05 15:28:36 +0900 (Wed, 05 Nov 2014)

  New Revision: 9056291564a9ebdfa9d2dc9aa09c09f2909890e5
  https://github.com/ranguba/groonga-client/commit/9056291564a9ebdfa9d2dc9aa09c09f2909890e5

  Message:
    response: add reader for header contents

  Added files:
    test/response/test-base.rb
  Modified files:
    lib/groonga/client/response/base.rb

  Modified: lib/groonga/client/response/base.rb (+25 -0)
===================================================================
--- lib/groonga/client/response/base.rb    2014-11-05 15:22:42 +0900 (5d059bc)
+++ lib/groonga/client/response/base.rb    2014-11-05 15:28:36 +0900 (2f0d8a5)
@@ -142,6 +142,31 @@ module Groonga
           self.body = body
           self.raw = nil
         end
+
+        # @return [Integer] The status code of the response.
+        # @since 0.1.0
+        def status_code
+          (header || [0])[0]
+        end
+
+        # @return [Time] The time of the request is accepted.
+        # @since 0.1.0
+        def start_time
+          Time.at((header || [0, 0])[1])
+        end
+
+        # @return [Time] The elapsed time of the request.
+        # @since 0.1.0
+        def elapsed_time
+          (header || [0, 0, 0.0])[2]
+        end
+
+        # @return [Boolean] `true` if the request is processed successfully,
+        #   `false` otherwise.
+        # @since 0.1.0
+        def success?
+          status_code.zero?
+        end
       end
     end
   end

  Added: test/response/test-base.rb (+95 -0) 100644
===================================================================
--- /dev/null
+++ test/response/test-base.rb    2014-11-05 15:28:36 +0900 (927cdb8)
@@ -0,0 +1,95 @@
+# Copyright (C) 2014  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 "response/helper"
+
+class TestResponseBase < Test::Unit::TestCase
+  class TestHeader < self
+    class TestStatusCode < self
+      def test_have_header
+        header = [
+          -21,
+          1396012478.14975,
+          0.00050806999206543,
+        ]
+        response = Groonga::Client::Response::Base.new(nil, header, nil)
+        assert_equal(-21, response.status_code)
+      end
+
+      def test_no_header
+        response = Groonga::Client::Response::Error.new(nil, nil, nil)
+        assert_equal(0, response.status_code)
+      end
+    end
+
+    class TestStartTime < self
+      def test_have_header
+        start_time = 1396012478.14975
+        header = [
+          -21,
+          start_time,
+          0.00050806999206543,
+        ]
+        response = Groonga::Client::Response::Base.new(nil, header, nil)
+        assert_equal(Time.at(start_time), response.start_time)
+      end
+
+      def test_no_header
+        response = Groonga::Client::Response::Error.new(nil, nil, nil)
+        assert_equal(Time.at(0), response.start_time)
+      end
+    end
+
+    class TestElapsedTime < self
+      def test_have_header
+        elapsed_time = 0.00050806999206543
+        header = [
+          -21,
+          1396012478.14975,
+          elapsed_time,
+        ]
+        response = Groonga::Client::Response::Base.new(nil, header, nil)
+        assert_equal(elapsed_time, response.elapsed_time)
+      end
+
+      def test_no_header
+        response = Groonga::Client::Response::Error.new(nil, nil, nil)
+        assert_equal(0.0, response.elapsed_time)
+      end
+    end
+
+    class TestSuccess < self
+      def test_have_header
+        header = [
+          -21,
+          1396012478.14975,
+          0.00050806999206543,
+        ]
+        response = Groonga::Client::Response::Base.new(nil, header, nil)
+        assert do
+          not response.success?
+        end
+      end
+
+      def test_no_header
+        response = Groonga::Client::Response::Error.new(nil, nil, nil)
+        assert do
+          response.success?
+        end
+      end
+    end
+  end
+end
-------------- next part --------------
HTML����������������������������...
Download 



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