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 'modules/SchedulersJobs/SchedulersJob.php';
40 class JobQueueTest extends Sugar_PHPUnit_Framework_TestCase
42 public function setUp()
44 $this->jq = new TestSugarJobQueue();
47 public function tearDown()
49 $GLOBALS['db']->query("DELETE FROM job_queue WHERE scheduler_id='unittest'");
50 SugarTestUserUtilities::removeAllCreatedAnonymousUsers();
53 public function testSubmitJob()
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);
65 $this->assertNotEmpty($id, "Bad job ID");
66 $job = new SchedulersJob();
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");
73 public function testGetJob()
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);
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");
89 $job = $this->jq->getJob("nosuchjob");
90 $this->assertNull($job, "Bad return on non-existing job");
93 public function testCleanup()
95 $job = new SchedulersJob();
96 $job->update_date_modified = false;
97 $job->status = SchedulersJob::JOB_STATUS_RUNNING;
98 $job->scheduler_id = 'unittest';
99 $job->execute_time = $GLOBALS['timedate']->nowDb();
100 $job->date_entered = '2010-01-01 12:00:00';
101 $job->date_modified = '2010-01-01 12:00:00';
102 $job->name = "Unit test Job 1";
103 $job->target = "test::test";
106 $this->jq->cleanup();
108 $job = new SchedulersJob();
109 $job->retrieve($jobid);
110 $this->assertEquals(SchedulersJob::JOB_STATUS_DONE, $job->status, "Wrong status");
111 $this->assertEquals(SchedulersJob::JOB_FAILURE, $job->resolution, "Wrong resolution");
114 public function testDelete()
116 $job = new SchedulersJob();
117 $job->status = SchedulersJob::JOB_STATUS_RUNNING;
118 $job->scheduler_id = 'unittest';
119 $job->name = "Unit test Job 1";
120 $job->target = "test::test";
123 $this->jq->deleteJob($jobid);
125 $job = new SchedulersJob();
126 $job->retrieve($jobid);
127 $this->assertEmpty($job->id, "Job not deleted");
130 public function testGetNextJob()
132 // should get only jobs with status QUEUED, in date_entered order, and mark them as running
133 // Clean up the queue
134 $GLOBALS['db']->query("DELETE FROM job_queue WHERE status='".SchedulersJob::JOB_STATUS_QUEUED."'");
135 $job = $this->jq->nextJob("unit test");
136 $this->assertNull($job, "Extra job found");
138 $job = new SchedulersJob();
139 $job->status = SchedulersJob::JOB_STATUS_QUEUED;
140 $job->scheduler_id = 'unittest';
141 $job->date_entered = '2010-01-01 12:00:00';
142 $job->name = "Old Job";
143 $job->target = "test::test";
146 // another job, later date
147 $job = new SchedulersJob();
148 $job->status = SchedulersJob::JOB_STATUS_QUEUED;
149 $job->scheduler_id = 'unittest';
150 $job->date_entered = '2012-01-01 12:00:00';
151 $job->name = "Newer Job";
152 $job->target = "test::test";
155 // job with execute date in the future
156 $job = new SchedulersJob();
157 $job->status = SchedulersJob::JOB_STATUS_QUEUED;
158 $job->scheduler_id = 'unittest';
159 $job->execute_time = $GLOBALS['timedate']->getNow()->modify("+3 days")->asDb();
160 $job->date_entered = '2010-01-01 12:00:00';
161 $job->name = "Future Job";
162 $job->target = "test::test";
166 $job = new SchedulersJob();
167 $job->status = SchedulersJob::JOB_STATUS_RUNNING;
168 $job->scheduler_id = 'unittest';
169 $job->date_entered = '2010-01-01 12:00:00';
170 $job->name = "Running Job";
171 $job->target = "test::test";
175 $job = new SchedulersJob();
176 $job->status = SchedulersJob::JOB_STATUS_DONE;
177 $job->scheduler_id = 'unittest';
178 $job->date_entered = '2010-01-01 12:00:00';
179 $job->name = "Done Job";
180 $job->target = "test::test";
184 $job = $this->jq->nextJob("unit test");
185 $this->assertEquals($jobid1, $job->id, "Wrong job fetched");
186 $this->assertEquals(SchedulersJob::JOB_STATUS_RUNNING, $job->status, "Wrong status");
187 $this->assertEquals("unit test", $job->client, "Wrong client");
188 // check that DB record matches
189 $job = new SchedulersJob();
190 $job->retrieve($jobid1);
191 $this->assertEquals(SchedulersJob::JOB_STATUS_RUNNING, $job->status, "Wrong status");
192 $this->assertEquals("unit test", $job->client, "Wrong client");
193 // get the second one
194 $job = $this->jq->nextJob("unit test");
195 $this->assertEquals($jobid2, $job->id, "Wrong job fetched");
196 $this->assertEquals(SchedulersJob::JOB_STATUS_RUNNING, $job->status, "Wrong status");
197 $this->assertEquals("unit test", $job->client, "Wrong client");
198 // try to get the third one, should get null
199 $job = $this->jq->nextJob("unit test");
200 $this->assertNull($job, "Extra job found");
205 class TestSugarJobQueue extends SugarJobQueue
207 public function getJob($jobId)
209 return parent::getJob($jobId);