How to test a participant?

91 views
Skip to first unread message

Nicola

unread,
Dec 7, 2011, 11:06:58 AM12/7/11
to openwfe...@googlegroups.com
Hi,
this is my first post here, so please forgive me if this is a faq. As the
subject says, I'd like to test a participant (and, more generally, a process
involving several participants). The only example I have found in the
documentation (at http://ruote.rubyforge.org/testing_processes.html) involves
undefined participants (intercepted with catchall). As soon as I modify the
example to deal with a “real” participant, I get an error, as the following
minimal example shows:

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

John Mettraux

unread,
Dec 7, 2011, 4:49:47 PM12/7/11
to openwfe...@googlegroups.com

On Wed, Dec 07, 2011 at 05:06:58PM +0100, Nicola wrote:
>
> this is my first post here, so please forgive me if this is a faq. As the
> subject says, I'd like to test a participant (and, more generally, a process
> involving several participants). The only example I have found in the
> documentation (at http://ruote.rubyforge.org/testing_processes.html) involves
> undefined participants (intercepted with catchall). As soon as I modify the
> example to deal with a “real” participant, I get an error, as the following
> minimal example shows:
>
> (...)

>
> 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?

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

Nicola

unread,
Dec 9, 2011, 10:15:07 AM12/9/11
to openwfe...@googlegroups.com
In article <20111207214...@sanma.local>,
John Mettraux <jmet...@gmail.com> wrote:

> 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

Reply all
Reply to author
Forward
0 new messages