PageRenderTime 61ms CodeModel.GetById 28ms RepoModel.GetById 1ms app.codeStats 0ms

/test/rinda/test_rinda.rb

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