summaryrefslogtreecommitdiff
path: root/java/systests/src/main/java/org/apache/qpid/test/client/failover/FailoverTest.java
blob: 9d1e461f05ed5111dd2d230bd4a62506418a9ee6 (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
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
package org.apache.qpid.test.client.failover;

import junit.framework.TestCase;
import org.apache.qpid.client.AMQConnection;
import org.apache.qpid.client.AMQConnectionFactory;
import org.apache.qpid.client.AMQQueue;
import org.apache.qpid.client.transport.TransportConnection;
import org.apache.qpid.jms.ConnectionListener;
import org.apache.qpid.server.registry.ApplicationRegistry;
import org.apache.qpid.url.URLSyntaxException;
import org.apache.log4j.Logger;

import javax.jms.Connection;
import javax.jms.JMSException;
import javax.jms.Message;
import javax.jms.MessageConsumer;
import javax.jms.MessageProducer;
import javax.jms.Session;
import javax.jms.TextMessage;
import java.util.concurrent.CountDownLatch;

public class FailoverTest extends TestCase implements ConnectionListener
{
    private static final Logger _logger = Logger.getLogger(FailoverTest.class);

    private static final int NUM_BROKERS = 2;
    private static final String BROKER = "amqp://guest:guest@/test?brokerlist='vm://:%d;vm://:%d'";
    private static final String QUEUE = "queue";
    private static final int NUM_MESSAGES = 10;
    private Connection con;
    private AMQConnectionFactory conFactory;
    private Session prodSess;
    private AMQQueue q;
    private MessageProducer prod;
    private Session conSess;
    private MessageConsumer consumer;

    private static int usedBrokers = 0;
    private CountDownLatch failoverComplete;

    @Override
    protected void setUp() throws Exception
    {
        super.setUp();
        // Create two VM brokers

        for (int i = 0; i < NUM_BROKERS; i++)
        {
            usedBrokers++;

            TransportConnection.createVMBroker(usedBrokers);
        }

        conFactory = new AMQConnectionFactory(String.format(BROKER, usedBrokers - 1, usedBrokers));
        _logger.info("Connecting on:" + conFactory.getConnectionURL());
        con = conFactory.createConnection();
        ((AMQConnection) con).setConnectionListener(this);
        con.start();
        failoverComplete = new CountDownLatch(1);
    }

    private void init(boolean transacted, int mode) throws JMSException
    {
        prodSess = con.createSession(transacted, mode);
        q = new AMQQueue("amq.direct", QUEUE);
        prod = prodSess.createProducer(q);
        conSess = con.createSession(transacted, mode);
        consumer = conSess.createConsumer(q);
    }

    @Override
    protected void tearDown() throws Exception
    {
        try
        {
            con.close();
        }
        catch (Exception e)
        {

        }

        try
        {
            TransportConnection.killAllVMBrokers();
            ApplicationRegistry.removeAll();
        }
        catch (Exception e)
        {
            fail("Unable to clean up");
        }
        super.tearDown();
    }

    private void consumeMessages(int toConsume) throws JMSException
    {
        Message msg;
        for (int i = 0; i < toConsume; i++)
        {
            msg = consumer.receive(1000);
            assertNotNull("Message " + i + " was null!", msg);
            assertEquals("message " + i, ((TextMessage) msg).getText());
        }
    }

    private void sendMessages(int totalMessages) throws JMSException
    {
        for (int i = 0; i < totalMessages; i++)
        {
            prod.send(prodSess.createTextMessage("message " + i));
        }

//        try
//        {
//            Thread.sleep(100 * totalMessages);
//        }
//        catch (InterruptedException e)
//        {
//            //evil ignoring of IE
//        }
    }

    public void testP2PFailover() throws Exception
    {
        testP2PFailover(NUM_MESSAGES, true);
    }

    public void testP2PFailoverWithMessagesLeft() throws Exception
    {
        testP2PFailover(NUM_MESSAGES, false);
    }

    private void testP2PFailover(int totalMessages, boolean consumeAll) throws JMSException
    {
        Message msg = null;
        init(false, Session.AUTO_ACKNOWLEDGE);
        sendMessages(totalMessages);

        // Consume some messages
        int toConsume = totalMessages;
        if (!consumeAll)
        {
            toConsume = totalMessages / 2;
        }

        consumeMessages(toConsume);

        _logger.info("Failing over");

        causeFailure();

        msg = consumer.receive(500);
        //todo: reinstate
        assertNull("Should not have received message from new broker!", msg);
        // Check that messages still sent / received
        sendMessages(totalMessages);
        consumeMessages(totalMessages);
    }

    private void causeFailure()
    {
        _logger.info("Failover");

        TransportConnection.killVMBroker(usedBrokers - 1);
        ApplicationRegistry.remove(usedBrokers - 1);

        _logger.info("Awaiting Failover completion");
        try
        {
            failoverComplete.await();
        }
        catch (InterruptedException e)
        {
            //evil ignore IE.
        }
    }

    public void testClientAckFailover() throws Exception
    {
        init(false, Session.CLIENT_ACKNOWLEDGE);
        sendMessages(1);
        Message msg = consumer.receive();
        assertNotNull("Expected msgs not received", msg);


        causeFailure();

        Exception failure = null;
        try
        {
            msg.acknowledge();
        }
        catch (Exception e)
        {
            failure = e;
        }
        assertNotNull("Exception should be thrown", failure);
    }

    // This test disabled so that it doesn't add 4 minnutes to the length of time it takes to run, which would be lame
    public void txest4MinuteFailover() throws Exception
    {
        conFactory = new AMQConnectionFactory("amqp://guest:guest@/test?brokerlist='vm://:"+(usedBrokers-1)+"?connectdelay='60000'&retries='2''");
        _logger.info("Connecting on:" + conFactory.getConnectionURL());
        con = conFactory.createConnection();
        ((AMQConnection) con).setConnectionListener(this);
        con.start();

        long failTime = System.currentTimeMillis() + 60000;
        causeFailure();
        assertTrue("Failover did not take long enough", System.currentTimeMillis() > failTime);
    }

    public void bytesSent(long count)
    {
    }

    public void bytesReceived(long count)
    {
    }

    public boolean preFailover(boolean redirect)
    {
        return true;
    }

    public boolean preResubscribe()
    {
        return true;
    }

    public void failoverComplete()
    {
        failoverComplete.countDown();
    }
}