Skip to content

Commit db0c1ee

Browse files
committed
Pull up setUp and tearDown. Use abort() where possible instead of
close() (see also bug 19676). Introduce abstract methods createResources and releaseResources on BrokerTestCase, which help keep things clean independent of any channel or connection exceptions. Use fail() in DurableOnTransient instead of a flag. Remove redundant channelOpen and channelClose methods from TransactionsBase.
1 parent c4f0019 commit db0c1ee

File tree

8 files changed

+123
-132
lines changed

8 files changed

+123
-132
lines changed

test/src/com/rabbitmq/client/test/Bug19219Test.java

Lines changed: 2 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -63,17 +63,8 @@ public class Bug19219Test extends BrokerTestCase {
6363
private static final Semaphore init = new Semaphore(0);
6464
private static final CountDownLatch resume = new CountDownLatch(1);
6565

66-
@Override protected void setUp() throws Exception {
67-
super.setUp();
68-
openConnection();
69-
openChannel();
70-
}
71-
72-
@Override protected void tearDown() throws Exception {
73-
closeChannel();
74-
closeConnection();
75-
super.tearDown();
76-
}
66+
protected void createResources() throws IOException {}
67+
protected void releaseResources() throws IOException {}
7768

7869
public static TestSuite suite() {
7970
TestSuite suite = new TestSuite("Bug19219");

test/src/com/rabbitmq/client/test/functional/BrokerTestCase.java

Lines changed: 49 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,18 +29,59 @@
2929

3030
import junit.framework.TestCase;
3131

32+
import com.rabbitmq.client.AlreadyClosedException;
3233
import com.rabbitmq.client.Channel;
3334
import com.rabbitmq.client.Connection;
3435
import com.rabbitmq.client.ConnectionFactory;
3536

36-
public class BrokerTestCase extends TestCase
37+
public abstract class BrokerTestCase extends TestCase
3738
{
3839
public ConnectionFactory connectionFactory = new ConnectionFactory();
3940

4041
public Connection connection;
4142
public Channel channel;
4243
public int ticket;
4344

45+
protected void setUp()
46+
throws IOException
47+
{
48+
openConnection();
49+
openChannel();
50+
51+
createResources();
52+
}
53+
54+
protected void tearDown()
55+
throws IOException
56+
{
57+
closeChannel();
58+
closeConnection();
59+
60+
openConnection();
61+
openChannel();
62+
releaseResources();
63+
closeChannel();
64+
closeConnection();
65+
}
66+
67+
/**
68+
* Should create any AMQP resources needed by the test. Will be
69+
* called by BrokerTestCase's implementation of setUp, after the
70+
* connection and channel have been opened.
71+
*/
72+
protected abstract void createResources()
73+
throws IOException;
74+
75+
/**
76+
* Should destroy any AMQP resources that were created by the
77+
* test. Will be called by BrokerTestCase's implementation of
78+
* tearDown, after the connection and channel have been closed and
79+
* reopened specifically for this method. After this method
80+
* completes, the connection and channel will be closed again.
81+
*/
82+
protected abstract void releaseResources()
83+
throws IOException;
84+
4485
public void openConnection()
4586
throws IOException
4687
{
@@ -53,7 +94,7 @@ public void closeConnection()
5394
throws IOException
5495
{
5596
if (connection != null) {
56-
connection.close();
97+
connection.abort();
5798
connection = null;
5899
}
59100
}
@@ -69,7 +110,12 @@ public void closeChannel()
69110
throws IOException
70111
{
71112
if (channel != null) {
72-
channel.close();
113+
try {
114+
channel.close();
115+
} catch (AlreadyClosedException ace) {
116+
// The API is broken so we have to catch this here.
117+
// See bug 19676.
118+
}
73119
channel = null;
74120
}
75121
}

test/src/com/rabbitmq/client/test/functional/DurableOnTransient.java

Lines changed: 13 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -32,24 +32,9 @@
3232

3333
public class DurableOnTransient extends BrokerTestCase
3434
{
35-
3635
protected static final String Q = "DurableQueue";
3736
protected static final String X = "TransientExchange";
3837

39-
protected void setUp()
40-
throws IOException
41-
{
42-
openConnection();
43-
openChannel();
44-
}
45-
46-
protected void tearDown()
47-
throws IOException
48-
{
49-
closeChannel();
50-
closeConnection();
51-
}
52-
5338
private GetResponse basicGet()
5439
throws IOException
5540
{
@@ -64,30 +49,26 @@ private void basicPublish()
6449
"persistent message".getBytes());
6550
}
6651

67-
public void testBind()
68-
throws IOException, Exception
69-
{
70-
boolean B = false;
52+
protected void createResources() throws IOException {
7153
// Transient exchange
7254
channel.exchangeDeclare(ticket, X, "direct", false);
7355
// durable queue
7456
channel.queueDeclare(ticket, Q, true);
75-
// The following should raise an exception
57+
}
58+
59+
protected void releaseResources() throws IOException {
60+
channel.queueDelete(ticket, Q);
61+
channel.exchangeDelete(ticket, X);
62+
}
63+
64+
public void testBind()
65+
throws IOException
66+
{
7667
try {
7768
channel.queueBind(ticket, Q, X, "");
69+
fail("Expected exception from queueBind");
7870
} catch (IOException ee) {
79-
// Channel and connection have been closed. We need to
80-
// delete the queue below and therefore need to reconnect.
81-
super.connection=null;
82-
setUp();
83-
// Say that the expected behaviour is Ok
84-
B = true;
71+
// Pass!
8572
}
86-
channel.queueDelete(ticket, Q);
87-
channel.exchangeDelete(ticket, X);
88-
89-
assertTrue(B);
90-
9173
}
92-
9374
}

test/src/com/rabbitmq/client/test/functional/NoRequeueOnCancel.java

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -33,20 +33,12 @@ public class NoRequeueOnCancel extends BrokerTestCase
3333
{
3434
protected final String Q = "NoRequeueOnCancel";
3535

36-
protected void setUp()
37-
throws IOException
38-
{
39-
openConnection();
40-
openChannel();
36+
protected void createResources() throws IOException {
4137
channel.queueDeclare(ticket, Q);
4238
}
4339

44-
protected void tearDown()
45-
throws IOException
46-
{
40+
protected void releaseResources() throws IOException {
4741
channel.queueDelete(ticket, Q);
48-
closeChannel();
49-
closeConnection();
5042
}
5143

5244
public void testNoRequeueOnCancel()

test/src/com/rabbitmq/client/test/functional/PersisterRestartBase.java

Lines changed: 2 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -48,19 +48,8 @@ public class PersisterRestartBase extends BrokerTestCase
4848
// rabbit_persister.erl
4949
protected final int PERSISTER_SNAPSHOT_THRESHOLD = 500;
5050

51-
protected void setUp()
52-
throws IOException
53-
{
54-
openConnection();
55-
openChannel();
56-
}
57-
58-
protected void tearDown()
59-
throws IOException
60-
{
61-
closeChannel();
62-
closeConnection();
63-
}
51+
protected void createResources() throws IOException {}
52+
protected void releaseResources() throws IOException {}
6453

6554
protected void restart()
6655
throws IOException

test/src/com/rabbitmq/client/test/functional/RequeueOnClose.java

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,21 @@ public abstract class RequeueOnClose
4242

4343
protected abstract void close() throws IOException;
4444

45+
protected void setUp()
46+
throws IOException
47+
{
48+
// Override to disable the default behaviour from BrokerTestCase.
49+
}
50+
51+
protected void tearDown()
52+
throws IOException
53+
{
54+
// Override to disable the default behaviour from BrokerTestCase.
55+
}
56+
57+
protected void createResources() throws IOException {}
58+
protected void releaseResources() throws IOException {}
59+
4560
public void injectMessage()
4661
throws IOException
4762
{

test/src/com/rabbitmq/client/test/functional/Routing.java

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -37,24 +37,16 @@ public class Routing extends BrokerTestCase
3737
protected final String Q1 = "foo";
3838
protected final String Q2 = "bar";
3939

40-
protected void setUp()
41-
throws IOException
42-
{
43-
openConnection();
44-
openChannel();
40+
protected void createResources() throws IOException {
4541
channel.exchangeDeclare(ticket, E, "direct");
4642
channel.queueDeclare(ticket, Q1);
4743
channel.queueDeclare(ticket, Q2);
4844
}
4945

50-
protected void tearDown()
51-
throws IOException
52-
{
46+
protected void releaseResources() throws IOException {
5347
channel.queueDelete(ticket, Q1);
5448
channel.queueDelete(ticket, Q2);
5549
channel.exchangeDelete(ticket, E);
56-
closeChannel();
57-
closeConnection();
5850
}
5951

6052
private void bind(String queue, String routingKey)

0 commit comments

Comments
 (0)