From 5689c08bbc9193b1bcbc65b6ed23ef682da3239e Mon Sep 17 00:00:00 2001 From: Colin Kuskie Date: Fri, 12 Sep 2008 22:59:43 +0000 Subject: [PATCH] Fix multiple bugs related to Singleton workflows: Can't use undef in a where clause. Have to use the same JSON encoding in all places. Added tests for singleton workflow instances. --- docs/changelog/7.x.x.txt | 2 ++ lib/WebGUI/Workflow/Instance.pm | 17 +++++++++++------ t/Workflow/Instance.t | 27 +++++++++++++++++---------- 3 files changed, 30 insertions(+), 16 deletions(-) diff --git a/docs/changelog/7.x.x.txt b/docs/changelog/7.x.x.txt index 112715139..769cc4b6d 100644 --- a/docs/changelog/7.x.x.txt +++ b/docs/changelog/7.x.x.txt @@ -38,6 +38,8 @@ - fixed: Thingy Things can now have no fields and still be functional. - fixed: Can no longer buy badges from receipt or direct link page if quantity is empty - fixed: Tickets on add to badge screen now show the time at the event's timezone + - It was possible to run multiple singleton workflows at the same time. + 7.5.22 - fixed: Layout template now gets prepared correctly - fixed: When user does not have permissions to search/edit a thing a proper error is shown. diff --git a/lib/WebGUI/Workflow/Instance.pm b/lib/WebGUI/Workflow/Instance.pm index 5df9f6b91..ef440f635 100644 --- a/lib/WebGUI/Workflow/Instance.pm +++ b/lib/WebGUI/Workflow/Instance.pm @@ -61,12 +61,17 @@ sub create { my ($class, $session, $properties, $skipRealtime) = @_; # do singleton check - my ($isSingleton) = $session->db->quickArray("select count(*) from Workflow where workflowId=? and - mode='singleton'",[$properties->{workflowId}]); - my $params = (exists $properties->{parameters}) - ? JSON->new->utf8->canonical->encode({parameters => $properties->{parameters}}) - : undef; - my ($count) = $session->db->quickArray("select count(*) from WorkflowInstance where workflowId=? and parameters=?",[$properties->{workflowId},$params]); + my $placeHolders = [$properties->{workflowId}]; + my ($isSingleton) = $session->db->quickArray("select count(*) from Workflow where workflowId=? and mode='singleton'",$placeHolders); + my $sql = "select count(*) from WorkflowInstance where workflowId=?"; + if (exists $properties->{parameters}) { + push @{ $placeHolders }, JSON->new->utf8->pretty->encode({parameters => $properties->{parameters}}); + $sql .= ' and parameters=?'; + } + else { + $sql .= ' and parameters IS NULL'; + } + my ($count) = $session->db->quickArray($sql,$placeHolders); if ($isSingleton && $count) { $session->log->info("An instance of singleton workflow $properties->{workflowId} already exists, not creating a new one"); return undef diff --git a/t/Workflow/Instance.t b/t/Workflow/Instance.t index 7ca62eed5..8acb0e355 100644 --- a/t/Workflow/Instance.t +++ b/t/Workflow/Instance.t @@ -42,23 +42,24 @@ my $session = WebGUI::Test->session; #---------------------------------------------------------------------------- # Tests -plan tests => 8; # Increment this number for each test you create +plan tests => 11; # Increment this number for each test you create #---------------------------------------------------------------------------- # put your tests here -###############################################################################3 +############################################################################### # # create a workflow instance # -###############################################################################3 +############################################################################### my $wf = WebGUI::Workflow->create( $session, { title => 'WebGUI::Workflow::Instance Test', description => 'Description', - type => 'None' + type => 'None', + mode => 'singleton', } ); isa_ok($wf, 'WebGUI::Workflow', 'workflow created for test'); @@ -66,9 +67,6 @@ isa_ok($wf, 'WebGUI::Workflow', 'workflow created for test'); # create an instance of $wfId my $properties = { workflowId=>$wf->getId, - methodName=>"new", - className=>"None", - parameters=>'encode me', }; my $dateUpdated = time(); my $instance = WebGUI::Workflow::Instance->create($session, $properties); @@ -79,12 +77,21 @@ is($instance->get('priority'), 2, 'Default instance priority is 2'); cmp_ok(abs ($instance->get('lastUpdate')-$dateUpdated), '<=', 3, 'Date updated field set correctly when instance is created'); ##Singleton checks +my $otherInstance = WebGUI::Workflow::Instance->create($session, $properties); +is ($otherInstance, undef, 'create: only allows one instance of a singleton to be created'); -###############################################################################3 +$instance->set({ 'parameters' => {session => 1}, }); +$otherInstance = WebGUI::Workflow::Instance->create($session, {workflowId => $wf->getId, parameters => { session => 1,} }); +is($otherInstance, undef, 'create: another singleton instance can not be created if it the same parameters as a currently existing instance'); +$otherInstance = WebGUI::Workflow::Instance->create($session, {workflowId => $wf->getId, parameters => { session => 2,}}); +isnt ($otherInstance, undef, 'create: another singleton instance can be created if it has different parameters'); +$otherInstance->delete; + +############################################################################### # # getWorkflow # -###############################################################################3 +############################################################################### my $instanceWorkflow = $instance->getWorkflow; is($instanceWorkflow->getId, $wf->getId, 'getWorkflow returns a copy of the workflow for the instance'); @@ -95,5 +102,5 @@ is($instanceWorkflow->getId, $wf->getId, 'getWorkflow, caching check'); #---------------------------------------------------------------------------- # Cleanup END { - $wf->delete; ##Deleting a Workflow deletes its instances, too. + #$wf->delete; ##Deleting a Workflow deletes its instances, too. }