test/unit/storage.rb in ruote-2.1.11 vs test/unit/storage.rb in ruote-2.2.0

- old
+ new

@@ -6,14 +6,17 @@ # require File.join(File.dirname(__FILE__), %w[ .. test_helper.rb ]) require_json -#require_patron require File.join(File.dirname(__FILE__), %w[ .. functional storage_helper.rb ]) +require 'ruote/fei' +require 'ruote/participant' + + # # note : using the 'errors' type, but this test is about generic storage, not # about errors per se. # @@ -23,20 +26,29 @@ @s = determine_storage({}) #@s.add_type('errors') + @s.purge_type!('errors') + @s.purge_type!('expressions') + @s.purge_type!('msgs') + @s.purge_type!('workitems') + @s.put( '_id' => 'toto', 'type' => 'errors', 'message' => 'testing') end + def teardown - @s.get_many('errors').each do |d| - @s.delete(d) - end + @s.purge_type!('errors') + @s.purge_type!('expressions') + @s.purge_type!('msgs') + @s.purge_type!('workitems') + + @s.shutdown end def test_get_configuration assert_not_nil @s.get_configuration('engine') @@ -212,16 +224,11 @@ assert_equal 10, @s.get_many('errors', nil, :limit => 10).size end def test_get_many_options - 30.times do |i| - @s.put( - '_id' => sprintf("yy!%0.2d", i), - 'type' => 'errors', - 'msg' => "whatever #{i}") - end + load_30_errors # limit assert_equal 10, @s.get_many('errors', nil, :limit => 10).size @@ -252,8 +259,145 @@ assert_equal( %w[ yy!29 yy!28 yy!27 ], @s.get_many( 'errors', nil, :skip => 0, :limit => 3, :descending => true ).collect { |d| d['_id'] }) + end + + def test_dump + + load_30_errors + + assert @s.dump('errors').length > 0 + end + + def test_ids_and_errors + + load_30_errors + + assert_equal 31, @s.ids('errors').length + end + + def test_ids_are_sorted + + load_30_errors + + assert_equal @s.ids('errors').sort, @s.ids('errors') + end + + def test_reserve + + taoe = Thread.abort_on_exception + Thread.abort_on_exception = true + + reserved = [] + threads = [] + + threads << Thread.new do + i = 0 + loop do + @s.put_msg('launch', 'tree' => i) + i = i + 1 + end + end + + 2.times do + + threads << Thread.new do + loop do + msgs = @s.get_msgs + msgs[0, 100].each do |msg| + next if msg['tree'].nil? + next unless @s.reserve(msg) + if reserved.include?(msg['tree']) + puts "=" * 80 + p [ :dbl, :r, msg['_rev'], :t, msg['tree'] ] + end + reserved << msg['tree'] + sleep(rand * 0.01) + end + end + end + end + + sleep 7 + + threads.each { |t| t.terminate } + + Thread.abort_on_exception = taoe + + assert_equal false, reserved.empty? + assert_equal reserved.size, reserved.uniq.size + end + + def test_by_field + + return unless @s.respond_to?(:by_field) + + load_workitems + + assert_equal 3, @s.by_field('workitems', 'place', 'kyouto').size + assert_equal 1, @s.by_field('workitems', 'place', 'sendai').size + assert_equal Hash, @s.by_field('workitems', 'place', 'sendai').first.class + end + + def test_by_participant + + return unless @s.respond_to?(:by_participant) + + load_workitems + + assert_equal 2, @s.by_participant('workitems', 'fujiwara', {}).size + assert_equal 1, @s.by_participant('workitems', 'shingen', {}).size + assert_equal Hash, @s.by_participant('workitems', 'shingen', {}).first.class + end + + def test_query_workitems + + return unless @s.respond_to?(:query_workitems) + + load_workitems + + assert_equal 3, @s.query_workitems('place' => 'kyouto').size + assert_equal 1, @s.query_workitems('place' => 'kyouto', 'at' => 'kamo').size + + assert_equal( + Ruote::Workitem, @s.query_workitems('place' => 'kyouto').first.class) + end + + protected + + def load_30_errors + + 30.times do |i| + @s.put( + '_id' => sprintf("yy!%0.2d", i), + 'type' => 'errors', + 'msg' => "whatever #{i}") + end + end + + def put_workitem(wfid, participant_name, fields) + + @s.put( + 'type' => 'workitems', + '_id' => "wi!0_0!12ff!#{wfid}", + 'participant_name' => participant_name, + 'wfid' => wfid, + 'fields' => fields) + end + + def load_workitems + + put_workitem( + '20110218-nadanada', 'fujiwara', 'place' => 'kyouto') + put_workitem( + '20110218-nedenada', 'fujiwara', 'place' => 'kyouto', 'at' => 'kamo') + put_workitem( + '20110218-nadanodo', 'taira', 'place' => 'kyouto') + put_workitem( + '20110218-nodonada', 'date', 'place' => 'sendai') + put_workitem( + '20110218-nadanudu', 'shingen', 'place' => 'nagoya') end end