PageRenderTime 30ms CodeModel.GetById 20ms RepoModel.GetById 0ms app.codeStats 0ms

/test/functional/ft_3_participant_registration.rb

http://github.com/jmettraux/ruote
Ruby | 590 lines | 406 code | 154 blank | 30 comment | 1 complexity | 430e1596074f238331b2a39995e53bd8 MD5 | raw file
  1. #
  2. # testing ruote
  3. #
  4. # Mon May 18 22:25:57 JST 2009
  5. #
  6. require File.expand_path('../base', __FILE__)
  7. require 'ruote'
  8. class FtParticipantRegistrationTest < Test::Unit::TestCase
  9. include FunctionalBase
  10. def test_participant_register
  11. @dashboard.register_participant :alpha do |workitem|
  12. tracer << 'alpha'
  13. end
  14. @dashboard.register_participant /^user_/, Ruote::NullParticipant
  15. wait_for(2)
  16. assert_equal(
  17. 'participant_registered',
  18. logger.log[0]['action'])
  19. assert_equal(
  20. %w[ alpha /^user_/ ],
  21. logger.log.collect { |msg| msg['regex'] })
  22. assert_equal(
  23. [ [ '^alpha$',
  24. [ 'Ruote::BlockParticipant',
  25. { 'on_workitem' => "proc do |workitem|\n tracer << 'alpha'\n end" } ] ],
  26. [ '^user_',
  27. [ 'Ruote::NullParticipant',
  28. {} ] ] ],
  29. @dashboard.participant_list.collect { |pe| pe.to_a })
  30. end
  31. def test_participant_register_position
  32. @dashboard.register_participant :ur, Ruote::StorageParticipant
  33. assert_equal(
  34. %w[ ^ur$ ],
  35. @dashboard.participant_list.collect { |pe| pe.regex.to_s })
  36. @dashboard.register_participant(
  37. :first, Ruote::StorageParticipant, :position => :first)
  38. @dashboard.register_participant(
  39. :last, Ruote::StorageParticipant, :position => :last)
  40. assert_equal(
  41. %w[ ^first$ ^ur$ ^last$ ],
  42. @dashboard.participant_list.collect { |pe| pe.regex.to_s })
  43. @dashboard.register_participant(
  44. :x, Ruote::StorageParticipant, :position => -2)
  45. assert_equal(
  46. %w[ ^first$ ^ur$ ^x$ ^last$ ],
  47. @dashboard.participant_list.collect { |pe| pe.regex.to_s })
  48. end
  49. def test_participant_register_before
  50. @dashboard.register_participant :alpha, 'AlphaParticipant'
  51. @dashboard.register_participant :bravo, 'BravoParticipant'
  52. @dashboard.register_participant :alpha, 'AlphaPrimeParticipant', :pos => :after
  53. assert_equal(
  54. [ %w[ ^alpha$ AlphaParticipant ],
  55. %w[ ^alpha$ AlphaPrimeParticipant ],
  56. %w[ ^bravo$ BravoParticipant ] ],
  57. @dashboard.participant_list.collect { |e| [ e.regex, e.classname ] })
  58. end
  59. def test_participant_register_after
  60. @dashboard.register_participant :alpha, 'AlphaParticipant'
  61. @dashboard.register_participant :alpha, 'AlphaPrimeParticipant', :pos => :before
  62. assert_equal(
  63. [ %w[ ^alpha$ AlphaPrimeParticipant ],
  64. %w[ ^alpha$ AlphaParticipant ] ],
  65. @dashboard.participant_list.collect { |e| [ e.regex, e.classname ] })
  66. end
  67. def test_participant_register_before_after_corner_cases
  68. @dashboard.register_participant :alpha, 'KlassA', :pos => :before
  69. @dashboard.register_participant :bravo, 'KlassB', :pos => :after
  70. assert_equal(
  71. [ %w[ ^alpha$ KlassA ],
  72. %w[ ^bravo$ KlassB ] ],
  73. @dashboard.participant_list.collect { |e| [ e.regex, e.classname ] })
  74. end
  75. def test_participant_register_over
  76. @dashboard.register_participant :alpha, 'KlassA'
  77. @dashboard.register_participant :bravo, 'KlassB'
  78. @dashboard.register_participant :alpha, 'KlassAa', :pos => :over
  79. @dashboard.register_participant :charly, 'KlassC', :pos => :over
  80. assert_equal(
  81. [ %w[ ^alpha$ KlassAa ],
  82. %w[ ^bravo$ KlassB ],
  83. %w[ ^charly$ KlassC ] ],
  84. @dashboard.participant_list.collect { |e| [ e.regex, e.classname ] })
  85. end
  86. def test_double_registration
  87. @dashboard.register_participant :alpha do |workitem|
  88. tracer << 'alpha'
  89. end
  90. @dashboard.register_participant :alpha do |workitem|
  91. tracer << 'alpha'
  92. end
  93. assert_equal 1, @dashboard.context.plist.send(:get_list)['list'].size
  94. end
  95. def test_register_and_return_something
  96. pa = @dashboard.register_participant :alpha do |workitem|
  97. end
  98. pb = @dashboard.register_participant :bravo, Ruote::StorageParticipant
  99. assert_nil pa
  100. assert_equal Ruote::StorageParticipant, pb.class
  101. end
  102. def test_participant_unregister_by_name
  103. @dashboard.register_participant :alpha do |workitem|
  104. end
  105. @dashboard.unregister_participant(:alpha)
  106. wait_for(2)
  107. Thread.pass
  108. msg = logger.log.last
  109. assert_equal 'participant_unregistered', msg['action']
  110. assert_equal '^alpha$', msg['regex']
  111. end
  112. def test_participant_unregister
  113. @dashboard.register_participant :alpha do |workitem|
  114. end
  115. @dashboard.unregister_participant('alpha')
  116. wait_for(2)
  117. msg = logger.log.last
  118. assert_equal 'participant_unregistered', msg['action']
  119. assert_equal '^alpha$', msg['regex']
  120. assert_equal 0, @dashboard.context.plist.list.size
  121. end
  122. class MyParticipant
  123. @@down = false
  124. def self.down
  125. @@down
  126. end
  127. def initialize
  128. end
  129. def shutdown
  130. @@down = true
  131. end
  132. end
  133. def test_participant_shutdown
  134. alpha = @dashboard.register :alpha, MyParticipant
  135. @dashboard.context.plist.shutdown
  136. assert_equal true, MyParticipant.down
  137. end
  138. def test_participant_list_of_names
  139. pa = @dashboard.register_participant :alpha do |workitem|
  140. end
  141. assert_equal [ '^alpha$' ], @dashboard.context.plist.names
  142. end
  143. def test_register_require_path
  144. rpath = File.expand_path(
  145. "../#{Time.now.to_f}_#{$$}_required_participant", __FILE__)
  146. path = "#{rpath}.rb"
  147. File.open(path, 'wb') do |f|
  148. f.write(%{
  149. class RequiredParticipant
  150. include Ruote::LocalParticipant
  151. def initialize(opts)
  152. @opts = opts
  153. end
  154. def consume(workitem)
  155. workitem.fields['message'] = @opts['message']
  156. reply(workitem)
  157. end
  158. end
  159. })
  160. end
  161. @dashboard.register_participant(
  162. :alfred,
  163. 'RequiredParticipant',
  164. :require_path => rpath, :message => 'hello')
  165. assert_equal [ '^alfred$' ], @dashboard.context.plist.names
  166. # first run
  167. assert_equal(
  168. [ 'RequiredParticipant',
  169. { 'require_path' => rpath, 'message' => 'hello' } ],
  170. @dashboard.context.plist.lookup_info('alfred', nil))
  171. wfid = @dashboard.launch(Ruote.define { alfred })
  172. r = @dashboard.wait_for(wfid)
  173. assert_equal 'hello', r['workitem']['fields']['message']
  174. # second run
  175. File.open(path, 'wb') do |f|
  176. f.write(%{
  177. class RequiredParticipant
  178. include Ruote::LocalParticipant
  179. def initialize(opts)
  180. @opts = opts
  181. end
  182. def consume(workitem)
  183. workitem.fields['message'] = 'second run'
  184. reply(workitem)
  185. end
  186. end
  187. })
  188. end
  189. wfid = @dashboard.launch(Ruote.define { alfred })
  190. r = @dashboard.wait_for(wfid)
  191. # since it's a 'require', the code isn't reloaded
  192. assert_equal 'hello', r['workitem']['fields']['message']
  193. FileUtils.rm(path)
  194. end
  195. def test_reqister_load_path
  196. path = File.join(
  197. File.dirname(__FILE__), "#{Time.now.to_f}_#{$$}_loaded_participant.rb")
  198. File.open(path, 'wb') do |f|
  199. f.write(%{
  200. class LoadedParticipant
  201. include Ruote::LocalParticipant
  202. def initialize(opts)
  203. @opts = opts
  204. end
  205. def consume(workitem)
  206. workitem.fields['message'] = @opts['message']
  207. reply(workitem)
  208. end
  209. end
  210. })
  211. end
  212. @dashboard.register_participant(
  213. :alfred,
  214. 'LoadedParticipant',
  215. :load_path => path, :message => 'bondzoi')
  216. assert_equal [ '^alfred$' ], @dashboard.context.plist.names
  217. # first run
  218. assert_equal(
  219. [ 'LoadedParticipant',
  220. { 'load_path' => path, 'message' => 'bondzoi' } ],
  221. @dashboard.context.plist.lookup_info('alfred', nil))
  222. wfid = @dashboard.launch(Ruote.define { alfred })
  223. r = @dashboard.wait_for(wfid)
  224. assert_equal 'bondzoi', r['workitem']['fields']['message']
  225. # second run
  226. File.open(path, 'wb') do |f|
  227. f.write(%{
  228. class LoadedParticipant
  229. include Ruote::LocalParticipant
  230. def initialize(opts)
  231. @opts = opts
  232. end
  233. def consume(workitem)
  234. workitem.fields['message'] = 'second run'
  235. reply(workitem)
  236. end
  237. end
  238. })
  239. end
  240. wfid = @dashboard.launch(Ruote.define { alfred })
  241. r = @dashboard.wait_for(wfid)
  242. # since it's a 'load', the code is reloaded
  243. assert_equal 'second run', r['workitem']['fields']['message']
  244. FileUtils.rm(path)
  245. end
  246. def test_participant_list
  247. @dashboard.register_participant 'alpha', Ruote::StorageParticipant
  248. #assert_equal(
  249. # [ '/^alpha$/ ==> Ruote::StorageParticipant {}' ],
  250. # @dashboard.participant_list.collect { |pe| pe.to_s })
  251. plist = @dashboard.participant_list
  252. assert_equal 1, plist.size
  253. assert_equal '^alpha$', plist.first.regex
  254. assert_equal 'Ruote::StorageParticipant', plist.first.classname
  255. # launching a process with a missing participant
  256. wfid = @dashboard.launch(Ruote.define { bravo })
  257. @dashboard.wait_for(wfid)
  258. assert_equal 1, @dashboard.process(wfid).errors.size
  259. # fixing the error by updating the participant list
  260. list = @dashboard.participant_list
  261. list.first.regex = '^.+$' # instead of '^alpha$'
  262. @dashboard.participant_list = list
  263. # replay at error
  264. @dashboard.replay_at_error(@dashboard.process(wfid).errors.first)
  265. @dashboard.wait_for(:bravo)
  266. # bravo should hold a workitem
  267. assert_equal 1, @dashboard.storage_participant.size
  268. assert_equal 'bravo', @dashboard.storage_participant.first.participant_name
  269. end
  270. def test_participant_list_update
  271. @dashboard.register_participant 'alpha', Ruote::StorageParticipant
  272. #assert_equal(
  273. # [ '/^alpha$/ ==> Ruote::StorageParticipant {}' ],
  274. # @dashboard.participant_list.collect { |pe| pe.to_s })
  275. plist = @dashboard.participant_list
  276. assert_equal 1, plist.size
  277. assert_equal '^alpha$', plist.first.regex
  278. assert_equal 'Ruote::StorageParticipant', plist.first.classname
  279. # 0
  280. @dashboard.participant_list = [
  281. { 'regex' => '^bravo$',
  282. 'classname' => 'Ruote::StorageParticipant',
  283. 'options' => {} },
  284. { 'regex' => '^charly$',
  285. 'classname' => 'Ruote::StorageParticipant',
  286. 'options' => {} }
  287. ]
  288. #assert_equal(
  289. # [
  290. # '/^bravo$/ ==> Ruote::StorageParticipant {}',
  291. # '/^charly$/ ==> Ruote::StorageParticipant {}'
  292. # ],
  293. # @dashboard.participant_list.collect { |pe| pe.to_s })
  294. plist = @dashboard.participant_list
  295. assert_equal 2, plist.size
  296. assert_equal '^bravo$', plist.first.regex
  297. assert_equal '^charly$', plist.last.regex
  298. assert_equal 'Ruote::StorageParticipant', plist.first.classname
  299. assert_equal 'Ruote::StorageParticipant', plist.last.classname
  300. # 1
  301. @dashboard.participant_list = [
  302. [ '^charly$', [ 'Ruote::StorageParticipant', {} ] ],
  303. [ '^bravo$', [ 'Ruote::StorageParticipant', {} ] ]
  304. ]
  305. plist = @dashboard.participant_list
  306. assert_equal 2, plist.size
  307. assert_equal '^charly$', plist.first.regex
  308. assert_equal '^bravo$', plist.last.regex
  309. assert_equal 'Ruote::StorageParticipant', plist.first.classname
  310. assert_equal 'Ruote::StorageParticipant', plist.last.classname
  311. # 2
  312. @dashboard.participant_list = [
  313. [ '^delta$', Ruote::StorageParticipant, {} ],
  314. [ '^echo$', 'Ruote::StorageParticipant', {} ]
  315. ]
  316. plist = @dashboard.participant_list
  317. assert_equal 2, plist.size
  318. assert_equal '^delta$', plist.first.regex
  319. assert_equal '^echo$', plist.last.regex
  320. assert_equal 'Ruote::StorageParticipant', plist.first.classname
  321. assert_equal 'Ruote::StorageParticipant', plist.last.classname
  322. end
  323. class ParticipantCharlie; end
  324. def test_register_block
  325. @dashboard.register do
  326. alpha 'Participants::Alpha', 'flavour' => 'vanilla'
  327. participant 'bravo', 'Participants::Bravo', :flavour => 'peach'
  328. participant 'charlie', 'Participants::Charlie'
  329. participant 'david' do |wi|
  330. p wi
  331. end
  332. catchall 'Participants::Zebda', 'flavour' => 'coconut'
  333. end
  334. assert_equal 5, @dashboard.participant_list.size
  335. assert_equal(
  336. %w[ ^alpha$ ^bravo$ ^charlie$ ^david$ ^.+$ ],
  337. @dashboard.participant_list.collect { |pe| pe.regex.to_s })
  338. assert_equal(
  339. %w[ Participants::Alpha
  340. Participants::Bravo
  341. Participants::Charlie
  342. Ruote::BlockParticipant
  343. Participants::Zebda ],
  344. @dashboard.participant_list.collect { |pe| pe.classname })
  345. assert_equal(
  346. %w[ vanilla peach nil nil coconut ],
  347. @dashboard.participant_list.collect { |pe|
  348. (pe.options['flavour'] || 'nil') rescue 'nil'
  349. })
  350. end
  351. def test_register_block_and_block
  352. @dashboard.register do
  353. alpha do |workitem|
  354. a
  355. end
  356. participant 'bravo' do |workitem|
  357. b
  358. end
  359. end
  360. assert_equal(
  361. [ [ 'on_workitem' ], [ 'on_workitem' ] ],
  362. @dashboard.participant_list.collect { |pe| pe.options.keys })
  363. end
  364. def test_register_block_catchall_default
  365. @dashboard.register do
  366. catchall
  367. end
  368. assert_equal(
  369. %w[ Ruote::StorageParticipant ],
  370. @dashboard.participant_list.collect { |pe| pe.classname })
  371. end
  372. def test_register_block_catch_all
  373. @dashboard.register do
  374. catch_all
  375. end
  376. assert_equal(
  377. %w[ Ruote::StorageParticipant ],
  378. @dashboard.participant_list.collect { |pe| pe.classname })
  379. end
  380. def test_register_block_override_false
  381. @dashboard.register do
  382. alpha 'KlassA'
  383. alpha 'KlassB'
  384. end
  385. plist = @dashboard.participant_list
  386. assert_equal(%w[ ^alpha$ ^alpha$ ], plist.collect { |pe| pe.regex })
  387. assert_equal(%w[ KlassA KlassB ], plist.collect { |pe| pe.classname })
  388. assert_equal({}, plist.first.options)
  389. end
  390. def test_register_block_clears
  391. @dashboard.register 'alpha', 'AlphaParticipant'
  392. @dashboard.register do
  393. bravo 'BravoParticipant'
  394. end
  395. assert_equal 1, @dashboard.participant_list.size
  396. end
  397. def test_register_block_clear_option
  398. @dashboard.register 'alpha', 'AlphaParticipant'
  399. @dashboard.register :clear => false do
  400. bravo 'BravoParticipant'
  401. end
  402. assert_equal 2, @dashboard.participant_list.size
  403. end
  404. def test_argument_error_on_instantiated_participant
  405. assert_raise ArgumentError do
  406. @dashboard.register 'alpha', Ruote::StorageParticipant.new
  407. end
  408. assert_raise ArgumentError do
  409. @dashboard.register 'alpha', Ruote::StorageParticipant.new, 'hello' => 'kitty'
  410. end
  411. end
  412. class AaParticipant
  413. include Ruote::LocalParticipant
  414. attr_reader :opts
  415. def initialize(opts)
  416. @opts = opts
  417. end
  418. end
  419. class BbParticipant < AaParticipant
  420. def accept?(workitem)
  421. false
  422. end
  423. end
  424. def test_engine_participant
  425. @dashboard.register do
  426. alpha AaParticipant
  427. bravo BbParticipant
  428. catchall AaParticipant, :catch_all => 'oh yeah'
  429. end
  430. assert_equal AaParticipant, @dashboard.participant('alpha').class
  431. assert_equal BbParticipant, @dashboard.participant('bravo').class
  432. assert_equal AaParticipant, @dashboard.participant('charly').class
  433. assert_equal 'oh yeah', @dashboard.participant('charly').opts['catch_all']
  434. assert_equal Ruote::Context, @dashboard.participant('alpha').context.class
  435. end
  436. end