Sha256: f6b82e231868cf1c75caf3dc73e9f139b518ba5974b368c496f6915f4cf0ea7e

Contents?: true

Size: 1.89 KB

Versions: 5

Compression:

Stored size: 1.89 KB

Contents

#!/usr/bin/ruby

$: << "../lib"

require "runit/testcase"
require "runit/cui/testrunner"
require 'rubygems'
require 'needle'
require 'jiji/util/synchronize_interceptor'
require 'fileutils'

class SynchronizeInterceptorTest <  RUNIT::TestCase
  
  def setup
    @dir = File.dirname(__FILE__) + "/CSVTest.tmp"
    FileUtils.mkdir_p @dir    
  end

  def teardown
    FileUtils.rm_rf @dir
  end

  def test_basic
    
    # レジストリ
    p = proc {
      5.times {|i| print "#{i},"; sleep 0.1 }
    }
    registry = Needle::Registry.define {|builder|
      builder.loop_no { p }
      builder.loop_x1 { p }
      builder.loop_x2 { p }
      builder.loop_y1 { p }
      builder.loop_y2 { p }
    }
    
    # インターセプタを適用
    registry.intercept( :loop_x1 ).with { SynchronizeInterceptor }.with_options( :id=>"x" )
    registry.intercept( :loop_x2 ).with { SynchronizeInterceptor }.with_options( :id=>"x" )
    registry.intercept( :loop_y1 ).with { SynchronizeInterceptor }.with_options( :id=>"y" )
    registry.intercept( :loop_y2 ).with { SynchronizeInterceptor }.with_options( :id=>"y" )
    
    loops = [
      :loop_no, :loop_x1, :loop_x2, :loop_y1, :loop_y2
    ]
    loops.each {|a|
      loops.each {|b|
        puts "\n\n---#{a} x #{b}"
        [registry[a],registry[b]].map {|x|
          Thread.fork(x) {|l| l.call }
        }.each{|t| t.value }
      }
    }
  end
  
  # 同じインターセプタが2重に適用されている場合のテスト
  def test_dual
    registry = Needle::Registry.new {|r|
        r.register( :a ) {
          proc { puts "aaa" }
        }
        r.register( :b ) {
          a = r.a
          proc { a.call; puts "bbb" }
        }
    }
    registry.intercept( :a ).with { SynchronizeInterceptor }.with_options( :id=>"x" )
    registry.intercept( :b ).with { SynchronizeInterceptor }.with_options( :id=>"x" )
    
    b = registry.b
    3.times{|i| b.call }
  end
 
  
end

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
unageanu-jiji-0.1.0 test/test_SynchronizeInterceptor.rb
unageanu-jiji-1.0.0 test/test_SynchronizeInterceptor.rb
unageanu-jiji-1.0.1 test/test_SynchronizeInterceptor.rb
unageanu-jiji-1.0.2 test/test_SynchronizeInterceptor.rb
unageanu-jiji-1.0.3 test/test_SynchronizeInterceptor.rb