summaryrefslogtreecommitdiff
path: root/java/junit-toolkit/src/main/org/apache/qpid/junit/concurrency/ThreadTestExample.java
blob: b9865f2e220c6ce1a0673741725ba669aefc666b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
/*
 *
 * 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.qpid.junit.concurrency;

import org.apache.log4j.Logger;

/**
 * An example to illustrate the use of the {@link ThreadTestCoordinator} and {@link TestRunnable}s.
 *
 * <p/><table id="crc"><caption>CRC Card</caption>
 * <tr><th> Responsibilities <th> Collaborations
 * <tr><td> Demo multi-threaded testing.
 * </table>
 *
 * @author Rupert Smith
 */
public class ThreadTestExample
{
    /** Used for logging. */
    private static final Logger log = Logger.getLogger(ThreadTestExample.class);

    /** Test thread 1. */
    TestRunnable testThread1 =
        new TestRunnable()
        {
            public void runWithExceptions() throws Exception
            {
                log.debug("public void run(): called");
                log.info("in testThread0, block 1");

                // Wait for t2 to allow t1 to continue.
                allow(new int[] { 1 });
                waitFor(new int[] { 1 }, false);

                log.info("in testThread0, block 2");

                // Wait for t2 to allow t1 to continue. T2 is allowed to be blocked elsewhere than giving explicit
                // permission to allow t1 to continue.
                allow(new int[] { 1 });
                waitFor(new int[] { 1 }, true);

                log.info("in testThread0, block 3");

                // Release thread 2 from waiting on the shared lock.
                synchronized (sharedLock)
                {
                    sharedLock.notifyAll();
                }

                allow(new int[] { 1 });
            }
        };

    /** A shared lock between the test threads. */
    final Object sharedLock = new Object();

    /** Test thread 2. */
    TestRunnable testThread2 =
        new TestRunnable()
        {
            public void runWithExceptions() throws Exception
            {
                log.debug("public void run(): called");
                log.info("in testThread1, block 1");

                // Wait for t1 to allow t2 to continue.
                allow(new int[] { 0 });
                waitFor(new int[] { 0 }, false);

                log.info("in testThread1, block 2");

                // Wait on another resource. T1 should accept this as permission to continue.
                try
                {
                    synchronized (sharedLock)
                    {
                        log.debug("in testThread1, waiting on shared lock.");
                        sharedLock.wait();
                    }
                }
                catch (InterruptedException e)
                {
                    // Bail-out with a runtime if this happens.
                    throw new RuntimeException("Interrupted whilst waiting for shared lock.", e);
                }

                log.info("in testThread1, finished waiting on shared lock.");

                // allow(new int[] { 0 });

                // Wait for t1 to allow t2 to continue.
                waitFor(new int[] { 0 }, false);

                log.info("in testThread1, block 3");

                allow(new int[] { 0 });
            }
        };

    /**
     * Executes the test threads with coordination.
     *
     * @param args Ignored.
     */
    public void main(String[] args)
    {
        ThreadTestCoordinator tt = new ThreadTestCoordinator(2);

        tt.addTestThread(testThread1, 0);
        tt.addTestThread(testThread2, 1);
        tt.setDeadlockTimeout(500);
        tt.run();

        String errorMessage = tt.joinAndRetrieveMessages();

        // Print any error messages or exceptions.
        log.info(errorMessage);

        if (!tt.getExceptions().isEmpty())
        {
            for (Exception e : tt.getExceptions())
            {
                log.warn("Exception thrown during test thread: ", e);
            }
        }
    }
}