-
Notifications
You must be signed in to change notification settings - Fork 912
/
RedistributorLeakTest.java
144 lines (122 loc) · 5.51 KB
/
RedistributorLeakTest.java
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
/*
* 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.activemq.artemis.tests.leak;
import javax.jms.Connection;
import javax.jms.ConnectionFactory;
import javax.jms.Destination;
import javax.jms.Message;
import javax.jms.MessageConsumer;
import javax.jms.MessageProducer;
import javax.jms.Session;
import javax.jms.TextMessage;
import java.lang.invoke.MethodHandles;
import io.github.checkleak.core.CheckLeak;
import org.apache.activemq.artemis.api.core.QueueConfiguration;
import org.apache.activemq.artemis.api.core.RoutingType;
import org.apache.activemq.artemis.core.message.impl.CoreMessage;
import org.apache.activemq.artemis.core.server.ActiveMQServer;
import org.apache.activemq.artemis.core.server.impl.AddressInfo;
import org.apache.activemq.artemis.core.server.impl.MessageReferenceImpl;
import org.apache.activemq.artemis.core.server.impl.QueueImpl;
import org.apache.activemq.artemis.tests.util.ActiveMQTestBase;
import org.apache.activemq.artemis.tests.util.CFUtil;
import org.apache.activemq.artemis.utils.RandomUtil;
import org.apache.activemq.artemis.utils.collections.LinkedListImpl;
import org.junit.Assert;
import org.junit.Assume;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
public class RedistributorLeakTest extends ActiveMQTestBase {
private static final Logger logger = LoggerFactory.getLogger(MethodHandles.lookup().lookupClass());
ActiveMQServer server;
public void startServer() throws Exception {
server = createServer(false, true);
server.start();
}
@BeforeClass
public static void beforeClass() throws Exception {
Assume.assumeTrue(CheckLeak.isLoaded());
}
@Override
@Before
public void setUp() throws Exception {
startServer();
}
@Override
public void tearDown() throws Exception {
super.tearDown();
server = null;
}
@Test
public void testRedistributor() throws Exception {
CheckLeak checkLeak = new CheckLeak();
final int NUMBER_OF_MESSAGES = 500;
String addressName = "Queue" + RandomUtil.randomString();
server.addAddressInfo(new AddressInfo(addressName).addRoutingType(RoutingType.ANYCAST));
QueueImpl queue = (QueueImpl) server.createQueue(new QueueConfiguration().setName(addressName).setRoutingType(RoutingType.ANYCAST));
ConnectionFactory factory = CFUtil.createConnectionFactory("core", "tcp://localhost:61616");
try (Connection connection = factory.createConnection()) {
Session session = connection.createSession(true, Session.SESSION_TRANSACTED);
MessageProducer producer = session.createProducer(session.createQueue(addressName));
for (int i = 0; i < NUMBER_OF_MESSAGES; i++) {
Message message = session.createTextMessage("hello " + i);
message.setJMSPriority(1 + (i % 5));
producer.send(message);
}
session.commit();
Destination jmsQueue = session.createQueue(addressName);
connection.start();
// creating one consumer per messages, just for part of the messages sent
for (int i = 0; i < NUMBER_OF_MESSAGES / 10; i++) {
MessageConsumer consumer = session.createConsumer(jmsQueue);
Message message = consumer.receive(1000);
Assert.assertNotNull(message);
queue.flushExecutor();
consumer.close();
}
session.rollback();
}
int numberOfIterators = checkLeak.getAllObjects(LinkedListImpl.Iterator.class).length;
Assert.assertEquals(0, numberOfIterators);
// Adding and cancelling a few redistributors
for (int i = 0; i < 10; i++) {
queue.addRedistributor(0);
queue.flushExecutor();
queue.cancelRedistributor();
queue.flushExecutor();
}
numberOfIterators = checkLeak.getAllObjects(LinkedListImpl.Iterator.class).length;
Assert.assertEquals("Redistributors are leaking " + LinkedListImpl.Iterator.class.getName(), 0, numberOfIterators);
try (Connection connection = factory.createConnection()) {
Session session = connection.createSession(true, Session.SESSION_TRANSACTED);
Destination destination = session.createQueue(addressName);
connection.start();
MessageConsumer consumer = session.createConsumer(destination);
for (int i = 0; i < NUMBER_OF_MESSAGES; i++) {
TextMessage message = (TextMessage) consumer.receive(1000);
Assert.assertNotNull(message);
Assert.assertEquals("hello " + i, message.getText());
}
session.commit();
}
Assert.assertEquals(0, checkLeak.getAllObjects(MessageReferenceImpl.class).length);
Assert.assertEquals(0, checkLeak.getAllObjects(CoreMessage.class).length);
}
}