Skip to content

Commit fcc8cf3

Browse files
committed
Use count down latch in test
To avoid using Thread#stop(), which is deprecated since 1998. The test does not explain why or if Thread#stop() is necessary to the test. It's unlikely as the target thread has stopped already when stop is called in the test. (cherry picked from commit 6aaa203)
1 parent 637a0bf commit fcc8cf3

File tree

1 file changed

+39
-50
lines changed

1 file changed

+39
-50
lines changed
Lines changed: 39 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
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.
22
//
33
// This software, the RabbitMQ Java client library, is triple-licensed under the
44
// Mozilla Public License 2.0 ("MPL"), the GNU General Public License version 2
@@ -15,66 +15,55 @@
1515

1616
package com.rabbitmq.client.test;
1717

18-
import org.junit.Test;
18+
import static org.junit.Assert.assertNull;
19+
import static org.junit.Assert.assertTrue;
1920

2021
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;
2325

2426
/**
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.
3232
*/
3333
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;
3736

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");
4440
}
41+
}
4542

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);
5047

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;
6055
}
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();
6562

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);
7364

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+
}
8069
}

0 commit comments

Comments
 (0)