|
1 |
| -// Copyright (c) 2007-2020 VMware, Inc. or its affiliates. All rights reserved. |
| 1 | +// Copyright (c) 2007-2021 VMware, Inc. or its affiliates. All rights reserved. |
2 | 2 | //
|
3 | 3 | // This software, the RabbitMQ Java client library, is triple-licensed under the
|
4 | 4 | // Mozilla Public License 2.0 ("MPL"), the GNU General Public License version 2
|
|
15 | 15 |
|
16 | 16 | package com.rabbitmq.client.test;
|
17 | 17 |
|
18 |
| -import org.junit.Test; |
| 18 | +import static org.junit.Assert.assertNull; |
| 19 | +import static org.junit.Assert.assertTrue; |
19 | 20 |
|
20 | 21 | import java.io.IOException;
|
21 |
| - |
22 |
| -import static org.junit.Assert.*; |
| 22 | +import java.util.concurrent.CountDownLatch; |
| 23 | +import java.util.concurrent.TimeUnit; |
| 24 | +import org.junit.Test; |
23 | 25 |
|
24 | 26 | /**
|
25 |
| - * Test for bug 20004 - deadlock through internal synchronization on |
26 |
| - * the channel object. This is more properly a unit test, but since it |
27 |
| - * requires a connection to a broker, it's grouped with the functional |
28 |
| - * tests. |
29 |
| - * <p/> |
30 |
| - * Test calls channel.queueDeclare, while synchronising on channel, from |
31 |
| - * an independent thread. |
| 27 | + * Test for bug 20004 - deadlock through internal synchronization on the channel object. This is |
| 28 | + * more properly a unit test, but since it requires a connection to a broker, it's grouped with the |
| 29 | + * functional tests. |
| 30 | + * |
| 31 | + * <p>Test calls channel.queueDeclare, while synchronising on channel, from an independent thread. |
32 | 32 | */
|
33 | 33 | public class Bug20004Test extends BrokerTestCase {
|
34 |
| - private volatile Exception caughtException = null; |
35 |
| - private volatile boolean completed = false; |
36 |
| - private volatile boolean created = false; |
| 34 | + private volatile Exception caughtException = null; |
| 35 | + private volatile boolean created = false; |
37 | 36 |
|
38 |
| - protected void releaseResources() |
39 |
| - throws IOException |
40 |
| - { |
41 |
| - if (created) { |
42 |
| - channel.queueDelete("Bug20004Test"); |
43 |
| - } |
| 37 | + protected void releaseResources() throws IOException { |
| 38 | + if (created) { |
| 39 | + channel.queueDelete("Bug20004Test"); |
44 | 40 | }
|
| 41 | + } |
45 | 42 |
|
46 |
| - @SuppressWarnings("deprecation") |
47 |
| - @Test public void bug20004() throws IOException |
48 |
| - { |
49 |
| - final Bug20004Test testInstance = this; |
| 43 | + @Test |
| 44 | + public void bug20004() throws InterruptedException { |
| 45 | + final Bug20004Test testInstance = this; |
| 46 | + CountDownLatch completedLatch = new CountDownLatch(1); |
50 | 47 |
|
51 |
| - Thread declaringThread = new Thread(new Runnable() { |
52 |
| - public void run() { |
53 |
| - try { |
54 |
| - synchronized (channel) { |
55 |
| - channel.queueDeclare("Bug20004Test", false, false, false, null); |
56 |
| - testInstance.created = true; |
57 |
| - } |
58 |
| - } catch (Exception e) { |
59 |
| - testInstance.caughtException = e; |
| 48 | + Thread declaringThread = |
| 49 | + new Thread( |
| 50 | + () -> { |
| 51 | + try { |
| 52 | + synchronized (channel) { |
| 53 | + channel.queueDeclare("Bug20004Test", false, false, false, null); |
| 54 | + testInstance.created = true; |
60 | 55 | }
|
61 |
| - testInstance.completed = true; |
62 |
| - } |
63 |
| - }); |
64 |
| - declaringThread.start(); |
| 56 | + } catch (Exception e) { |
| 57 | + testInstance.caughtException = e; |
| 58 | + } |
| 59 | + completedLatch.countDown(); |
| 60 | + }); |
| 61 | + declaringThread.start(); |
65 | 62 |
|
66 |
| - // poll (100ms) for `completed`, up to 5s |
67 |
| - long endTime = System.currentTimeMillis() + 5000; |
68 |
| - while (!completed && (System.currentTimeMillis() < endTime)) { |
69 |
| - try { |
70 |
| - Thread.sleep(100); |
71 |
| - } catch (InterruptedException ie) {} |
72 |
| - } |
| 63 | + boolean completed = completedLatch.await(5, TimeUnit.SECONDS); |
73 | 64 |
|
74 |
| - declaringThread.stop(); // see bug 20012. |
75 |
| - |
76 |
| - assertTrue("Deadlock detected?", completed); |
77 |
| - assertNull("queueDeclare threw an exception", caughtException); |
78 |
| - assertTrue("unknown sequence of events", created); |
79 |
| - } |
| 65 | + assertTrue("Deadlock detected?", completed); |
| 66 | + assertNull("queueDeclare threw an exception", caughtException); |
| 67 | + assertTrue("unknown sequence of events", created); |
| 68 | + } |
80 | 69 | }
|
0 commit comments