ENGINE = Ruote::Engine.new(Ruote::Worker.new(Ruote::HashStorage.new()))
ENGINE.register_participant :dummy do |workitem|
workitem.result = 10
end
pdef = Ruote.process_definition do
dummy
end
wfid = ENGINE.launch(pdef)
ENGINE.wait_for(:dummy)
workitem = ENGINE.storage_participant.first
puts workitem.result # should be 10
ENGINE.storage_participant.reply(workitem)
ENGINE.wait_for(wfid)
but it fails because the retrieved workitem is nil. Apparently, but not
obviously (at least for me), I am doing something wrong. I am using Ruote 2.2.0
and Ruby 1.9.2. How can the above be fixed?
Nicola
Hello Nicola,
welcome to the ruote mailing list.
The participant dummy is not a storage participant so you won't find the
workitem there.
Here is a version of your test that works:
---8<---
require 'rubygems'
require 'ruote'
ENGINE = Ruote::Engine.new(Ruote::Worker.new(Ruote::HashStorage.new()))
ENGINE.register_participant :dummy0 do |workitem|
workitem.result = 10
end
ENGINE.register_participant :dummy1, Ruote::StorageParticipant
pdef = Ruote.process_definition do
dummy0
dummy1
end
wfid = ENGINE.launch(pdef)
ENGINE.wait_for(:dummy1)
workitem = ENGINE.storage_participant.first
puts workitem.result
ENGINE.storage_participant.reply(workitem)
ENGINE.wait_for(wfid)
--->8---
Here is a second version, that peeks at the result of the wait_for call:
---8<---
require 'rubygems'
require 'ruote'
ENGINE = Ruote::Engine.new(Ruote::Worker.new(Ruote::HashStorage.new()))
ENGINE.register_participant :dummy do |workitem|
workitem.result = 10
end
pdef = Ruote.process_definition do
dummy
end
wfid = ENGINE.launch(pdef)
ENGINE.storage_participant.reply(workitem)
r = ENGINE.wait_for(wfid)
p r['workitem']['fields']['__result__']
--->8---
If you want to "unit test" participants, you'd better go with something like:
---8<---
require 'ruote'
class SumParticipant
include Ruote::LocalParticipant
def consume(workitem)
items = workitem.fields['items'] || []
workitem.fields['sum'] = items.inject(0) { |s, (item, count)| s + count }
reply_to_engine(workitem)
end
def cancel(fei, flavour)
# do nothing
end
end
--->8---
and
---8<---
require 'rubygems'
require File.expand_path('../sum_participant', __FILE__)
require 'test/unit'
class SumParticipantTest < Test::Unit::TestCase
def setup
@participant = SumParticipant.new
def @participant.reply_to_engine(workitem)
$reply = workitem
end
$reply = nil
end
def new_workitem(fields={})
Ruote::Workitem.new('fields' => fields)
end
def test_sum
@participant.consume(
new_workitem('items' => [ [ 'hat', 10 ], ['car', 2 ] ]))
assert_equal 12, $reply.fields['sum']
end
def test_sum_when_no_items
@participant.consume(new_workitem)
assert_equal 0, $reply.fields['sum']
end
end
--->8---
Which directly exercises the participant's consume method.
I have to update the document at:
http://ruote.rubyforge.org/testing_participants.html
Thanks for reminding me of that TODOs.
Best regards,
--
John Mettraux - http://lambda.io/processi
> The participant dummy is not a storage participant so you won't find the
> workitem there.
I've looked at the documentation better and now I understand that. And your
examples have got me up and running.
Thanks!
Nicola