PageRenderTime 47ms CodeModel.GetById 19ms RepoModel.GetById 0ms app.codeStats 0ms

/test/drb/test_drb.rb

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