Sha256: 83a934fe53b8f6f05d95a57c3d6e6c07dbe98e55a1268254000752b861c7eff7

Contents?: true

Size: 1.57 KB

Versions: 302

Compression:

Stored size: 1.57 KB

Contents

require File.join(File.expand_path(File.dirname(__FILE__)), '../../..', 'test_helper.rb')

require 'rbbt-util'
require 'rbbt/util/log'
require 'rbbt/util/concurrency/processes'

class TestConcurrencyProcess < Test::Unit::TestCase

  def test_process
    q = RbbtProcessQueue.new 10

    res = []

    q.callback do |v|
      res << v
    end

    q.init do |i|
      i * 2
    end

    times = 500
    t = TSV.setup({"a" => 1}, :type => :single)

    times.times do |i|
      q.process i
    end

    q.join

    assert_equal times, res.length
    assert_equal [0, 2, 4], res.sort[0..2]
  end

  def test_each
    times = 5000
    elems = (0..times-1).to_a

    TmpFile.with_file do |dir|
      RbbtProcessQueue.each(elems) do |elem|
        Open.write(File.join(dir, elem.to_s), "DONE")
      end

      assert_equal times, Dir.glob(File.join(dir, '*')).length
    end
  end

  def test_process_abort
    assert_raise Aborted do
      q = RbbtProcessQueue.new 10

      res = []

      q.callback do |v|
        res << v
      end

      q.init do |i|
        sleep 1 while true
      end

      times = 500
      t = TSV.setup({"a" => 1}, :type => :single)

      times.times do |i|
        q.process i
      end

      sleep 2
      q.clean


      q.join
    end
  end


  def test_process_respawn
    q = RbbtProcessQueue.new 2, nil, nil, true

    res = []

    q.callback do |v|
      res << v
    end

    q.init do |i|
      $str ||="-"
      $str = $str *  (2 + rand(5).to_i)
      sleep 0.1
      "."
    end

    times = 50

    times.times do |i|
      q.process i
    end

    q.join
  end
end


Version data entries

302 entries across 302 versions & 1 rubygems

Version Path
rbbt-util-5.22.1 test/rbbt/util/concurrency/test_processes.rb
rbbt-util-5.22.0 test/rbbt/util/concurrency/test_processes.rb
rbbt-util-5.21.137 test/rbbt/util/concurrency/test_processes.rb
rbbt-util-5.21.136 test/rbbt/util/concurrency/test_processes.rb
rbbt-util-5.21.135 test/rbbt/util/concurrency/test_processes.rb
rbbt-util-5.21.134 test/rbbt/util/concurrency/test_processes.rb
rbbt-util-5.21.133 test/rbbt/util/concurrency/test_processes.rb
rbbt-util-5.21.132 test/rbbt/util/concurrency/test_processes.rb
rbbt-util-5.21.131 test/rbbt/util/concurrency/test_processes.rb
rbbt-util-5.21.130 test/rbbt/util/concurrency/test_processes.rb
rbbt-util-5.21.129 test/rbbt/util/concurrency/test_processes.rb
rbbt-util-5.21.128 test/rbbt/util/concurrency/test_processes.rb
rbbt-util-5.21.127 test/rbbt/util/concurrency/test_processes.rb
rbbt-util-5.21.126 test/rbbt/util/concurrency/test_processes.rb
rbbt-util-5.21.125 test/rbbt/util/concurrency/test_processes.rb
rbbt-util-5.21.123 test/rbbt/util/concurrency/test_processes.rb
rbbt-util-5.21.122 test/rbbt/util/concurrency/test_processes.rb
rbbt-util-5.21.121 test/rbbt/util/concurrency/test_processes.rb
rbbt-util-5.21.120 test/rbbt/util/concurrency/test_processes.rb
rbbt-util-5.21.119 test/rbbt/util/concurrency/test_processes.rb