~ubuntu-branches/ubuntu/oneiric/tomcat7/oneiric-security

« back to all changes in this revision

Viewing changes to test/org/apache/tomcat/util/threads/TestCounterLatch.java

  • Committer: Bazaar Package Importer
  • Author(s): tony mancill, Miguel Landaeta, tony mancill
  • Date: 2011-06-23 20:26:29 UTC
  • mfrom: (1.1.1 upstream)
  • Revision ID: james.westby@ubuntu.com-20110623202629-w1v0jejj19swux10
Tags: 7.0.16-1
[ Miguel Landaeta ]
* New upstream release.
* Add missing deps and symlinks for commons-pool ands commons-dbcp jars.

[ tony mancill ]
* Add logrotate file for catalina.out.
* Add build-arch target to debian/rules.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/*
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
8
 
 *
9
 
 *      http://www.apache.org/licenses/LICENSE-2.0
10
 
 *
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.
16
 
 */
17
 
package org.apache.tomcat.util.threads;
18
 
 
19
 
import junit.framework.TestCase;
20
 
 
21
 
public class TestCounterLatch extends TestCase {
22
 
 
23
 
    private volatile CounterLatch latch = null;
24
 
 
25
 
    @Override
26
 
    public void tearDown() {
27
 
        CounterLatch temp = latch;
28
 
        if (temp!=null) temp.releaseAll();
29
 
        latch = null;
30
 
    }
31
 
 
32
 
    public void testNoThreads() throws Exception {
33
 
        latch = new CounterLatch(0,0);
34
 
        assertEquals("No threads should be waiting", false, latch.hasQueuedThreads());
35
 
    }
36
 
 
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() {
41
 
            @Override
42
 
            public void run() {
43
 
                try {
44
 
                    latch.await();
45
 
                } catch (InterruptedException x) {
46
 
                    x.printStackTrace();
47
 
                }
48
 
            }
49
 
        };
50
 
        testThread.start();
51
 
        Thread.sleep(50);
52
 
        assertEquals("0 threads should be waiting", 0, latch.getQueuedThreads().size());
53
 
        latch.countUp();
54
 
        Thread.sleep(50);
55
 
        assertEquals("No threads should be waiting", false, latch.hasQueuedThreads());
56
 
    }
57
 
 
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() {
62
 
            @Override
63
 
            public void run() {
64
 
                try {
65
 
                    latch.await();
66
 
                } catch (InterruptedException x) {
67
 
                    x.printStackTrace();
68
 
                }
69
 
            }
70
 
        };
71
 
        latch.countUp();
72
 
        testThread.start();
73
 
        Thread.sleep(50);
74
 
        assertEquals("1 threads should be waiting", 1, latch.getQueuedThreads().size());
75
 
        latch.countUp();
76
 
        Thread.sleep(50);
77
 
        assertEquals("No threads should be waiting", false, latch.hasQueuedThreads());
78
 
    }
79
 
 
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() {
84
 
            @Override
85
 
            public void run() {
86
 
                try {
87
 
                    //System.out.println("Entering ["+Thread.currentThread().getName()+"]");
88
 
                    latch.await();
89
 
                } catch (InterruptedException x) {
90
 
                    x.printStackTrace();
91
 
                }
92
 
                //System.out.println("Exiting ["+Thread.currentThread().getName()+"]");
93
 
            }
94
 
        };
95
 
        latch.countDown();
96
 
        testThread.start();
97
 
        Thread.sleep(50);
98
 
        assertEquals("1 threads should be waiting", 1, latch.getQueuedThreads().size());
99
 
        latch.countDown();
100
 
        Thread.sleep(50);
101
 
        assertEquals("No threads should be waiting", false, latch.hasQueuedThreads());
102
 
    }
103
 
    
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() {
108
 
            @Override
109
 
            public void run() {
110
 
                try {
111
 
                    latch.await();
112
 
                } catch (InterruptedException x) {
113
 
                    x.printStackTrace();
114
 
                }
115
 
            }
116
 
        };
117
 
        latch.countDown();
118
 
        testThread.start();
119
 
        Thread.sleep(50);
120
 
        assertEquals("1 threads should be waiting", 1, latch.getQueuedThreads().size());
121
 
        latch.releaseAll();
122
 
        Thread.sleep(50);
123
 
        assertEquals("No threads should be waiting", false, latch.hasQueuedThreads());
124
 
    }    
125
 
}