raindrops.git  about / heads / tags
real-time stats for preforking Rack servers
blob 074969427af0efc4afe64f4fc6bc858ecae99759 4007 bytes (raw)
$ git show v0.19.1:test/test_raindrops.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
 
# -*- encoding: binary -*-
require 'test/unit'
require 'raindrops'

class TestRaindrops < Test::Unit::TestCase

  def test_raindrop_counter_max
    assert_kind_of Integer, Raindrops::MAX
    assert Raindrops::MAX > 0
    printf "Raindrops::MAX = 0x%x\n", Raindrops::MAX
  end

  def test_raindrop_size
    assert_kind_of Integer, Raindrops::SIZE
    assert Raindrops::SIZE > 0
    puts "Raindrops::SIZE = #{Raindrops::SIZE}"
  end

  def test_page_size
    assert_kind_of Integer, Raindrops::PAGE_SIZE
    assert Raindrops::PAGE_SIZE > Raindrops::SIZE
  end

  def test_size_and_capa
    rd = Raindrops.new(4)
    assert_equal 4, rd.size
    assert rd.capa >= rd.size
  end

  def test_ary
    rd = Raindrops.new(4)
    assert_equal [0, 0, 0, 0] , rd.to_ary
  end

  def test_incr_no_args
    rd = Raindrops.new(4)
    assert_equal 1, rd.incr(0)
    assert_equal [1, 0, 0, 0], rd.to_ary
  end

  def test_incr_args
    rd = Raindrops.new(4)
    assert_equal 6, rd.incr(3, 6)
    assert_equal [0, 0, 0, 6], rd.to_ary
  end

  def test_decr_args
    rd = Raindrops.new(4)
    rd[3] = 6
    assert_equal 5, rd.decr(3, 1)
    assert_equal [0, 0, 0, 5], rd.to_ary
  end

  def test_incr_shared
    rd = Raindrops.new(2)
    5.times do
      pid = fork { rd.incr(1) }
      _, status = Process.waitpid2(pid)
      assert status.success?
    end
    assert_equal [0, 5], rd.to_ary
  end

  def test_incr_decr
    rd = Raindrops.new(1)
    fork { 1000000.times { rd.incr(0) } }
    1000.times { rd.decr(0) }
    statuses = Process.waitall
    statuses.each { |pid, status| assert status.success? }
    assert_equal [999000], rd.to_ary
  end

  def test_bad_incr
    rd = Raindrops.new(1)
    assert_raises(ArgumentError) { rd.incr(-1) }
    assert_raises(ArgumentError) { rd.incr(2) }
    assert_raises(ArgumentError) { rd.incr(0xffffffff) }
  end

  def test_dup
    @rd = Raindrops.new(1)
    rd = @rd.dup
    assert_equal 1, @rd.incr(0)
    assert_equal 1, rd.incr(0)
    assert_equal 2, rd.incr(0)
    assert_equal 2, rd[0]
    assert_equal 1, @rd[0]
  end

  def test_clone
    @rd = Raindrops.new(1)
    rd = @rd.clone
    assert_equal 1, @rd.incr(0)
    assert_equal 1, rd.incr(0)
    assert_equal 2, rd.incr(0)
    assert_equal 2, rd[0]
    assert_equal 1, @rd[0]
  end

  def test_big
    expect = (1..256).map { 0 }
    rd = Raindrops.new(256)
    assert_equal expect, rd.to_ary
    assert_nothing_raised { rd[255] = 5 }
    assert_equal 5, rd[255]
    assert_nothing_raised { rd[2] = 2 }

    expect[255] = 5
    expect[2] = 2
    assert_equal expect, rd.to_ary
  end

  def test_resize
    rd = Raindrops.new(4)
    assert_equal 4, rd.size
    assert_equal rd.capa, rd.size = rd.capa
    assert_equal rd.capa, rd.to_ary.size
    assert_equal 0, rd[rd.capa - 1]
    assert_equal 1, rd.incr(rd.capa - 1)
    assert_raises(ArgumentError) { rd[rd.capa] }
  end

  def test_resize_mremap
    rd = Raindrops.new(4)
    assert_equal 4, rd.size
    old_capa = rd.capa
    rd.size = rd.capa + 1
    assert_equal old_capa * 2, rd.capa

    # mremap() is currently broken with MAP_SHARED
    # https://bugzilla.kernel.org/show_bug.cgi?id=8691
    assert_equal 0, rd[old_capa]
    assert_equal rd.capa, rd.to_ary.size
    assert_equal 0, rd[rd.capa - 1]
    assert_equal 1, rd.incr(rd.capa - 1)
    assert_raises(ArgumentError) { rd[rd.capa] }
  rescue RangeError
  end # if RUBY_PLATFORM =~ /linux/

  def test_evaporate
    rd = Raindrops.new 1
    assert_nil rd.evaporate!
    assert_raises(StandardError) { rd.evaporate! }
  end

  def test_evaporate_with_fork
    tmp = Raindrops.new 2
    pid = fork do
      tmp.incr 0
      exit(tmp.evaporate! == nil)
    end
    _, status = Process.waitpid2(pid)
    assert status.success?
    assert_equal [ 1, 0 ], tmp.to_ary
    tmp.incr 1
    assert_equal [ 1, 1 ], tmp.to_ary
    pid = fork do
      tmp.incr 1
      exit([ 1, 2 ] == tmp.to_ary)
    end
    _, status = Process.waitpid2(pid)
    assert status.success?
    assert_equal [ 1, 2 ], tmp.to_ary
  end
end

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