|
2 | 2 |
|
3 | 3 | import com.rabbitmq.client.AMQP; |
4 | 4 | import com.rabbitmq.client.impl.Frame; |
5 | | -import com.rabbitmq.client.impl.nio.ByteBufferInputStream; |
6 | 5 | import com.rabbitmq.client.impl.nio.ByteBufferOutputStream; |
7 | 6 | import org.junit.Test; |
8 | 7 |
|
9 | 8 | import java.io.ByteArrayOutputStream; |
10 | | -import java.io.DataInputStream; |
11 | 9 | import java.io.DataOutputStream; |
12 | 10 | import java.io.IOException; |
13 | 11 | import java.nio.ByteBuffer; |
14 | 12 | import java.nio.channels.ReadableByteChannel; |
15 | 13 | import java.nio.channels.WritableByteChannel; |
16 | | -import java.util.*; |
| 14 | +import java.util.ArrayList; |
| 15 | +import java.util.Iterator; |
| 16 | +import java.util.LinkedList; |
| 17 | +import java.util.List; |
| 18 | +import java.util.Random; |
17 | 19 |
|
18 | | -import static org.hamcrest.Matchers.*; |
19 | | -import static org.junit.Assert.*; |
| 20 | +import static org.hamcrest.Matchers.equalTo; |
| 21 | +import static org.junit.Assert.assertThat; |
20 | 22 |
|
21 | 23 | /** |
22 | 24 | * |
23 | 25 | */ |
24 | 26 | public class FrameTest { |
25 | 27 |
|
26 | | - @Test public void readFrames() throws IOException { |
27 | | - Random random = new Random(); |
28 | | - int nbOfFrames = 100; |
29 | | - AccumulatorReadableByteChannel channel = new AccumulatorReadableByteChannel(); |
30 | | - |
31 | | - for(int i = 0; i < nbOfFrames; i++) { |
32 | | - byte[] payload = new byte[random.nextInt(2000) + 1]; |
33 | | - Frame frame = new Frame(AMQP.FRAME_METHOD, 1, payload); |
34 | | - channel.add(frame); |
35 | | - } |
36 | | - |
37 | | - ByteBuffer buffer = ByteBuffer.allocate(8192); |
38 | | - |
39 | | - DataInputStream inputStream = new DataInputStream( |
40 | | - new ByteBufferInputStream(channel, buffer) |
41 | | - ); |
42 | | - |
43 | | - int nbReadFrames = 0; |
44 | | - channel.read(buffer); |
45 | | - buffer.flip(); |
46 | | - while(buffer.hasRemaining()) { |
47 | | - Frame.readFrom(inputStream); |
48 | | - nbReadFrames++; |
49 | | - if(!buffer.hasRemaining()) { |
50 | | - buffer.clear(); |
51 | | - channel.read(buffer); |
52 | | - buffer.flip(); |
53 | | - } |
54 | | - |
55 | | - } |
56 | | - assertThat(nbReadFrames, equalTo(nbOfFrames)); |
57 | | - } |
58 | | - |
59 | | - @Test public void readLargeFrame() throws IOException { |
60 | | - AccumulatorReadableByteChannel channel = new AccumulatorReadableByteChannel(); |
61 | | - |
62 | | - int [] framesSize = new int [] {100, 75, 20000, 150}; |
63 | | - for (int frameSize : framesSize) { |
64 | | - Frame frame = new Frame(AMQP.FRAME_METHOD, 1, new byte[frameSize]); |
65 | | - channel.add(frame); |
66 | | - } |
67 | | - |
68 | | - ByteBuffer buffer = ByteBuffer.allocate(8192); |
69 | | - |
70 | | - DataInputStream inputStream = new DataInputStream( |
71 | | - new ByteBufferInputStream(channel, buffer) |
72 | | - ); |
73 | | - |
74 | | - int nbReadFrames = 0; |
75 | | - channel.read(buffer); |
76 | | - buffer.flip(); |
77 | | - while(buffer.hasRemaining()) { |
78 | | - Frame.readFrom(inputStream); |
79 | | - nbReadFrames++; |
80 | | - if(!buffer.hasRemaining()) { |
81 | | - buffer.clear(); |
82 | | - channel.read(buffer); |
83 | | - buffer.flip(); |
84 | | - } |
85 | | - |
86 | | - } |
87 | | - assertThat(nbReadFrames, equalTo(framesSize.length)); |
88 | | - } |
89 | | - |
90 | 28 | @Test |
91 | 29 | public void writeFrames() throws IOException { |
92 | 30 | List<Frame> frames = new ArrayList<Frame>(); |
|
0 commit comments