]> CyberLeo.Net >> Repos - Github/sugarcrm.git/blob - tests/include/SugarQueue/JobQueueTest.php
Release $ver
[Github/sugarcrm.git] / tests / include / SugarQueue / JobQueueTest.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 'modules/SchedulersJobs/SchedulersJob.php';
39
40 class JobQueueTest extends Sugar_PHPUnit_Framework_TestCase
41 {
42     public function setUp()
43     {
44         $this->jq = new TestSugarJobQueue();
45     }
46
47     public function tearDown()
48     {
49         $GLOBALS['db']->query("DELETE FROM job_queue WHERE scheduler_id='unittest'");
50         SugarTestUserUtilities::removeAllCreatedAnonymousUsers();
51     }
52
53     public function testSubmitJob()
54     {
55         $GLOBALS['current_user'] = SugarTestUserUtilities::createAnonymousUser();
56         $job = new SchedulersJob();
57         $job->status = SchedulersJob::JOB_STATUS_RUNNING;
58         $job->scheduler_id = 'unittest';
59         $now = $GLOBALS['timedate']->nowDb();
60         $job->name = "Unit test Job 1";
61         $job->target = "test::test";
62         $job->assigned_user_id = $GLOBALS['current_user']->id;
63         $id = $this->jq->submitJob($job);
64
65         $this->assertNotEmpty($id, "Bad job ID");
66         $job = new SchedulersJob();
67         $job->retrieve($id);
68         $this->assertEquals(SchedulersJob::JOB_PENDING, $job->resolution, "Wrong resolution");
69         $this->assertEquals(SchedulersJob::JOB_STATUS_QUEUED, $job->status, "Wrong status");
70         $this->assertEquals($now, $job->execute_time_db, "Wrong execute time");
71     }
72
73     public function testGetJob()
74     {
75         $job = new SchedulersJob();
76         $job->status = SchedulersJob::JOB_STATUS_RUNNING;
77         $job->scheduler_id = 'unittest';
78         $now = $GLOBALS['timedate']->nowDb();
79         $job->name = "Unit test Job 1";
80         $job->target = "test::test";
81         $id = $this->jq->submitJob($job);
82
83         $this->assertNotEmpty($id, "Bad job ID");
84         $job = $this->jq->getJob($id);
85         $this->assertEquals(SchedulersJob::JOB_PENDING, $job->resolution, "Wrong resolution");
86         $this->assertEquals(SchedulersJob::JOB_STATUS_QUEUED, $job->status, "Wrong status");
87         $this->assertEquals($now, $job->execute_time_db, "Wrong execute time");
88
89         $job = $this->jq->getJob("nosuchjob");
90         $this->assertNull($job, "Bad return on non-existing job");
91     }
92
93     public function testCleanup()
94     {
95         $job = new SchedulersJob();
96         $job->status = SchedulersJob::JOB_STATUS_RUNNING;
97         $job->scheduler_id = 'unittest';
98         $job->execute_time = $GLOBALS['timedate']->nowDb();
99         $job->date_entered = '2010-01-01 12:00:00';
100         $job->date_modified = '2010-01-01 12:00:00';
101         $job->name = "Unit test Job 1";
102         $job->target = "test::test";
103         $job->save();
104         $jobid = $job->id;
105         $this->jq->cleanup();
106
107         $job = new SchedulersJob();
108         $job->retrieve($jobid);
109         $this->assertEquals(SchedulersJob::JOB_STATUS_DONE, $job->status, "Wrong status");
110         $this->assertEquals(SchedulersJob::JOB_FAILURE, $job->resolution, "Wrong resolution");
111     }
112
113     public function testDelete()
114     {
115         $job = new SchedulersJob();
116         $job->status = SchedulersJob::JOB_STATUS_RUNNING;
117         $job->scheduler_id = 'unittest';
118         $job->name = "Unit test Job 1";
119         $job->target = "test::test";
120         $job->save();
121         $jobid = $job->id;
122         $this->jq->deleteJob($jobid);
123
124         $job = new SchedulersJob();
125         $job->retrieve($jobid);
126         $this->assertEmpty($job->id, "Job not deleted");
127     }
128
129     public function testGetNextJob()
130     {
131         // should get only jobs with status QUEUED, in date_entered order, and mark them as running
132         // Clean up the queue
133         $GLOBALS['db']->query("DELETE FROM job_queue WHERE status='".SchedulersJob::JOB_STATUS_QUEUED."'");
134         $job = $this->jq->nextJob("unit test");
135         $this->assertNull($job, "Extra job found");
136         // older job
137         $job = new SchedulersJob();
138         $job->status = SchedulersJob::JOB_STATUS_QUEUED;
139         $job->scheduler_id = 'unittest';
140         $job->date_entered = '2010-01-01 12:00:00';
141         $job->name = "Old Job";
142         $job->target = "test::test";
143         $job->save();
144         $jobid1 = $job->id;
145         // another job, later date
146         $job = new SchedulersJob();
147         $job->status = SchedulersJob::JOB_STATUS_QUEUED;
148         $job->scheduler_id = 'unittest';
149         $job->date_entered = '2012-01-01 12:00:00';
150         $job->name = "Newer Job";
151         $job->target = "test::test";
152         $job->save();
153         $jobid2 = $job->id;
154         // job with execute date in the future
155         $job = new SchedulersJob();
156         $job->status = SchedulersJob::JOB_STATUS_QUEUED;
157         $job->scheduler_id = 'unittest';
158         $job->execute_time = $GLOBALS['timedate']->getNow()->modify("+3 days")->asDb();
159         $job->date_entered = '2010-01-01 12:00:00';
160         $job->name = "Future Job";
161         $job->target = "test::test";
162         $job->save();
163         $jobid3 = $job->id;
164         //running job
165         $job = new SchedulersJob();
166         $job->status = SchedulersJob::JOB_STATUS_RUNNING;
167         $job->scheduler_id = 'unittest';
168         $job->date_entered = '2010-01-01 12:00:00';
169         $job->name = "Running Job";
170         $job->target = "test::test";
171         $job->save();
172         $jobid4 = $job->id;
173         // done job
174         $job = new SchedulersJob();
175         $job->status = SchedulersJob::JOB_STATUS_DONE;
176         $job->scheduler_id = 'unittest';
177         $job->date_entered = '2010-01-01 12:00:00';
178         $job->name = "Done Job";
179         $job->target = "test::test";
180         $job->save();
181         $jobid5 = $job->id;
182         // get the first one
183         $job = $this->jq->nextJob("unit test");
184         $this->assertEquals($jobid1, $job->id, "Wrong job fetched");
185         $this->assertEquals(SchedulersJob::JOB_STATUS_RUNNING, $job->status, "Wrong status");
186         $this->assertEquals("unit test", $job->client, "Wrong client");
187         // check that DB record matches
188         $job = new SchedulersJob();
189         $job->retrieve($jobid1);
190         $this->assertEquals(SchedulersJob::JOB_STATUS_RUNNING, $job->status, "Wrong status");
191         $this->assertEquals("unit test", $job->client, "Wrong client");
192         // get the second one
193         $job = $this->jq->nextJob("unit test");
194         $this->assertEquals($jobid2, $job->id, "Wrong job fetched");
195         $this->assertEquals(SchedulersJob::JOB_STATUS_RUNNING, $job->status, "Wrong status");
196         $this->assertEquals("unit test", $job->client, "Wrong client");
197         // try to get the third one, should get null
198         $job = $this->jq->nextJob("unit test");
199         $this->assertNull($job, "Extra job found");
200     }
201
202 }
203
204 class TestSugarJobQueue extends SugarJobQueue
205 {
206     public function getJob($jobId)
207     {
208         return parent::getJob($jobId);
209     }
210 }