]> CyberLeo.Net >> Repos - Github/sugarcrm.git/blob - tests/include/SugarQueue/CronTest.php
Release 6.5.6
[Github/sugarcrm.git] / tests / include / SugarQueue / CronTest.php
1 <?php
2 /*********************************************************************************
3  * SugarCRM Community Edition is a customer relationship management program developed by
4  * SugarCRM, Inc. Copyright (C) 2004-2012 SugarCRM Inc.
5  * 
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.
12  * 
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
16  * details.
17  * 
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
21  * 02110-1301 USA.
22  * 
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.
25  * 
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.
29  * 
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  ********************************************************************************/
36
37 require_once 'include/SugarQueue/SugarJobQueue.php';
38 require_once 'include/SugarQueue/SugarCronJobs.php';
39 require_once 'modules/SchedulersJobs/SchedulersJob.php';
40
41 class CronTest extends Sugar_PHPUnit_Framework_TestCase
42 {
43     static public $jobCalled = false;
44
45     public static function setUpBeforeClass()
46     {
47         $GLOBALS['current_user'] = SugarTestUserUtilities::createAnonymousUser();
48         // clean up queue
49                 $GLOBALS['db']->query("DELETE FROM job_queue WHERE status='queued'");
50     }
51
52     public static function tearDownAdterClass()
53     {
54         SugarTestUserUtilities::removeAllCreatedAnonymousUsers();
55     }
56
57     public function setUp()
58     {
59         $this->jq = $jobq = new SugarCronJobs();
60         self::$jobCalled = false;
61     }
62
63     public function tearDown()
64     {
65         $GLOBALS['db']->query("DELETE FROM job_queue WHERE scheduler_id='unittest'");
66     }
67
68     public function testThrottle()
69     {
70         $this->jq->throttle();
71         $this->assertFalse($this->jq->throttle(), "Should prohibit second time");
72         // wait a bit
73         sleep(2);
74         $this->jq->min_interval = 1;
75         $this->assertTrue($this->jq->throttle(), "Should allow after delay");
76     }
77
78     public static function cronJobFunction()
79     {
80         self::$jobCalled = true;
81         return true;
82     }
83
84     public static function cronJobLongFunction()
85     {
86         self::$jobCalled = true;
87         sleep(2);
88         return true;
89     }
90
91     public function testQueueJob()
92     {
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;
100         $job->save();
101         $jobid = $job->id;
102
103         $this->jq->min_interval = 0; // disable throttle
104         $this->jq->disable_schedulers = true;
105         $this->jq->runCycle();
106
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");
113     }
114
115     public function testQueueFailJob()
116     {
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;
124         $job->save();
125         $jobid = $job->id;
126
127         $this->jq->min_interval = 0; // disable throttle
128         $this->jq->disable_schedulers = true;
129         $this->jq->runCycle();
130
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");
136     }
137
138     public function testJobsCount()
139     {
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;
149         $job->save();
150         $jobid1 = $job->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;
160         $job->save();
161         $jobid2 = $job->id;
162
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();
167
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");
177     }
178
179     public function testJobsTimeCutoff()
180     {
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;
190         $job->save();
191         $jobid1 = $job->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;
201         $job->save();
202         $jobid2 = $job->id;
203
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();
209
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");
219     }
220
221     public function testJobsCleanup()
222     {
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;
234         $job->save();
235         $jobid1 = $job->id;
236
237         $this->jq->min_interval = 0; // disable throttle
238         $this->jq->disable_schedulers = true;
239         $this->jq->runCycle();
240
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");
247     }
248
249 }