2 /*********************************************************************************
3 * SugarCRM Community Edition is a customer relationship management program developed by
4 * SugarCRM, Inc. Copyright (C) 2004-2012 SugarCRM Inc.
6 * This program is free software; you can redistribute it and/or modify it under
7 * the terms of the GNU Affero General Public License version 3 as published by the
8 * Free Software Foundation with the addition of the following permission added
9 * to Section 15 as permitted in Section 7(a): FOR ANY PART OF THE COVERED WORK
10 * IN WHICH THE COPYRIGHT IS OWNED BY SUGARCRM, SUGARCRM DISCLAIMS THE WARRANTY
11 * OF NON INFRINGEMENT OF THIRD PARTY RIGHTS.
13 * This program is distributed in the hope that it will be useful, but WITHOUT
14 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
15 * FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License for more
18 * You should have received a copy of the GNU Affero General Public License along with
19 * this program; if not, see http://www.gnu.org/licenses or write to the Free
20 * Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
23 * You can contact SugarCRM, Inc. headquarters at 10050 North Wolfe Road,
24 * SW2-130, Cupertino, CA 95014, USA. or at email address contact@sugarcrm.com.
26 * The interactive user interfaces in modified source and object code versions
27 * of this program must display Appropriate Legal Notices, as required under
28 * Section 5 of the GNU Affero General Public License version 3.
30 * In accordance with Section 7(b) of the GNU Affero General Public License version 3,
31 * these Appropriate Legal Notices must retain the display of the "Powered by
32 * SugarCRM" logo. If the display of the logo is not reasonably feasible for
33 * technical reasons, the Appropriate Legal Notices must display the words
34 * "Powered by SugarCRM".
35 ********************************************************************************/
37 require_once 'include/SugarQueue/SugarJobQueue.php';
38 require_once 'include/SugarQueue/SugarCronJobs.php';
39 require_once 'modules/SchedulersJobs/SchedulersJob.php';
41 class CronTest extends Sugar_PHPUnit_Framework_TestCase
43 static public $jobCalled = false;
45 public static function setUpBeforeClass()
47 $GLOBALS['current_user'] = SugarTestUserUtilities::createAnonymousUser();
49 $GLOBALS['db']->query("DELETE FROM job_queue WHERE status='queued'");
52 public static function tearDownAdterClass()
54 SugarTestUserUtilities::removeAllCreatedAnonymousUsers();
57 public function setUp()
59 $this->jq = $jobq = new SugarCronJobs();
60 self::$jobCalled = false;
63 public function tearDown()
65 $GLOBALS['db']->query("DELETE FROM job_queue WHERE scheduler_id='unittest'");
68 public function testThrottle()
70 $this->jq->throttle();
71 $this->assertFalse($this->jq->throttle(), "Should prohibit second time");
74 $this->jq->min_interval = 1;
75 $this->assertTrue($this->jq->throttle(), "Should allow after delay");
78 public static function cronJobFunction()
80 self::$jobCalled = true;
84 public static function cronJobLongFunction()
86 self::$jobCalled = true;
91 public function testQueueJob()
93 $job = new SchedulersJob();
94 $job->status = SchedulersJob::JOB_STATUS_QUEUED;
95 $job->scheduler_id = 'unittest';
96 $job->execute_time = TimeDate::getInstance()->nowDb();
97 $job->name = "Unit test Job";
98 $job->target = "function::CronTest::cronJobFunction";
99 $job->assigned_user_id = $GLOBALS['current_user']->id;
103 $this->jq->min_interval = 0; // disable throttle
104 $this->jq->disable_schedulers = true;
105 $this->jq->runCycle();
107 $this->assertTrue(self::$jobCalled, "Job was not called");
108 $this->assertTrue($this->jq->runOk(), "Wrong OK flag");
109 $job = new SchedulersJob();
110 $job->retrieve($jobid);
111 $this->assertEquals(SchedulersJob::JOB_SUCCESS, $job->resolution, "Wrong resolution");
112 $this->assertEquals(SchedulersJob::JOB_STATUS_DONE, $job->status, "Wrong status");
115 public function testQueueFailJob()
117 $job = new SchedulersJob();
118 $job->status = SchedulersJob::JOB_STATUS_QUEUED;
119 $job->scheduler_id = 'unittest';
120 $job->execute_time = TimeDate::getInstance()->nowDb();
121 $job->name = "Unit test Job";
122 $job->target = "function::test::test";
123 $job->assigned_user_id = $GLOBALS['current_user']->id;
127 $this->jq->min_interval = 0; // disable throttle
128 $this->jq->disable_schedulers = true;
129 $this->jq->runCycle();
131 $this->assertFalse($this->jq->runOk(), "Wrong OK flag");
132 $job = new SchedulersJob();
133 $job->retrieve($jobid);
134 $this->assertEquals(SchedulersJob::JOB_FAILURE, $job->resolution, "Wrong resolution");
135 $this->assertEquals(SchedulersJob::JOB_STATUS_DONE, $job->status, "Wrong status");
138 public function testJobsCount()
140 // job 1 - oldest, should be executed
141 $job = new SchedulersJob();
142 $job->status = SchedulersJob::JOB_STATUS_QUEUED;
143 $job->scheduler_id = 'unittest';
144 $job->execute_time = TimeDate::getInstance()->nowDb();
145 $job->date_entered = '2010-01-01 12:00:00';
146 $job->name = "Unit test Job 1";
147 $job->target = "function::CronTest::cronJobFunction";
148 $job->assigned_user_id = $GLOBALS['current_user']->id;
151 // job 2 - newer, should not be executed
152 $job = new SchedulersJob();
153 $job->status = SchedulersJob::JOB_STATUS_QUEUED;
154 $job->scheduler_id = 'unittest';
155 $job->execute_time = TimeDate::getInstance()->nowDb();
156 $job->date_entered = '2012-01-01 12:00:00';
157 $job->name = "Unit test Job 2";
158 $job->target = "function::CronTest::cronJobFunction";
159 $job->assigned_user_id = $GLOBALS['current_user']->id;
163 $this->jq->min_interval = 0; // disable throttle
164 $this->jq->max_jobs = 1; // only one job per cycle
165 $this->jq->disable_schedulers = true;
166 $this->jq->runCycle();
168 $this->assertTrue(self::$jobCalled, "Job was not called");
169 $job = new SchedulersJob();
170 $job->retrieve($jobid1);
171 $this->assertEquals(SchedulersJob::JOB_STATUS_DONE, $job->status, "Wrong status");
172 $this->assertEquals(SchedulersJob::JOB_SUCCESS, $job->resolution, "Wrong resolution");
173 // test that second one wasn't run
174 $job = new SchedulersJob();
175 $job->retrieve($jobid2);
176 $this->assertEquals(SchedulersJob::JOB_STATUS_QUEUED, $job->status, "Wrong status");
179 public function testJobsTimeCutoff()
181 // job 1 - oldest, should be executed
182 $job = new SchedulersJob();
183 $job->status = SchedulersJob::JOB_STATUS_QUEUED;
184 $job->scheduler_id = 'unittest';
185 $job->execute_time = TimeDate::getInstance()->nowDb();
186 $job->date_entered = '2010-01-01 12:00:00';
187 $job->name = "Unit test Job 1";
188 $job->target = "function::CronTest::cronJobLongFunction";
189 $job->assigned_user_id = $GLOBALS['current_user']->id;
192 // job 2 - newer, should not be executed
193 $job = new SchedulersJob();
194 $job->status = SchedulersJob::JOB_STATUS_QUEUED;
195 $job->scheduler_id = 'unittest';
196 $job->execute_time = TimeDate::getInstance()->nowDb();
197 $job->date_entered = '2012-01-01 12:00:00';
198 $job->name = "Unit test Job 2";
199 $job->target = "function::CronTest::cronJobLongFunction";
200 $job->assigned_user_id = $GLOBALS['current_user']->id;
204 $this->jq->min_interval = 0; // disable throttle
205 $this->jq->max_jobs = 10;
206 $this->jq->max_runtime = 1; // only 1 sec runtime
207 $this->jq->disable_schedulers = true;
208 $this->jq->runCycle();
210 $this->assertTrue(self::$jobCalled, "Job was not called");
211 $job = new SchedulersJob();
212 $job->retrieve($jobid1);
213 $this->assertEquals(SchedulersJob::JOB_STATUS_DONE, $job->status, "Wrong status");
214 $this->assertEquals(SchedulersJob::JOB_SUCCESS, $job->resolution, "Wrong resolution");
215 // test that second one wasn't run
216 $job = new SchedulersJob();
217 $job->retrieve($jobid2);
218 $this->assertEquals(SchedulersJob::JOB_STATUS_QUEUED, $job->status, "Wrong status");
221 public function testJobsCleanup()
223 // job 1 - oldest, should be executed
224 $job = new SchedulersJob();
225 $job->update_date_modified = false;
226 $job->status = SchedulersJob::JOB_STATUS_RUNNING;
227 $job->scheduler_id = 'unittest';
228 $job->execute_time = TimeDate::getInstance()->nowDb();
229 $job->date_entered = '2010-01-01 12:00:00';
230 $job->date_modified = '2010-01-01 12:00:00';
231 $job->name = "Unit test Job 1";
232 $job->target = "function::CronTest::cronJobFunction";
233 $job->assigned_user_id = $GLOBALS['current_user']->id;
237 $this->jq->min_interval = 0; // disable throttle
238 $this->jq->disable_schedulers = true;
239 $this->jq->runCycle();
241 $this->assertFalse(self::$jobCalled, "Job was called");
242 $this->assertFalse($this->jq->runOk(), "Wrong OK flag");
243 $job = new SchedulersJob();
244 $job->retrieve($jobid1);
245 $this->assertEquals(SchedulersJob::JOB_STATUS_DONE, $job->status, "Wrong status");
246 $this->assertEquals(SchedulersJob::JOB_FAILURE, $job->resolution, "Wrong resolution");