PageRenderTime 56ms CodeModel.GetById 31ms RepoModel.GetById 1ms app.codeStats 0ms

/test/mri/drb/test_drb.rb

http://github.com/jruby/jruby
Ruby | 358 lines | 290 code | 67 blank | 1 comment | 1 complexity | 2c226366f0e37173197a5423941fb4e7 MD5 | raw file
Possible License(s): GPL-3.0, BSD-3-Clause, GPL-2.0, JSON, LGPL-2.1
  1. # frozen_string_literal: false
  2. require_relative 'drbtest'
  3. module DRbTests
  4. class TestDRbCore < Test::Unit::TestCase
  5. include DRbCore
  6. def setup
  7. setup_service 'ut_drb.rb'
  8. super
  9. end
  10. def teardown
  11. super
  12. DRbService.finish
  13. end
  14. end
  15. class TestDRbYield < Test::Unit::TestCase
  16. include DRbBase
  17. def setup
  18. setup_service 'ut_drb.rb'
  19. super
  20. end
  21. def teardown
  22. super
  23. DRbService.finish
  24. end
  25. def test_01_one
  26. @there.echo_yield_1([]) {|one|
  27. assert_equal([], one)
  28. }
  29. @there.echo_yield_1(1) {|one|
  30. assert_equal(1, one)
  31. }
  32. @there.echo_yield_1(nil) {|one|
  33. assert_equal(nil, one)
  34. }
  35. end
  36. def test_02_two
  37. @there.echo_yield_2([], []) {|one, two|
  38. assert_equal([], one)
  39. assert_equal([], two)
  40. }
  41. @there.echo_yield_2(1, 2) {|one, two|
  42. assert_equal(1, one)
  43. assert_equal(2, two)
  44. }
  45. @there.echo_yield_2(3, nil) {|one, two|
  46. assert_equal(3, one)
  47. assert_equal(nil, two)
  48. }
  49. @there.echo_yield_1([:key, :value]) {|one, two|
  50. assert_equal(:key, one)
  51. assert_equal(:value, two)
  52. }
  53. end
  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. def test_04_many_to_one
  78. @there.echo_yield_0 {|*s|
  79. assert_equal([], s)
  80. }
  81. @there.echo_yield(nil) {|*s|
  82. assert_equal([nil], s)
  83. }
  84. @there.echo_yield(1) {|*s|
  85. assert_equal([1], s)
  86. }
  87. @there.echo_yield(1, 2) {|*s|
  88. assert_equal([1, 2], s)
  89. }
  90. @there.echo_yield(1, 2, 3) {|*s|
  91. assert_equal([1, 2, 3], s)
  92. }
  93. @there.echo_yield([], []) {|*s|
  94. assert_equal([[], []], s)
  95. }
  96. @there.echo_yield([]) {|*s|
  97. assert_equal([[]], s)
  98. }
  99. end
  100. def test_05_array_subclass
  101. @there.xarray_each {|x| assert_kind_of(XArray, x)}
  102. @there.xarray_each {|*x| assert_kind_of(XArray, x[0])}
  103. end
  104. def test_06_taint
  105. x = proc {}
  106. assert(! x.tainted?)
  107. @there.echo_yield(x) {|o|
  108. assert_equal(x, o)
  109. assert(! x.tainted?)
  110. }
  111. end
  112. end
  113. class TestDRbRubyYield < TestDRbYield
  114. def echo_yield(*arg)
  115. yield(*arg)
  116. end
  117. def echo_yield_0
  118. yield
  119. end
  120. def echo_yield_1(a)
  121. yield(a)
  122. end
  123. def echo_yield_2(a, b)
  124. yield(a, b)
  125. end
  126. def xarray_each
  127. xary = [XArray.new([0])]
  128. xary.each do |x|
  129. yield(x)
  130. end
  131. end
  132. def setup
  133. @there = self
  134. end
  135. def teardown
  136. end
  137. end
  138. class TestDRbRuby18Yield < TestDRbRubyYield
  139. class YieldTest18
  140. def echo_yield(*arg, &proc)
  141. proc.call(*arg)
  142. end
  143. def echo_yield_0(&proc)
  144. proc.call
  145. end
  146. def echo_yield_1(a, &proc)
  147. proc.call(a)
  148. end
  149. def echo_yield_2(a, b, &proc)
  150. proc.call(a, b)
  151. end
  152. def xarray_each(&proc)
  153. xary = [XArray.new([0])]
  154. xary.each(&proc)
  155. end
  156. end
  157. def setup
  158. @there = YieldTest18.new
  159. end
  160. end
  161. class TestDRbAry < Test::Unit::TestCase
  162. include DRbAry
  163. def setup
  164. setup_service 'ut_array.rb'
  165. super
  166. end
  167. def teardown
  168. super
  169. DRbService.finish
  170. end
  171. end
  172. class TestDRbMServer < Test::Unit::TestCase
  173. include DRbBase
  174. def setup
  175. setup_service 'ut_drb.rb'
  176. super
  177. @server = (1..3).collect do |n|
  178. DRb::DRbServer.new("druby://localhost:0", Onecky.new(n.to_s))
  179. end
  180. end
  181. def teardown
  182. @server.each do |s|
  183. s.stop_service
  184. end
  185. super
  186. DRbService.finish
  187. end
  188. def test_01
  189. assert_equal(6, @there.sample(@server[0].front, @server[1].front, @server[2].front))
  190. end
  191. end
  192. class TestDRbSafe1 < TestDRbAry
  193. def setup
  194. setup_service 'ut_safe1.rb'
  195. end
  196. def teardown
  197. super
  198. DRbService.finish
  199. end
  200. end
  201. class TestDRbEval # < Test::Unit::TestCase
  202. def setup
  203. super
  204. @ext = DRbService.ext_service('ut_eval.rb')
  205. @there = @ext.front
  206. end
  207. def teardown
  208. @ext.stop_service if @ext
  209. end
  210. def test_01_safe1_safe4_eval
  211. assert_raise(SecurityError) do
  212. @there.method_missing(:instance_eval, 'ENV.inspect')
  213. end
  214. assert_raise(SecurityError) do
  215. @there.method_missing(:send, :eval, 'ENV.inspect')
  216. end
  217. remote_class = @there.remote_class
  218. assert_raise(SecurityError) do
  219. remote_class.class_eval('ENV.inspect')
  220. end
  221. assert_raise(SecurityError) do
  222. remote_class.module_eval('ENV.inspect')
  223. end
  224. four = @there.four
  225. assert_equal(1, four.method_missing(:send, :eval, '1'))
  226. remote_class = four.remote_class
  227. assert_equal(1, remote_class.class_eval('1'))
  228. assert_equal(1, remote_class.module_eval('1'))
  229. assert_raise(SecurityError) do
  230. remote_class.class_eval('ENV = {}')
  231. end
  232. assert_raise(SecurityError) do
  233. remote_class.module_eval('ENV = {}')
  234. end
  235. end
  236. end
  237. class TestDRbLarge < Test::Unit::TestCase
  238. include DRbBase
  239. def setup
  240. setup_service 'ut_large.rb'
  241. super
  242. end
  243. def teardown
  244. super
  245. DRbService.finish
  246. end
  247. def test_01_large_ary
  248. ary = [2] * 10240
  249. assert_equal(10240, @there.size(ary))
  250. assert_equal(20480, @there.sum(ary))
  251. end
  252. def test_02_large_ary
  253. ary = ["Hello, World"] * 10240
  254. assert_equal(10240, @there.size(ary))
  255. end
  256. def test_03_large_ary
  257. ary = [Thread.current] * 10240
  258. assert_equal(10240, @there.size(ary))
  259. end
  260. def test_04_many_arg
  261. assert_raise(DRb::DRbConnError) {
  262. @there.arg_test(1, 2, 3, 4, 5, 6, 7, 8, 9, 0)
  263. }
  264. end
  265. def test_05_too_large_ary
  266. ary = ["Hello, World"] * 102400
  267. exception = nil
  268. begin
  269. @there.size(ary)
  270. rescue StandardError
  271. exception = $!
  272. end
  273. assert_kind_of(StandardError, exception)
  274. end
  275. end
  276. class TestBug4409 < Test::Unit::TestCase
  277. include DRbBase
  278. def setup
  279. setup_service 'ut_eq.rb'
  280. super
  281. end
  282. def teardown
  283. super
  284. DRbService.finish
  285. end
  286. def test_bug4409
  287. foo = @there.foo
  288. assert(@there.foo?(foo))
  289. end
  290. end
  291. end