use of com.mysql.cj.jdbc.JdbcPropertySetImpl in project aws-mysql-jdbc by awslabs.
the class FailoverConnectionPluginTest method initFailoverPlugin.
private FailoverConnectionPlugin initFailoverPlugin(Properties properties) throws SQLException {
final JdbcPropertySet propertySet = new JdbcPropertySetImpl();
propertySet.initializeProperties(properties);
when(mockConnection.getPropertySet()).thenReturn(propertySet);
return new FailoverConnectionPlugin(mockCurrentConnectionProvider, propertySet, mockNextPlugin, mockLogger, mockConnectionProvider, () -> mockTopologyService, () -> mockClusterMetricContainer);
}
use of com.mysql.cj.jdbc.JdbcPropertySetImpl in project aws-mysql-jdbc by awslabs.
the class SimplePacketReaderTest method readPayloadErrors.
// test error handling when reading payload
@Test
public void readPayloadErrors() throws IOException {
RuntimeProperty<Integer> maxAllowedPacket = new JdbcPropertySetImpl().getProperty(PropertyKey.maxAllowedPacket);
MockSocketConnection connection = new FixedBufferSocketConnection(new byte[] { 5, 4 });
MessageReader<NativePacketHeader, NativePacketPayload> reader = new SimplePacketReader(connection, maxAllowedPacket);
// can't read 3 bytes if the buffer only has 2
try {
reader.readMessage(Optional.empty(), new NativePacketHeader(new byte[] { 3, 0, 0, 0 }));
fail("Shouldn't be able to read more than 2 bytes");
} catch (EOFException ex) {
assertTrue(connection.forceClosed, "Connection should be force closed when payload read fails");
}
// any IO errors during read should hang up the connection
connection = new MockSocketConnection() {
@Override
public int readFully(byte[] b, int off, int len) throws IOException {
throw new IOException("arbitrary failure");
}
};
reader = new SimplePacketReader(connection, maxAllowedPacket);
try {
reader.readMessage(Optional.empty(), new NativePacketHeader(new byte[] { 3, 0, 0, 0 }));
fail("IOException should be thrown");
} catch (IOException ex) {
assertTrue(connection.forceClosed, "Connection should be force closed when payload read fails");
}
}
use of com.mysql.cj.jdbc.JdbcPropertySetImpl in project aws-mysql-jdbc by awslabs.
the class SimplePacketReaderTest method heuristicTestWithRandomPackets.
// generate some random packets for the reader
@Test
public void heuristicTestWithRandomPackets() throws IOException {
// can't exceed 127 without changing code
int numPackets = 127;
int maxPacketSize = 127;
// >>>>>>>> generate random test packets <<<<<<<<
// the sizes are random. the sequence is the packet # (in the array). payload is repeated packet #
Random rand = new Random();
int[] packetLengths = new int[numPackets];
int totalBufferSize = 0;
for (int i = 0; i < numPackets; ++i) {
packetLengths[i] = rand.nextInt(maxPacketSize);
totalBufferSize += packetLengths[i] + NativeConstants.HEADER_LENGTH;
}
ByteBuffer buffer = ByteBuffer.allocate(totalBufferSize);
for (int i = 0; i < numPackets; ++i) {
// i = packet number (in array of random test packets)
// header
buffer.put((byte) packetLengths[i]);
buffer.put((byte) 0);
buffer.put((byte) 0);
buffer.put((byte) i);
// payload
for (int j = 0; j < packetLengths[i]; ++j) {
buffer.put((byte) i);
}
}
buffer.clear();
// >>>>>>>> read the packets <<<<<<<<
RuntimeProperty<Integer> maxAllowedPacket = new JdbcPropertySetImpl().getProperty(PropertyKey.maxAllowedPacket);
MockSocketConnection connection = new FixedBufferSocketConnection(buffer.array());
MessageReader<NativePacketHeader, NativePacketPayload> reader = new SimplePacketReader(connection, maxAllowedPacket);
NativePacketPayload readBuffer = new NativePacketPayload(new byte[maxPacketSize]);
for (int i = 0; i < numPackets; ++i) {
NativePacketHeader hdr = reader.readHeader();
// check length against generated lengths
assertEquals(packetLengths[i], hdr.getMessageSize());
// each packet sequence is the packet # in the array
assertEquals(i, hdr.getMessageSequence());
assertEquals(i, reader.getMessageSequence());
reader.readMessage(Optional.of(readBuffer), hdr);
// check payload bytes also match packet #
for (int j = 0; j < packetLengths[i]; ++j) {
assertEquals(i, readBuffer.getByteBuffer()[j]);
}
}
}
Aggregations