001: /*
002: * Copyright 2001-2007 Geert Bevin <gbevin[remove] at uwyn dot com>
003: * Distributed under the terms of either:
004: * - the common development and distribution license (CDDL), v1.0; or
005: * - the GNU Lesser General Public License, v2.1 or later
006: * $Id: TestMemoryScheduler.java 3634 2007-01-08 21:42:24Z gbevin $
007: */
008: package com.uwyn.rife.scheduler.schedulermanagers;
009:
010: import com.uwyn.rife.scheduler.Executor;
011: import com.uwyn.rife.scheduler.Scheduler;
012: import com.uwyn.rife.scheduler.Task;
013: import com.uwyn.rife.scheduler.TaskManager;
014: import com.uwyn.rife.scheduler.TestTasktypes;
015: import com.uwyn.rife.scheduler.exceptions.FrequencyException;
016: import com.uwyn.rife.scheduler.exceptions.NoExecutorForTasktypeException;
017: import com.uwyn.rife.scheduler.exceptions.SchedulerException;
018: import com.uwyn.rife.scheduler.exceptions.TaskManagerException;
019: import com.uwyn.rife.scheduler.exceptions.UnableToRetrieveTasksToProcessException;
020: import com.uwyn.rife.tools.ExceptionUtils;
021: import java.util.ArrayList;
022: import java.util.Calendar;
023: import java.util.Collection;
024: import java.util.Date;
025: import junit.framework.TestCase;
026:
027: public class TestMemoryScheduler extends TestCase {
028: public TestMemoryScheduler(String name) {
029: super (name);
030: }
031:
032: public void testInstantiateScheduler() {
033: Scheduler scheduler = new MemoryScheduler().getScheduler();
034: assertNotNull(scheduler);
035: }
036:
037: public void testStartStopScheduler() {
038: Scheduler scheduler = new MemoryScheduler().getScheduler();
039: try {
040: scheduler.start();
041: synchronized (scheduler) {
042: scheduler.interrupt();
043:
044: try {
045: scheduler.wait();
046: } catch (InterruptedException e) {
047: assertTrue(
048: ExceptionUtils.getExceptionStackTrace(e),
049: false);
050: }
051: }
052: } catch (NoExecutorForTasktypeException e) {
053: assertTrue(ExceptionUtils.getExceptionStackTrace(e), false);
054: } catch (UnableToRetrieveTasksToProcessException e) {
055: assertTrue(ExceptionUtils.getExceptionStackTrace(e), false);
056: }
057: }
058:
059: public void testAddExecutor() {
060: Scheduler scheduler = new MemoryScheduler().getScheduler();
061: Executor executor = new TestExecutor();
062:
063: assertNull(scheduler.getExecutor(executor.getHandledTasktype()));
064: try {
065: scheduler.addExecutor(executor);
066: } catch (SchedulerException e) {
067: assertTrue(ExceptionUtils.getExceptionStackTrace(e), false);
068: }
069: assertEquals(executor, scheduler.getExecutor(executor
070: .getHandledTasktype()));
071: assertTrue(scheduler.removeExecutor(executor));
072: }
073:
074: public void testOneshotTaskExecution() {
075: int sleeptime = 60 * 1000;
076: Scheduler scheduler = new MemoryScheduler().getScheduler();
077: TestExecutor executor = new TestExecutor();
078: TaskManager taskmanager = scheduler.getTaskManager();
079: Task task = new Task();
080:
081: try {
082: task.setType(TestTasktypes.UPLOAD_GROUPS);
083: task.setPlanned(System.currentTimeMillis());
084: task.setFrequency(null);
085: task.setBusy(false);
086: } catch (FrequencyException e) {
087: assertTrue(ExceptionUtils.getExceptionStackTrace(e), false);
088: }
089:
090: try {
091: scheduler.addExecutor(executor);
092: } catch (SchedulerException e) {
093: assertTrue(ExceptionUtils.getExceptionStackTrace(e), false);
094: }
095: scheduler.setSleepTime(sleeptime);
096: try {
097: task.setId(taskmanager.addTask(task));
098: task = taskmanager.getTask(task.getId());
099: } catch (TaskManagerException e) {
100: assertTrue(ExceptionUtils.getExceptionStackTrace(e), false);
101: }
102:
103: try {
104: scheduler.start();
105: try {
106: Thread.sleep(sleeptime * 2);
107: } catch (InterruptedException e) {
108: assertTrue(ExceptionUtils.getExceptionStackTrace(e),
109: false);
110: }
111: synchronized (scheduler) {
112: scheduler.interrupt();
113:
114: try {
115: scheduler.wait();
116: } catch (InterruptedException e) {
117: assertTrue(
118: ExceptionUtils.getExceptionStackTrace(e),
119: false);
120: }
121: }
122:
123: Collection<Task> executed_tasks = executor
124: .getExecutedTasks();
125: assertEquals(1, executed_tasks.size());
126: Task executed_task = executed_tasks.iterator().next();
127: assertTrue(task.equals(executed_task));
128: assertSame(executed_task.getTaskManager(), taskmanager);
129: } catch (NoExecutorForTasktypeException e) {
130: assertTrue(ExceptionUtils.getExceptionStackTrace(e), false);
131: } catch (UnableToRetrieveTasksToProcessException e) {
132: assertTrue(ExceptionUtils.getExceptionStackTrace(e), false);
133: }
134: }
135:
136: public void testRepeatingTaskExecution() {
137: int scheduler_sleeptime = 30 * 1000; // 30 seconds
138: int task_frequency = 60 * 1000; // 1 minute
139: int thread_sleeptime = scheduler_sleeptime * 6; // 3 minutes
140: Scheduler scheduler = new MemoryScheduler().getScheduler();
141: TestExecutor executor = new TestExecutor();
142: TaskManager taskmanager = scheduler.getTaskManager();
143: Task task = new Task();
144:
145: try {
146: task.setType(TestTasktypes.UPLOAD_GROUPS);
147: // set back a while in the past to test the catch up rescheduling
148: task.setPlanned(System.currentTimeMillis()
149: - (scheduler_sleeptime * 10));
150: task.setFrequency("* * * * *");
151: task.setBusy(false);
152: } catch (FrequencyException e) {
153: assertTrue(ExceptionUtils.getExceptionStackTrace(e), false);
154: }
155:
156: scheduler.setSleepTime(scheduler_sleeptime);
157:
158: try {
159: scheduler.addExecutor(executor);
160: } catch (SchedulerException e) {
161: assertTrue(ExceptionUtils.getExceptionStackTrace(e), false);
162: }
163:
164: try {
165: task.setId(taskmanager.addTask(task));
166: task = taskmanager.getTask(task.getId());
167: } catch (TaskManagerException e) {
168: assertTrue(ExceptionUtils.getExceptionStackTrace(e), false);
169: }
170:
171: Collection<Task> executed_tasks = null;
172: int executed_tasks_size = -1;
173: try {
174: scheduler.start();
175: try {
176: Thread.sleep(thread_sleeptime);
177: executed_tasks = executor.getExecutedTasks();
178: executed_tasks_size = executed_tasks.size();
179: } catch (InterruptedException e) {
180: assertTrue(ExceptionUtils.getExceptionStackTrace(e),
181: false);
182: }
183: synchronized (scheduler) {
184: scheduler.interrupt();
185:
186: try {
187: scheduler.wait();
188: } catch (InterruptedException e) {
189: assertTrue(
190: ExceptionUtils.getExceptionStackTrace(e),
191: false);
192: }
193: }
194:
195: // task frequency fits in the thread sleep time
196: long number_of_executions = (thread_sleeptime / task_frequency) + 1;
197:
198: // System.out.println("\nMemory\n"+executor.getFirstExecution().getTime().getTime()+" : "+executor.getFirstExecution().getTime().toGMTString()+"\n"+now.getTime()+" : "+now.toGMTString()+"\ntask_frequency = "+task_frequency+"\nnumber_of_executions = "+number_of_executions+"\nexecuted_tasks_size = "+executed_tasks_size);
199: Date now = new Date();
200: assertTrue(
201: "\nFAILED Memory\n"
202: + executor.getFirstExecution().getTime()
203: .getTime()
204: + " : "
205: + executor.getFirstExecution().getTime()
206: .toGMTString() + "\n"
207: + now.getTime() + " : " + now.toGMTString()
208: + "\ntask_frequency = " + task_frequency
209: + "\nnumber_of_executions = "
210: + number_of_executions
211: + "\nexecuted_tasks_size = "
212: + executed_tasks_size,
213: number_of_executions == executed_tasks_size
214: || number_of_executions == executed_tasks_size + 1);
215: for (Task executed_task : executed_tasks) {
216: assertEquals(task.getId(), executed_task.getId());
217: assertEquals(task.getType(), executed_task.getType());
218: assertEquals(task.getFrequency(), executed_task
219: .getFrequency());
220: assertTrue(task.getPlanned() <= executed_task
221: .getPlanned());
222: assertSame(executed_task.getTaskManager(), taskmanager);
223: }
224:
225: try {
226: taskmanager.removeTask(task.getId());
227: } catch (TaskManagerException e) {
228: assertTrue(ExceptionUtils.getExceptionStackTrace(e),
229: false);
230: }
231: } catch (NoExecutorForTasktypeException e) {
232: assertTrue(ExceptionUtils.getExceptionStackTrace(e), false);
233: } catch (UnableToRetrieveTasksToProcessException e) {
234: assertTrue(ExceptionUtils.getExceptionStackTrace(e), false);
235: }
236: }
237:
238: class TestExecutor extends Executor {
239: private Calendar mFirstExecution = null;
240: private ArrayList<Task> mExecutedTasks = null;
241:
242: public TestExecutor() {
243: mExecutedTasks = new ArrayList<Task>();
244: }
245:
246: public boolean executeTask(Task task) {
247: synchronized (this ) {
248: if (null == mFirstExecution) {
249: mFirstExecution = Calendar.getInstance();
250: mFirstExecution.setTimeInMillis(System
251: .currentTimeMillis());
252: }
253: mExecutedTasks.add(task);
254: }
255:
256: return true;
257: }
258:
259: public Collection<Task> getExecutedTasks() {
260: synchronized (this ) {
261: return mExecutedTasks;
262: }
263: }
264:
265: public Calendar getFirstExecution() {
266: synchronized (this ) {
267: return mFirstExecution;
268: }
269: }
270:
271: public String getHandledTasktype() {
272: return TestTasktypes.UPLOAD_GROUPS;
273: }
274:
275: protected long getRescheduleDelay() {
276: return 100;
277: }
278: }
279: }
|