From 97b7d4a0fc662d1ea658f3d453cf8e75911e5084 Mon Sep 17 00:00:00 2001 From: Tim Otten Date: Mon, 17 Nov 2014 22:44:32 -0800 Subject: [PATCH] CRM-15578 - Pull up static $mailsProcessed and reset during testing These static variables were leaking between test-cases in a way which caused the second test to get stuck on an infinite loop. It has been quite difficult to identify the tests which are interacting (api_v3_JobProcessMailingTest and api_v3_MailingTest), the particular loop which went infinitely, and the leaky variables. I don't understand why these statics exist, but I don't have time to fully grok it -- right now, the branch "master-civimail-abtest" has gone off on a bit of a limb with failing tests, I just want to bring it back to a stable place. Promoting & resetting the static is the simplest way. --- CRM/Mailing/BAO/MailingJob.php | 16 ++++++++++++---- api/v3/Mailing.php | 6 ++++++ tests/phpunit/api/v3/JobProcessMailingTest.php | 2 ++ tests/phpunit/api/v3/MailingTest.php | 3 +++ 4 files changed, 23 insertions(+), 4 deletions(-) diff --git a/CRM/Mailing/BAO/MailingJob.php b/CRM/Mailing/BAO/MailingJob.php index 22524f7152..675ac31563 100644 --- a/CRM/Mailing/BAO/MailingJob.php +++ b/CRM/Mailing/BAO/MailingJob.php @@ -41,6 +41,15 @@ require_once 'Mail.php'; class CRM_Mailing_BAO_MailingJob extends CRM_Mailing_DAO_MailingJob { CONST MAX_CONTACTS_TO_PROCESS = 1000; + /** + *(Dear God Why) Keep a global count of mails processed within the current + * request. + * + * @static + * @var int + */ + static $mailsProcessed = 0; + /** * class constructor */ @@ -534,8 +543,7 @@ VALUES (%1, %2, %3, %4, %5, %6, %7) } $eq->query($query); - static $config = NULL; - static $mailsProcessed = 0; + $config = NULL; if ($config == NULL) { $config = CRM_Core_Config::singleton(); @@ -570,7 +578,7 @@ VALUES (%1, %2, %3, %4, %5, %6, %7) if ( $config->mailerBatchLimit > 0 && - $mailsProcessed >= $config->mailerBatchLimit + self::$mailsProcessed >= $config->mailerBatchLimit ) { if (!empty($fields)) { $this->deliverGroup($fields, $mailing, $mailer, $job_date, $attachments); @@ -578,7 +586,7 @@ VALUES (%1, %2, %3, %4, %5, %6, %7) $eq->free(); return FALSE; } - $mailsProcessed++; + self::$mailsProcessed++; $fields[] = array( 'id' => $eq->id, diff --git a/api/v3/Mailing.php b/api/v3/Mailing.php index 466c832ecb..013a327483 100755 --- a/api/v3/Mailing.php +++ b/api/v3/Mailing.php @@ -472,8 +472,14 @@ ORDER BY e.is_bulkmail DESC, e.is_primary DESC } $isComplete = FALSE; + $config = CRM_Core_Config::singleton(); + $mailerJobSize = (property_exists($config, 'mailerJobSize')) ? $config->mailerJobSize : NULL; while (!$isComplete) { + // Q: In CRM_Mailing_BAO_Mailing::processQueue(), the three runJobs*() + // functions are all called. Why does Mailing.send_test only call one? + // CRM_Mailing_BAO_MailingJob::runJobs_pre($mailerJobSize, NULL); $isComplete = CRM_Mailing_BAO_MailingJob::runJobs($testEmailParams); + // CRM_Mailing_BAO_MailingJob::runJobs_post(NULL); } //return delivered mail info diff --git a/tests/phpunit/api/v3/JobProcessMailingTest.php b/tests/phpunit/api/v3/JobProcessMailingTest.php index 18f5dbe8d2..1a2964d7e4 100644 --- a/tests/phpunit/api/v3/JobProcessMailingTest.php +++ b/tests/phpunit/api/v3/JobProcessMailingTest.php @@ -57,6 +57,7 @@ class api_v3_JobProcessMailingTest extends CiviUnitTestCase { function setUp() { parent::setUp(); + CRM_Mailing_BAO_MailingJob::$mailsProcessed = 0; // DGW $this->_groupID = $this->groupCreate(); $this->_email = 'test@test.test'; $this->_params = array( @@ -77,6 +78,7 @@ class api_v3_JobProcessMailingTest extends CiviUnitTestCase { $this->_mut->stop(); $this->quickCleanup(array('civicrm_mailing', 'civicrm_mailing_job', 'civicrm_contact')); CRM_Utils_Hook::singleton()->reset(); + CRM_Mailing_BAO_MailingJob::$mailsProcessed = 0; // DGW parent::tearDown(); } diff --git a/tests/phpunit/api/v3/MailingTest.php b/tests/phpunit/api/v3/MailingTest.php index 01f31a6f18..c52728ff78 100755 --- a/tests/phpunit/api/v3/MailingTest.php +++ b/tests/phpunit/api/v3/MailingTest.php @@ -53,6 +53,7 @@ class api_v3_MailingTest extends CiviUnitTestCase { function setUp() { parent::setUp(); + CRM_Mailing_BAO_MailingJob::$mailsProcessed = 0; // DGW $this->_contactIDs = array(); $this->_groupID = $this->groupCreate(); $this->_groupIDs = array(); @@ -73,6 +74,8 @@ class api_v3_MailingTest extends CiviUnitTestCase { foreach ($this->_groupIDs as $groupID) { $this->groupDelete($groupID); } + CRM_Mailing_BAO_MailingJob::$mailsProcessed = 0; // DGW + parent::tearDown(); } /** -- 2.25.1