3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.7 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2015 |
7 +--------------------------------------------------------------------+
8 | This file is a part of CiviCRM. |
10 | CiviCRM is free software; you can copy, modify, and distribute it |
11 | under the terms of the GNU Affero General Public License |
12 | Version 3, 19 November 2007 and the CiviCRM Licensing Exception. |
14 | CiviCRM is distributed in the hope that it will be useful, but |
15 | WITHOUT ANY WARRANTY; without even the implied warranty of |
16 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
17 | See the GNU Affero General Public License for more details. |
19 | You should have received a copy of the GNU Affero General Public |
20 | License and the CiviCRM Licensing Exception along |
21 | with this program; if not, contact CiviCRM LLC |
22 | at info[AT]civicrm[DOT]org. If you have questions about the |
23 | GNU Affero General Public License or the licensing of CiviCRM, |
24 | see the CiviCRM license FAQ at http://civicrm.org/licensing |
25 +--------------------------------------------------------------------+
29 * File for the CiviCRM APIv3 job functions
31 * @package CiviCRM_APIv3
34 * @copyright CiviCRM LLC (c) 2004-2015
35 * @version $Id: Job.php 30879 2010-11-22 15:45:55Z shot $
38 require_once 'CiviTest/CiviUnitTestCase.php';
39 //@todo - why doesn't class loader find these (I tried renaming)
40 require_once 'CiviTest/CiviMailUtils.php';
43 * Class api_v3_JobTest
45 class api_v3_JobProcessMailingTest
extends CiviUnitTestCase
{
46 protected $_apiversion = 3;
48 public $DBResetRequired = FALSE;
49 public $_entity = 'Job';
50 public $_params = array();
54 protected $defaultSettings;
61 public function setUp() {
62 $this->cleanupMailingTest();
64 CRM_Mailing_BAO_MailingJob
::$mailsProcessed = 0; // DGW
65 $this->_groupID
= $this->groupCreate();
66 $this->_email
= 'test@test.test';
67 $this->_params
= array(
68 'subject' => 'Accidents in cars cause children',
69 'body_text' => 'BEWARE children need regular infusions of toys. Santa knows your {domain.address}. There is no {action.optOutUrl}.',
70 'name' => 'mailing name',
72 'groups' => array('include' => array($this->_groupID
)),
73 'scheduled_date' => 'now',
75 $this->defaultSettings
= array(
76 'recipients' => 20, // int, #contacts to receive mailing
77 'workers' => 1, // int, #concurrent cron jobs
78 'iterations' => 1, // int, #times to spawn all the workers
79 'lockHold' => 0, // int, #extra seconds each cron job should hold lock
80 'mailerBatchLimit' => 0, // int, max# recipients to send in a given cron run
81 'mailerJobsMax' => 0, // int, max# concurrent jobs
82 'mailerJobSize' => 0, // int, max# recipients in each job
83 'mailThrottleTime' => 0, // int, microseconds separating messages
85 $this->_mut
= new CiviMailUtils($this, TRUE);
86 $this->callAPISuccess('mail_settings', 'get', array('api.mail_settings.create' => array('domain' => 'chaos.org')));
91 public function tearDown() {
92 //$this->_mut->clearMessages();
94 CRM_Utils_Hook
::singleton()->reset();
95 CRM_Mailing_BAO_MailingJob
::$mailsProcessed = 0; // DGW
96 //$this->cleanupMailingTest();
100 public function testBasic() {
101 $this->createContactsInGroup(10, $this->_groupID
);
102 Civi
::settings()->add(array(
103 'mailerBatchLimit' => 2,
105 $this->callAPISuccess('mailing', 'create', $this->_params
);
106 $this->_mut
->assertRecipients(array());
107 $this->callAPISuccess('job', 'process_mailing', array());
108 $this->_mut
->assertRecipients($this->getRecipients(1, 2));
111 public function concurrencyExamples() {
114 // Launch 3 workers, but mailerJobsMax limits us to 1 worker.
119 // FIXME: lockHold is unrealistic/unrepresentative. In reality, this situation fails because
120 // the data.* locks trample the worker.* locks. However, setting lockHold allows us to
121 // approximate the behavior of what would happen *if* the lock-implementation didn't suffer
122 // trampling effects.
124 'mailerBatchLimit' => 4,
125 'mailerJobsMax' => 1,
128 0 => 2, // 2 jobs which produce 0 messages
129 4 => 1, // 1 job which produces 4 messages
134 // Launch 3 workers, but mailerJobsMax limits us to 2 workers.
139 // FIXME: lockHold is unrealistic/unrepresentative. In reality, this situation fails because
140 // the data.* locks trample the worker.* locks. However, setting lockHold allows us to
141 // approximate the behavior of what would happen *if* the lock-implementation didn't suffer
142 // trampling effects.
144 'mailerBatchLimit' => 5,
145 'mailerJobsMax' => 2,
148 0 => 1, // 1 job which produce 0 messages
149 5 => 2, // 2 jobs which produce 5 messages
154 // Launch 3 workers and saturate them (mailerJobsMax=3)
159 'mailerBatchLimit' => 6,
160 'mailerJobsMax' => 3,
163 6 => 3, // 3 jobs which produce 6 messages
168 // Launch 4 workers and saturate them (mailerJobsMax=0)
173 'mailerBatchLimit' => 6,
174 'mailerJobsMax' => 0,
177 6 => 3, // 3 jobs which produce 6 messages
178 2 => 1, // 1 job which produces 2 messages
183 // Launch 1 worker, 3 times in a row. Deliver everything.
189 'mailerBatchLimit' => 7,
192 7 => 1, // 1 job which produces 7 messages
193 3 => 1, // 1 job which produces 3 messages
194 0 => 1, // 1 job which produces 0 messages
199 // Launch 2 worker, 3 times in a row. Deliver everything.
205 'mailerBatchLimit' => 3,
208 3 => 3, // 3 jobs which produce 3 messages
209 1 => 1, // 1 job which produces 1 messages
210 0 => 2, // 2 jobs which produce 0 messages
219 * Setup various mail configuration options (eg $mailerBatchLimit,
220 * $mailerJobMax) and spawn multiple worker threads ($workers).
221 * Allow the threads to complete. (Optionally, repeat the above
222 * process.) Finally, check to see if the right number of
223 * jobs delivered the right number of messages.
225 * @param array $settings
226 * An array of settings (eg mailerBatchLimit, workers). See comments
227 * for $this->defaultSettings.
228 * @param array $expectedTallies
229 * A listing of the number cron-runs keyed by their size.
230 * For example, array(10=>2) means that there 2 cron-runs
231 * which delivered 10 messages each.
232 * @param int $expectedTotal
233 * The total number of contacts for whom messages should have
235 * @dataProvider concurrencyExamples
237 public function testConcurrency($settings, $expectedTallies, $expectedTotal) {
238 $settings = array_merge($this->defaultSettings
, $settings);
240 $this->createContactsInGroup($settings['recipients'], $this->_groupID
);
241 Civi
::settings()->add(CRM_Utils_Array
::subset($settings, array(
247 $this->callAPISuccess('mailing', 'create', $this->_params
);
249 $this->_mut
->assertRecipients(array());
251 $allApiResults = array();
252 for ($iterationId = 0; $iterationId < $settings['iterations']; $iterationId++
) {
253 $apiCalls = $this->createExternalAPI();
254 $apiCalls->addEnv(array('CIVICRM_CRON_HOLD' => $settings['lockHold']));
255 for ($workerId = 0; $workerId < $settings['workers']; $workerId++
) {
256 $apiCalls->addCall('job', 'process_mailing', array());
259 $this->assertEquals($settings['workers'], $apiCalls->getRunningCount());
262 $allApiResults = array_merge($allApiResults, $apiCalls->getResults());
265 $actualTallies = $this->tallyApiResults($allApiResults);
266 $this->assertEquals($expectedTallies, $actualTallies, 'API tallies should match.' . print_r(array(
267 'expectedTallies' => $expectedTallies,
268 'actualTallies' => $actualTallies,
269 'apiResults' => $allApiResults,
271 $this->_mut
->assertRecipients($this->getRecipients(1, $expectedTotal));
272 $this->assertEquals(0, $apiCalls->getRunningCount());
276 * Create contacts in group.
279 * @param int $groupID
280 * @param string $domain
282 public function createContactsInGroup($count, $groupID, $domain = 'nul.example.com') {
283 for ($i = 1; $i <= $count; $i++
) {
284 $contactID = $this->individualCreate(array('first_name' => $count, 'email' => 'mail' . $i . '@' . $domain));
285 $this->callAPISuccess('group_contact', 'create', array(
286 'contact_id' => $contactID,
287 'group_id' => $groupID,
294 * Construct the list of email addresses for $count recipients.
301 public function getRecipients($start, $count, $domain = 'nul.example.com') {
302 $recipients = array();
303 for ($i = $start; $i < ($start +
$count); $i++
) {
304 $recipients[][0] = 'mail' . $i . '@' . $domain;
309 protected function cleanupMailingTest() {
310 $this->quickCleanup(array(
312 'civicrm_mailing_job',
313 'civicrm_mailing_spool',
314 'civicrm_mailing_group',
315 'civicrm_mailing_recipients',
316 'civicrm_mailing_event_queue',
317 'civicrm_mailing_event_bounce',
318 'civicrm_mailing_event_delivered',
320 'civicrm_group_contact',
326 * Categorize results based on (a) whether they succeeded
327 * and (b) the number of messages sent.
329 * @param array $apiResults
331 * One key 'error' for all failures.
332 * A separate key for each distinct quantity.
334 protected function tallyApiResults($apiResults) {
336 foreach ($apiResults as $apiResult) {
337 $key = !empty($apiResult['is_error']) ?
'error' : $apiResult['values']['processed'];
338 $ret[$key] = !empty($ret[$key]) ?
1 +
$ret[$key] : 1;