|
| 1 | +package com.github.shyiko.mysql.binlog.io; |
| 2 | + |
| 3 | +import com.github.shyiko.mysql.binlog.event.RawBinaryLogEvent; |
| 4 | +import com.github.shyiko.mysql.binlog.event.deserialization.BinaryLogEventDataReader; |
| 5 | +import com.github.shyiko.mysql.binlog.network.ServerException; |
| 6 | + |
| 7 | +import org.junit.Test; |
| 8 | +import org.mockito.Mockito; |
| 9 | + |
| 10 | +import static org.junit.Assert.*; |
| 11 | +import static org.mockito.Matchers.any; |
| 12 | +import static org.mockito.Matchers.anyInt; |
| 13 | +import static org.mockito.Mockito.doThrow; |
| 14 | +import static org.mockito.Mockito.when; |
| 15 | + |
| 16 | +import java.io.IOException; |
| 17 | +import java.util.Arrays; |
| 18 | + |
| 19 | +public class RawEventsReaderTest { |
| 20 | + |
| 21 | + @Test |
| 22 | + public void nextRawEvent_shouldReturnNull_onNoMoreData() throws IOException { |
| 23 | + byte[] bytes = {}; |
| 24 | + RawEventsReader rawEventsReader = new RawEventsReader(new ByteArrayInputStream(bytes)); |
| 25 | + |
| 26 | + RawBinaryLogEvent rawEvent = rawEventsReader.nextRawEvent(); |
| 27 | + |
| 28 | + assertNull(rawEvent); |
| 29 | + } |
| 30 | + |
| 31 | + @Test |
| 32 | + public void nextRawEvent_shouldReturnEof_onEofPacketReceived() throws IOException { |
| 33 | + byte[] bytes = {0x00, 0x00, 0x00, 0x00, (byte)0xfe}; |
| 34 | + RawEventsReader rawEventsReader = new RawEventsReader(new ByteArrayInputStream(bytes)); |
| 35 | + |
| 36 | + RawBinaryLogEvent rawEvent = rawEventsReader.nextRawEvent(); |
| 37 | + |
| 38 | + assertTrue(rawEvent.isServerEof()); |
| 39 | + } |
| 40 | + |
| 41 | + @Test(expected = IOException.class) |
| 42 | + public void nextRawEvent_shouldThrowIOException_onInputStreamException() throws IOException { |
| 43 | + ByteArrayInputStream mockStream = Mockito.mock(ByteArrayInputStream.class); |
| 44 | + when(mockStream.peek()).thenReturn(0x01); |
| 45 | + doThrow(IOException.class).when(mockStream).fill(any(), anyInt(), anyInt()); |
| 46 | + RawEventsReader rawEventsReader = new RawEventsReader(mockStream); |
| 47 | + |
| 48 | + rawEventsReader.nextRawEvent(); |
| 49 | + } |
| 50 | + |
| 51 | + @Test(expected = ServerException.class) |
| 52 | + public void nextRawEvent_shouldThrowServerException_onServerError() throws IOException { |
| 53 | + byte[] bytes = { |
| 54 | + 0x15, 0x00, 0x00, 0x00, (byte)0xff, 0x01, 0x01, 0x23, 0x48, 0x59, 0x30, 0x31, 0x39, |
| 55 | + 0x6b, 0x65, 0x72, 0x6e, 0x65, 0x6c, 0x5f, 0x70, 0x61, 0x6e, 0x69, 0x63 |
| 56 | + }; |
| 57 | + RawEventsReader rawEventsReader = new RawEventsReader(new ByteArrayInputStream(bytes)); |
| 58 | + |
| 59 | + RawBinaryLogEvent rawEvent = rawEventsReader.nextRawEvent(); |
| 60 | + } |
| 61 | + |
| 62 | + @Test |
| 63 | + public void nextRawEvent_shouldReturnRawEvent_onOnePacketEvent() throws IOException { |
| 64 | + byte[] bytes = { |
| 65 | + 0x09, 0x00, 0x00, 0x00, 0x00, |
| 66 | + 0x0f, 0x07, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 |
| 67 | + }; |
| 68 | + RawEventsReader rawEventsReader = new RawEventsReader(new ByteArrayInputStream(bytes)); |
| 69 | + |
| 70 | + RawBinaryLogEvent rawEvent = rawEventsReader.nextRawEvent(); |
| 71 | + |
| 72 | + assertFalse(rawEvent.isServerEof()); |
| 73 | + BinaryLogEventDataReader dataReader = rawEvent.getEventDataReader(); |
| 74 | + byte[] eventBytes = dataReader.readBytes(dataReader.available()); |
| 75 | + |
| 76 | + byte[] expectedEventBytes = {0x0f, 0x07, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}; |
| 77 | + assertArrayEquals(expectedEventBytes, eventBytes); |
| 78 | + } |
| 79 | + |
| 80 | + @Test |
| 81 | + public void nextRawEvent_shouldReturnRawEvent_onTwoPacketEvent() throws IOException { |
| 82 | + byte[] eventBytes = generateMultiPacketData(); |
| 83 | + byte[] packetBytes = eventBytesToPacketBytes(eventBytes); |
| 84 | + |
| 85 | + RawEventsReader rawEventsReader = new RawEventsReader(new ByteArrayInputStream(packetBytes)); |
| 86 | + |
| 87 | + RawBinaryLogEvent rawEvent = rawEventsReader.nextRawEvent(); |
| 88 | + |
| 89 | + assertFalse(rawEvent.isServerEof()); |
| 90 | + BinaryLogEventDataReader dataReader = rawEvent.getEventDataReader(); |
| 91 | + byte[] actualEventBytes = dataReader.readBytes(dataReader.available()); |
| 92 | + |
| 93 | + assertArrayEquals(eventBytes, actualEventBytes); |
| 94 | + } |
| 95 | + |
| 96 | + private static byte[] generateMultiPacketData() { |
| 97 | + final int firstPacketSize = RawEventsReader.MAX_PACKET_LENGTH - 1; |
| 98 | + final int secondPacketSize = 32; |
| 99 | + |
| 100 | + byte[] eventBytes = new byte[firstPacketSize + secondPacketSize]; |
| 101 | + Arrays.fill(eventBytes, 0, firstPacketSize, (byte)0x11); |
| 102 | + Arrays.fill(eventBytes, firstPacketSize, eventBytes.length, (byte)0x22); |
| 103 | + |
| 104 | + return eventBytes; |
| 105 | + } |
| 106 | + |
| 107 | + private static byte[] eventBytesToPacketBytes(byte[] eventBytes) { |
| 108 | + int numPackets = eventBytes.length / RawEventsReader.MAX_PACKET_LENGTH + 1; |
| 109 | + int packetHeadersLength = (numPackets - 1) * 4 + 5; |
| 110 | + |
| 111 | + byte[] packetBytes = new byte[eventBytes.length + packetHeadersLength]; |
| 112 | + int eventBytesLeft = eventBytes.length; |
| 113 | + int packetBytesOffset = 0; |
| 114 | + int packetNumber = 0; |
| 115 | + while (eventBytesLeft > 0) { |
| 116 | + int packetLength = Math.min(eventBytesLeft, RawEventsReader.MAX_PACKET_LENGTH); |
| 117 | + packetBytes[packetBytesOffset] = (byte) (packetLength & 0xFF); |
| 118 | + packetBytes[packetBytesOffset+1] = (byte) ((packetLength >>> 8) & 0xFF); |
| 119 | + packetBytes[packetBytesOffset+2] = (byte) ((packetLength >>> 16) & 0xFF); |
| 120 | + |
| 121 | + packetBytesOffset += 3; |
| 122 | + packetBytes[packetBytesOffset++] = (byte)packetNumber; |
| 123 | + if (packetNumber == 0) { |
| 124 | + packetBytes[packetBytesOffset++] = 0x00; |
| 125 | + packetLength--; |
| 126 | + } |
| 127 | + System.arraycopy( |
| 128 | + eventBytes, |
| 129 | + eventBytes.length - eventBytesLeft, |
| 130 | + packetBytes, |
| 131 | + packetBytesOffset, |
| 132 | + packetLength); |
| 133 | + |
| 134 | + packetBytesOffset += packetLength; |
| 135 | + eventBytesLeft -= packetLength; |
| 136 | + packetNumber++; |
| 137 | + } |
| 138 | + |
| 139 | + return packetBytes; |
| 140 | + } |
| 141 | +} |
0 commit comments