PageRenderTime 58ms CodeModel.GetById 21ms RepoModel.GetById 0ms app.codeStats 1ms

/test/rinda/test_rinda.rb

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