PageRenderTime 52ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 1ms

/test/rinda/test_rinda.rb

http://github.com/ruby/ruby
Ruby | 892 lines | 734 code | 149 blank | 9 comment | 38 complexity | 5bcc1fdcac20dd12736679b939a9b79e MD5 | raw file
Possible License(s): GPL-2.0, BSD-3-Clause, AGPL-3.0
  1. # frozen_string_literal: false
  2. require 'test/unit'
  3. require 'drb/drb'
  4. require 'drb/eq'
  5. require 'rinda/ring'
  6. require 'rinda/tuplespace'
  7. require 'timeout'
  8. require 'singleton'
  9. module Rinda
  10. class MockClock
  11. include Singleton
  12. class MyTS < Rinda::TupleSpace
  13. def keeper_thread
  14. nil
  15. end
  16. def stop_keeper
  17. if @keeper
  18. @keeper.kill
  19. @keeper.join
  20. @keeper = nil
  21. end
  22. end
  23. end
  24. def initialize
  25. @now = 2
  26. @reso = 1
  27. @ts = nil
  28. @inf = 2**31 - 1
  29. end
  30. def start_keeper
  31. @now = 2
  32. @reso = 1
  33. @ts&.stop_keeper
  34. @ts = MyTS.new
  35. @ts.write([2, :now])
  36. @inf = 2**31 - 1
  37. end
  38. def stop_keeper
  39. @ts.stop_keeper
  40. end
  41. def now
  42. @now.to_f
  43. end
  44. def at(n)
  45. n
  46. end
  47. def _forward(n=nil)
  48. now ,= @ts.take([nil, :now])
  49. @now = now + n
  50. @ts.write([@now, :now])
  51. end
  52. def forward(n)
  53. while n > 0
  54. _forward(@reso)
  55. n -= @reso
  56. Thread.pass
  57. end
  58. end
  59. def rewind
  60. @ts.take([nil, :now])
  61. @ts.write([@inf, :now])
  62. @ts.take([nil, :now])
  63. @now = 2
  64. @ts.write([2, :now])
  65. end
  66. def sleep(n=nil)
  67. now ,= @ts.read([nil, :now])
  68. @ts.read([(now + n)..@inf, :now])
  69. 0
  70. end
  71. end
  72. module Time
  73. def sleep(n)
  74. @m.sleep(n)
  75. end
  76. module_function :sleep
  77. def at(n)
  78. n
  79. end
  80. module_function :at
  81. def now
  82. defined?(@m) && @m ? @m.now : 2
  83. end
  84. module_function :now
  85. def rewind
  86. @m.rewind
  87. end
  88. module_function :rewind
  89. def forward(n)
  90. @m.forward(n)
  91. end
  92. module_function :forward
  93. @m = MockClock.instance
  94. end
  95. class TupleSpace
  96. def sleep(n)
  97. Kernel.sleep(n * 0.01)
  98. end
  99. end
  100. module TupleSpaceTestModule
  101. def setup
  102. MockClock.instance.start_keeper
  103. end
  104. def teardown
  105. MockClock.instance.stop_keeper
  106. end
  107. def sleep(n)
  108. if Thread.current == Thread.main
  109. Time.forward(n)
  110. else
  111. Time.sleep(n)
  112. end
  113. end
  114. def thread_join(th)
  115. while th.alive?
  116. Kernel.sleep(0.1)
  117. sleep(1)
  118. end
  119. th.value
  120. end
  121. def test_00_tuple
  122. tuple = Rinda::TupleEntry.new([1,2,3])
  123. assert(!tuple.canceled?)
  124. assert(!tuple.expired?)
  125. assert(tuple.alive?)
  126. end
  127. def test_00_template
  128. tmpl = Rinda::Template.new([1,2,3])
  129. assert_equal(3, tmpl.size)
  130. assert_equal(3, tmpl[2])
  131. assert(tmpl.match([1,2,3]))
  132. assert(!tmpl.match([1,nil,3]))
  133. tmpl = Rinda::Template.new([/^rinda/i, nil, :hello])
  134. assert_equal(3, tmpl.size)
  135. assert(tmpl.match(['Rinda', 2, :hello]))
  136. assert(!tmpl.match(['Rinda', 2, Symbol]))
  137. assert(!tmpl.match([1, 2, :hello]))
  138. assert(tmpl.match([/^rinda/i, 2, :hello]))
  139. tmpl = Rinda::Template.new([Symbol])
  140. assert_equal(1, tmpl.size)
  141. assert(tmpl.match([:hello]))
  142. assert(tmpl.match([Symbol]))
  143. assert(!tmpl.match(['Symbol']))
  144. tmpl = Rinda::Template.new({"message"=>String, "name"=>String})
  145. assert_equal(2, tmpl.size)
  146. assert(tmpl.match({"message"=>"Hello", "name"=>"Foo"}))
  147. assert(!tmpl.match({"message"=>"Hello", "name"=>"Foo", "1"=>2}))
  148. assert(!tmpl.match({"message"=>"Hi", "name"=>"Foo", "age"=>1}))
  149. assert(!tmpl.match({"message"=>"Hello", "no_name"=>"Foo"}))
  150. assert_raise(Rinda::InvalidHashTupleKey) do
  151. Rinda::Template.new({:message=>String, "name"=>String})
  152. end
  153. tmpl = Rinda::Template.new({"name"=>String})
  154. assert_equal(1, tmpl.size)
  155. assert(tmpl.match({"name"=>"Foo"}))
  156. assert(!tmpl.match({"message"=>"Hello", "name"=>"Foo"}))
  157. assert(!tmpl.match({"message"=>:symbol, "name"=>"Foo", "1"=>2}))
  158. assert(!tmpl.match({"message"=>"Hi", "name"=>"Foo", "age"=>1}))
  159. assert(!tmpl.match({"message"=>"Hello", "no_name"=>"Foo"}))
  160. tmpl = Rinda::Template.new({"message"=>String, "name"=>String})
  161. assert_equal(2, tmpl.size)
  162. assert(tmpl.match({"message"=>"Hello", "name"=>"Foo"}))
  163. assert(!tmpl.match({"message"=>"Hello", "name"=>"Foo", "1"=>2}))
  164. assert(!tmpl.match({"message"=>"Hi", "name"=>"Foo", "age"=>1}))
  165. assert(!tmpl.match({"message"=>"Hello", "no_name"=>"Foo"}))
  166. tmpl = Rinda::Template.new({"message"=>String})
  167. assert_equal(1, tmpl.size)
  168. assert(tmpl.match({"message"=>"Hello"}))
  169. assert(!tmpl.match({"message"=>"Hello", "name"=>"Foo"}))
  170. assert(!tmpl.match({"message"=>"Hello", "name"=>"Foo", "1"=>2}))
  171. assert(!tmpl.match({"message"=>"Hi", "name"=>"Foo", "age"=>1}))
  172. assert(!tmpl.match({"message"=>"Hello", "no_name"=>"Foo"}))
  173. tmpl = Rinda::Template.new({"message"=>String, "name"=>nil})
  174. assert_equal(2, tmpl.size)
  175. assert(tmpl.match({"message"=>"Hello", "name"=>"Foo"}))
  176. assert(!tmpl.match({"message"=>"Hello", "name"=>"Foo", "1"=>2}))
  177. assert(!tmpl.match({"message"=>"Hi", "name"=>"Foo", "age"=>1}))
  178. assert(!tmpl.match({"message"=>"Hello", "no_name"=>"Foo"}))
  179. assert_raise(Rinda::InvalidHashTupleKey) do
  180. @ts.write({:message=>String, "name"=>String})
  181. end
  182. @ts.write([1, 2, 3])
  183. assert_equal([1, 2, 3], @ts.take([1, 2, 3]))
  184. @ts.write({'1'=>1, '2'=>2, '3'=>3})
  185. assert_equal({'1'=>1, '2'=>2, '3'=>3}, @ts.take({'1'=>1, '2'=>2, '3'=>3}))
  186. entry = @ts.write(['1'=>1, '2'=>2, '3'=>3])
  187. assert_raise(Rinda::RequestExpiredError) do
  188. assert_equal({'1'=>1, '2'=>2, '3'=>3}, @ts.read({'1'=>1}, 0))
  189. end
  190. entry.cancel
  191. end
  192. def test_00_DRbObject
  193. ro = DRbObject.new(nil, "druby://host:1234")
  194. tmpl = Rinda::DRbObjectTemplate.new
  195. assert(tmpl === ro)
  196. tmpl = Rinda::DRbObjectTemplate.new("druby://host:1234")
  197. assert(tmpl === ro)
  198. tmpl = Rinda::DRbObjectTemplate.new("druby://host:12345")
  199. assert(!(tmpl === ro))
  200. tmpl = Rinda::DRbObjectTemplate.new(/^druby:\/\/host:/)
  201. assert(tmpl === ro)
  202. ro = DRbObject.new_with(12345, 1234)
  203. assert(!(tmpl === ro))
  204. ro = DRbObject.new_with("druby://foo:12345", 1234)
  205. assert(!(tmpl === ro))
  206. tmpl = Rinda::DRbObjectTemplate.new(/^druby:\/\/(foo|bar):/)
  207. assert(tmpl === ro)
  208. ro = DRbObject.new_with("druby://bar:12345", 1234)
  209. assert(tmpl === ro)
  210. ro = DRbObject.new_with("druby://baz:12345", 1234)
  211. assert(!(tmpl === ro))
  212. end
  213. def test_inp_rdp
  214. assert_raise(Rinda::RequestExpiredError) do
  215. @ts.take([:empty], 0)
  216. end
  217. assert_raise(Rinda::RequestExpiredError) do
  218. @ts.read([:empty], 0)
  219. end
  220. end
  221. def test_ruby_talk_264062
  222. th = Thread.new {
  223. assert_raise(Rinda::RequestExpiredError) do
  224. @ts.take([:empty], 1)
  225. end
  226. }
  227. sleep(10)
  228. thread_join(th)
  229. th = Thread.new {
  230. assert_raise(Rinda::RequestExpiredError) do
  231. @ts.read([:empty], 1)
  232. end
  233. }
  234. sleep(10)
  235. thread_join(th)
  236. end
  237. def test_symbol_tuple
  238. @ts.write([:symbol, :symbol])
  239. @ts.write(['string', :string])
  240. assert_equal([[:symbol, :symbol]], @ts.read_all([:symbol, nil]))
  241. assert_equal([[:symbol, :symbol]], @ts.read_all([Symbol, nil]))
  242. assert_equal([], @ts.read_all([:nil, nil]))
  243. end
  244. def test_core_01
  245. 5.times do
  246. @ts.write([:req, 2])
  247. end
  248. assert_equal([[:req, 2], [:req, 2], [:req, 2], [:req, 2], [:req, 2]],
  249. @ts.read_all([nil, nil]))
  250. taker = Thread.new(5) do |count|
  251. s = 0
  252. count.times do
  253. tuple = @ts.take([:req, Integer])
  254. assert_equal(2, tuple[1])
  255. s += tuple[1]
  256. end
  257. @ts.write([:ans, s])
  258. s
  259. end
  260. assert_equal(10, thread_join(taker))
  261. assert_equal([:ans, 10], @ts.take([:ans, 10]))
  262. assert_equal([], @ts.read_all([nil, nil]))
  263. end
  264. def test_core_02
  265. taker = Thread.new(5) do |count|
  266. s = 0
  267. count.times do
  268. tuple = @ts.take([:req, Integer])
  269. assert_equal(2, tuple[1])
  270. s += tuple[1]
  271. end
  272. @ts.write([:ans, s])
  273. s
  274. end
  275. 5.times do
  276. @ts.write([:req, 2])
  277. end
  278. assert_equal(10, thread_join(taker))
  279. assert_equal([:ans, 10], @ts.take([:ans, 10]))
  280. assert_equal([], @ts.read_all([nil, nil]))
  281. end
  282. def test_core_03_notify
  283. notify1 = @ts.notify(nil, [:req, Integer])
  284. notify2 = @ts.notify(nil, {"message"=>String, "name"=>String})
  285. 5.times do
  286. @ts.write([:req, 2])
  287. end
  288. 5.times do
  289. tuple = @ts.take([:req, Integer])
  290. assert_equal(2, tuple[1])
  291. end
  292. 5.times do
  293. assert_equal(['write', [:req, 2]], notify1.pop)
  294. end
  295. 5.times do
  296. assert_equal(['take', [:req, 2]], notify1.pop)
  297. end
  298. @ts.write({"message"=>"first", "name"=>"3"})
  299. @ts.write({"message"=>"second", "name"=>"1"})
  300. @ts.write({"message"=>"third", "name"=>"0"})
  301. @ts.take({"message"=>"third", "name"=>"0"})
  302. @ts.take({"message"=>"first", "name"=>"3"})
  303. assert_equal(["write", {"message"=>"first", "name"=>"3"}], notify2.pop)
  304. assert_equal(["write", {"message"=>"second", "name"=>"1"}], notify2.pop)
  305. assert_equal(["write", {"message"=>"third", "name"=>"0"}], notify2.pop)
  306. assert_equal(["take", {"message"=>"third", "name"=>"0"}], notify2.pop)
  307. assert_equal(["take", {"message"=>"first", "name"=>"3"}], notify2.pop)
  308. end
  309. def test_cancel_01
  310. entry = @ts.write([:removeme, 1])
  311. assert_equal([[:removeme, 1]], @ts.read_all([nil, nil]))
  312. entry.cancel
  313. assert_equal([], @ts.read_all([nil, nil]))
  314. template = nil
  315. taker = Thread.new do
  316. assert_raise(Rinda::RequestCanceledError) do
  317. @ts.take([:take, nil], 10) do |t|
  318. template = t
  319. Thread.new do
  320. template.cancel
  321. end
  322. end
  323. end
  324. end
  325. sleep(2)
  326. thread_join(taker)
  327. assert(template.canceled?)
  328. @ts.write([:take, 1])
  329. assert_equal([[:take, 1]], @ts.read_all([nil, nil]))
  330. end
  331. def test_cancel_02
  332. entry = @ts.write([:removeme, 1])
  333. assert_equal([[:removeme, 1]], @ts.read_all([nil, nil]))
  334. entry.cancel
  335. assert_equal([], @ts.read_all([nil, nil]))
  336. template = nil
  337. reader = Thread.new do
  338. assert_raise(Rinda::RequestCanceledError) do
  339. @ts.read([:take, nil], 10) do |t|
  340. template = t
  341. Thread.new do
  342. template.cancel
  343. end
  344. end
  345. end
  346. end
  347. sleep(2)
  348. thread_join(reader)
  349. assert(template.canceled?)
  350. @ts.write([:take, 1])
  351. assert_equal([[:take, 1]], @ts.read_all([nil, nil]))
  352. end
  353. class SimpleRenewer
  354. def initialize(sec, n = 1)
  355. @sec = sec
  356. @n = n
  357. end
  358. def renew
  359. return -1 if @n <= 0
  360. @n -= 1
  361. return @sec
  362. end
  363. end
  364. def test_00_renewer
  365. tuple = Rinda::TupleEntry.new([1,2,3], true)
  366. assert(!tuple.canceled?)
  367. assert(tuple.expired?)
  368. assert(!tuple.alive?)
  369. tuple = Rinda::TupleEntry.new([1,2,3], 1)
  370. assert(!tuple.canceled?)
  371. assert(!tuple.expired?)
  372. assert(tuple.alive?)
  373. sleep(2)
  374. assert(tuple.expired?)
  375. assert(!tuple.alive?)
  376. @renewer = SimpleRenewer.new(1,2)
  377. tuple = Rinda::TupleEntry.new([1,2,3], @renewer)
  378. assert(!tuple.canceled?)
  379. assert(!tuple.expired?)
  380. assert(tuple.alive?)
  381. sleep(1)
  382. assert(!tuple.canceled?)
  383. assert(!tuple.expired?)
  384. assert(tuple.alive?)
  385. sleep(2)
  386. assert(tuple.expired?)
  387. assert(!tuple.alive?)
  388. end
  389. end
  390. class TupleSpaceTest < Test::Unit::TestCase
  391. include TupleSpaceTestModule
  392. def setup
  393. super
  394. ThreadGroup.new.add(Thread.current)
  395. @ts = Rinda::TupleSpace.new(1)
  396. end
  397. def teardown
  398. # implementation-dependent
  399. @ts.instance_eval{
  400. if th = @keeper
  401. th.kill
  402. th.join
  403. end
  404. }
  405. super
  406. end
  407. end
  408. class TupleSpaceProxyTest < Test::Unit::TestCase
  409. include TupleSpaceTestModule
  410. def setup
  411. super
  412. ThreadGroup.new.add(Thread.current)
  413. @ts_base = Rinda::TupleSpace.new(1)
  414. @ts = Rinda::TupleSpaceProxy.new(@ts_base)
  415. @server = DRb.start_service("druby://localhost:0")
  416. end
  417. def teardown
  418. # implementation-dependent
  419. @ts_base.instance_eval{
  420. if th = @keeper
  421. th.kill
  422. th.join
  423. end
  424. }
  425. @server.stop_service
  426. DRb::DRbConn.stop_pool
  427. super
  428. end
  429. def test_remote_array_and_hash
  430. # Don't remove ary/hsh local variables.
  431. # These are necessary to protect objects from GC.
  432. ary = [1, 2, 3]
  433. @ts.write(DRbObject.new(ary))
  434. assert_equal([1, 2, 3], @ts.take([1, 2, 3], 0))
  435. hsh = {'head' => 1, 'tail' => 2}
  436. @ts.write(DRbObject.new(hsh))
  437. assert_equal({'head' => 1, 'tail' => 2},
  438. @ts.take({'head' => 1, 'tail' => 2}, 0))
  439. end
  440. def test_take_bug_8215
  441. skip "this test randomly fails on mswin" if /mswin/ =~ RUBY_PLATFORM
  442. service = DRb.start_service("druby://localhost:0", @ts_base)
  443. uri = service.uri
  444. args = [EnvUtil.rubybin, *%W[-rdrb/drb -rdrb/eq -rrinda/ring -rrinda/tuplespace -e]]
  445. take = spawn(*args, <<-'end;', uri)
  446. uri = ARGV[0]
  447. DRb.start_service("druby://localhost:0")
  448. ro = DRbObject.new_with_uri(uri)
  449. ts = Rinda::TupleSpaceProxy.new(ro)
  450. th = Thread.new do
  451. ts.take([:test_take, nil])
  452. rescue Interrupt
  453. # Expected
  454. end
  455. Kernel.sleep(0.1)
  456. th.raise(Interrupt) # causes loss of the taken tuple
  457. ts.write([:barrier, :continue])
  458. Kernel.sleep
  459. end;
  460. @ts_base.take([:barrier, :continue])
  461. write = spawn(*args, <<-'end;', uri)
  462. uri = ARGV[0]
  463. DRb.start_service("druby://localhost:0")
  464. ro = DRbObject.new_with_uri(uri)
  465. ts = Rinda::TupleSpaceProxy.new(ro)
  466. ts.write([:test_take, 42])
  467. end;
  468. status = Process.wait(write)
  469. assert_equal([[:test_take, 42]], @ts_base.read_all([:test_take, nil]),
  470. '[bug:8215] tuple lost')
  471. ensure
  472. service.stop_service if service
  473. DRb::DRbConn.stop_pool
  474. signal = /mswin|mingw/ =~ RUBY_PLATFORM ? "KILL" : "TERM"
  475. Process.kill(signal, write) if write && status.nil?
  476. Process.kill(signal, take) if take
  477. Process.wait(write) if write && status.nil?
  478. Process.wait(take) if take
  479. end
  480. end
  481. module RingIPv6
  482. def prepare_ipv6(r)
  483. begin
  484. Socket.getifaddrs.each do |ifaddr|
  485. next unless ifaddr.addr
  486. next unless ifaddr.addr.ipv6_linklocal?
  487. next if ifaddr.name[0, 2] == "lo"
  488. r.multicast_interface = ifaddr.ifindex
  489. return ifaddr
  490. end
  491. rescue NotImplementedError
  492. # ifindex() function may not be implemented on Windows.
  493. return if
  494. Socket.ip_address_list.any? { |addrinfo| addrinfo.ipv6? && !addrinfo.ipv6_loopback? }
  495. end
  496. skip 'IPv6 not available'
  497. end
  498. def ipv6_mc(rf, hops = nil)
  499. ifaddr = prepare_ipv6(rf)
  500. rf.multicast_hops = hops if hops
  501. begin
  502. v6mc = rf.make_socket("ff02::1")
  503. rescue Errno::EINVAL
  504. # somehow Debian 6.0.7 needs ifname
  505. v6mc = rf.make_socket("ff02::1%#{ifaddr.name}")
  506. rescue Errno::EADDRNOTAVAIL
  507. return # IPv6 address for multicast not available
  508. rescue Errno::ENETDOWN
  509. return # Network is down
  510. rescue Errno::EHOSTUNREACH
  511. return # Unreachable for some reason
  512. end
  513. begin
  514. yield v6mc
  515. ensure
  516. v6mc.close
  517. end
  518. end
  519. end
  520. class TestRingServer < Test::Unit::TestCase
  521. def setup
  522. @port = Rinda::Ring_PORT
  523. @ts = Rinda::TupleSpace.new
  524. @rs = Rinda::RingServer.new(@ts, [], @port)
  525. @server = DRb.start_service("druby://localhost:0")
  526. end
  527. def teardown
  528. @rs.shutdown
  529. # implementation-dependent
  530. @ts.instance_eval{
  531. if th = @keeper
  532. th.kill
  533. th.join
  534. end
  535. }
  536. @server.stop_service
  537. DRb::DRbConn.stop_pool
  538. end
  539. def test_do_reply
  540. with_timeout(30) {_test_do_reply}
  541. end
  542. def _test_do_reply
  543. called = nil
  544. callback = proc { |ts|
  545. called = ts
  546. }
  547. callback = DRb::DRbObject.new callback
  548. @ts.write [:lookup_ring, callback]
  549. @rs.do_reply
  550. wait_for(30) {called}
  551. assert_same @ts, called
  552. end
  553. def test_do_reply_local
  554. skip 'timeout-based test becomes unstable with --jit-wait' if RubyVM::MJIT.enabled?
  555. with_timeout(30) {_test_do_reply_local}
  556. end
  557. def _test_do_reply_local
  558. called = nil
  559. callback = proc { |ts|
  560. called = ts
  561. }
  562. @ts.write [:lookup_ring, callback]
  563. @rs.do_reply
  564. wait_for(30) {called}
  565. assert_same @ts, called
  566. end
  567. def test_make_socket_unicast
  568. v4 = @rs.make_socket('127.0.0.1')
  569. assert_equal('127.0.0.1', v4.local_address.ip_address)
  570. assert_equal(@port, v4.local_address.ip_port)
  571. end
  572. def test_make_socket_ipv4_multicast
  573. begin
  574. v4mc = @rs.make_socket('239.0.0.1')
  575. rescue Errno::ENOBUFS => e
  576. skip "Missing multicast support in OS: #{e.message}"
  577. end
  578. begin
  579. if Socket.const_defined?(:SO_REUSEPORT) then
  580. assert(v4mc.getsockopt(:SOCKET, :SO_REUSEPORT).bool)
  581. else
  582. assert(v4mc.getsockopt(:SOCKET, :SO_REUSEADDR).bool)
  583. end
  584. rescue TypeError
  585. if /aix/ =~ RUBY_PLATFORM
  586. skip "Known bug in getsockopt(2) on AIX"
  587. end
  588. raise $!
  589. end
  590. assert_equal('0.0.0.0', v4mc.local_address.ip_address)
  591. assert_equal(@port, v4mc.local_address.ip_port)
  592. end
  593. def test_make_socket_ipv6_multicast
  594. skip 'IPv6 not available' unless
  595. Socket.ip_address_list.any? { |addrinfo| addrinfo.ipv6? && !addrinfo.ipv6_loopback? }
  596. begin
  597. v6mc = @rs.make_socket('ff02::1')
  598. rescue Errno::EADDRNOTAVAIL
  599. return # IPv6 address for multicast not available
  600. rescue Errno::ENOBUFS => e
  601. skip "Missing multicast support in OS: #{e.message}"
  602. end
  603. if Socket.const_defined?(:SO_REUSEPORT) then
  604. assert v6mc.getsockopt(:SOCKET, :SO_REUSEPORT).bool
  605. else
  606. assert v6mc.getsockopt(:SOCKET, :SO_REUSEADDR).bool
  607. end
  608. assert_equal('::1', v6mc.local_address.ip_address)
  609. assert_equal(@port, v6mc.local_address.ip_port)
  610. end
  611. def test_ring_server_ipv4_multicast
  612. @rs.shutdown
  613. begin
  614. @rs = Rinda::RingServer.new(@ts, [['239.0.0.1', '0.0.0.0']], @port)
  615. rescue Errno::ENOBUFS => e
  616. skip "Missing multicast support in OS: #{e.message}"
  617. end
  618. v4mc = @rs.instance_variable_get('@sockets').first
  619. begin
  620. if Socket.const_defined?(:SO_REUSEPORT) then
  621. assert(v4mc.getsockopt(:SOCKET, :SO_REUSEPORT).bool)
  622. else
  623. assert(v4mc.getsockopt(:SOCKET, :SO_REUSEADDR).bool)
  624. end
  625. rescue TypeError
  626. if /aix/ =~ RUBY_PLATFORM
  627. skip "Known bug in getsockopt(2) on AIX"
  628. end
  629. raise $!
  630. end
  631. assert_equal('0.0.0.0', v4mc.local_address.ip_address)
  632. assert_equal(@port, v4mc.local_address.ip_port)
  633. end
  634. def test_ring_server_ipv6_multicast
  635. skip 'IPv6 not available' unless
  636. Socket.ip_address_list.any? { |addrinfo| addrinfo.ipv6? && !addrinfo.ipv6_loopback? }
  637. @rs.shutdown
  638. begin
  639. @rs = Rinda::RingServer.new(@ts, [['ff02::1', '::1', 0]], @port)
  640. rescue Errno::EADDRNOTAVAIL
  641. return # IPv6 address for multicast not available
  642. end
  643. v6mc = @rs.instance_variable_get('@sockets').first
  644. if Socket.const_defined?(:SO_REUSEPORT) then
  645. assert v6mc.getsockopt(:SOCKET, :SO_REUSEPORT).bool
  646. else
  647. assert v6mc.getsockopt(:SOCKET, :SO_REUSEADDR).bool
  648. end
  649. assert_equal('::1', v6mc.local_address.ip_address)
  650. assert_equal(@port, v6mc.local_address.ip_port)
  651. end
  652. def test_shutdown
  653. @rs.shutdown
  654. assert_nil(@rs.do_reply, 'otherwise should hang forever')
  655. end
  656. private
  657. def with_timeout(n)
  658. aoe = Thread.abort_on_exception
  659. Thread.abort_on_exception = true
  660. tl0 = Thread.list
  661. tl = nil
  662. th = Thread.new(Thread.current) do |mth|
  663. sleep n
  664. (tl = Thread.list - tl0).each {|t|t.raise(Timeout::Error)}
  665. mth.raise(Timeout::Error)
  666. end
  667. tl0 << th
  668. yield
  669. rescue Timeout::Error => e
  670. $stderr.puts "TestRingServer#with_timeout: timeout in #{n}s:"
  671. $stderr.puts caller
  672. if tl
  673. bt = e.backtrace
  674. tl.each do |t|
  675. begin
  676. t.value
  677. rescue Timeout::Error => e
  678. bt.unshift("")
  679. bt[0, 0] = e.backtrace
  680. end
  681. end
  682. end
  683. raise Timeout::Error, "timeout", bt
  684. ensure
  685. if th
  686. th.kill
  687. th.join
  688. end
  689. Thread.abort_on_exception = aoe
  690. end
  691. def wait_for(n)
  692. t = n + Process.clock_gettime(Process::CLOCK_MONOTONIC, :second)
  693. until yield
  694. if t < Process.clock_gettime(Process::CLOCK_MONOTONIC, :second)
  695. flunk "timeout during waiting call"
  696. end
  697. sleep 0.1
  698. end
  699. end
  700. end
  701. class TestRingFinger < Test::Unit::TestCase
  702. include RingIPv6
  703. def setup
  704. @rf = Rinda::RingFinger.new
  705. end
  706. def test_make_socket_unicast
  707. v4 = @rf.make_socket('127.0.0.1')
  708. assert(v4.getsockopt(:SOL_SOCKET, :SO_BROADCAST).bool)
  709. rescue TypeError
  710. if /aix/ =~ RUBY_PLATFORM
  711. skip "Known bug in getsockopt(2) on AIX"
  712. end
  713. raise $!
  714. ensure
  715. v4.close if v4
  716. end
  717. def test_make_socket_ipv4_multicast
  718. v4mc = @rf.make_socket('239.0.0.1')
  719. assert_equal(1, v4mc.getsockopt(:IPPROTO_IP, :IP_MULTICAST_LOOP).ipv4_multicast_loop)
  720. assert_equal(1, v4mc.getsockopt(:IPPROTO_IP, :IP_MULTICAST_TTL).ipv4_multicast_ttl)
  721. ensure
  722. v4mc.close if v4mc
  723. end
  724. def test_make_socket_ipv6_multicast
  725. ipv6_mc(@rf) do |v6mc|
  726. assert_equal(1, v6mc.getsockopt(:IPPROTO_IPV6, :IPV6_MULTICAST_LOOP).int)
  727. assert_equal(1, v6mc.getsockopt(:IPPROTO_IPV6, :IPV6_MULTICAST_HOPS).int)
  728. end
  729. end
  730. def test_make_socket_ipv4_multicast_hops
  731. @rf.multicast_hops = 2
  732. v4mc = @rf.make_socket('239.0.0.1')
  733. assert_equal(2, v4mc.getsockopt(:IPPROTO_IP, :IP_MULTICAST_TTL).ipv4_multicast_ttl)
  734. ensure
  735. v4mc.close if v4mc
  736. end
  737. def test_make_socket_ipv6_multicast_hops
  738. ipv6_mc(@rf, 2) do |v6mc|
  739. assert_equal(2, v6mc.getsockopt(:IPPROTO_IPV6, :IPV6_MULTICAST_HOPS).int)
  740. end
  741. end
  742. end
  743. end