PageRenderTime 44ms CodeModel.GetById 13ms RepoModel.GetById 0ms app.codeStats 1ms

/test/functional/ft_25_receiver.rb

http://github.com/jmettraux/ruote
Ruby | 417 lines | 279 code | 122 blank | 16 comment | 4 complexity | 0cc95fb618ab543a192982dcc4cb2ef6 MD5 | raw file
  1. #
  2. # testing ruote
  3. #
  4. # Wed Aug 12 23:24:16 JST 2009
  5. #
  6. require File.expand_path('../base', __FILE__)
  7. require 'ruote/participant'
  8. class FtReceiverTest < Test::Unit::TestCase
  9. include FunctionalBase
  10. def setup
  11. super
  12. @pdef = Ruote.process_definition :name => 'test' do
  13. sequence do
  14. alpha
  15. echo '.'
  16. end
  17. end
  18. @dashboard.register_participant 'alpha', MyParticipant
  19. end
  20. class MyParticipant < Ruote::Participant
  21. def on_workitem
  22. @context.stash[:wi] = workitem
  23. # no reply to the engine
  24. end
  25. # do not let the dispatch happen in its own thread, this makes
  26. # wait_for(:alpha) synchronous.
  27. #
  28. def do_not_thread
  29. true
  30. end
  31. end
  32. class MyReceiver < Ruote::Receiver
  33. attr_reader :context
  34. end
  35. def test_my_receiver_init
  36. cid = @dashboard.context.object_id
  37. receiver = MyReceiver.new(@dashboard)
  38. assert_equal cid, receiver.context.object_id
  39. assert_not_nil receiver.context.storage
  40. receiver = MyReceiver.new(@dashboard.context)
  41. assert_equal cid, receiver.context.object_id
  42. assert_not_nil receiver.context.storage
  43. receiver = MyReceiver.new(@dashboard.worker)
  44. assert_equal cid, receiver.context.object_id
  45. assert_not_nil receiver.context.storage
  46. receiver = MyReceiver.new(@dashboard.storage)
  47. assert_equal cid, receiver.context.object_id
  48. assert_not_nil receiver.context.storage
  49. @dashboard.storage.instance_variable_set(:@context, nil)
  50. receiver = MyReceiver.new(@dashboard.storage)
  51. assert_not_equal cid, receiver.context.object_id
  52. assert_not_nil receiver.context.storage
  53. end
  54. def test_my_receiver
  55. receiver = MyReceiver.new(@dashboard.context)
  56. wfid = @dashboard.launch(@pdef)
  57. wait_for(:alpha)
  58. while @dashboard.context.stash[:wi].nil? do
  59. Thread.pass
  60. end
  61. assert_equal 3, @dashboard.process(wfid).expressions.size
  62. receiver.receive(@dashboard.context.stash[:wi])
  63. wait_for(wfid)
  64. assert_nil @dashboard.process(wfid)
  65. rcv = logger.log.select { |e| e['action'] == 'receive' }.first
  66. assert_equal 'FtReceiverTest::MyReceiver', rcv['receiver']
  67. end
  68. def test_engine_receive
  69. wfid = @dashboard.launch(@pdef)
  70. wait_for(:alpha)
  71. @dashboard.receive(@dashboard.context.stash[:wi])
  72. wait_for(wfid)
  73. assert_nil @dashboard.process(wfid)
  74. rcv = logger.log.select { |e| e['action'] == 'receive' }.first
  75. assert_equal 'Ruote::Dashboard', rcv['receiver']
  76. end
  77. class MyOtherParticipant < Ruote::Participant
  78. def on_workitem
  79. @context.receiver.pass(workitem.to_h)
  80. end
  81. end
  82. class MyOtherReceiver < Ruote::Receiver
  83. def initialize(context, opts={})
  84. super(context, opts)
  85. @count = 0
  86. end
  87. def pass(workitem)
  88. if @count < 1
  89. @context.error_handler.action_handle(
  90. 'dispatch', workitem['fei'], RuntimeError.new('something went wrong'))
  91. else
  92. reply(workitem)
  93. end
  94. @count = @count + 1
  95. end
  96. end
  97. def test_receiver_triggered_dispatch_error
  98. class << @dashboard.context
  99. def receiver
  100. @rcv ||= MyOtherReceiver.new(engine)
  101. end
  102. end
  103. @dashboard.register_participant :alpha, MyOtherParticipant
  104. pdef = Ruote.process_definition do
  105. alpha
  106. end
  107. wfid = @dashboard.launch(pdef)
  108. wait_for(wfid)
  109. ps = @dashboard.process(wfid)
  110. err = ps.errors.first
  111. assert_equal 2, ps.expressions.size
  112. assert_equal 1, ps.errors.size
  113. assert_equal '#<RuntimeError: something went wrong>', err.message
  114. assert_equal String, err.msg['put_at'].class
  115. @dashboard.replay_at_error(err)
  116. wait_for(wfid)
  117. ps = @dashboard.process(wfid)
  118. assert_nil ps
  119. end
  120. def test_receiver_fexp_and_wi
  121. @dashboard.register_participant :alpha, Ruote::StorageParticipant
  122. wfid = @dashboard.launch(Ruote.define do
  123. alpha
  124. end)
  125. @dashboard.wait_for('dispatched')
  126. wi = @dashboard.storage_participant.first
  127. assert_equal wfid, wi.fei.wfid
  128. assert_equal wfid, @dashboard.fexp(wi).fei.wfid
  129. assert_equal wfid, @dashboard.fexp(wi.fei).fei.wfid
  130. assert_equal wfid, @dashboard.fexp(wi.fei.sid).fei.wfid
  131. assert_equal wfid, @dashboard.fexp(wi.fei.sid).h.applied_workitem['fei']['wfid']
  132. assert_equal wfid, @dashboard.workitem(wi).wfid
  133. assert_equal wfid, @dashboard.workitem(wi.fei).wfid
  134. assert_equal wfid, @dashboard.workitem(wi.fei.sid).wfid
  135. end
  136. class FlunkParticipant < Ruote::Participant
  137. # Since Participant extends ReceiverMixin, we can call #flunk
  138. #
  139. def on_workitem
  140. flunk(workitem, ArgumentError, 'out of order')
  141. end
  142. end
  143. def test_flunk
  144. @dashboard.register :alpha, FlunkParticipant
  145. wfid =
  146. @dashboard.launch(Ruote.define do
  147. alpha
  148. end)
  149. r = @dashboard.wait_for(wfid)
  150. assert_equal 'error_intercepted', r['action']
  151. assert_equal 'ArgumentError', r['error']['class']
  152. assert_equal 'out of order', r['error']['message']
  153. assert_match __FILE__, r['error']['trace'].first
  154. ps = @dashboard.ps(wfid)
  155. assert_equal String, ps.errors.first.at.class
  156. end
  157. class StringFlunkParticipant < Ruote::Participant
  158. def on_workitem
  159. flunk(workitem, 'out of order')
  160. end
  161. end
  162. def test_string_flunk
  163. @dashboard.register :alpha, StringFlunkParticipant
  164. wfid =
  165. @dashboard.launch(Ruote.define do
  166. alpha
  167. end)
  168. r = @dashboard.wait_for(wfid)
  169. assert_equal 'error_intercepted', r['action']
  170. assert_equal 'RuntimeError', r['error']['class']
  171. assert_equal 'out of order', r['error']['message']
  172. assert_match __FILE__, r['error']['trace'].first
  173. ps = @dashboard.ps(wfid)
  174. assert_equal String, ps.errors.first.at.class
  175. end
  176. class BacktraceFlunkParticipant < Ruote::Participant
  177. def on_workitem
  178. flunk(workitem, ArgumentError, 'nada', %w[ aaa bbb ccc ])
  179. end
  180. end
  181. def test_backtrace_flunk
  182. @dashboard.register :alpha, BacktraceFlunkParticipant
  183. wfid =
  184. @dashboard.launch(Ruote.define do
  185. alpha
  186. end)
  187. r = @dashboard.wait_for(wfid)
  188. assert_equal 'error_intercepted', r['action']
  189. assert_equal 'ArgumentError', r['error']['class']
  190. assert_equal 'nada', r['error']['message']
  191. assert_equal %w[ aaa bbb ccc ], r['error']['trace']
  192. ps = @dashboard.ps(wfid)
  193. assert_equal String, ps.errors.first.at.class
  194. end
  195. class ExceptionInstanceFlunkParticipant < Ruote::Participant
  196. def on_workitem
  197. flunk(workitem, RuntimeError.new('out of order'))
  198. end
  199. end
  200. def test_exception_instance_flunk
  201. @dashboard.register :alpha, ExceptionInstanceFlunkParticipant
  202. wfid =
  203. @dashboard.launch(Ruote.define do
  204. alpha
  205. end)
  206. r = @dashboard.wait_for(wfid)
  207. assert_equal 'error_intercepted', r['action']
  208. assert_equal 'RuntimeError', r['error']['class']
  209. assert_equal 'out of order', r['error']['message']
  210. assert_match __FILE__, r['error']['trace'].first
  211. ps = @dashboard.ps(wfid)
  212. assert_equal String, ps.errors.first.at.class
  213. end
  214. class NonInstantiationFlunkParticipant < Ruote::Participant
  215. def on_workitem
  216. #flunk(
  217. # workitem, 'SomeUnknownConstant', 'out of order', [ 'some backtrace' ])
  218. #
  219. # Rather
  220. #
  221. flunk(
  222. workitem,
  223. Ruote::ReceivedError.new('SomeConstant', 'out of order', [ 'trace' ]))
  224. end
  225. end
  226. def test_non_instantiation_flunk
  227. @dashboard.register :alpha, NonInstantiationFlunkParticipant
  228. wfid =
  229. @dashboard.launch(Ruote.define do
  230. alpha
  231. end)
  232. r = @dashboard.wait_for(wfid)
  233. assert_equal 'error_intercepted', r['action']
  234. assert_equal 'Ruote::ReceivedError', r['error']['class']
  235. assert_equal 'SomeConstant: out of order', r['error']['message']
  236. assert_match 'trace', r['error']['trace'].first
  237. ps = @dashboard.ps(wfid)
  238. err = ps.errors.first
  239. assert_equal String, err.at.class
  240. assert_equal [ 'SomeConstant', 'out of order' ], err.details
  241. end
  242. class AutoInstantiationFlunkParticipant < Ruote::Participant
  243. def on_workitem
  244. flunk(
  245. workitem, 'ArgumentError', 'out of order', [ 'some backtrace' ])
  246. end
  247. end
  248. def test_auto_instantiation_flunk
  249. @dashboard.register :alpha, AutoInstantiationFlunkParticipant
  250. wfid =
  251. @dashboard.launch(Ruote.define do
  252. alpha
  253. end)
  254. r = @dashboard.wait_for(wfid)
  255. assert_equal 'error_intercepted', r['action']
  256. assert_equal 'ArgumentError', r['error']['class']
  257. assert_equal 'out of order', r['error']['message']
  258. assert_match 'trace', r['error']['trace'].first
  259. ps = @dashboard.ps(wfid)
  260. assert_equal String, ps.errors.first.at.class
  261. end
  262. class ::MultipleArgumentsError < RuntimeError
  263. def initialize(a, b)
  264. @a = a
  265. @b = b
  266. end
  267. def message
  268. "#{@a} #{@b}"
  269. end
  270. end
  271. class MultipleArgumentsFlunkParticipant < Ruote::Participant
  272. def on_workitem
  273. flunk(
  274. workitem,
  275. MultipleArgumentsError,
  276. 'first',
  277. 'second',
  278. [ 'some backtrace' ])
  279. end
  280. end
  281. def test_multiple_arguments_flunk
  282. @dashboard.register :alpha, MultipleArgumentsFlunkParticipant
  283. wfid =
  284. @dashboard.launch(Ruote.define do
  285. alpha
  286. end)
  287. r = @dashboard.wait_for(wfid)
  288. assert_equal 'error_intercepted', r['action']
  289. assert_equal 'MultipleArgumentsError', r['error']['class']
  290. assert_equal 'first second', r['error']['message']
  291. assert_match 'some backtrace', r['error']['trace'].first
  292. ps = @dashboard.ps(wfid)
  293. assert_equal String, ps.errors.first.at.class
  294. end
  295. end