"Fossies" - the Fresh Open Source Software Archive

Member "ruby-2.7.4/test/drb/test_drb.rb" (7 Jul 2021, 7422 Bytes) of package /linux/misc/ruby-2.7.4.tar.gz:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) Ruby source code syntax highlighting (style: standard) with prefixed line numbers and code folding option. Alternatively you can here view or download the uninterpreted source code file.

    1 # frozen_string_literal: false
    2 require_relative 'drbtest'
    3 
    4 module DRbTests
    5 
    6 class TestDRbCore < Test::Unit::TestCase
    7   include DRbCore
    8 
    9   def setup
   10     super
   11     setup_service 'ut_drb.rb'
   12   end
   13 end
   14 
   15 module DRbYield
   16   include DRbBase
   17 
   18   def test_01_one
   19     @there.echo_yield_1([]) {|one|
   20       assert_equal([], one)
   21     }
   22 
   23     @there.echo_yield_1(1) {|one|
   24       assert_equal(1, one)
   25     }
   26 
   27     @there.echo_yield_1(nil) {|one|
   28       assert_equal(nil, one)
   29     }
   30   end
   31 
   32   def test_02_two
   33     @there.echo_yield_2([], []) {|one, two|
   34       assert_equal([], one)
   35       assert_equal([], two)
   36     }
   37 
   38     @there.echo_yield_2(1, 2) {|one, two|
   39       assert_equal(1, one)
   40       assert_equal(2, two)
   41     }
   42 
   43     @there.echo_yield_2(3, nil) {|one, two|
   44       assert_equal(3, one)
   45       assert_equal(nil, two)
   46     }
   47 
   48     @there.echo_yield_1([:key, :value]) {|one, two|
   49       assert_equal(:key, one)
   50       assert_equal(:value, two)
   51     }
   52   end
   53 
   54   def test_03_many
   55     @there.echo_yield_0 {|*s|
   56       assert_equal([], s)
   57     }
   58     @there.echo_yield(nil) {|*s|
   59       assert_equal([nil], s)
   60     }
   61     @there.echo_yield(1) {|*s|
   62       assert_equal([1], s)
   63     }
   64     @there.echo_yield(1, 2) {|*s|
   65       assert_equal([1, 2], s)
   66     }
   67     @there.echo_yield(1, 2, 3) {|*s|
   68       assert_equal([1, 2, 3], s)
   69     }
   70     @there.echo_yield([], []) {|*s|
   71       assert_equal([[], []], s)
   72     }
   73     @there.echo_yield([]) {|*s|
   74       assert_equal([[]], s) # !
   75     }
   76   end
   77 
   78   def test_04_many_to_one
   79     @there.echo_yield_0 {|*s|
   80       assert_equal([], s)
   81     }
   82     @there.echo_yield(nil) {|*s|
   83       assert_equal([nil], s)
   84     }
   85     @there.echo_yield(1) {|*s|
   86       assert_equal([1], s)
   87     }
   88     @there.echo_yield(1, 2) {|*s|
   89       assert_equal([1, 2], s)
   90     }
   91     @there.echo_yield(1, 2, 3) {|*s|
   92       assert_equal([1, 2, 3], s)
   93     }
   94     @there.echo_yield([], []) {|*s|
   95       assert_equal([[], []], s)
   96     }
   97     @there.echo_yield([]) {|*s|
   98       assert_equal([[]], s)
   99     }
  100   end
  101 
  102   def test_05_array_subclass
  103     @there.xarray_each {|x| assert_kind_of(XArray, x)}
  104     @there.xarray_each {|*x| assert_kind_of(XArray, x[0])}
  105   end
  106 end
  107 
  108 class TestDRbYield < Test::Unit::TestCase
  109   include DRbYield
  110 
  111   def setup
  112     super
  113     setup_service 'ut_drb.rb'
  114   end
  115 end
  116 
  117 class TestDRbRubyYield < Test::Unit::TestCase
  118   include DRbYield
  119 
  120   def setup
  121     @there = self
  122     super
  123   end
  124 
  125   def echo_yield(*arg)
  126     yield(*arg)
  127   end
  128 
  129   def echo_yield_0
  130     yield
  131   end
  132 
  133   def echo_yield_1(a)
  134     yield(a)
  135   end
  136 
  137   def echo_yield_2(a, b)
  138     yield(a, b)
  139   end
  140 
  141   def xarray_each
  142     xary = [XArray.new([0])]
  143     xary.each do |x|
  144       yield(x)
  145     end
  146   end
  147 
  148 end
  149 
  150 class TestDRbRuby18Yield < Test::Unit::TestCase
  151   include DRbYield
  152 
  153   class YieldTest18
  154     def echo_yield(*arg, &proc)
  155       proc.call(*arg)
  156     end
  157 
  158     def echo_yield_0(&proc)
  159       proc.call
  160     end
  161 
  162     def echo_yield_1(a, &proc)
  163       proc.call(a)
  164     end
  165 
  166     def echo_yield_2(a, b, &proc)
  167       proc.call(a, b)
  168     end
  169 
  170     def xarray_each(&proc)
  171       xary = [XArray.new([0])]
  172       xary.each(&proc)
  173     end
  174 
  175   end
  176 
  177   def setup
  178     @there = YieldTest18.new
  179     super
  180   end
  181 end
  182 
  183 class TestDRbAry < Test::Unit::TestCase
  184   include DRbAry
  185 
  186   def setup
  187     super
  188     setup_service 'ut_array.rb'
  189   end
  190 end
  191 
  192 class TestDRbMServer < Test::Unit::TestCase
  193   include DRbBase
  194 
  195   def setup
  196     super
  197     setup_service 'ut_drb.rb'
  198     @server = (1..3).collect do |n|
  199       DRb::DRbServer.new("druby://localhost:0", Onecky.new(n.to_s))
  200     end
  201   end
  202 
  203   def teardown
  204     @server.each do |s|
  205       s.stop_service
  206     end
  207     super
  208   end
  209 
  210   def test_01
  211     assert_equal(6, @there.sample(@server[0].front, @server[1].front, @server[2].front))
  212   end
  213 end
  214 
  215 class TestDRbSafe1 < Test::Unit::TestCase
  216   include DRbAry
  217   def setup
  218     super
  219     setup_service 'ut_safe1.rb'
  220   end
  221 end
  222 
  223 class TestDRbLarge < Test::Unit::TestCase
  224   include DRbBase
  225 
  226   def setup
  227     super
  228     setup_service 'ut_large.rb'
  229   end
  230 
  231   def test_01_large_ary
  232     ary = [2] * 10240
  233     assert_equal(10240, @there.size(ary))
  234     assert_equal(20480, @there.sum(ary))
  235     assert_equal(2 ** 10240, @there.multiply(ary))
  236     assert_equal(2, @there.avg(ary))
  237     assert_equal(2, @there.median(ary))
  238   end
  239 
  240   def test_02_large_ary
  241     ary = ["Hello, World"] * 10240
  242     assert_equal(10240, @there.size(ary))
  243     assert_equal(ary[0..ary.length].inject(:+), @there.sum(ary))
  244     assert_raise(TypeError) {@there.multiply(ary)}
  245     assert_raise(TypeError) {@there.avg(ary)}
  246     assert_raise(TypeError) {@there.median(ary)}
  247   end
  248 
  249   def test_03_large_ary
  250     ary = [Thread.current] * 10240
  251     assert_equal(10240, @there.size(ary))
  252   end
  253 
  254   def test_04_many_arg
  255     assert_raise(DRb::DRbConnError) {
  256       @there.arg_test(1, 2, 3, 4, 5, 6, 7, 8, 9, 0)
  257     }
  258   end
  259 
  260   def test_05_too_large_ary
  261     ary = ["Hello, World"] * 102400
  262     exception = nil
  263     begin
  264       @there.size(ary)
  265     rescue StandardError
  266       exception = $!
  267     end
  268     assert_kind_of(StandardError, exception)
  269   end
  270 
  271   def test_06_array_operations
  272     ary = [1,50,3,844,7,45,23]
  273     assert_equal(7, @there.size(ary))
  274     assert_equal(973, @there.sum(ary))
  275     assert_equal(917217000, @there.multiply(ary))
  276     assert_equal(139.0, @there.avg(ary))
  277     assert_equal(23.0, @there.median(ary))
  278 
  279     ary2 = [1,2,3,4]
  280     assert_equal(4, @there.size(ary2))
  281     assert_equal(10, @there.sum(ary2))
  282     assert_equal(24, @there.multiply(ary2))
  283     assert_equal(2.5, @there.avg(ary2))
  284     assert_equal(2.5, @there.median(ary2))
  285 
  286   end
  287 
  288   def test_07_one_element_array
  289     ary = [50]
  290     assert_equal(1, @there.size(ary))
  291     assert_equal(50, @there.sum(ary))
  292     assert_equal(50, @there.multiply(ary))
  293     assert_equal(50.0, @there.avg(ary))
  294     assert_equal(50.0, @there.median(ary))
  295   end
  296 
  297   def test_08_empty_array
  298     ary = []
  299     assert_equal(0, @there.size(ary))
  300     assert_equal(nil, @there.sum(ary))
  301     assert_equal(nil, @there.multiply(ary))
  302     assert_equal(nil, @there.avg(ary))
  303     assert_equal(nil, @there.median(ary))
  304   end
  305 end
  306 
  307 class TestBug4409 < Test::Unit::TestCase
  308   include DRbBase
  309 
  310   def setup
  311     super
  312     setup_service 'ut_eq.rb'
  313   end
  314 
  315   def test_bug4409
  316     foo = @there.foo
  317     assert_operator(@there, :foo?, foo)
  318   end
  319 end
  320 
  321 class TestDRbAnyToS < Test::Unit::TestCase
  322   class BO < BasicObject
  323   end
  324 
  325   def test_any_to_s
  326     server = DRb::DRbServer.new('druby://localhost:0')
  327     server.singleton_class.send(:public, :any_to_s)
  328     assert_equal("foo:String", server.any_to_s("foo"))
  329     assert_match(/\A#<DRbTests::TestDRbAnyToS::BO:0x[0-9a-f]+>\z/, server.any_to_s(BO.new))
  330     server.stop_service
  331     server.thread.join
  332     DRb::DRbConn.stop_pool
  333   end
  334 end
  335 
  336 class TestDRbTCP < Test::Unit::TestCase
  337   def test_immediate_close
  338     server = DRb::DRbServer.new('druby://localhost:0')
  339     host, port, = DRb::DRbTCPSocket.send(:parse_uri, server.uri)
  340     socket = TCPSocket.open host, port
  341     socket.shutdown
  342     socket.close
  343     client = DRb::DRbTCPSocket.new(server.uri, socket)
  344     assert client
  345     client.close
  346     server.stop_service
  347     server.thread.join
  348     DRb::DRbConn.stop_pool
  349   end
  350 end
  351 
  352 class TestBug16634 < Test::Unit::TestCase
  353   include DRbBase
  354 
  355   def setup
  356     super
  357     setup_service 'ut_drb.rb'
  358   end
  359 
  360   def test_bug16634
  361     assert_equal(42, @there.keyword_test1(a: 42))
  362     assert_equal("default", @there.keyword_test2)
  363     assert_equal(42, @there.keyword_test2(b: 42))
  364     assert_equal({:a=>42, :b=>42}, @there.keyword_test3(a: 42, b: 42))
  365   end
  366 end
  367 
  368 end