metropolis.git  about / heads / tags
key-value store for Rack
blob 690fb8282edf9183d1596df4b2629d6f2bbc4d22 6673 bytes (raw)
$ git show HEAD:test/test_tc_hdb.rb	# shows this blob on the CLI

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
 
# -*- encoding: binary -*-
require './test/rack_read_write.rb'
require 'tokyocabinet' # FIXME: emits warning with 1.29 gem
$-w = true
require 'metropolis'

class Test_TC_HDB < Test::Unit::TestCase
  attr_reader :tmp, :o, :uri
  include TestRackReadWrite

  def setup
    tmp = Tempfile.new('tchdb')
    @path_pattern = tmp.path + ".%01x.tch"
    tmp.close!
    @uri = "tc:///"
    @app_opts = { :path_pattern => @path_pattern, :uri => @uri }
  end

  def teardown
    Dir[@path_pattern.sub!(/%\d*x/, '*')].each { |x| File.unlink(x) }
  end

  def osetup
    Metropolis.new(@app_opts)
  end

  def test_create_put_get_delete
    o = osetup
    r = o.put('hello', { 'rack.input' => StringIO.new('world') })
    assert_equal 201, r[0].to_i
    assert_equal 'text/plain', r[1]['Content-Type']
    assert_equal '8', r[1]['Content-Length']
    assert_equal "Created\n", r[2].join('')

    r = o.put('hellox', { 'rack.input' => StringIO.new('worldx') })
    assert_equal 201, r[0].to_i
    assert_equal 'text/plain', r[1]['Content-Type']
    assert_equal '8', r[1]['Content-Length']
    assert_equal "Created\n", r[2].join('')

    r = o.get('hello', {})
    assert_equal 200, r[0].to_i
    assert_equal 'application/octet-stream', r[1]['Content-Type']
    assert_equal '5', r[1]['Content-Length']
    assert_equal %w(world), r[2]

    r = o.head('hello', {})
    assert_equal 200, r[0].to_i
    assert_equal 'application/octet-stream', r[1]['Content-Type']
    assert_equal '5', r[1]['Content-Length']
    assert_equal [], r[2]

    r = o.get('hellox', {})
    assert_equal 200, r[0].to_i
    assert_equal 'application/octet-stream', r[1]['Content-Type']
    assert_equal '6', r[1]['Content-Length']
    assert_equal %w(worldx), r[2]

    r = o.delete('hellox')
    assert_equal 200, r[0].to_i
    assert_equal 'text/plain', r[1]['Content-Type']
    assert_equal '3', r[1]['Content-Length']
    assert_equal "OK\n", r[2].join('')

    r = o.delete('hellox')
    assert_equal 404, r[0].to_i
    assert_equal 'text/plain', r[1]['Content-Type']
    assert_equal '10', r[1]['Content-Length']
    assert_equal "Not Found\n", r[2].join('')

    r = o.get('hellox', {})
    assert_equal 404, r[0].to_i
    assert_equal 'text/plain', r[1]['Content-Type']
    assert_equal '10', r[1]['Content-Length']
    assert_equal "Not Found\n", r[2].join('')

    r = o.head('hellox', {})
    assert_equal 404, r[0].to_i
    assert_equal 'text/plain', r[1]['Content-Type']
    assert_equal '10', r[1]['Content-Length']
    assert_equal "", r[2].join('')
  end

  def test_putkeep
    o = osetup
    env = {
      "rack.input" => StringIO.new("hello"),
      "HTTP_X_TT_PDMODE" => "1"
    }
    assert_equal 201, o.put("x", env)[0]
    env["rack.input"] = StringIO.new("wrong")
    assert_equal 409, o.put("x", env)[0]
    assert_equal "hello", o.get("x", {})[2].join('')
  end

  def test_putcat
    o = osetup
    env = {
      "rack.input" => StringIO.new("hello"),
      "HTTP_X_TT_PDMODE" => "2"
    }
    assert_equal 201, o.put("x", env)[0]
    env["rack.input"] = StringIO.new("MOAR")
    assert_equal 201, o.put("x", env)[0]
    assert_equal "helloMOAR", o.get("x", {})[2].join('')
  end

  def test_multiproc
    nr = 2
    key = "k"
    str = "." * (1024 * 1024)
    nr.times {
      fork {
        o = osetup
        sio = StringIO.new(str)
        env = { "rack.input" => sio }
        100.times {
          o.put(key, env)
          sio.rewind
          o.get(key, {})
        }
      }
    }
    res = Process.waitall
    assert_equal nr, res.size
    res.each { |(pid, status)| assert status.success? }
  end if ENV["TEST_EXPENSIVE"]

  def test_readonly
    key = "x"
    wr = osetup
    wr.put(key, { "rack.input" => StringIO.new("OK") })
    o = Metropolis.new(@app_opts.merge(:readonly => true))
    %w(PUT DELETE).each do |rm|
      env = {
        "rack.input" => StringIO.new("FAIL"),
        "REQUEST_METHOD" => rm,
        "PATH_INFO" => "/#{key}"
      }
      assert_equal 403, o.call(env)[0]
    end
    env = {
      "REQUEST_METHOD" => "GET",
      "PATH_INFO" => "/#{key}",
    }
    assert_equal 200, o.call(env)[0]
    assert_equal '2', o.call(env)[1]["Content-Length"]
    assert_equal 'application/octet-stream', o.call(env)[1]["Content-Type"]
    assert_equal "OK", o.call(env)[2].join('')

    env["REQUEST_METHOD"] = "HEAD"
    assert_equal 200, o.call(env)[0]
    assert_equal '2', o.call(env)[1]["Content-Length"]
    assert_equal 'application/octet-stream', o.call(env)[1]["Content-Type"]
    assert_equal "", o.call(env)[2].join('')
  end

  def test_create_toplevel
    k = "x"
    nr_bytes = 1024 * 1024 * 20
    data = "0" * nr_bytes
    obj = nil
    assert_nothing_raised { obj = Metropolis.new(@app_opts) }

    query = "large=true&apow=3&bnum=65536&compress=deflate"
    assert_nothing_raised {
      obj = Metropolis.new(@app_opts.merge(:uri => "#{uri}?#{query}"))
    }
    optimize_args = obj.instance_variable_get(:@optimize)
    flags = TokyoCabinet::HDB::TLARGE | TokyoCabinet::HDB::TDEFLATE
    assert_equal flags, optimize_args[3]
    assert_equal 65536, optimize_args[0]
    assert_nil optimize_args[2]
    assert_equal 3, optimize_args[1]
    assert_nothing_raised { obj.get(k, {}) }
    assert_nothing_raised { obj.put(k,{'rack.input' => StringIO.new(data)}) }

    opts = @app_opts.merge(:uri => "#{uri}?#{query}", :readonly => true)
    obj = Metropolis.new(opts)
    assert_equal data, obj.get(k, {})[2].join('')
    obj.close!

    opts = @app_opts.merge(:uri => uri, :readonly => true)
    obj = Metropolis.new(opts)
    assert_equal data, obj.get(k, {})[2].join('')
    obj.close!
    sum = obj.instance_eval {
      @dbv.inject(0) { |size, (hdb,path)| size += File.stat(path).size }
    }
    assert sum <= nr_bytes, "#{sum} > #{nr_bytes}"
    obj.close!
  end

  def test_exclusive
    opts = @app_opts.merge(:uri => uri, :exclusive => true)
    @app = Metropolis.new(opts)
    assert_equal(app.method(:reader), app.method(:writer))
    basic_rest
  end

  def test_no_rdlock
    opts = @app_opts.merge(:uri => "#{uri}?rdlock=false")
    @app = Metropolis.new(opts)
    nolck = ::TokyoCabinet::HDB::ONOLCK
    flags = @app.instance_variable_get(:@rd_flags)
    assert((flags & nolck) == nolck)
    flags = @app.instance_variable_get(:@wr_flags)
    assert((flags & nolck) == 0)
    basic_rest
  end

  def test_no_wrlock
    @app = Metropolis.new(@app_opts.merge(:uri => "#{uri}?wrlock=false"))
    nolck = ::TokyoCabinet::HDB::ONOLCK
    flags = @app.instance_variable_get(:@wr_flags)
    assert((flags & nolck) == nolck)
    flags = @app.instance_variable_get(:@rd_flags)
    assert((flags & nolck) == 0)
    basic_rest
  end
end

git clone https://yhbt.net/metropolis.git