2 * Copyright 2011, Big Switch Networks, Inc.
4 * This program and the accompanying materials are made available under the
5 * terms of the Eclipse Public License v1.0 which accompanies this distribution,
6 * and is available at http://www.eclipse.org/legal/epl-v10.html
9 package org.opendaylight.groupbasedpolicy.util;
11 import static org.junit.Assert.assertEquals;
12 import static org.junit.Assert.assertFalse;
13 import static org.junit.Assert.assertTrue;
15 import java.util.concurrent.Executors;
16 import java.util.concurrent.ScheduledExecutorService;
17 import java.util.concurrent.TimeUnit;
19 import org.junit.Before;
20 import org.junit.Test;
22 public class SingletonTaskTest {
25 public int finished = 0;
29 public void setUp() throws Exception {
36 public void testBasic() throws InterruptedException {
37 ScheduledExecutorService ses =
38 Executors.newSingleThreadScheduledExecutor();
40 SingletonTask st1 = new SingletonTask(ses, new Runnable() {
46 st1.reschedule(0, null);
48 ses.awaitTermination(5, TimeUnit.SECONDS);
50 assertEquals("Check that task ran", 1, ran);
54 public void testDelay() throws InterruptedException {
55 ScheduledExecutorService ses =
56 Executors.newSingleThreadScheduledExecutor();
58 SingletonTask st1 = new SingletonTask(ses, new Runnable() {
62 time = System.nanoTime();
65 long start = System.nanoTime();
66 st1.reschedule(10, TimeUnit.MILLISECONDS);
67 assertFalse("Check that task hasn't run yet", ran > 0);
70 ses.awaitTermination(5, TimeUnit.SECONDS);
72 assertEquals("Check that task ran", 1, ran);
73 assertTrue("Check that time passed appropriately",
74 (time - start) >= TimeUnit.NANOSECONDS.convert(10, TimeUnit.MILLISECONDS));
78 public void testReschedule() throws InterruptedException {
79 ScheduledExecutorService ses =
80 Executors.newSingleThreadScheduledExecutor();
82 final Object tc = this;
83 SingletonTask st1 = new SingletonTask(ses, new Runnable() {
89 time = System.nanoTime();
92 long start = System.nanoTime();
93 st1.reschedule(20, TimeUnit.MILLISECONDS);
95 assertFalse("Check that task hasn't run yet", ran > 0);
96 st1.reschedule(20, TimeUnit.MILLISECONDS);
98 assertFalse("Check that task hasn't run yet", ran > 0);
99 st1.reschedule(20, TimeUnit.MILLISECONDS);
101 assertFalse("Check that task hasn't run yet", ran > 0);
102 st1.reschedule(20, TimeUnit.MILLISECONDS);
104 assertFalse("Check that task hasn't run yet", ran > 0);
105 st1.reschedule(20, TimeUnit.MILLISECONDS);
107 assertFalse("Check that task hasn't run yet", ran > 0);
108 st1.reschedule(20, TimeUnit.MILLISECONDS);
110 assertFalse("Check that task hasn't run yet", ran > 0);
111 st1.reschedule(20, TimeUnit.MILLISECONDS);
113 assertFalse("Check that task hasn't run yet", ran > 0);
114 st1.reschedule(20, TimeUnit.MILLISECONDS);
116 assertFalse("Check that task hasn't run yet", ran > 0);
119 ses.awaitTermination(5, TimeUnit.SECONDS);
121 assertEquals("Check that task ran only once", 1, ran);
122 assertTrue("Check that time passed appropriately: " + (time - start),
123 (time - start) >= TimeUnit.NANOSECONDS.convert(55, TimeUnit.MILLISECONDS));
127 public void testConcurrentAddDelay() throws InterruptedException {
128 ScheduledExecutorService ses =
129 Executors.newSingleThreadScheduledExecutor();
131 final Object tc = this;
132 SingletonTask st1 = new SingletonTask(ses, new Runnable() {
140 } catch (InterruptedException e) {
145 time = System.nanoTime();
150 long start = System.nanoTime();
151 st1.reschedule(5, TimeUnit.MILLISECONDS);
153 assertEquals("Check that task started", 1, ran);
154 assertEquals("Check that task not finished", 0, finished);
155 st1.reschedule(75, TimeUnit.MILLISECONDS);
156 assertTrue("Check task running state true", st1.context.taskRunning);
157 assertTrue("Check task should run state true", st1.context.taskShouldRun);
158 assertEquals("Check that task started", 1, ran);
159 assertEquals("Check that task not finished", 0, finished);
163 assertTrue("Check task running state false", !st1.context.taskRunning);
164 assertTrue("Check task should run state false", !st1.context.taskShouldRun);
165 assertEquals("Check that task ran exactly twice", 2, ran);
166 assertEquals("Check that task finished exactly twice", 2, finished);
168 assertTrue("Check that time passed appropriately: " + (time - start),
169 (time - start) >= TimeUnit.NANOSECONDS.convert(130, TimeUnit.MILLISECONDS));
170 assertTrue("Check that time passed appropriately: " + (time - start),
171 (time - start) <= TimeUnit.NANOSECONDS.convert(500, TimeUnit.MILLISECONDS));
174 ses.awaitTermination(15, TimeUnit.SECONDS);
178 public void testConcurrentAddDelay2() throws InterruptedException {
179 ScheduledExecutorService ses =
180 Executors.newSingleThreadScheduledExecutor();
182 final Object tc = this;
183 SingletonTask st1 = new SingletonTask(ses, new Runnable() {
191 } catch (InterruptedException e) {
196 time = System.nanoTime();
201 long start = System.nanoTime();
202 st1.reschedule(5, TimeUnit.MILLISECONDS);
204 assertEquals("Check that task started", 1, ran);
205 assertEquals("Check that task not finished", 0, finished);
206 st1.reschedule(25, TimeUnit.MILLISECONDS);
207 assertTrue("Check task running state true", st1.context.taskRunning);
208 assertTrue("Check task should run state true", st1.context.taskShouldRun);
209 assertEquals("Check that task started", 1, ran);
210 assertEquals("Check that task not finished", 0, finished);
214 assertTrue("Check task running state false", !st1.context.taskRunning);
215 assertTrue("Check task should run state false", !st1.context.taskShouldRun);
216 assertEquals("Check that task ran exactly twice", 2, ran);
217 assertEquals("Check that task finished exactly twice", 2, finished);
219 assertTrue("Check that time passed appropriately: " + (time - start),
220 (time - start) >= TimeUnit.NANOSECONDS.convert(100, TimeUnit.MILLISECONDS));
221 assertTrue("Check that time passed appropriately: " + (time - start),
222 (time - start) <= TimeUnit.NANOSECONDS.convert(500, TimeUnit.MILLISECONDS));
225 ses.awaitTermination(5, TimeUnit.SECONDS);
230 public void testConcurrentAddNoDelay() throws InterruptedException {
231 ScheduledExecutorService ses =
232 Executors.newSingleThreadScheduledExecutor();
234 final Object tc = this;
235 SingletonTask st1 = new SingletonTask(ses, new Runnable() {
243 } catch (InterruptedException e) {
248 time = System.nanoTime();
253 long start = System.nanoTime();
254 st1.reschedule(0, null);
256 assertEquals("Check that task started", 1, ran);
257 assertEquals("Check that task not finished", 0, finished);
258 st1.reschedule(0, null);
259 assertTrue("Check task running state true", st1.context.taskRunning);
260 assertTrue("Check task should run state true", st1.context.taskShouldRun);
261 assertEquals("Check that task started", 1, ran);
262 assertEquals("Check that task not finished", 0, finished);
266 assertTrue("Check task running state false", !st1.context.taskRunning);
267 assertTrue("Check task should run state false", !st1.context.taskShouldRun);
268 assertEquals("Check that task ran exactly twice", 2, ran);
269 assertEquals("Check that task finished exactly twice", 2, finished);
271 assertTrue("Check that time passed appropriately: " + (time - start),
272 (time - start) >= TimeUnit.NANOSECONDS.convert(90, TimeUnit.MILLISECONDS));
273 assertTrue("Check that time passed appropriately: " + (time - start),
274 (time - start) <= TimeUnit.NANOSECONDS.convert(500, TimeUnit.MILLISECONDS));
277 ses.awaitTermination(5, TimeUnit.SECONDS);