summaryrefslogtreecommitdiff
path: root/qpid/java/broker/src/test/java/org/apache/qpid/server/queue/QueueEntryImplThreadingTest.java
blob: d8d99dd8f94eccff7e00e7d4d9801c2e9f8331ab (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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
/*
 *
 * 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.server.queue;

import junit.framework.TestCase;
import org.apache.qpid.AMQException;
import org.apache.qpid.framing.BasicContentHeaderProperties;

import java.util.concurrent.TimeUnit;
import java.util.concurrent.locks.Condition;
import java.util.concurrent.locks.ReentrantLock;

public class QueueEntryImplThreadingTest extends TestCase
{
    private volatile boolean _running;
    private volatile Error _error;
    ReentrantLock _waitLock = new ReentrantLock();
    private final String _testName = this.getName();

    /** Test the Redelivered state of a QueueEntryImpl */
    public void test() throws AMQException, InterruptedException
    {
        final MockAMQMessage message = new MockAMQMessage(1L);

        ((BasicContentHeaderProperties) message.getContentHeaderBody().properties).setAppId(_testName);

        final QueueEntry entry = new MockQueueEntry(message);

        Runnable unloader = new Runnable()
        {
            public void run()
            {
                try
                {

                    while (_running)
                    {
                        entry.unload();
                    }
                }
                catch (Error e)
                {
                    signalError("Error whilst Unloading:", e);
                }
            }
        };

        Runnable loader = new Runnable()
        {
            public void run()
            {
                try
                {
                    while (_running)
                    {
                        AMQMessage loaded = entry.load();
                        assertNotNull("Entry Returned Null Message.", loaded);
                        assertEquals("Message ID not correct on loaded message.", message.getMessageId(),
                                     loaded.getMessageId());
                        assertEquals("Message Arrival time not correctly retrieved.", message.getArrivalTime(),
                                     loaded.getArrivalTime());
                        assertEquals("Custom AppId not correctly retrieved.", _testName,
                                     ((BasicContentHeaderProperties) loaded.getContentHeaderBody().properties).
                                             getAppIdAsString());
                    }
                }
                catch (Error e)
                {
                    signalError("Error whilst Loading:", e);
                }
            }
        };

        _running = true;
        //Start a couple of unloaders
        Thread unloadThread = new Thread(unloader);
        unloadThread.start();
        Thread unloadThread2 = new Thread(unloader);
        unloadThread2.start();

        //Start a couple of loaders
        Thread loadThread = new Thread(loader);
        loadThread.start();
        Thread loadThread2 = new Thread(loader);
        loadThread2.start();

        //Run for 3 seconds.
        long sleep = 3000;
        Condition wait = _waitLock.newCondition();
        while (sleep > 0 && _error == null)
        {
            try
            {
                _waitLock.lock();

                sleep = wait.awaitNanos(TimeUnit.MILLISECONDS.toNanos(sleep));
            }
            catch (InterruptedException e)
            {
                //Stop if we are interrupted
                fail(e.getMessage());
            }
            finally
            {
                _waitLock.unlock();
            }

        }

        _running = false;

        unloadThread.join();
        unloadThread2.join();

        loadThread.join();
        loadThread2.join();

        if (_error != null)
        {
            throw _error;
        }

    }

    private void signalError(String message, Error e)
    {
        System.err.println(message + e.getMessage());
        e.printStackTrace();
        _error = e;
        try
        {
            _waitLock.lock();
            _waitLock.notify();
        }
        finally
        {
            _waitLock.unlock();
        }

    }

}