|
| 1 | +// The contents of this file are subject to the Mozilla Public License |
| 2 | +// Version 1.1 (the "License"); you may not use this file except in |
| 3 | +// compliance with the License. You may obtain a copy of the License at |
| 4 | +// http://www.mozilla.org/MPL/ |
| 5 | +// |
| 6 | +// Software distributed under the License is distributed on an "AS IS" |
| 7 | +// basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See the |
| 8 | +// License for the specific language governing rights and limitations |
| 9 | +// under the License. |
| 10 | +// |
| 11 | +// The Original Code is RabbitMQ. |
| 12 | +// |
| 13 | +// The Initial Developers of the Original Code are LShift Ltd, |
| 14 | +// Cohesive Financial Technologies LLC, and Rabbit Technologies Ltd. |
| 15 | +// |
| 16 | +// Portions created before 22-Nov-2008 00:00:00 GMT by LShift Ltd, |
| 17 | +// Cohesive Financial Technologies LLC, or Rabbit Technologies Ltd |
| 18 | +// are Copyright (C) 2007-2008 LShift Ltd, Cohesive Financial |
| 19 | +// Technologies LLC, and Rabbit Technologies Ltd. |
| 20 | +// |
| 21 | +// Portions created by LShift Ltd are Copyright (C) 2007-2010 LShift |
| 22 | +// Ltd. Portions created by Cohesive Financial Technologies LLC are |
| 23 | +// Copyright (C) 2007-2010 Cohesive Financial Technologies |
| 24 | +// LLC. Portions created by Rabbit Technologies Ltd are Copyright |
| 25 | +// (C) 2007-2010 Rabbit Technologies Ltd. |
| 26 | +// |
| 27 | +// All Rights Reserved. |
| 28 | +// |
| 29 | +// Contributor(s): ______________________________________. |
| 30 | +// |
| 31 | + |
| 32 | +package com.rabbitmq.client.test.functional; |
| 33 | + |
| 34 | +import com.rabbitmq.client.test.BrokerTestCase; |
| 35 | + |
| 36 | +import java.io.IOException; |
| 37 | +import java.net.Socket; |
| 38 | + |
| 39 | +import com.rabbitmq.client.ConnectionFactory; |
| 40 | +import com.rabbitmq.client.GetResponse; |
| 41 | +import com.rabbitmq.client.impl.Frame; |
| 42 | +import com.rabbitmq.client.impl.FrameHandler; |
| 43 | +import com.rabbitmq.client.impl.SocketFrameHandler; |
| 44 | + |
| 45 | +/* Publish a message of size FRAME_MAX. The broker should split this |
| 46 | + * into two frames before sending back. */ |
| 47 | +public class FrameMax extends BrokerTestCase { |
| 48 | + /* This value for FrameMax is larger than the minimum and less |
| 49 | + * than what Rabbit suggests. */ |
| 50 | + final static int FRAME_MAX = 70000; |
| 51 | + final static int REAL_FRAME_MAX = FRAME_MAX - 8; |
| 52 | + final static String ROUTING_KEY = "something"; |
| 53 | + |
| 54 | + private String queueName; |
| 55 | + |
| 56 | + public FrameMax() { |
| 57 | + connectionFactory = new MyConnectionFactory(); |
| 58 | + connectionFactory.setRequestedFrameMax(FRAME_MAX); |
| 59 | + } |
| 60 | + |
| 61 | + @Override |
| 62 | + protected void createResources() |
| 63 | + throws IOException |
| 64 | + { |
| 65 | + queueName = channel.queueDeclare().getQueue(); |
| 66 | + channel.queueBind(queueName, "", ROUTING_KEY); |
| 67 | + } |
| 68 | + |
| 69 | + /* Frame content should be less or equal to frame-max - 8. */ |
| 70 | + public void testFrameSizes() |
| 71 | + throws IOException, InterruptedException |
| 72 | + { |
| 73 | + /* This should result in at least 3 frames. */ |
| 74 | + int howMuch = 2*FRAME_MAX; |
| 75 | + basicPublishVolatile(new byte[howMuch], ROUTING_KEY); |
| 76 | + /* Receive everything that was sent out. */ |
| 77 | + while (howMuch > 0) { |
| 78 | + try { |
| 79 | + GetResponse response = channel.basicGet(queueName, false); |
| 80 | + howMuch -= response.getBody().length; |
| 81 | + } catch (Exception e) { |
| 82 | + fail(e.getCause().toString()); |
| 83 | + } |
| 84 | + } |
| 85 | + } |
| 86 | + |
| 87 | + /* ConnectionFactory that uses MyFrameHandler rather than |
| 88 | + * SocketFrameHandler. */ |
| 89 | + private static class MyConnectionFactory extends ConnectionFactory { |
| 90 | + protected FrameHandler createFrameHandler(Socket sock) |
| 91 | + throws IOException |
| 92 | + { |
| 93 | + return new MyFrameHandler(sock); |
| 94 | + } |
| 95 | + } |
| 96 | + |
| 97 | + /* FrameHandler with added frame-max error checking. */ |
| 98 | + private static class MyFrameHandler extends SocketFrameHandler { |
| 99 | + public MyFrameHandler(Socket socket) |
| 100 | + throws IOException |
| 101 | + { |
| 102 | + super(socket); |
| 103 | + } |
| 104 | + |
| 105 | + public Frame readFrame() throws IOException { |
| 106 | + Frame f = super.readFrame(); |
| 107 | + int size = f.getPayload().length; |
| 108 | + if (size > REAL_FRAME_MAX) |
| 109 | + fail("Received frame of size " + size |
| 110 | + + ", which exceeds " + REAL_FRAME_MAX + "."); |
| 111 | + //System.out.printf("Received a frame of size %d.\n", f.getPayload().length); |
| 112 | + return f; |
| 113 | + } |
| 114 | + } |
| 115 | +} |
0 commit comments