| /* |
| * Licensed to the Apache Software Foundation (ASF) under one or more |
| * contributor license agreements. See the NOTICE file distributed with |
| * this work for additional information regarding copyright ownership. |
| * The ASF licenses this file to you under the Apache License, |
| * Version 2.0 (the "License"); you may not use this file except in |
| * compliance with the License. You may obtain a copy of the License at |
| * |
| * http://www.apache.org/licenses/LICENSE-2.0 |
| * |
| * Unless required by applicable law or agreed to in writing, software |
| * distributed under the License is distributed on an "AS IS" BASIS, |
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| * See the License for the specific language governing permissions and |
| * limitations under the License. |
| */ |
| package org.apache.pivot.util.concurrent.test; |
| |
| import org.apache.pivot.util.concurrent.ParallelTaskGroup; |
| import org.apache.pivot.util.concurrent.SerialTaskGroup; |
| import org.apache.pivot.util.concurrent.Task; |
| import org.apache.pivot.util.concurrent.TaskListener; |
| import org.junit.Test; |
| |
| public class TaskTest { |
| public static class SleepTask extends Task<Void> { |
| private long timeout = 0; |
| |
| public SleepTask(long timeout) { |
| this.timeout = timeout; |
| } |
| |
| @Override |
| public Void execute() { |
| System.out.println("Starting task " + this + "..."); |
| |
| try { |
| Thread.sleep(timeout); |
| } catch (InterruptedException exception) { |
| System.out.println(exception); |
| } |
| |
| System.out.println("...done"); |
| |
| return null; |
| } |
| |
| @Override |
| public String toString() { |
| return Long.toString(timeout); |
| } |
| } |
| |
| @Test |
| public void testTaskSequence() { |
| TaskListener<Void> taskListener = new TaskListener<Void>() { |
| @Override |
| public synchronized void taskExecuted(Task<Void> task) { |
| Throwable fault = task.getFault(); |
| |
| if (fault == null) { |
| System.out.println("EXECUTED"); |
| } else { |
| System.out.println("FAILED: " + fault); |
| } |
| |
| notify(); |
| } |
| }; |
| |
| SerialTaskGroup taskGroup = new SerialTaskGroup(); |
| |
| SleepTask task1 = new SleepTask(500); |
| taskGroup.getTasks().add(task1); |
| |
| SleepTask task2 = new SleepTask(1000); |
| taskGroup.getTasks().add(task2); |
| |
| SleepTask task3 = new SleepTask(2000); |
| taskGroup.getTasks().add(task3); |
| |
| synchronized (taskListener) { |
| taskGroup.execute(taskListener); |
| |
| try { |
| taskListener.wait(); |
| } catch (InterruptedException exception) { |
| } |
| } |
| } |
| |
| @Test |
| public void testTaskGroup() { |
| TaskListener<Void> taskListener = new TaskListener<Void>() { |
| @Override |
| public synchronized void taskExecuted(Task<Void> task) { |
| Throwable fault = task.getFault(); |
| |
| if (fault == null) { |
| System.out.println("EXECUTED"); |
| } else { |
| System.out.println("FAILED: " + fault); |
| } |
| |
| notify(); |
| } |
| }; |
| |
| ParallelTaskGroup taskGroup = new ParallelTaskGroup(); |
| |
| SleepTask task1 = new SleepTask(500); |
| taskGroup.getTasks().add(task1); |
| |
| SleepTask task2 = new SleepTask(1000); |
| taskGroup.getTasks().add(task2); |
| |
| SleepTask task3 = new SleepTask(2000); |
| taskGroup.getTasks().add(task3); |
| |
| synchronized (taskListener) { |
| taskGroup.execute(taskListener); |
| |
| try { |
| taskListener.wait(); |
| } catch (InterruptedException exception) { |
| } |
| } |
| } |
| } |