2
* Licensed to the Apache Software Foundation (ASF) under one or more
3
* contributor license agreements. See the NOTICE file distributed with
4
* this work for additional information regarding copyright ownership.
5
* The ASF licenses this file to You under the Apache License, Version 2.0
6
* (the "License"); you may not use this file except in compliance with
7
* the License. You may obtain a copy of the License at
9
* http://www.apache.org/licenses/LICENSE-2.0
11
* Unless required by applicable law or agreed to in writing, software
12
* distributed under the License is distributed on an "AS IS" BASIS,
13
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14
* See the License for the specific language governing permissions and
15
* limitations under the License.
17
package org.apache.tomcat.util.threads;
19
import junit.framework.TestCase;
21
public class TestCounterLatch extends TestCase {
23
private volatile CounterLatch latch = null;
26
public void tearDown() {
27
CounterLatch temp = latch;
28
if (temp!=null) temp.releaseAll();
32
public void testNoThreads() throws Exception {
33
latch = new CounterLatch(0,0);
34
assertEquals("No threads should be waiting", false, latch.hasQueuedThreads());
37
public void testOneThreadNoWait() throws Exception {
38
latch = new CounterLatch(0,1);
39
assertEquals("No threads should be waiting", false, latch.hasQueuedThreads());
40
Thread testThread = new Thread() {
45
} catch (InterruptedException x) {
52
assertEquals("0 threads should be waiting", 0, latch.getQueuedThreads().size());
55
assertEquals("No threads should be waiting", false, latch.hasQueuedThreads());
58
public void testOneThreadWaitCountUp() throws Exception {
59
latch = new CounterLatch(0,1);
60
assertEquals("No threads should be waiting", false, latch.hasQueuedThreads());
61
Thread testThread = new Thread() {
66
} catch (InterruptedException x) {
74
assertEquals("1 threads should be waiting", 1, latch.getQueuedThreads().size());
77
assertEquals("No threads should be waiting", false, latch.hasQueuedThreads());
80
public void testOneThreadWaitCountDown() throws Exception {
81
latch = new CounterLatch(1,0);
82
assertEquals("No threads should be waiting", false, latch.hasQueuedThreads());
83
Thread testThread = new Thread() {
87
//System.out.println("Entering ["+Thread.currentThread().getName()+"]");
89
} catch (InterruptedException x) {
92
//System.out.println("Exiting ["+Thread.currentThread().getName()+"]");
98
assertEquals("1 threads should be waiting", 1, latch.getQueuedThreads().size());
101
assertEquals("No threads should be waiting", false, latch.hasQueuedThreads());
104
public void testOneRelease() throws Exception {
105
latch = new CounterLatch(1,0);
106
assertEquals("No threads should be waiting", false, latch.hasQueuedThreads());
107
Thread testThread = new Thread() {
112
} catch (InterruptedException x) {
120
assertEquals("1 threads should be waiting", 1, latch.getQueuedThreads().size());
123
assertEquals("No threads should be waiting", false, latch.hasQueuedThreads());